YARP  2.3.68+225-20170329.5+gitb0d3289
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 
33 template <class T>
34 class yarp::os::BufferedPort : public Contactable,
35  public TypedReader<T>,
36  public TypedReaderCallback<T>
37 {
38 public:
39  using Contactable::open;
41 
45  typedef T ContentType;
46 
51  T example;
52  attached = false;
53  port.promiseType(example.getType());
55  interrupted = false;
56  }
57 
62  attached = false;
63  sharedOpen(port);
64  interrupted = false;
65  }
66 
70  virtual ~BufferedPort() {
71  close();
72  }
73 
74 
75  // documentation provided in Contactable
76  virtual bool open(const ConstString& name) {
78  return port.open(name);
79  }
80 
81  // documentation provided in Contactable
82  virtual bool open(const Contact& contact, bool registerName = true) {
84  return port.open(contact,registerName);
85  }
86 
87  // documentation provided in Contactable
88  virtual bool addOutput(const ConstString& name) {
89  return port.addOutput(name);
90  }
91 
92  // documentation provided in Contactable
93  virtual bool addOutput(const ConstString& name, const ConstString& carrier) {
94  return port.addOutput(name,carrier);
95  }
96 
97  // documentation provided in Contactable
98  virtual bool addOutput(const Contact& contact){
99  return port.addOutput(contact);
100  }
101 
102  // documentation provided in Contactable
103  virtual void close() {
104  port.close();
105  reader.detach();
106  writer.detach();
107  attached = false;
108  }
109 
110  // documentation provided in Contactable
111  virtual void interrupt() {
112  interrupted = true;
113  port.interrupt();
114  }
115 
116  virtual void resume() {
117  port.resume();
118  interrupted = false;
119  }
120 
124  virtual int getPendingReads() {
125  return reader.getPendingReads();
126  }
127 
128  // documentation provided in Contactable
129  virtual Contact where() const {
130  return port.where();
131  }
132 
133  // documentation provided in Contactable
134  virtual ConstString getName() const {
135  return where().getName();
136  }
137 
138 
155  T& prepare() {
156  return writer.get();
157  }
158 
159 
167  bool unprepare() {
168  return writer.unprepare();
169  }
170 
182  void write(bool forceStrict=false) {
183  writer.write(forceStrict);
184  }
185 
195  void writeStrict() {
196  write(true);
197  }
198 
204  void waitForWrite() {
205  writer.waitForWrite();
206  }
207 
214  void setStrict(bool strict=true) {
215  attachIfNeeded();
216  reader.setStrict(strict);
217  }
218 
228  virtual T *read(bool shouldWait=true) {
229  if(!port.isOpen()) return YARP_NULLPTR;
230  if (interrupted) return YARP_NULLPTR;
231  T *result = reader.read(shouldWait);
232  // in some circs PortReaderBuffer::read(true) may return false
233  while (result==YARP_NULLPTR && shouldWait && !reader.isClosed() &&
234  !interrupted) {
235  result = reader.read(shouldWait);
236  }
237  return result;
238  }
239 
240  virtual T *lastRead() {
241  return reader.lastRead();
242  }
243 
244  virtual bool isClosed() {
245  return reader.isClosed();
246  }
247 
249  attachIfNeeded();
250  this->reader.setReplier(reader);
251  }
252 
254  attachIfNeeded();
255  setReplier(reader);
256  }
257 
259  attachIfNeeded();
260  port.setAdminReader(reader);
261  }
262 
267  virtual void onRead(T& datum) {
268  YARP_UNUSED(datum);
269  // override this to do something
270  }
271 
277  attachIfNeeded();
278  reader.useCallback(callback);
279  }
280 
285  void useCallback() {
286  attachIfNeeded();
287  reader.useCallback(*this);
288  }
289 
291  attachIfNeeded();
292  reader.disableCallback();
293  }
294 
295  // documented in Contactable
296  virtual bool setEnvelope(PortWriter& envelope) {
297  return port.setEnvelope(envelope);
298  }
299 
300 
301  // documented in Contactable
302  virtual bool getEnvelope(PortReader& envelope) {
303  return reader.getEnvelope(envelope);
304  }
305 
306  // documented in Contactable
307  virtual int getInputCount() {
308  return port.getInputCount();
309  }
310 
311  // documented in Contactable
312  virtual int getOutputCount() {
313  return port.getOutputCount();
314  }
315 
316  // documented in Contactable
317  bool isWriting() {
318  return port.isWriting();
319  }
320 
321  // documented in Contactable
322  virtual void getReport(PortReport& reporter) {
323  port.getReport(reporter);
324  }
325 
326  // documented in Contactable
327  virtual void setReporter(PortReport& reporter) {
328  port.setReporter(reporter);
329  }
330 
331  // documented in Contactable
332  virtual void resetReporter() {
334  }
335 
336  // documented in TypedReader
337  virtual void *acquire() {
338  return reader.acquire();
339  }
340 
341  // documented in TypedReader
342  virtual void release(void *handle) {
343  reader.release(handle);
344  }
345 
346 
347  // documented in TypedReader
348  virtual void setTargetPeriod(double period) {
349  attachIfNeeded();
350  reader.setTargetPeriod(period);
351  }
352 
353  virtual Type getType() {
354  return port.getType();
355  }
356 
357  virtual void promiseType(const Type& typ) {
358  port.promiseType(typ);
359  }
360 
361  virtual void setInputMode(bool expectInput) {
362  port.setInputMode(expectInput);
363  }
364 
365  virtual void setOutputMode(bool expectOutput) {
366  port.setOutputMode(expectOutput);
367  }
368 
369  virtual void setRpcMode(bool expectRpc) {
370  port.setRpcMode(expectRpc);
371  }
372 
373 
374  virtual Property *acquireProperties(bool readOnly) {
375  return port.acquireProperties(readOnly);
376  }
377 
378  virtual void releaseProperties(Property *prop) {
379  port.releaseProperties(prop);
380  }
381 
382  virtual void includeNodeInName(bool flag) {
383  return port.includeNodeInName(flag);
384  }
385 
387  return port.setCallbackLock(mutex);
388  }
389 
390  virtual bool removeCallbackLock() {
391  return port.removeCallbackLock();
392  }
393 
394  virtual bool lockCallback() {
395  return port.lockCallback();
396  }
397 
398  virtual bool tryLockCallback() {
399  return port.tryLockCallback();
400  }
401 
402  virtual void unlockCallback() {
403  return port.unlockCallback();
404  }
405 
406 private:
411  bool attached;
412 
413  // forbid this
415  YARP_UNUSED(alt);
416  }
417 
418  // forbid this
419  const BufferedPort& operator = (const BufferedPort& alt) {
420  YARP_UNUSED(alt);
421  return *this;
422  }
423 
424  void attachIfNeeded() {
425  if (!attached) {
426  reader.attach(port);
427  writer.attach(port);
428  attached = true;
429  }
430  }
431 
432  bool sharedOpen(Port& port) {
433  bool ok = this->port.sharedOpen(port);
434  if (!ok) return false;
435  reader.attach(port);
436  writer.attach(port);
437  attached = true;
438  return true;
439  }
440 };
441 
442 #endif // YARP_OS_BUFFEREDPORT_H
bool sharedOpen(Port &port)
Definition: Port.cpp:625
void setRpcMode(bool expectRpc)
Configure the port to be RPC only.
Definition: Port.cpp:585
virtual bool addOutput(const ConstString &name, const ConstString &carrier)
Add an output connection to the specified port, using a specified carrier.
Definition: BufferedPort.h:93
virtual bool lockCallback()
Lock callbacks until unlockCallback() is called.
Definition: Port.cpp:646
virtual bool getEnvelope(PortReader &envelope)
Get the envelope information (e.g., a timestamp) from the last message received on the port...
Definition: BufferedPort.h:302
void setReader(PortReader &reader)
Set an external reader for port data.
Definition: BufferedPort.h:253
virtual ConstString getName() const
Get name of port.
Definition: BufferedPort.h:134
virtual void includeNodeInName(bool flag)
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
Definition: Port.cpp:621
bool isWriting()
Report whether the port is currently writing data.
Definition: Port.cpp:508
An abstract port.
Definition: Contactable.h:32
virtual bool open(const Contact &contact, bool registerName=true)
Start port operation with user-chosen network parameters.
Definition: BufferedPort.h:82
virtual bool removeCallbackLock()
Remove a lock on callbacks added with setCallbackLock()
Definition: BufferedPort.h:390
void useCallback(TypedReaderCallback< T > &callback)
Set an object whose onRead method will be called when data is available.
Definition: BufferedPort.h:276
A base class for objects that want information about port status changes.
Definition: PortReport.h:26
A class for storing options and configuration information.
Definition: Property.h:32
A callback for typed data from a port.
virtual void close()
Stop port activity.
Definition: BufferedPort.h:103
virtual bool setCallbackLock(yarp::os::Mutex *mutex)
Add a lock to use when invoking callbacks.
Definition: BufferedPort.h:386
virtual bool setEnvelope(PortWriter &envelope)
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: Port.cpp:515
Buffer incoming data to a port.
Buffer outgoing data to a port.
virtual int getOutputCount()
Determine how many output connections this port has.
Definition: Port.cpp:532
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:633
void close()
Stop port activity.
Definition: Port.cpp:335
A mini-server for network communication.
Definition: Port.h:37
virtual int getInputCount()
Determine how many connections are arriving into this port.
Definition: BufferedPort.h:307
virtual bool setEnvelope(PortWriter &envelope)
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: BufferedPort.h:296
virtual void setRpcMode(bool expectRpc)
Configure the port to be RPC only.
Definition: BufferedPort.h:369
virtual void onRead(T &datum)
this method will be called with new data, as long as you&#39;ve requested this be done by calling useCall...
Definition: BufferedPort.h:267
virtual Contact where() const
Returns information about how this port can be reached.
Definition: BufferedPort.h:129
BufferedPort(const BufferedPort &alt)
Definition: BufferedPort.h:414
virtual void releaseProperties(Property *prop)
End access unstructured port properties.
Definition: BufferedPort.h:378
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 void setTargetPeriod(double period)
Try to provide data periodically.
Definition: BufferedPort.h:348
The main, catch-all namespace for YARP.
virtual ~BufferedPort()
Destructor.
Definition: BufferedPort.h:70
virtual void resume()
Put the port back in an operative state after interrupt() has been called.
Definition: BufferedPort.h:116
A string with almost the same api as std::string.
Definition: ConstString.h:44
virtual bool addOutput(const ConstString &name)
Add an output connection to the specified port.
Definition: BufferedPort.h:88
virtual void resetReporter()
Remove the callback which is called upon any future connections and disconnections to/from the port...
Definition: Port.cpp:550
virtual T * read(bool shouldWait=true)
Read a message from the port.
Definition: BufferedPort.h:228
virtual bool lockCallback()
Lock callbacks until unlockCallback() is called.
Definition: BufferedPort.h:394
virtual void * acquire()
Take control of the last object read.
Definition: BufferedPort.h:337
bool addOutput(const ConstString &name)
Add an output connection to the specified port.
Definition: Port.cpp:327
virtual T * lastRead()
Get the last data returned by read()
Definition: BufferedPort.h:240
static Semaphore mutex(1)
virtual void getReport(PortReport &reporter)
Get information on the state of the port - connections etc.
Definition: BufferedPort.h:322
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: BufferedPort.h:167
virtual int getInputCount()
Determine how many connections are arriving into this port.
Definition: Port.cpp:526
PortWriterBuffer< T > writer
Definition: BufferedPort.h:407
virtual void includeNodeInName(bool flag)
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
Definition: BufferedPort.h:382
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
virtual bool tryLockCallback()
Try to lock callbacks until unlockCallback() is called.
Definition: Port.cpp:653
T ContentType
The type of content stored by this BufferedPort.
Definition: BufferedPort.h:45
void attach(Port &port)
Attach this buffer to a particular port.
virtual void setReporter(PortReport &reporter)
Set a callback to be called upon any future connections and disconnections to/from the port...
Definition: BufferedPort.h:327
void attach(Port &port)
Set the Port to which objects will be written.
virtual Type getType()
Get the type of data the port has committed to send/receive.
Definition: Port.cpp:605
virtual void release(void *handle)
Return control to YARP of an object previously taken control of with the acquire() method...
Definition: BufferedPort.h:342
BufferedPort(Port &port)
Wrap an existing unbuffered port.
Definition: BufferedPort.h:61
virtual Property * acquireProperties(bool readOnly)
Access unstructured port properties.
Definition: Port.cpp:613
bool open(const ConstString &name)
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:50
bool isWriting()
Report whether the port is currently writing data.
Definition: BufferedPort.h:317
virtual bool isClosed()
Definition: BufferedPort.h:244
ConstString getName() const
Get the name associated with this Contact.
Definition: Contact.cpp:203
const BufferedPort & operator=(const BufferedPort &alt)
Definition: BufferedPort.h:419
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
Definition: BufferedPort.h:195
virtual int getPendingReads()
Get the number of objects ready to be read.
Definition: BufferedPort.h:124
Contact where() const
Returns information about how this port can be reached.
Definition: Port.cpp:380
virtual bool open(const ConstString &name)
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: BufferedPort.h:76
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
Definition: Port.cpp:502
#define YARP_UNUSED(var)
Definition: api.h:112
virtual void promiseType(const Type &typ)
Commit the port to a particular type of data.
Definition: Port.cpp:609
virtual Type getType()
Get the type of data the port has committed to send/receive.
Definition: BufferedPort.h:353
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
Definition: BufferedPort.h:155
virtual void setReporter(PortReport &reporter)
Set a callback to be called upon any future connections and disconnections to/from the port...
Definition: Port.cpp:544
virtual void interrupt()
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:353
virtual bool addOutput(const Contact &contact)
Add an output connection to the specified port, using specified network parameters.
Definition: BufferedPort.h:98
virtual void getReport(PortReport &reporter)
Get information on the state of the port - connections etc.
Definition: Port.cpp:538
void waitForWrite()
Wait for any pending writes to complete.
Definition: BufferedPort.h:204
virtual void promiseType(const Type &typ)
Commit the port to a particular type of data.
Definition: BufferedPort.h:357
virtual void resume()
Put the port back in an operative state after interrupt() has been called.
Definition: Port.cpp:361
PortReaderBuffer< T > reader
Definition: BufferedPort.h:409
virtual bool setCallbackLock(yarp::os::Mutex *mutex=YARP_NULLPTR)
Add a lock to use when invoking callbacks.
Definition: Port.cpp:638
bool sharedOpen(Port &port)
Definition: BufferedPort.h:432
void setStrict(bool strict=true)
Never drop any messages read.
Definition: BufferedPort.h:214
ConstString name
Definition: Module.h:221
void disableCallback()
Remove a callback set up with useCallback()
Definition: BufferedPort.h:290
void setOutputMode(bool expectOutput)
Configure the port to allow or forbid outputs.
Definition: Port.cpp:578
virtual void unlockCallback()
Unlock callbacks.
Definition: Port.cpp:657
virtual bool removeCallbackLock()
Remove a lock on callbacks added with setCallbackLock()
Definition: Port.cpp:642
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
virtual void setOutputMode(bool expectOutput)
Configure the port to allow or forbid outputs.
Definition: BufferedPort.h:365
void setInputMode(bool expectInput)
Configure the port to allow or forbid inputs.
Definition: Port.cpp:571
virtual int getOutputCount()
Determine how many output connections this port has.
Definition: BufferedPort.h:312
BufferedPort()
Constructor.
Definition: BufferedPort.h:50
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:17
void setReplier(PortReader &reader)
If a message is received that requires a reply, use this handler.
Definition: BufferedPort.h:248
#define YARP_NULLPTR
Expands to either the standard nullptr or to 0 elsewhere.
Definition: compiler.h:2513
virtual void releaseProperties(Property *prop)
End access unstructured port properties.
Definition: Port.cpp:617
virtual void resetReporter()
Remove the callback which is called upon any future connections and disconnections to/from the port...
Definition: BufferedPort.h:332
virtual void interrupt()
Interrupt any current reads or writes attached to the port.
Definition: BufferedPort.h:111
virtual void setAdminReader(PortReader &reader)
Set an external reader for unrecognized administrative port messages.
Definition: Port.cpp:489
void useCallback()
Call own onRead() method – user can override that method to be informed about data as it arrives...
Definition: BufferedPort.h:285
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Definition: BufferedPort.h:182
virtual void unlockCallback()
Unlock callbacks.
Definition: BufferedPort.h:402
virtual bool tryLockCallback()
Try to lock callbacks until unlockCallback() is called.
Definition: BufferedPort.h:398
virtual Property * acquireProperties(bool readOnly)
Access unstructured port properties.
Definition: BufferedPort.h:374
virtual void setInputMode(bool expectInput)
Configure the port to allow or forbid inputs.
Definition: BufferedPort.h:361
A base class for sources of typed data.
void setAdminReader(PortReader &reader)
Set an external reader for unrecognized administrative port messages.
Definition: BufferedPort.h:258
Basic wrapper for mutual exclusion.
Definition: Mutex.h:25