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
yarp::wire_rep_utils::WireTwiddlerWriter::headerLength
size_t headerLength() const override
Definition: WireTwiddler.h:311
yarp::wire_rep_utils::WireTwiddlerGap::wire_unit_length
int wire_unit_length
Definition: WireTwiddler.h:37
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::wire_rep_utils::WireTwiddlerReader::WireTwiddlerReader
WireTwiddlerReader(yarp::os::InputStream &is, WireTwiddler &twiddler)
Definition: WireTwiddler.h:161
yarp::wire_rep_utils::WireTwiddler::getGap
const WireTwiddlerGap & getGap(int index)
Definition: WireTwiddler.h:118
YARP_wire_rep_utils_API
#define YARP_wire_rep_utils_API
Definition: api.h:15
yarp::wire_rep_utils::WireTwiddlerWriter::getReference
yarp::os::Portable * getReference() override
Definition: WireTwiddler.h:328
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
yarp::wire_rep_utils::WireTwiddlerWriter::attach
void attach(yarp::os::SizedWriter &parent, WireTwiddler &twiddler)
Definition: WireTwiddler.h:298
yarp::os::InputStream::close
virtual void close()=0
Terminate the stream.
ConnectionWriter.h
yarp::math::zeros
yarp::sig::Vector zeros(int s)
Creates a vector of zeros (defined in Math.h).
Definition: math.cpp:552
NetInt32.h
yarp::os::SizedWriter::startWrite
virtual void startWrite() const =0
Call when writing is about to begin.
yarp::wire_rep_utils::WireTwiddlerSrc::len
int len
Definition: WireTwiddler.h:202
numeric.h
yarp::wire_rep_utils::WireTwiddlerReader::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: WireTwiddler.h:188
yarp::wire_rep_utils::WireTwiddlerGap::shouldIgnoreExternal
bool shouldIgnoreExternal() const
Definition: WireTwiddler.h:66
yarp::wire_rep_utils::WireTwiddlerGap::getLength
int getLength() const
Definition: WireTwiddler.h:63
yarp::wire_rep_utils::WireTwiddlerWriter::~WireTwiddlerWriter
virtual ~WireTwiddlerWriter()
yarp::wire_rep_utils::WireTwiddlerWriter::getReplyHandler
yarp::os::PortReader * getReplyHandler() override
Definition: WireTwiddler.h:324
yarp::wire_rep_utils::WireTwiddlerWriter::stopWrite
void stopWrite() const override
Call when all writing is finished.
Definition: WireTwiddler.h:358
yarp::wire_rep_utils::WireTwiddlerWriter::data
const char * data(size_t index) const override
Definition: WireTwiddler.h:319
yarp::wire_rep_utils::WireTwiddlerGap::buffer_length
int buffer_length
Definition: WireTwiddler.h:34
yarp::wire_rep_utils::WireTwiddlerReader::~WireTwiddlerReader
virtual ~WireTwiddlerReader()
Definition: WireTwiddler.h:181
yarp::wire_rep_utils::WireTwiddlerGap::buffer_start
int buffer_start
Definition: WireTwiddler.h:33
yarp::wire_rep_utils::WireTwiddlerGap::getOrigin
const std::string & getOrigin() const
Definition: WireTwiddler.h:67
yarp::wire_rep_utils::WireTwiddlerSrc
Definition: WireTwiddler.h:199
yarp::wire_rep_utils::WireTwiddlerWriter::startWrite
void startWrite() const override
Call when writing is about to begin.
Definition: WireTwiddler.h:354
yarp::wire_rep_utils::WireTwiddlerWriter::WireTwiddlerWriter
WireTwiddlerWriter(yarp::os::SizedWriter &parent, WireTwiddler &twiddler)
Definition: WireTwiddler.h:243
yarp::wire_rep_utils::WireTwiddlerReader::close
void close() override
Terminate the stream.
Definition: WireTwiddler.h:186
yarp::wire_rep_utils::WireTwiddlerGap::WireTwiddlerGap
WireTwiddlerGap()
Definition: WireTwiddler.h:47
yarp::wire_rep_utils::WireTwiddlerWriter::dropRequested
bool dropRequested() override
Definition: WireTwiddler.h:350
yarp::os::SizedWriter::getReference
virtual Portable * getReference()=0
ManagedBytes.h
yarp::os::ManagedBytes
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
yarp::wire_rep_utils::WireTwiddlerGap::computing
bool computing
Definition: WireTwiddler.h:43
yarp::wire_rep_utils::WireTwiddlerGap::getExternalLength
int getExternalLength() const
Definition: WireTwiddler.h:65
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::wire_rep_utils::WireTwiddlerWriter::length
size_t length(size_t index) const override
Definition: WireTwiddler.h:315
yarp::wire_rep_utils::WireTwiddler::WireTwiddler
WireTwiddler()
Definition: WireTwiddler.h:74
yarp::wire_rep_utils::WireTwiddlerSrc::offset
int offset
Definition: WireTwiddler.h:203
yarp::wire_rep_utils::WireTwiddler::clear
void clear()
Definition: WireTwiddler.h:112
yarp::wire_rep_utils::WireTwiddlerGap::save_external
bool save_external
Definition: WireTwiddler.h:41
yarp::wire_rep_utils::WireTwiddlerGap::var_name
std::string var_name
Definition: WireTwiddler.h:45
yarp::wire_rep_utils::WireTwiddlerGap::flavor
int flavor
Definition: WireTwiddler.h:46
api.h
yarp::wire_rep_utils::WireTwiddlerSrc::src
char * src
Definition: WireTwiddler.h:201
yarp::wire_rep_utils::WireTwiddlerGap
Definition: WireTwiddler.h:31
buffer
Definition: V4L_camera.h:75
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
yarp::wire_rep_utils::WireTwiddler::~WireTwiddler
virtual ~WireTwiddler()
Definition: WireTwiddler.h:79
yarp::wire_rep_utils::WireTwiddler::getPrompt
const std::string & getPrompt() const
Definition: WireTwiddler.h:124
yarp::os::SizedWriter::getReplyHandler
virtual PortReader * getReplyHandler()=0
yarp::wire_rep_utils::WireTwiddlerGap::unit_length
int unit_length
Definition: WireTwiddler.h:36
yarp::wire_rep_utils::WireTwiddlerWriter
Definition: WireTwiddler.h:214
yarp::os::InputStream::isOk
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::wire_rep_utils::WireTwiddlerGap::length
int length
Definition: WireTwiddler.h:35
yarp::wire_rep_utils::WireTwiddler
Definition: WireTwiddler.h:72
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp::wire_rep_utils::WireTwiddlerReader::reset
void reset()
Definition: WireTwiddler.h:167
toString
std::string toString(const T &value)
convert an arbitrary type to string.
Definition: fakeMotionControl.cpp:121
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Bytes.h
yarp::wire_rep_utils::WireTwiddlerWriter::length
size_t length() const override
Definition: WireTwiddler.h:307
yarp::wire_rep_utils::WireTwiddlerGap::ignore_external
bool ignore_external
Definition: WireTwiddler.h:40
yarp::wire_rep_utils::WireTwiddlerGap::getStart
char * getStart() const
Definition: WireTwiddler.h:62
yarp::wire_rep_utils::WireTwiddlerSrc::WireTwiddlerSrc
WireTwiddlerSrc(char *src, int len, int offset)
Definition: WireTwiddler.h:205
yarp::wire_rep_utils::WireTwiddler::getGapCount
int getGapCount()
Definition: WireTwiddler.h:108
yarp::os::ManagedBytes::get
const char * get() const
Definition: ManagedBytes.cpp:154
InputStream.h
yarp::wire_rep_utils::WireTwiddlerGap::getUnitLength
int getUnitLength() const
Definition: WireTwiddler.h:64
yarp::wire_rep_utils::WireTwiddlerWriter::WireTwiddlerWriter
WireTwiddlerWriter()
Definition: WireTwiddler.h:271
yarp::wire_rep_utils::WireTwiddlerGap::byte_length
int byte_length
Definition: WireTwiddler.h:39
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971
yarp::wire_rep_utils::WireTwiddlerGap::load_external
bool load_external
Definition: WireTwiddler.h:42
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
yarp::wire_rep_utils::WireTwiddlerReader
Definition: WireTwiddler.h:143
yarp::os::SizedWriter::stopWrite
virtual void stopWrite() const =0
Call when all writing is finished.
yarp::wire_rep_utils::WireTwiddlerGap::origin
std::string origin
Definition: WireTwiddler.h:44
SizedWriter.h
Bottle.h
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
yarp::wire_rep_utils::WireTwiddlerGap::byte_start
char * byte_start
Definition: WireTwiddler.h:38
yarp::os::NetInt32
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:33