YARP
Yet Another Robot Platform
ConnectionRecorder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
12 #include <yarp/os/Vocab.h>
13 
15  reader(nullptr),
16  writer(nullptr),
17  writing(false),
18  wrote(false),
19  skipNextInt(false)
20 {
21 }
22 
24 {
25  reader = wrappedReader;
26  if (reader->isTextMode()) {
27  reader->convertTextMode();
28  }
29  writing = false;
30 }
31 
33 {
34  if (writing) {
35  if (writer->isTextMode()) {
36  writer->convertTextMode();
37  }
38  writing = false;
39  }
40 }
41 
43 {
44  return readerStore;
45 }
46 
48 {
49  return writerStore;
50 }
51 
53 {
54  return wrote;
55 }
56 
58 {
59  bool ok = reader->expectBlock(data, len);
60  if (ok) {
61  readerStore.appendBlock(data, len);
62  }
63  return ok;
64 }
65 
66 std::string yarp::os::impl::ConnectionRecorder::expectText(const char terminatingChar)
67 {
68  std::string str = reader->expectText(terminatingChar);
69  readerStore.appendText(str, terminatingChar);
70  return str;
71 }
72 
74 {
75  std::int8_t x = reader->expectInt8();
76  readerStore.appendInt8(x);
77  return x;
78 }
79 
81 {
82  std::int16_t x = reader->expectInt16();
83  readerStore.appendInt16(x);
84  return x;
85 }
86 
88 {
89  std::int32_t x = reader->expectInt32();
90  if (!skipNextInt) {
91  readerStore.appendInt32(x);
92  } else {
93  skipNextInt = false;
94  }
95  return x;
96 }
97 
99 {
100  std::int64_t x = reader->expectInt64();
101  readerStore.appendInt64(x);
102  return x;
103 }
104 
106 {
107  yarp::conf::float32_t x = reader->expectFloat32();
108  readerStore.appendFloat32(x);
109  return x;
110 }
111 
113 {
114  yarp::conf::float64_t x = reader->expectFloat64();
115  readerStore.appendFloat64(x);
116  return x;
117 }
118 
120 {
121  bool ok = reader->pushInt(x);
122  skipNextInt = skipNextInt || ok;
123  return ok;
124 }
125 
127 {
128  return false;
129 }
130 
132 {
133  return false;
134 }
135 
137 {
138  return false;
139 }
140 
142 {
143  return reader->getSize();
144 }
145 
147 {
148  writer = reader->getWriter();
149  writing = true;
150  wrote = true;
151  return this;
152 }
153 
155 {
156  return reader->getReference();
157 }
158 
160 {
161  return reader->getRemoteContact();
162 }
163 
165 {
166  return reader->getLocalContact();
167 }
168 
170 {
171  // shared
172  if (writing) {
173  return writer->isValid();
174  }
175  return reader->isValid();
176 }
177 
179 {
180  // shared
181  if (writing) {
182  return writer->isActive();
183  }
184  return reader->isActive();
185 }
186 
188 {
189  // shared
190  if (writing) {
191  return writer->isError();
192  }
193  return reader->isError();
194 }
195 
196 void yarp::os::impl::ConnectionRecorder::appendBlock(const char* data, size_t len)
197 {
198  writer->appendBlock(data, len);
199  writerStore.appendBlock(data, len);
200 }
201 
203 {
204  writer->appendInt8(data);
205  writerStore.appendInt8(data);
206 }
207 
209 {
210  writer->appendInt16(data);
211  writerStore.appendInt16(data);
212 }
213 
215 {
216  writer->appendInt32(data);
217  writerStore.appendInt32(data);
218 }
219 
221 {
222  writer->appendInt64(data);
223  writerStore.appendInt64(data);
224 }
225 
227 {
228  writer->appendFloat32(data);
229  writerStore.appendFloat32(data);
230 }
231 
233 {
234  writer->appendFloat64(data);
235  writerStore.appendFloat64(data);
236 }
237 
238 void yarp::os::impl::ConnectionRecorder::appendText(const std::string& str, const char terminate)
239 {
240  writer->appendText(str, terminate);
241  writerStore.appendText(str, terminate);
242 }
243 
245 {
246  writer->appendExternalBlock(data, len);
247  writerStore.appendExternalBlock(data, len);
248 }
249 
251 {
252  writer->declareSizes(argc, argv);
253 }
254 
256 {
257  writer->setReplyHandler(reader);
258 }
259 
261 {
262  writer->setReference(obj);
263 }
264 
266 {
267  if (hasReply()) {
268  connection.appendInt32(BOTTLE_TAG_LIST); // nested structure
269  connection.appendInt32(3); // with three elements
270  connection.appendInt32(BOTTLE_TAG_VOCAB);
271  connection.appendInt32(yarp::os::createVocab('r', 'p', 'c'));
272  bool ok = readerStore.write(connection);
273  if (ok) {
274  writerStore.write(connection);
275  }
276  return ok;
277  }
278  return readerStore.write(connection);
279 }
280 
282 {
283 }
284 
286 {
287  return blank;
288 }
289 
290 
292 {
293  return nullptr;
294 }
295 
297 {
298  return reader->setSize(len);
299 }
#define BOTTLE_TAG_VOCAB
Definition: Bottle.h:25
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
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:39
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
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:51
float float32_t
Definition: numeric.h:50
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22