YARP
Yet Another Robot Platform
WireTwiddler.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 YARP2_WIRETWIDDLER
7#define YARP2_WIRETWIDDLER
8
9#include <yarp/conf/numeric.h>
10#include <yarp/os/Bottle.h>
12#include <yarp/os/NetInt32.h>
13
14#include <yarp/os/SizedWriter.h>
15#include <yarp/os/InputStream.h>
17#include <yarp/os/Bytes.h>
18
20
21#include <vector>
22#include <string>
23
24namespace yarp::wire_rep_utils {
25
27{
28public:
31 int length;
40 std::string origin;
41 std::string var_name;
42 int flavor;
44 buffer_start = 0;
45 buffer_length = 0;
46 length = 0;
47 unit_length = 0;
48 byte_start = nullptr;
49 byte_length = 0;
50 ignore_external = false;
52 save_external = false;
53 load_external = false;
54 computing = false;
55 flavor = 0;
56 }
57
58 char *getStart() const { return byte_start; }
59 int getLength() const { return byte_length; }
60 int getUnitLength() const { return unit_length; }
61 int getExternalLength() const { return unit_length; }
62 bool shouldIgnoreExternal() const { return ignore_external; }
63 const std::string& getOrigin() const { return origin; }
64};
65
66
68{
69public:
71 buffer_start = 0;
72 writer = nullptr;
73 }
74
75 virtual ~WireTwiddler() {
76 if (writer) {
77 delete writer;
78 }
79 writer = nullptr;
80 }
81
82 bool configure(const char *txt, const char *prompt);
83
84
85 std::string fromTemplate(const yarp::os::Bottle& msg);
86
87 /*
88 bool setHeaderFormat(const char *txt) {
89 headerFormat = txt;
90 }
91 */
92
93private:
94
95 int buffer_start;
96 std::vector<yarp::os::NetInt32> buffer;
97 std::vector<WireTwiddlerGap> gaps;
99 std::string prompt;
100
101public:
102 void show();
103 int configure(yarp::os::Bottle& desc, int offset, bool& ignored,
104 const std::string& vtag);
105
107 return (int)gaps.size();
108 }
109
110 void clear() {
111 buffer_start = 0;
112 buffer.clear();
113 gaps.clear();
114 }
115
116 const WireTwiddlerGap& getGap(int index) {
117 return gaps[index];
118 }
119
120 std::string toString() const;
121
122 const std::string& getPrompt() const {
123 return prompt;
124 }
125
126 bool read(yarp::os::Bottle& bot, const yarp::os::Bytes& data);
127
129};
130
131
132/*
133
134 State:
135 index, sent
136
137 */
138
141{
142private:
144 WireTwiddler& twiddler;
145 char *cursor;
146 int sent;
147 int consumed;
148 int override_length;
149 int index;
150 bool recite;
151 yarp::os::NetInt32 lengthBuffer;
152 int pending_length;
153 int pending_strings;
154 int pending_string_length;
155 int pending_string_data;
158public:
160 WireTwiddler& twiddler) : is(is),
161 twiddler(twiddler) {
162 reset();
163 }
164
165 void reset() {
166 recite = false;
167 index = -1;
168 sent = 0;
169 consumed = 0;
170 cursor = nullptr;
171 pending_length = 0;
172 pending_strings = 0;
173 pending_string_length = 0;
174 pending_string_data = 0;
175 override_length = -1;
176 lengthBuffer = 0;
177 }
178
180
183
184 void close() override { is.close(); }
185
186 bool isOk() const override { return is.isOk(); }
187
190 const WireTwiddlerGap& gap);
191
192 void compute(const WireTwiddlerGap& gap);
193};
194
195
197{
198public:
199 char *src;
200 int len;
202
203 WireTwiddlerSrc(char *src, int len, int offset) {
204 this->src = src;
205 this->len = len;
206 this->offset = offset;
207 }
208};
209
212{
213private:
214 yarp::os::SizedWriter *parent;
215 WireTwiddler *twiddler;
216 std::vector<WireTwiddlerSrc> srcs;
217 int block;
218 int lastBlock;
219 int offset;
220 const char *blockPtr;
221 int blockLen;
222 yarp::os::NetInt32 lengthBuffer;
223 yarp::os::Bytes lengthBytes;
226 int accumOffset;
227 const char *activeEmit;
228 const WireTwiddlerGap *activeGap;
229 int activeEmitLength;
230 int activeEmitOffset;
231 const char *activeCheck;
232 bool errorState;
233 size_t scratchOffset;
234 yarp::os::NetInt32 codeExpected;
235 yarp::os::NetInt32 codeReceived;
236
237 void showBrokenExpectation(const yarp::os::NetInt32& expected,
238 const yarp::os::NetInt32& received,
239 int evidence);
240public:
242 WireTwiddler& twiddler) :
243 parent(&parent),
244 twiddler(&twiddler),
245 srcs(),
246 block(0),
247 lastBlock(0),
248 offset(0),
249 blockPtr(nullptr),
250 blockLen(0),
251 lengthBuffer(0),
252 lengthBytes(),
253 zeros(0),
254 scratch(0),
255 accumOffset(0),
256 activeEmit(nullptr),
257 activeGap(nullptr),
258 activeEmitLength(0),
259 activeEmitOffset(0),
260 activeCheck(nullptr),
261 errorState(false),
262 scratchOffset(0),
263 codeExpected(),
264 codeReceived()
265 {
266 update();
267 }
268
270 parent(nullptr),
271 twiddler(nullptr),
272 srcs(),
273 block(0),
274 lastBlock(0),
275 offset(0),
276 blockPtr(nullptr),
277 blockLen(0),
278 lengthBuffer(0),
279 lengthBytes(),
280 zeros(0),
281 scratch(0),
282 accumOffset(0),
283 activeEmit(nullptr),
284 activeGap(nullptr),
285 activeEmitLength(0),
286 activeEmitOffset(0),
287 activeCheck(nullptr),
288 errorState(false),
289 scratchOffset(0),
290 codeExpected(),
291 codeReceived()
292 {}
293
295
297 WireTwiddler& twiddler) {
298 this->parent = &parent;
299 this->twiddler = &twiddler;
300 update();
301 }
302
303 bool update();
304
305 size_t length() const override {
306 return srcs.size();
307 }
308
309 size_t headerLength() const override {
310 return 0;
311 }
312
313 size_t length(size_t index) const override {
314 return srcs[index].len;
315 }
316
317 const char *data(size_t index) const override {
318 if (srcs[index].offset < 0) {
319 return srcs[index].src;
320 }
321 return scratch.get()+srcs[index].offset;
322 }
323
325 return parent->getReplyHandler();
326 }
327
329 return parent->getReference();
330 }
331
332 bool skip(const char *start, int len);
333
334 bool transform(const WireTwiddlerGap& gap);
335
336 bool pass(int len);
337
338 bool pad(size_t len);
339
340 bool readLengthAndPass(int unitLength, const WireTwiddlerGap *gap = nullptr);
341
342 bool advance(int length, bool shouldEmit, bool shouldAccum=false,
343 bool shouldCheck=false);
344
345 bool emit(const char *src, int len);
346
347 int readLength();
348
349 //virtual void write(OutputStream& os);
350 bool dropRequested() override {
351 return false;
352 }
353
354 void startWrite() const override {
355 parent->startWrite();
356 }
357
358 void stopWrite() const override {
359 parent->stopWrite();
360 }
361};
362
363} // namespace yarp::wire_rep_utils
364
365#endif
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:64
A simple abstraction for a block of bytes.
Definition: Bytes.h:24
An interface for writing to a network connection.
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:25
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:20
virtual void close()=0
Terminate the stream.
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:21
const char * get() const
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:25
A class for storing options and configuration information.
Definition: Property.h:33
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:32
virtual Portable * getReference()=0
virtual void stopWrite() const =0
Call when all writing is finished.
virtual PortReader * getReplyHandler()=0
virtual void startWrite() const =0
Call when writing is about to begin.
const std::string & getOrigin() const
Definition: WireTwiddler.h:63
WireTwiddlerReader(yarp::os::InputStream &is, WireTwiddler &twiddler)
Definition: WireTwiddler.h:159
void close() override
Terminate the stream.
Definition: WireTwiddler.h:184
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: WireTwiddler.h:186
WireTwiddlerSrc(char *src, int len, int offset)
Definition: WireTwiddler.h:203
const char * data(size_t index) const override
Definition: WireTwiddler.h:317
yarp::os::Portable * getReference() override
Definition: WireTwiddler.h:328
yarp::os::PortReader * getReplyHandler() override
Definition: WireTwiddler.h:324
void startWrite() const override
Call when writing is about to begin.
Definition: WireTwiddler.h:354
size_t headerLength() const override
Definition: WireTwiddler.h:309
void attach(yarp::os::SizedWriter &parent, WireTwiddler &twiddler)
Definition: WireTwiddler.h:296
size_t length(size_t index) const override
Definition: WireTwiddler.h:313
WireTwiddlerWriter(yarp::os::SizedWriter &parent, WireTwiddler &twiddler)
Definition: WireTwiddler.h:241
void stopWrite() const override
Call when all writing is finished.
Definition: WireTwiddler.h:358
const WireTwiddlerGap & getGap(int index)
Definition: WireTwiddler.h:116
const std::string & getPrompt() const
Definition: WireTwiddler.h:122
std::string toString(const T &value)
convert an arbitrary type to string.
::ssize_t ssize_t
Definition: numeric.h:86
yarp::sig::Vector zeros(int s)
Creates a vector of zeros (defined in Math.h).
Definition: math.cpp:576
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:29
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:915
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1091
#define YARP_wire_rep_utils_API
Definition: api.h:12