YARP
Yet Another Robot Platform
WireTwiddler.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 YARP2_WIRETWIDDLER
10 #define YARP2_WIRETWIDDLER
11 
12 #include <yarp/conf/numeric.h>
13 #include <yarp/os/Bottle.h>
14 #include <yarp/os/ManagedBytes.h>
15 #include <yarp/os/NetInt32.h>
16 
17 #include <yarp/os/SizedWriter.h>
18 #include <yarp/os/InputStream.h>
20 #include <yarp/os/Bytes.h>
21 
23 
24 #include <vector>
25 #include <string>
26 
27 namespace yarp {
28 namespace wire_rep_utils {
29 
31 {
32 public:
35  int length;
38  char *byte_start;
43  bool computing;
44  std::string origin;
45  std::string var_name;
46  int flavor;
48  buffer_start = 0;
49  buffer_length = 0;
50  length = 0;
51  unit_length = 0;
52  byte_start = nullptr;
53  byte_length = 0;
54  ignore_external = false;
55  wire_unit_length = -1;
56  save_external = false;
57  load_external = false;
58  computing = false;
59  flavor = 0;
60  }
61 
62  char *getStart() const { return byte_start; }
63  int getLength() const { return byte_length; }
64  int getUnitLength() const { return unit_length; }
65  int getExternalLength() const { return unit_length; }
66  bool shouldIgnoreExternal() const { return ignore_external; }
67  const std::string& getOrigin() const { return origin; }
68 };
69 
70 
72 {
73 public:
75  buffer_start = 0;
76  writer = nullptr;
77  }
78 
79  virtual ~WireTwiddler() {
80  if (writer) delete writer;
81  writer = nullptr;
82  }
83 
84  bool configure(const char *txt, const char *prompt);
85 
86 
87  std::string fromTemplate(const yarp::os::Bottle& msg);
88 
89  /*
90  bool setHeaderFormat(const char *txt) {
91  headerFormat = txt;
92  }
93  */
94 
95 private:
96 
97  int buffer_start;
98  std::vector<yarp::os::NetInt32> buffer;
99  std::vector<WireTwiddlerGap> gaps;
101  std::string prompt;
102 
103 public:
104  void show();
105  int configure(yarp::os::Bottle& desc, int offset, bool& ignored,
106  const std::string& vtag);
107 
108  int getGapCount() {
109  return (int)gaps.size();
110  }
111 
112  void clear() {
113  buffer_start = 0;
114  buffer.clear();
115  gaps.clear();
116  }
117 
118  const WireTwiddlerGap& getGap(int index) {
119  return gaps[index];
120  }
121 
122  std::string toString() const;
123 
124  const std::string& getPrompt() const {
125  return prompt;
126  }
127 
128  bool read(yarp::os::Bottle& bot, const yarp::os::Bytes& data);
129 
131 };
132 
133 
134 /*
135 
136  State:
137  index, sent
138 
139  */
140 
142  public yarp::os::InputStream
143 {
144 private:
146  WireTwiddler& twiddler;
147  char *cursor;
148  int sent;
149  int consumed;
150  int override_length;
151  int index;
152  bool recite;
153  yarp::os::NetInt32 lengthBuffer;
154  int pending_length;
155  int pending_strings;
156  int pending_string_length;
157  int pending_string_data;
159  yarp::os::Property prop;
160 public:
162  WireTwiddler& twiddler) : is(is),
163  twiddler(twiddler) {
164  reset();
165  }
166 
167  void reset() {
168  recite = false;
169  index = -1;
170  sent = 0;
171  consumed = 0;
172  cursor = nullptr;
173  pending_length = 0;
174  pending_strings = 0;
175  pending_string_length = 0;
176  pending_string_data = 0;
177  override_length = -1;
178  lengthBuffer = 0;
179  }
180 
181  virtual ~WireTwiddlerReader() {}
182 
185 
186  void close() override { is.close(); }
187 
188  bool isOk() const override { return is.isOk(); }
189 
191  yarp::os::Bytes& b,
192  const WireTwiddlerGap& gap);
193 
194  void compute(const WireTwiddlerGap& gap);
195 };
196 
197 
199 {
200 public:
201  char *src;
202  int len;
203  int offset;
204 
205  WireTwiddlerSrc(char *src, int len, int offset) {
206  this->src = src;
207  this->len = len;
208  this->offset = offset;
209  }
210 };
211 
213  public yarp::os::SizedWriter
214 {
215 private:
216  yarp::os::SizedWriter *parent;
217  WireTwiddler *twiddler;
218  std::vector<WireTwiddlerSrc> srcs;
219  int block;
220  int lastBlock;
221  int offset;
222  const char *blockPtr;
223  int blockLen;
224  yarp::os::NetInt32 lengthBuffer;
225  yarp::os::Bytes lengthBytes;
227  yarp::os::ManagedBytes scratch;
228  int accumOffset;
229  const char *activeEmit;
230  const WireTwiddlerGap *activeGap;
231  int activeEmitLength;
232  int activeEmitOffset;
233  const char *activeCheck;
234  bool errorState;
235  size_t scratchOffset;
236  yarp::os::NetInt32 codeExpected;
237  yarp::os::NetInt32 codeReceived;
238 
239  void showBrokenExpectation(const yarp::os::NetInt32& expected,
240  const yarp::os::NetInt32& received,
241  int evidence);
242 public:
244  WireTwiddler& twiddler) :
245  parent(&parent),
246  twiddler(&twiddler),
247  srcs(),
248  block(0),
249  lastBlock(0),
250  offset(0),
251  blockPtr(nullptr),
252  blockLen(0),
253  lengthBuffer(0),
254  lengthBytes(),
255  zeros(0),
256  scratch(0),
257  accumOffset(0),
258  activeEmit(nullptr),
259  activeGap(nullptr),
260  activeEmitLength(0),
261  activeEmitOffset(0),
262  activeCheck(nullptr),
263  errorState(false),
264  scratchOffset(0),
265  codeExpected(),
266  codeReceived()
267  {
268  update();
269  }
270 
272  parent(nullptr),
273  twiddler(nullptr),
274  srcs(),
275  block(0),
276  lastBlock(0),
277  offset(0),
278  blockPtr(nullptr),
279  blockLen(0),
280  lengthBuffer(0),
281  lengthBytes(),
282  zeros(0),
283  scratch(0),
284  accumOffset(0),
285  activeEmit(nullptr),
286  activeGap(nullptr),
287  activeEmitLength(0),
288  activeEmitOffset(0),
289  activeCheck(nullptr),
290  errorState(false),
291  scratchOffset(0),
292  codeExpected(),
293  codeReceived()
294  {}
295 
297 
299  WireTwiddler& twiddler) {
300  this->parent = &parent;
301  this->twiddler = &twiddler;
302  update();
303  }
304 
305  bool update();
306 
307  size_t length() const override {
308  return srcs.size();
309  }
310 
311  size_t headerLength() const override {
312  return 0;
313  }
314 
315  size_t length(size_t index) const override {
316  return srcs[index].len;
317  }
318 
319  const char *data(size_t index) const override {
320  if (srcs[index].offset<0) return srcs[index].src;
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 wire_rep_utils
364 } // namespace yarp
365 
366 #endif
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
An interface for writing to a network connection.
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
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:23
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:25
const char * get() const
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 class for storing options and configuration information.
Definition: Property.h:37
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
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:67
WireTwiddlerReader(yarp::os::InputStream &is, WireTwiddler &twiddler)
Definition: WireTwiddler.h:161
void close() override
Terminate the stream.
Definition: WireTwiddler.h:186
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: WireTwiddler.h:188
WireTwiddlerSrc(char *src, int len, int offset)
Definition: WireTwiddler.h:205
void startWrite() const override
Call when writing is about to begin.
Definition: WireTwiddler.h:354
yarp::os::Portable * getReference() override
Definition: WireTwiddler.h:328
const char * data(size_t index) const override
Definition: WireTwiddler.h:319
size_t headerLength() const override
Definition: WireTwiddler.h:311
void attach(yarp::os::SizedWriter &parent, WireTwiddler &twiddler)
Definition: WireTwiddler.h:298
size_t length(size_t index) const override
Definition: WireTwiddler.h:315
yarp::os::PortReader * getReplyHandler() override
Definition: WireTwiddler.h:324
WireTwiddlerWriter(yarp::os::SizedWriter &parent, WireTwiddler &twiddler)
Definition: WireTwiddler.h:243
void stopWrite() const override
Call when all writing is finished.
Definition: WireTwiddler.h:358
const std::string & getPrompt() const
Definition: WireTwiddler.h:124
const WireTwiddlerGap & getGap(int index)
Definition: WireTwiddler.h:118
std::string toString(const T &value)
convert an arbitrary type to string.
::ssize_t ssize_t
Definition: numeric.h:60
yarp::sig::Vector zeros(int s)
Creates a vector of zeros (defined in Math.h).
Definition: math.cpp:552
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:33
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:656
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:804
The main, catch-all namespace for YARP.
Definition: environment.h:18
#define YARP_wire_rep_utils_API
Definition: api.h:15