YARP  2.3.70
Yet Another Robot Platform
BufferedPort.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 RobotCub Consortium
3  * Authors: Paul Fitzpatrick
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
5  */
6 
7 #ifndef YARP_OS_BUFFEREDPORT_H
8 #define YARP_OS_BUFFEREDPORT_H
9 
10 #include <yarp/os/Contactable.h>
11 #include <yarp/os/Port.h>
14 
15 namespace yarp {
16  namespace os {
17  template <class T> class BufferedPort;
18  }
19 }
20 
55 template <class T>
56 class yarp::os::BufferedPort : public Contactable,
57  public TypedReader<T>,
58  public TypedReaderCallback<T>
59 {
60 public:
61  using Contactable::open;
63 
67  typedef T ContentType;
68 
73  T example;
74  attached = false;
75  port.promiseType(example.getType());
77  interrupted = false;
78  }
79 
84  attached = false;
85  sharedOpen(port);
86  interrupted = false;
87  }
88 
92  virtual ~BufferedPort() {
93  close();
94  }
95 
96 
97  // documentation provided in Contactable
98  virtual bool open(const ConstString& name) YARP_OVERRIDE {
100  return port.open(name);
101  }
102 
103  // documentation provided in Contactable
104  virtual bool open(const Contact& contact, bool registerName = true) YARP_OVERRIDE {
105  attachIfNeeded();
106  return port.open(contact, registerName);
107  }
108 
109  // documentation provided in Contactable
110  virtual bool addOutput(const ConstString& name) YARP_OVERRIDE {
111  return port.addOutput(name);
112  }
113 
114  // documentation provided in Contactable
115  virtual bool addOutput(const ConstString& name, const ConstString& carrier) YARP_OVERRIDE {
116  return port.addOutput(name, carrier);
117  }
118 
119  // documentation provided in Contactable
120  virtual bool addOutput(const Contact& contact) YARP_OVERRIDE {
121  return port.addOutput(contact);
122  }
123 
124  // documentation provided in Contactable
125  virtual void close() YARP_OVERRIDE {
126  port.close();
127  reader.detach();
128  writer.detach();
129  attached = false;
130  }
131 
132  // documentation provided in Contactable
133  virtual void interrupt() YARP_OVERRIDE {
134  interrupted = true;
135  port.interrupt();
136  }
137 
138  virtual void resume() YARP_OVERRIDE {
139  port.resume();
140  interrupted = false;
141  }
142 
146  virtual int getPendingReads() YARP_OVERRIDE {
147  return reader.getPendingReads();
148  }
149 
150  // documentation provided in Contactable
151  virtual Contact where() const YARP_OVERRIDE {
152  return port.where();
153  }
154 
155  // documentation provided in Contactable
156  virtual ConstString getName() const YARP_OVERRIDE {
157  return where().getName();
158  }
159 
160 
177  T& prepare() {
178  return writer.get();
179  }
180 
181 
189  bool unprepare() {
190  return writer.unprepare();
191  }
192 
204  void write(bool forceStrict=false) {
205  if(isClosed())
206  {
207  return;
208  }
209  writer.write(forceStrict);
210  }
211 
221  void writeStrict() {
222  write(true);
223  }
224 
230  void waitForWrite() {
231  writer.waitForWrite();
232  }
233 
240  void setStrict(bool strict=true) YARP_OVERRIDE {
241  attachIfNeeded();
242  reader.setStrict(strict);
243  }
244 
254  virtual T *read(bool shouldWait=true) YARP_OVERRIDE {
255  if (!port.isOpen()) return YARP_NULLPTR;
256  if (interrupted) return YARP_NULLPTR;
257  T *result = reader.read(shouldWait);
258  // in some circs PortReaderBuffer::read(true) may return false
259  while (result==YARP_NULLPTR && shouldWait && !reader.isClosed() &&
260  !interrupted) {
261  result = reader.read(shouldWait);
262  }
263  return result;
264  }
265 
266  virtual T *lastRead() YARP_OVERRIDE {
267  return reader.lastRead();
268  }
269 
270  virtual bool isClosed() YARP_OVERRIDE {
271  return reader.isClosed();
272  }
273 
274  void setReplier(PortReader& reader) YARP_OVERRIDE {
275  attachIfNeeded();
276  this->reader.setReplier(reader);
277  }
278 
279  void setReader(PortReader& reader) YARP_OVERRIDE {
280  attachIfNeeded();
282  }
283 
284  void setAdminReader(PortReader& reader) YARP_OVERRIDE {
285  attachIfNeeded();
287  }
288 
293  virtual void onRead(T& datum) YARP_OVERRIDE {
294  YARP_UNUSED(datum);
295  // override this to do something
296  }
297 
302  void useCallback(TypedReaderCallback<T>& callback) YARP_OVERRIDE {
303  attachIfNeeded();
304  reader.useCallback(callback);
305  }
306 
311  void useCallback() {
312  attachIfNeeded();
313  reader.useCallback(*this);
314  }
315 
316  void disableCallback() YARP_OVERRIDE {
317  attachIfNeeded();
318  reader.disableCallback();
319  }
320 
321  // documented in Contactable
322  virtual bool setEnvelope(PortWriter& envelope) YARP_OVERRIDE {
323  return port.setEnvelope(envelope);
324  }
325 
326 
327  // documented in Contactable
328  virtual bool getEnvelope(PortReader& envelope) YARP_OVERRIDE {
329  return reader.getEnvelope(envelope);
330  }
331 
332  // documented in Contactable
333  virtual int getInputCount() YARP_OVERRIDE {
334  return port.getInputCount();
335  }
336 
337  // documented in Contactable
338  virtual int getOutputCount() YARP_OVERRIDE {
339  return port.getOutputCount();
340  }
341 
342  // documented in Contactable
343  bool isWriting() YARP_OVERRIDE {
344  return port.isWriting();
345  }
346 
347  // documented in Contactable
348  virtual void getReport(PortReport& reporter) YARP_OVERRIDE {
349  port.getReport(reporter);
350  }
351 
352  // documented in Contactable
353  virtual void setReporter(PortReport& reporter) YARP_OVERRIDE {
354  port.setReporter(reporter);
355  }
356 
357  // documented in Contactable
358  virtual void resetReporter() YARP_OVERRIDE {
360  }
361 
362  // documented in TypedReader
363  virtual void *acquire() YARP_OVERRIDE {
364  return reader.acquire();
365  }
366 
367  // documented in TypedReader
368  virtual void release(void *handle) YARP_OVERRIDE {
369  reader.release(handle);
370  }
371 
372 
373  // documented in TypedReader
374  virtual void setTargetPeriod(double period) YARP_OVERRIDE {
375  attachIfNeeded();
376  reader.setTargetPeriod(period);
377  }
378 
379  virtual Type getType() YARP_OVERRIDE {
380  return port.getType();
381  }
382 
383  virtual void promiseType(const Type& typ) YARP_OVERRIDE {
384  port.promiseType(typ);
385  }
386 
387  virtual void setInputMode(bool expectInput) YARP_OVERRIDE {
388  port.setInputMode(expectInput);
389  }
390 
391  virtual void setOutputMode(bool expectOutput) YARP_OVERRIDE {
392  port.setOutputMode(expectOutput);
393  }
394 
395  virtual void setRpcMode(bool expectRpc) YARP_OVERRIDE {
396  port.setRpcMode(expectRpc);
397  }
398 
399 
400  virtual Property *acquireProperties(bool readOnly) YARP_OVERRIDE {
401  return port.acquireProperties(readOnly);
402  }
403 
404  virtual void releaseProperties(Property *prop) YARP_OVERRIDE {
405  port.releaseProperties(prop);
406  }
407 
408  virtual void includeNodeInName(bool flag) YARP_OVERRIDE {
409  return port.includeNodeInName(flag);
410  }
411 
412  virtual bool setCallbackLock(yarp::os::Mutex *mutex) YARP_OVERRIDE {
413  return port.setCallbackLock(mutex);
414  }
415 
416  virtual bool removeCallbackLock() YARP_OVERRIDE {
417  return port.removeCallbackLock();
418  }
419 
420  virtual bool lockCallback() YARP_OVERRIDE {
421  return port.lockCallback();
422  }
423 
424  virtual bool tryLockCallback() YARP_OVERRIDE {
425  return port.tryLockCallback();
426  }
427 
428  virtual void unlockCallback() YARP_OVERRIDE {
429  return port.unlockCallback();
430  }
431 
432 private:
437  bool attached;
438 
439  // forbid this
441  YARP_UNUSED(alt);
442  }
443 
444  // forbid this
445  const BufferedPort& operator = (const BufferedPort& alt) {
446  YARP_UNUSED(alt);
447  return *this;
448  }
449 
450  void attachIfNeeded() {
451  if (!attached) {
452  reader.attach(port);
453  writer.attach(port);
454  attached = true;
455  }
456  }
457 
458  bool sharedOpen(Port& port) {
459  bool ok = this->port.sharedOpen(port);
460  if (!ok) return false;
461  reader.attach(port);
462  writer.attach(port);
463  attached = true;
464  return true;
465  }
466 };
467 
468 #endif // YARP_OS_BUFFEREDPORT_H
bool sharedOpen(Port &port)
Definition: Port.cpp:610
virtual int getInputCount() YARP_OVERRIDE
Determine how many connections are arriving into this port.
Definition: BufferedPort.h:333
virtual void resetReporter() YARP_OVERRIDE
Remove the callback which is called upon any future connections and disconnections to/from the port...
Definition: BufferedPort.h:358
Contact where() const YARP_OVERRIDE
Returns information about how this port can be reached.
Definition: Port.cpp:380
virtual bool addOutput(const ConstString &name) YARP_OVERRIDE
Add an output connection to the specified port.
Definition: BufferedPort.h:110
virtual void promiseType(const Type &typ) YARP_OVERRIDE
Commit the port to a particular type of data.
Definition: BufferedPort.h:383
virtual void setInputMode(bool expectInput) YARP_OVERRIDE
Configure the port to allow or forbid inputs.
Definition: BufferedPort.h:387
virtual void setTargetPeriod(double period) YARP_OVERRIDE
Try to provide data periodically.
Definition: BufferedPort.h:374
An abstract port.
Definition: Contactable.h:32
virtual void interrupt() YARP_OVERRIDE
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:353
A base class for objects that want information about port status changes.
Definition: PortReport.h:26
virtual void includeNodeInName(bool flag) YARP_OVERRIDE
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
Definition: BufferedPort.h:408
A class for storing options and configuration information.
Definition: Property.h:32
A callback for typed data from a port.
virtual int getInputCount() YARP_OVERRIDE
Determine how many connections are arriving into this port.
Definition: Port.cpp:511
virtual bool setCallbackLock(yarp::os::Mutex *mutex) YARP_OVERRIDE
Add a lock to use when invoking callbacks.
Definition: BufferedPort.h:412
Buffer incoming data to a port.
Buffer outgoing data to a port.
virtual void resume() YARP_OVERRIDE
Put the port back in an operative state after interrupt() has been called.
Definition: Port.cpp:361
virtual void setRpcMode(bool expectRpc) YARP_OVERRIDE
Configure the port to be RPC only.
Definition: BufferedPort.h:395
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:618
virtual bool setEnvelope(PortWriter &envelope) YARP_OVERRIDE
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: Port.cpp:500
virtual void includeNodeInName(bool flag) YARP_OVERRIDE
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
Definition: Port.cpp:606
virtual void resetReporter() YARP_OVERRIDE
Remove the callback which is called upon any future connections and disconnections to/from the port...
Definition: Port.cpp:535
virtual void setReporter(PortReport &reporter) YARP_OVERRIDE
Set a callback to be called upon any future connections and disconnections to/from the port...
Definition: BufferedPort.h:353
A mini-server for network communication.
Definition: Port.h:45
void setInputMode(bool expectInput) YARP_OVERRIDE
Configure the port to allow or forbid inputs.
Definition: Port.cpp:556
virtual int getOutputCount() YARP_OVERRIDE
Determine how many output connections this port has.
Definition: Port.cpp:517
virtual void unlockCallback() YARP_OVERRIDE
Unlock callbacks.
Definition: BufferedPort.h:428
virtual bool addOutput(const Contact &contact) YARP_OVERRIDE
Add an output connection to the specified port, using specified network parameters.
Definition: BufferedPort.h:120
void setReader(PortReader &reader) YARP_OVERRIDE
Set an external reader for port data.
Definition: BufferedPort.h:279
BufferedPort(const BufferedPort &alt)
Definition: BufferedPort.h:440
bool open(const ConstString &name) YARP_OVERRIDE
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:50
virtual T * lastRead() YARP_OVERRIDE
Get the last data returned by read()
Definition: BufferedPort.h:266
bool open()
Start port operation, with automatically-chosen network parameters.
Definition: Contactable.cpp:14
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
virtual bool lockCallback() YARP_OVERRIDE
Lock callbacks until unlockCallback() is called.
Definition: Port.cpp:631
virtual void getReport(PortReport &reporter) YARP_OVERRIDE
Get information on the state of the port - connections etc.
Definition: BufferedPort.h:348
The main, catch-all namespace for YARP.
virtual ~BufferedPort()
Destructor.
Definition: BufferedPort.h:92
A string with almost the same api as std::string.
Definition: ConstString.h:44
virtual bool isClosed() YARP_OVERRIDE
Definition: BufferedPort.h:270
bool isWriting() YARP_OVERRIDE
Report whether the port is currently writing data.
Definition: Port.cpp:493
virtual void release(void *handle) YARP_OVERRIDE
Return control to YARP of an object previously taken control of with the acquire() method...
Definition: BufferedPort.h:368
virtual int getPendingReads() YARP_OVERRIDE
Get the number of objects ready to be read.
Definition: BufferedPort.h:146
virtual Property * acquireProperties(bool readOnly) YARP_OVERRIDE
Access unstructured port properties.
Definition: Port.cpp:598
void disableCallback() YARP_OVERRIDE
Remove a callback set up with useCallback()
Definition: BufferedPort.h:316
virtual void releaseProperties(Property *prop) YARP_OVERRIDE
End access unstructured port properties.
Definition: Port.cpp:602
static Semaphore mutex(1)
virtual bool addOutput(const ConstString &name, const ConstString &carrier) YARP_OVERRIDE
Add an output connection to the specified port, using a specified carrier.
Definition: BufferedPort.h:115
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: BufferedPort.h:189
virtual Property * acquireProperties(bool readOnly) YARP_OVERRIDE
Access unstructured port properties.
Definition: BufferedPort.h:400
PortWriterBuffer< T > writer
Definition: BufferedPort.h:433
virtual void interrupt() YARP_OVERRIDE
Interrupt any current reads or writes attached to the port.
Definition: BufferedPort.h:133
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
virtual Type getType() YARP_OVERRIDE
Get the type of data the port has committed to send/receive.
Definition: Port.cpp:590
T ContentType
The type of content stored by this BufferedPort.
Definition: BufferedPort.h:67
void attach(Port &port)
Attach this buffer to a particular port.
virtual void releaseProperties(Property *prop) YARP_OVERRIDE
End access unstructured port properties.
Definition: BufferedPort.h:404
void attach(Port &port)
Set the Port to which objects will be written.
virtual void resume() YARP_OVERRIDE
Put the port back in an operative state after interrupt() has been called.
Definition: BufferedPort.h:138
virtual bool lockCallback() YARP_OVERRIDE
Lock callbacks until unlockCallback() is called.
Definition: BufferedPort.h:420
BufferedPort(Port &port)
Wrap an existing unbuffered port.
Definition: BufferedPort.h:83
bool isWriting() YARP_OVERRIDE
Report whether the port is currently writing data.
Definition: BufferedPort.h:343
virtual void unlockCallback() YARP_OVERRIDE
Unlock callbacks.
Definition: Port.cpp:642
virtual bool removeCallbackLock() YARP_OVERRIDE
Remove a lock on callbacks added with setCallbackLock()
Definition: BufferedPort.h:416
ConstString getName() const
Get the name associated with this Contact.
Definition: Contact.cpp:206
const BufferedPort & operator=(const BufferedPort &alt)
Definition: BufferedPort.h:445
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
Definition: BufferedPort.h:221
virtual void close() YARP_OVERRIDE
Stop port activity.
Definition: BufferedPort.h:125
virtual void onRead(T &datum) YARP_OVERRIDE
this method will be called with new data, as long as you&#39;ve requested this be done by calling useCall...
Definition: BufferedPort.h:293
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
Definition: Port.cpp:487
virtual void setOutputMode(bool expectOutput) YARP_OVERRIDE
Configure the port to allow or forbid outputs.
Definition: BufferedPort.h:391
virtual void setAdminReader(PortReader &reader) YARP_OVERRIDE
Set an external reader for unrecognized administrative port messages.
Definition: Port.cpp:474
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
Definition: BufferedPort.h:177
virtual Type getType() YARP_OVERRIDE
Get the type of data the port has committed to send/receive.
Definition: BufferedPort.h:379
void waitForWrite()
Wait for any pending writes to complete.
Definition: BufferedPort.h:230
virtual T * read(bool shouldWait=true) YARP_OVERRIDE
Read a message from the port.
Definition: BufferedPort.h:254
PortReaderBuffer< T > reader
Definition: BufferedPort.h:435
void setReplier(PortReader &reader) YARP_OVERRIDE
If a message is received that requires a reply, use this handler.
Definition: BufferedPort.h:274
bool sharedOpen(Port &port)
Definition: BufferedPort.h:458
virtual int getOutputCount() YARP_OVERRIDE
Determine how many output connections this port has.
Definition: BufferedPort.h:338
ConstString name
Definition: Module.h:221
virtual bool open(const Contact &contact, bool registerName=true) YARP_OVERRIDE
Start port operation with user-chosen network parameters.
Definition: BufferedPort.h:104
void setAdminReader(PortReader &reader) YARP_OVERRIDE
Set an external reader for unrecognized administrative port messages.
Definition: BufferedPort.h:284
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
virtual ConstString getName() const YARP_OVERRIDE
Get name of port.
Definition: BufferedPort.h:156
virtual void promiseType(const Type &typ) YARP_OVERRIDE
Commit the port to a particular type of data.
Definition: Port.cpp:594
virtual bool setEnvelope(PortWriter &envelope) YARP_OVERRIDE
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: BufferedPort.h:322
BufferedPort()
Constructor.
Definition: BufferedPort.h:72
void close() YARP_OVERRIDE
Stop port activity.
Definition: Port.cpp:335
void setRpcMode(bool expectRpc) YARP_OVERRIDE
Configure the port to be RPC only.
Definition: Port.cpp:570
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:17
virtual void getReport(PortReport &reporter) YARP_OVERRIDE
Get information on the state of the port - connections etc.
Definition: Port.cpp:523
virtual Contact where() const YARP_OVERRIDE
Returns information about how this port can be reached.
Definition: BufferedPort.h:151
virtual void setReporter(PortReport &reporter) YARP_OVERRIDE
Set a callback to be called upon any future connections and disconnections to/from the port...
Definition: Port.cpp:529
void useCallback(TypedReaderCallback< T > &callback) YARP_OVERRIDE
Set an object whose onRead method will be called when data is available.
Definition: BufferedPort.h:302
void setOutputMode(bool expectOutput) YARP_OVERRIDE
Configure the port to allow or forbid outputs.
Definition: Port.cpp:563
bool addOutput(const ConstString &name) YARP_OVERRIDE
Add an output connection to the specified port.
Definition: Port.cpp:327
virtual bool tryLockCallback() YARP_OVERRIDE
Try to lock callbacks until unlockCallback() is called.
Definition: BufferedPort.h:424
virtual void * acquire() YARP_OVERRIDE
Take control of the last object read.
Definition: BufferedPort.h:363
virtual bool open(const ConstString &name) YARP_OVERRIDE
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: BufferedPort.h:98
void useCallback()
Call own onRead() method – user can override that method to be informed about data as it arrives...
Definition: BufferedPort.h:311
virtual bool getEnvelope(PortReader &envelope) YARP_OVERRIDE
Get the envelope information (e.g., a timestamp) from the last message received on the port...
Definition: BufferedPort.h:328
void setStrict(bool strict=true) YARP_OVERRIDE
Never drop any messages read.
Definition: BufferedPort.h:240
virtual bool tryLockCallback() YARP_OVERRIDE
Try to lock callbacks until unlockCallback() is called.
Definition: Port.cpp:638
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Definition: BufferedPort.h:204
virtual bool setCallbackLock(yarp::os::Mutex *mutex=YARP_NULLPTR) YARP_OVERRIDE
Add a lock to use when invoking callbacks.
Definition: Port.cpp:623
virtual bool removeCallbackLock() YARP_OVERRIDE
Remove a lock on callbacks added with setCallbackLock()
Definition: Port.cpp:627
A base class for sources of typed data.
Basic wrapper for mutual exclusion.
Definition: Mutex.h:25