YARP
Yet Another Robot Platform
TcpRosCarrier.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef TCPROSCARRIER_INC
7 #define TCPROSCARRIER_INC
8 
9 #include <yarp/os/Carrier.h>
10 
14 
15 #include "RosHeader.h"
16 #include "TcpRosStream.h"
17 
18 #define TCPROS_TRANSLATE_INHIBIT (-1)
19 #define TCPROS_TRANSLATE_UNKNOWN (0)
20 #define TCPROS_TRANSLATE_IMAGE (1)
21 #define TCPROS_TRANSLATE_BOTTLE_BLOB (2)
22 #define TCPROS_TRANSLATE_TWIDDLER (3)
23 
25  public yarp::os::Carrier
26 {
27 private:
28  bool firstRound;
29  bool sender;
30  int headerLen1;
31  int headerLen2;
32  int raw;
33  int translate;
37  int seq;
40  std::string kind;
41  bool persistent;
42  std::string wire_type;
43  std::string user_type;
44  std::string md5sum;
45  std::string message_definition;
46 
47  std::string getRosType(yarp::os::ConnectionState& proto);
48 
49 protected:
50  bool isService;
51 public:
53  firstRound = true;
54  sender = false;
55  headerLen1 = 0;
56  headerLen2 = 0;
57  isService = false;
58  raw = -1;
59  translate = TCPROS_TRANSLATE_UNKNOWN;
60  seq = 0;
61  persistent = true;
62  }
63 
64  Carrier *create() const override {
65  return new TcpRosCarrier();
66  }
67 
68  std::string getName() const override {
69  return isService?"rossrv":"tcpros";
70  }
71 
72  bool isConnectionless() const override {
73  return false;
74  }
75 
76  bool canAccept() const override {
77  return true;
78  }
79 
80  bool canOffer() const override {
81  return true;
82  }
83 
84  bool isTextMode() const override {
85  return false;
86  }
87 
88  bool isBareMode() const override {
89  return true;
90  }
91 
92  bool canEscape() const override {
93  return false;
94  }
95 
96  bool requireAck() const override {
97  return false;
98  }
99 
100  bool supportReply() const override {
101  return true;
102  }
103 
104  bool isPush() const override {
105  // if topic-like, pull ; if service-like, push!
106  return isService;
107  }
108 
109  bool isLocal() const override {
110  return false;
111  }
112 
113  std::string toString() const override {
114  return isService?"rossrv_carrier":"tcpros_carrier";
115  }
116 
117  void getHeader(yarp::os::Bytes& header) const override {
118  // no header, will need to do some fancy footwork
119  const char *target = "NONONONO";
120  for (size_t i=0; i<8 && i<header.length(); i++) {
121  header.get()[i] = target[i];
122  }
123  }
124 
125  bool checkHeader(const yarp::os::Bytes& header) override;
126 
127  void setParameters(const yarp::os::Bytes& header) override;
128 
129  // Now, the initial hand-shaking
130 
131  bool prepareSend(yarp::os::ConnectionState& proto) override {
132  return true;
133  }
134 
135  bool sendHeader(yarp::os::ConnectionState& proto) override;
136 
137  bool expectSenderSpecifier(yarp::os::ConnectionState& proto) override;
138 
140  return true;
141  }
142 
144  sender = false;
145  return true;
146  }
147 
148  bool expectReplyToHeader(yarp::os::ConnectionState& proto) override;
149 
150  bool isActive() const override {
151  return true;
152  }
153 
154 
155  // Payload time!
156 
157  bool write(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
158 
159  bool reply(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
160 
162  return true;
163  }
164 
165  bool expectIndex(yarp::os::ConnectionState& proto) override {
166  return true;
167  }
168 
169  bool sendAck(yarp::os::ConnectionState& proto) override {
170  return true;
171  }
172 
173  bool expectAck(yarp::os::ConnectionState& proto) override {
174  return true;
175  }
176 
177  std::string getBootstrapCarrierName() const override { return {}; }
178 
179  virtual int connect(const yarp::os::Contact& src,
180  const yarp::os::Contact& dest,
181  const yarp::os::ContactStyle& style,
182  int mode,
183  bool reversed) override;
184 
185 private:
186  void processRosHeader(RosHeader& header);
187 
188 };
189 
190 /*
191  * Set up an explicit service carrier, so that we know the
192  * direction of data flow as early as possible. Its name
193  * is "rossrv" (see TcpRosCarrier::getName)
194  */
196  public TcpRosCarrier
197 {
198 public:
200  isService = true;
201  }
202 
203  Carrier *create() const override {
204  return new RosSrvCarrier();
205  }
206 };
207 
208 #endif
#define TCPROS_TRANSLATE_UNKNOWN
Definition: TcpRosCarrier.h:19
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:64
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:92
bool isBareMode() const override
Check if carrier excludes type information from payload.
Definition: TcpRosCarrier.h:88
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: TcpRosCarrier.h:84
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:80
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:96
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:76
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: TcpRosCarrier.h:68
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: TcpRosCarrier.h:72
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:25
size_t length() const
Definition: Bytes.cpp:22
const char * get() const
Definition: Bytes.cpp:27
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:45
The basic state of a connection - route, streams in use, etc.
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:24
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:33
As far as YARP is concerned, on the wire to/from ROS a raw image has:
Definition: WireImage.h:58