YARP
Yet Another Robot Platform
ConnectionRecorder.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
8 
9 #include <yarp/os/Vocab.h>
10 
12  reader(nullptr),
13  writer(nullptr),
14  writing(false),
15  wrote(false),
16  skipNextInt(false)
17 {
18 }
19 
21 {
22  reader = wrappedReader;
23  if (reader->isTextMode()) {
24  reader->convertTextMode();
25  }
26  writing = false;
27 }
28 
30 {
31  if (writing) {
32  if (writer->isTextMode()) {
33  writer->convertTextMode();
34  }
35  writing = false;
36  }
37 }
38 
40 {
41  return readerStore;
42 }
43 
45 {
46  return writerStore;
47 }
48 
50 {
51  return wrote;
52 }
53 
55 {
56  bool ok = reader->expectBlock(data, len);
57  if (ok) {
58  readerStore.appendBlock(data, len);
59  }
60  return ok;
61 }
62 
63 std::string yarp::os::impl::ConnectionRecorder::expectText(const char terminatingChar)
64 {
65  std::string str = reader->expectText(terminatingChar);
66  readerStore.appendText(str, terminatingChar);
67  return str;
68 }
69 
71 {
72  std::int8_t x = reader->expectInt8();
73  readerStore.appendInt8(x);
74  return x;
75 }
76 
78 {
79  std::int16_t x = reader->expectInt16();
80  readerStore.appendInt16(x);
81  return x;
82 }
83 
85 {
86  std::int32_t x = reader->expectInt32();
87  if (!skipNextInt) {
88  readerStore.appendInt32(x);
89  } else {
90  skipNextInt = false;
91  }
92  return x;
93 }
94 
96 {
97  std::int64_t x = reader->expectInt64();
98  readerStore.appendInt64(x);
99  return x;
100 }
101 
103 {
104  yarp::conf::float32_t x = reader->expectFloat32();
105  readerStore.appendFloat32(x);
106  return x;
107 }
108 
110 {
111  yarp::conf::float64_t x = reader->expectFloat64();
112  readerStore.appendFloat64(x);
113  return x;
114 }
115 
117 {
118  bool ok = reader->pushInt(x);
119  skipNextInt = skipNextInt || ok;
120  return ok;
121 }
122 
124 {
125  return false;
126 }
127 
129 {
130  return false;
131 }
132 
134 {
135  return false;
136 }
137 
139 {
140  return reader->getSize();
141 }
142 
144 {
145  writer = reader->getWriter();
146  writing = true;
147  wrote = true;
148  return this;
149 }
150 
152 {
153  return reader->getReference();
154 }
155 
157 {
158  return reader->getRemoteContact();
159 }
160 
162 {
163  return reader->getLocalContact();
164 }
165 
167 {
168  // shared
169  if (writing) {
170  return writer->isValid();
171  }
172  return reader->isValid();
173 }
174 
176 {
177  // shared
178  if (writing) {
179  return writer->isActive();
180  }
181  return reader->isActive();
182 }
183 
185 {
186  // shared
187  if (writing) {
188  return writer->isError();
189  }
190  return reader->isError();
191 }
192 
193 void yarp::os::impl::ConnectionRecorder::appendBlock(const char* data, size_t len)
194 {
195  writer->appendBlock(data, len);
196  writerStore.appendBlock(data, len);
197 }
198 
200 {
201  writer->appendInt8(data);
202  writerStore.appendInt8(data);
203 }
204 
206 {
207  writer->appendInt16(data);
208  writerStore.appendInt16(data);
209 }
210 
212 {
213  writer->appendInt32(data);
214  writerStore.appendInt32(data);
215 }
216 
218 {
219  writer->appendInt64(data);
220  writerStore.appendInt64(data);
221 }
222 
224 {
225  writer->appendFloat32(data);
226  writerStore.appendFloat32(data);
227 }
228 
230 {
231  writer->appendFloat64(data);
232  writerStore.appendFloat64(data);
233 }
234 
235 void yarp::os::impl::ConnectionRecorder::appendText(const std::string& str, const char terminate)
236 {
237  writer->appendText(str, terminate);
238  writerStore.appendText(str, terminate);
239 }
240 
242 {
243  writer->appendExternalBlock(data, len);
244  writerStore.appendExternalBlock(data, len);
245 }
246 
248 {
249  writer->declareSizes(argc, argv);
250 }
251 
253 {
254  writer->setReplyHandler(reader);
255 }
256 
258 {
259  writer->setReference(obj);
260 }
261 
263 {
264  if (hasReply()) {
265  connection.appendInt32(BOTTLE_TAG_LIST); // nested structure
266  connection.appendInt32(3); // with three elements
267  connection.appendInt32(BOTTLE_TAG_VOCAB32);
268  connection.appendInt32(yarp::os::createVocab32('r', 'p', 'c'));
269  bool ok = readerStore.write(connection);
270  if (ok) {
271  writerStore.write(connection);
272  }
273  return ok;
274  }
275  return readerStore.write(connection);
276 }
277 
279 {
280 }
281 
283 {
284  return blank;
285 }
286 
287 
289 {
290  return nullptr;
291 }
292 
294 {
295  return reader->setSize(len);
296 }
#define BOTTLE_TAG_LIST
Definition: Bottle.h:28
#define BOTTLE_TAG_VOCAB32
Definition: Bottle.h:23
An interface for reading from a network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
An interface for writing to a network connection.
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
A base class for nested structures that can be searched.
Definition: Searchable.h:66
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:33
A helper for creating cached object descriptions.
bool expectBlock(char *data, size_t len) override
Read a block of data from the network connection.
std::string expectText(const char terminatingChar) override
Read some text from the network connection.
void declareSizes(int argc, int *argv) override
If you can easily determine how many blocks there are in a message, call this first,...
yarp::conf::float64_t expectFloat64() override
Read a 64-bit floating point number from the network connection.
yarp::os::ConnectionWriter * getWriter() override
Gets a way to reply to the message, if possible.
void setReference(yarp::os::Portable *obj) override
Stores a direct pointer to the object being sent.
bool convertTextMode() override
Reads in a standard description in text mode, and converts it to a standard description in binary.
const yarp::os::Searchable & getConnectionModifiers() const override
Access modifiers associated with the connection, if any.
std::int32_t expectInt32() override
Read a 32-bit integer from the network connection.
void setReplyHandler(yarp::os::PortReader &reader) override
This sets a handler to deal with replies to the message.
std::int64_t expectInt64() override
Read a 64-bit integer from the network connection.
void appendText(const std::string &str, const char terminate) override
Send a terminated string to the network connection.
yarp::os::SizedWriter * getBuffer() const override
void requestDrop() override
Tag the connection to be dropped after the current message.
void init(yarp::os::ConnectionReader *wrappedReader)
Call this to wrap a specific ConnectionReader.
void fini()
Call this when all reading/writing has been done.
void appendFloat64(yarp::conf::float64_t data) override
Send a representation of a 64-bit floating point number to the network connection.
yarp::os::Contact getLocalContact() const override
Gets information about who is receiving the data, if that information is available.
size_t getSize() const override
Checks how much data is available.
void appendInt8(std::int8_t data) override
Send a representation of a 8-bit integer to the network connection.
const yarp::os::impl::BufferedConnectionWriter & getMessage() const
bool isTextMode() const override
Check if the connection is text mode.
void appendFloat32(yarp::conf::float32_t data) override
Send a representation of a 32-bit floating point number to the network connection.
void appendBlock(const char *data, size_t len) override
Send a block of data to the network connection.
yarp::conf::float32_t expectFloat32() override
Read a 32-bit floating point number from the network connection.
std::int8_t expectInt8() override
Read a 8-bit integer from the network connection.
bool isBareMode() const override
Check if the connection is bare mode.
void appendInt64(std::int64_t data) override
Send a representation of a 64-bit integer to the network connection.
void appendInt32(std::int32_t data) override
Send a representation of a 32-bit integer to the network connection.
yarp::os::Portable * getReference() const override
Get a direct pointer to the object being sent, if possible.
bool pushInt(int x) override
Store an integer to return on the next call to expectInt()
void appendExternalBlock(const char *data, size_t len) override
Send a block of data to the network connection, without making a copy.
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
yarp::os::Contact getRemoteContact() const override
Gets information about who is supplying the data being read, if that information is available.
std::int16_t expectInt16() override
Read a 16-bit integer from the network connection.
void appendInt16(std::int16_t data) override
Send a representation of a 16-bit integer to the network connection.
const yarp::os::impl::BufferedConnectionWriter & getReply() const
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:28