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
TcpRosCarrier::expectAck
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition: TcpRosCarrier.h:176
WireTwiddler.h
TcpRosCarrier::supportReply
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition: TcpRosCarrier.h:103
yarp::os::ContactStyle
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:27
TcpRosCarrier::isBareMode
bool isBareMode() const override
Check if carrier excludes type information from payload.
Definition: TcpRosCarrier.h:91
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
yarp::wire_rep_utils::RosWireImage
As far as YARP is concerned, on the wire to/from ROS a raw image has:
Definition: WireImage.h:61
RosSrvCarrier::RosSrvCarrier
RosSrvCarrier()
Definition: TcpRosCarrier.h:202
TcpRosCarrier::create
Carrier * create() const override
Factory method.
Definition: TcpRosCarrier.h:67
TcpRosCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: TcpRosCarrier.h:75
RosSrvCarrier
Definition: TcpRosCarrier.h:200
TcpRosCarrier::isService
bool isService
Definition: TcpRosCarrier.h:53
TcpRosCarrier::expectReplyToHeader
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: TcpRosCarrier.cpp:157
TcpRosCarrier::getHeader
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...
Definition: TcpRosCarrier.h:120
TcpRosCarrier::isTextMode
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: TcpRosCarrier.h:87
TcpRosCarrier::respondToHeader
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
Definition: TcpRosCarrier.h:146
TcpRosCarrier::expectIndex
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: TcpRosCarrier.h:168
RosSrvCarrier::create
Carrier * create() const override
Factory method.
Definition: TcpRosCarrier.h:206
yarp::wire_rep_utils::SizedWriterTail
Definition: WireBottle.h:22
TcpRosCarrier::sendIndex
virtual bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer)
Definition: TcpRosCarrier.h:164
yarp::os::Bytes::get
const char * get() const
Definition: Bytes.cpp:30
TcpRosCarrier::getBootstrapCarrierName
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...
Definition: TcpRosCarrier.h:180
yarp::os::Bytes::length
size_t length() const
Definition: Bytes.cpp:25
RosHeader.h
TcpRosCarrier::expectSenderSpecifier
bool expectSenderSpecifier(yarp::os::ConnectionState &proto) override
Expect the name of the sending port.
Definition: TcpRosCarrier.cpp:230
TcpRosCarrier::checkHeader
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...
Definition: TcpRosCarrier.cpp:36
yarp::wire_rep_utils::WireImage
Definition: WireImage.h:175
TcpRosCarrier
Definition: TcpRosCarrier.h:29
TcpRosCarrier::prepareSend
bool prepareSend(yarp::os::ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
Definition: TcpRosCarrier.h:134
TcpRosCarrier::sendHeader
bool sendHeader(yarp::os::ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition: TcpRosCarrier.cpp:93
yarp::wire_rep_utils::WireTwiddlerWriter
Definition: WireTwiddler.h:214
TcpRosCarrier::canOffer
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition: TcpRosCarrier.h:83
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
TcpRosCarrier::write
bool write(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Write a message.
Definition: TcpRosCarrier.cpp:337
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
TcpRosCarrier::connect
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.
Definition: TcpRosCarrier.cpp:451
TcpRosCarrier::canAccept
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition: TcpRosCarrier.h:79
yarp::wire_rep_utils::WireTwiddler
Definition: WireTwiddler.h:72
TcpRosCarrier::isPush
bool isPush() const override
Check if carrier is "push" or "pull" style.
Definition: TcpRosCarrier.h:107
Carrier.h
TCPROS_TRANSLATE_UNKNOWN
#define TCPROS_TRANSLATE_UNKNOWN
Definition: TcpRosCarrier.h:22
TcpRosCarrier::canEscape
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: TcpRosCarrier.h:95
RosHeader
Definition: RosHeader.h:16
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
TcpRosCarrier::TcpRosCarrier
TcpRosCarrier()
Definition: TcpRosCarrier.h:55
TcpRosCarrier::reply
bool reply(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Definition: TcpRosCarrier.cpp:442
TcpRosCarrier::sendAck
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition: TcpRosCarrier.h:172
WireBottle.h
TcpRosCarrier::setParameters
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: TcpRosCarrier.cpp:28
TcpRosCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: TcpRosCarrier.h:71
TcpRosCarrier::toString
std::string toString() const override
Get name of carrier.
Definition: TcpRosCarrier.h:116
TcpRosCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: TcpRosCarrier.h:99
TcpRosStream.h
WireImage.h
TcpRosCarrier::isLocal
bool isLocal() const override
Check if carrier operates within a single process.
Definition: TcpRosCarrier.h:112
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
TcpRosCarrier::expectExtraHeader
bool expectExtraHeader(yarp::os::ConnectionState &proto) override
Receive any carrier-specific header.
Definition: TcpRosCarrier.h:142
TcpRosCarrier::isActive
bool isActive() const override
Check if carrier is alive and error free.
Definition: TcpRosCarrier.h:153