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