YARP
Yet Another Robot Platform
TcpRosCarrier.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef TCPROSCARRIER_INC
10 #define TCPROSCARRIER_INC
11 
12 #include <yarp/os/Carrier.h>
13 
17 
18 #include "RosHeader.h"
19 #include "TcpRosStream.h"
20 
21 #define TCPROS_TRANSLATE_INHIBIT (-1)
22 #define TCPROS_TRANSLATE_UNKNOWN (0)
23 #define TCPROS_TRANSLATE_IMAGE (1)
24 #define TCPROS_TRANSLATE_BOTTLE_BLOB (2)
25 #define TCPROS_TRANSLATE_TWIDDLER (3)
26 
28  public yarp::os::Carrier
29 {
30 private:
31  bool firstRound;
32  bool sender;
33  int headerLen1;
34  int headerLen2;
35  int raw;
36  int translate;
40  int seq;
43  std::string kind;
44  bool persistent;
45  std::string wire_type;
46  std::string user_type;
47  std::string md5sum;
48  std::string message_definition;
49 
50  std::string getRosType(yarp::os::ConnectionState& proto);
51 
52 protected:
53  bool isService;
54 public:
56  firstRound = true;
57  sender = false;
58  headerLen1 = 0;
59  headerLen2 = 0;
60  isService = false;
61  raw = -1;
62  translate = TCPROS_TRANSLATE_UNKNOWN;
63  seq = 0;
64  persistent = true;
65  }
66 
67  Carrier *create() const override {
68  return new TcpRosCarrier();
69  }
70 
71  std::string getName() const override {
72  return isService?"rossrv":"tcpros";
73  }
74 
75  bool isConnectionless() const override {
76  return false;
77  }
78 
79  bool canAccept() const override {
80  return true;
81  }
82 
83  bool canOffer() const override {
84  return true;
85  }
86 
87  bool isTextMode() const override {
88  return false;
89  }
90 
91  bool isBareMode() const override {
92  return true;
93  }
94 
95  bool canEscape() const override {
96  return false;
97  }
98 
99  bool requireAck() const override {
100  return false;
101  }
102 
103  bool supportReply() const override {
104  return true;
105  }
106 
107  bool isPush() const override {
108  // if topic-like, pull ; if service-like, push!
109  return isService;
110  }
111 
112  bool isLocal() const override {
113  return false;
114  }
115 
116  std::string toString() const override {
117  return isService?"rossrv_carrier":"tcpros_carrier";
118  }
119 
120  void getHeader(yarp::os::Bytes& header) const override {
121  // no header, will need to do some fancy footwork
122  const char *target = "NONONONO";
123  for (size_t i=0; i<8 && i<header.length(); i++) {
124  header.get()[i] = target[i];
125  }
126  }
127 
128  bool checkHeader(const yarp::os::Bytes& header) override;
129 
130  void setParameters(const yarp::os::Bytes& header) override;
131 
132  // Now, the initial hand-shaking
133 
134  bool prepareSend(yarp::os::ConnectionState& proto) override {
135  return true;
136  }
137 
138  bool sendHeader(yarp::os::ConnectionState& proto) override;
139 
140  bool expectSenderSpecifier(yarp::os::ConnectionState& proto) override;
141 
143  return true;
144  }
145 
147  sender = false;
148  return true;
149  }
150 
151  bool expectReplyToHeader(yarp::os::ConnectionState& proto) override;
152 
153  bool isActive() const override {
154  return true;
155  }
156 
157 
158  // Payload time!
159 
160  bool write(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
161 
162  bool reply(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
163 
165  return true;
166  }
167 
168  bool expectIndex(yarp::os::ConnectionState& proto) override {
169  return true;
170  }
171 
172  bool sendAck(yarp::os::ConnectionState& proto) override {
173  return true;
174  }
175 
176  bool expectAck(yarp::os::ConnectionState& proto) override {
177  return true;
178  }
179 
180  std::string getBootstrapCarrierName() const override { return {}; }
181 
182  virtual int connect(const yarp::os::Contact& src,
183  const yarp::os::Contact& dest,
184  const yarp::os::ContactStyle& style,
185  int mode,
186  bool reversed) override;
187 
188 private:
189  void processRosHeader(RosHeader& header);
190 
191 };
192 
193 /*
194  * Set up an explicit service carrier, so that we know the
195  * direction of data flow as early as possible. Its name
196  * is "rossrv" (see TcpRosCarrier::getName)
197  */
199  public TcpRosCarrier
200 {
201 public:
203  isService = true;
204  }
205 
206  Carrier *create() const override {
207  return new RosSrvCarrier();
208  }
209 };
210 
211 #endif
#define TCPROS_TRANSLATE_UNKNOWN
Definition: TcpRosCarrier.h:22
Carrier * create() const override
Factory method.
void getHeader(yarp::os::Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Carrier * create() const override
Factory method.
Definition: TcpRosCarrier.h:67
virtual int connect(const yarp::os::Contact &src, const yarp::os::Contact &dest, const yarp::os::ContactStyle &style, int mode, bool reversed) override
Some carrier types may require special connection logic.
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: TcpRosCarrier.h:95
bool isBareMode() const override
Check if carrier excludes type information from payload.
Definition: TcpRosCarrier.h:91
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: TcpRosCarrier.h:87
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
std::string getBootstrapCarrierName() const override
Get the name of the carrier that should be used prior to handshaking, if a port is registered with th...
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition: TcpRosCarrier.h:83
virtual bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer)
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: TcpRosCarrier.h:99
bool reply(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
bool isPush() const override
Check if carrier is "push" or "pull" style.
bool prepareSend(yarp::os::ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
bool expectExtraHeader(yarp::os::ConnectionState &proto) override
Receive any carrier-specific header.
std::string toString() const override
Get name of carrier.
bool write(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Write a message.
bool isActive() const override
Check if carrier is alive and error free.
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
bool expectSenderSpecifier(yarp::os::ConnectionState &proto) override
Expect the name of the sending port.
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition: TcpRosCarrier.h:79
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: TcpRosCarrier.h:71
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: TcpRosCarrier.h:75
bool sendHeader(yarp::os::ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
bool checkHeader(const yarp::os::Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
bool isLocal() const override
Check if carrier operates within a single process.
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
size_t length() const
Definition: Bytes.cpp:25
const char * get() const
Definition: Bytes.cpp:30
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
The basic state of a connection - route, streams in use, etc.
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:27
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
As far as YARP is concerned, on the wire to/from ROS a raw image has:
Definition: WireImage.h:61