YARP  2.3.68+283-20170530.4+git10f9853
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) {
100  return port.open(name);
101  }
102 
103  // documentation provided in Contactable
104  virtual bool open(const Contact& contact, bool registerName = true) {
105  attachIfNeeded();
106  return port.open(contact,registerName);
107  }
108 
109  // documentation provided in Contactable
110  virtual bool addOutput(const ConstString& name) {
111  return port.addOutput(name);
112  }
113 
114  // documentation provided in Contactable
115  virtual bool addOutput(const ConstString& name, const ConstString& carrier) {
116  return port.addOutput(name,carrier);
117  }
118 
119  // documentation provided in Contactable
120  virtual bool addOutput(const Contact& contact){
121  return port.addOutput(contact);
122  }
123 
124  // documentation provided in Contactable
125  virtual void close() {
126  port.close();
127  reader.detach();
128  writer.detach();
129  attached = false;
130  }
131 
132  // documentation provided in Contactable
133  virtual void interrupt() {
134  interrupted = true;
135  port.interrupt();
136  }
137 
138  virtual void resume() {
139  port.resume();
140  interrupted = false;
141  }
142 
146  virtual int getPendingReads() {
147  return reader.getPendingReads();
148  }
149 
150  // documentation provided in Contactable
151  virtual Contact where() const {
152  return port.where();
153  }
154 
155  // documentation provided in Contactable
156  virtual ConstString getName() const {
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  writer.write(forceStrict);
206  }
207 
217  void writeStrict() {
218  write(true);
219  }
220 
226  void waitForWrite() {
227  writer.waitForWrite();
228  }
229 
236  void setStrict(bool strict=true) {
237  attachIfNeeded();
238  reader.setStrict(strict);
239  }
240 
250  virtual T *read(bool shouldWait=true) {
251  if(!port.isOpen()) return YARP_NULLPTR;
252  if (interrupted) return YARP_NULLPTR;
253  T *result = reader.read(shouldWait);
254  // in some circs PortReaderBuffer::read(true) may return false
255  while (result==YARP_NULLPTR && shouldWait && !reader.isClosed() &&
256  !interrupted) {
257  result = reader.read(shouldWait);
258  }
259  return result;
260  }
261 
262  virtual T *lastRead() {
263  return reader.lastRead();
264  }
265 
266  virtual bool isClosed() {
267  return reader.isClosed();
268  }
269 
271  attachIfNeeded();
272  this->reader.setReplier(reader);
273  }
274 
276  attachIfNeeded();
277  setReplier(reader);
278  }
279 
281  attachIfNeeded();
282  port.setAdminReader(reader);
283  }
284 
289  virtual void onRead(T& datum) {
290  YARP_UNUSED(datum);
291  // override this to do something
292  }
293 
299  attachIfNeeded();
300  reader.useCallback(callback);
301  }
302 
307  void useCallback() {
308  attachIfNeeded();
309  reader.useCallback(*this);
310  }
311 
313  attachIfNeeded();
314  reader.disableCallback();
315  }
316 
317  // documented in Contactable
318  virtual bool setEnvelope(PortWriter& envelope) {
319  return port.setEnvelope(envelope);
320  }
321 
322 
323  // documented in Contactable
324  virtual bool getEnvelope(PortReader& envelope) {
325  return reader.getEnvelope(envelope);
326  }
327 
328  // documented in Contactable
329  virtual int getInputCount() {
330  return port.getInputCount();
331  }
332 
333  // documented in Contactable
334  virtual int getOutputCount() {
335  return port.getOutputCount();
336  }
337 
338  // documented in Contactable
339  bool isWriting() {
340  return port.isWriting();
341  }
342 
343  // documented in Contactable
344  virtual void getReport(PortReport& reporter) {
345  port.getReport(reporter);
346  }
347 
348  // documented in Contactable
349  virtual void setReporter(PortReport& reporter) {
350  port.setReporter(reporter);
351  }
352 
353  // documented in Contactable
354  virtual void resetReporter() {
356  }
357 
358  // documented in TypedReader
359  virtual void *acquire() {
360  return reader.acquire();
361  }
362 
363  // documented in TypedReader
364  virtual void release(void *handle) {
365  reader.release(handle);
366  }
367 
368 
369  // documented in TypedReader
370  virtual void setTargetPeriod(double period) {
371  attachIfNeeded();
372  reader.setTargetPeriod(period);
373  }
374 
375  virtual Type getType() {
376  return port.getType();
377  }
378 
379  virtual void promiseType(const Type& typ) {
380  port.promiseType(typ);
381  }
382 
383  virtual void setInputMode(bool expectInput) {
384  port.setInputMode(expectInput);
385  }
386 
387  virtual void setOutputMode(bool expectOutput) {
388  port.setOutputMode(expectOutput);
389  }
390 
391  virtual void setRpcMode(bool expectRpc) {
392  port.setRpcMode(expectRpc);
393  }
394 
395 
396  virtual Property *acquireProperties(bool readOnly) {
397  return port.acquireProperties(readOnly);
398  }
399 
400  virtual void releaseProperties(Property *prop) {
401  port.releaseProperties(prop);
402  }
403 
404  virtual void includeNodeInName(bool flag) {
405  return port.includeNodeInName(flag);
406  }
407 
409  return port.setCallbackLock(mutex);
410  }
411 
412  virtual bool removeCallbackLock() {
413  return port.removeCallbackLock();
414  }
415 
416  virtual bool lockCallback() {
417  return port.lockCallback();
418  }
419 
420  virtual bool tryLockCallback() {
421  return port.tryLockCallback();
422  }
423 
424  virtual void unlockCallback() {
425  return port.unlockCallback();
426  }
427 
428 private:
433  bool attached;
434 
435  // forbid this
437  YARP_UNUSED(alt);
438  }
439 
440  // forbid this
441  const BufferedPort& operator = (const BufferedPort& alt) {
442  YARP_UNUSED(alt);
443  return *this;
444  }
445 
446  void attachIfNeeded() {
447  if (!attached) {
448  reader.attach(port);
449  writer.attach(port);
450  attached = true;
451  }
452  }
453 
454  bool sharedOpen(Port& port) {
455  bool ok = this->port.sharedOpen(port);
456  if (!ok) return false;
457  reader.attach(port);
458  writer.attach(port);
459  attached = true;
460  return true;
461  }
462 };
463 
464 #endif // YARP_OS_BUFFEREDPORT_H
bool sharedOpen(Port &port)
Definition: Port.cpp:610
void setRpcMode(bool expectRpc)
Configure the port to be RPC only.
Definition: Port.cpp:570
virtual bool addOutput(const ConstString &name, const ConstString &carrier)
Add an output connection to the specified port, using a specified carrier.
Definition: BufferedPort.h:115
virtual bool lockCallback()
Lock callbacks until unlockCallback() is called.
Definition: Port.cpp:631
virtual bool getEnvelope(PortReader &envelope)
Get the envelope information (e.g., a timestamp) from the last message received on the port...
Definition: BufferedPort.h:324
void setReader(PortReader &reader)
Set an external reader for port data.
Definition: BufferedPort.h:275
virtual ConstString getName() const
Get name of port.
Definition: BufferedPort.h:156
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:606
bool isWriting()
Report whether the port is currently writing data.
Definition: Port.cpp:493
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:104
virtual bool removeCallbackLock()
Remove a lock on callbacks added with setCallbackLock()
Definition: BufferedPort.h:412
void useCallback(TypedReaderCallback< T > &callback)
Set an object whose onRead method will be called when data is available.
Definition: BufferedPort.h:298
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:125
virtual bool setCallbackLock(yarp::os::Mutex *mutex)
Add a lock to use when invoking callbacks.
Definition: BufferedPort.h:408
virtual bool setEnvelope(PortWriter &envelope)
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: Port.cpp:500
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:517
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:618
void close()
Stop port activity.
Definition: Port.cpp:335
A mini-server for network communication.
Definition: Port.h:45
virtual int getInputCount()
Determine how many connections are arriving into this port.
Definition: BufferedPort.h:329
virtual bool setEnvelope(PortWriter &envelope)
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: BufferedPort.h:318
virtual void setRpcMode(bool expectRpc)
Configure the port to be RPC only.
Definition: BufferedPort.h:391
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:289
virtual Contact where() const
Returns information about how this port can be reached.
Definition: BufferedPort.h:151
BufferedPort(const BufferedPort &alt)
Definition: BufferedPort.h:436
virtual void releaseProperties(Property *prop)
End access unstructured port properties.
Definition: BufferedPort.h:400
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:370
The main, catch-all namespace for YARP.
virtual ~BufferedPort()
Destructor.
Definition: BufferedPort.h:92
virtual void resume()
Put the port back in an operative state after interrupt() has been called.
Definition: BufferedPort.h:138
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:110
virtual void resetReporter()
Remove the callback which is called upon any future connections and disconnections to/from the port...
Definition: Port.cpp:535
virtual T * read(bool shouldWait=true)
Read a message from the port.
Definition: BufferedPort.h:250
virtual bool lockCallback()
Lock callbacks until unlockCallback() is called.
Definition: BufferedPort.h:416
virtual void * acquire()
Take control of the last object read.
Definition: BufferedPort.h:359
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:262
static Semaphore mutex(1)
virtual void getReport(PortReport &reporter)
Get information on the state of the port - connections etc.
Definition: BufferedPort.h:344
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: BufferedPort.h:189
virtual int getInputCount()
Determine how many connections are arriving into this port.
Definition: Port.cpp:511
PortWriterBuffer< T > writer
Definition: BufferedPort.h:429
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:404
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:638
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 setReporter(PortReport &reporter)
Set a callback to be called upon any future connections and disconnections to/from the port...
Definition: BufferedPort.h:349
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:590
virtual void release(void *handle)
Return control to YARP of an object previously taken control of with the acquire() method...
Definition: BufferedPort.h:364
BufferedPort(Port &port)
Wrap an existing unbuffered port.
Definition: BufferedPort.h:83
virtual Property * acquireProperties(bool readOnly)
Access unstructured port properties.
Definition: Port.cpp:598
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:339
virtual bool isClosed()
Definition: BufferedPort.h:266
ConstString getName() const
Get the name associated with this Contact.
Definition: Contact.cpp:203
const BufferedPort & operator=(const BufferedPort &alt)
Definition: BufferedPort.h:441
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
Definition: BufferedPort.h:217
virtual int getPendingReads()
Get the number of objects ready to be read.
Definition: BufferedPort.h:146
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:98
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
Definition: Port.cpp:487
#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:594
virtual Type getType()
Get the type of data the port has committed to send/receive.
Definition: BufferedPort.h:375
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
Definition: BufferedPort.h:177
virtual void setReporter(PortReport &reporter)
Set a callback to be called upon any future connections and disconnections to/from the port...
Definition: Port.cpp:529
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:120
virtual void getReport(PortReport &reporter)
Get information on the state of the port - connections etc.
Definition: Port.cpp:523
void waitForWrite()
Wait for any pending writes to complete.
Definition: BufferedPort.h:226
virtual void promiseType(const Type &typ)
Commit the port to a particular type of data.
Definition: BufferedPort.h:379
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:431
virtual bool setCallbackLock(yarp::os::Mutex *mutex=YARP_NULLPTR)
Add a lock to use when invoking callbacks.
Definition: Port.cpp:623
bool sharedOpen(Port &port)
Definition: BufferedPort.h:454
void setStrict(bool strict=true)
Never drop any messages read.
Definition: BufferedPort.h:236
ConstString name
Definition: Module.h:221
void disableCallback()
Remove a callback set up with useCallback()
Definition: BufferedPort.h:312
void setOutputMode(bool expectOutput)
Configure the port to allow or forbid outputs.
Definition: Port.cpp:563
virtual void unlockCallback()
Unlock callbacks.
Definition: Port.cpp:642
virtual bool removeCallbackLock()
Remove a lock on callbacks added with setCallbackLock()
Definition: Port.cpp:627
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:387
void setInputMode(bool expectInput)
Configure the port to allow or forbid inputs.
Definition: Port.cpp:556
virtual int getOutputCount()
Determine how many output connections this port has.
Definition: BufferedPort.h:334
BufferedPort()
Constructor.
Definition: BufferedPort.h:72
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:270
#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:602
virtual void resetReporter()
Remove the callback which is called upon any future connections and disconnections to/from the port...
Definition: BufferedPort.h:354
virtual void interrupt()
Interrupt any current reads or writes attached to the port.
Definition: BufferedPort.h:133
virtual void setAdminReader(PortReader &reader)
Set an external reader for unrecognized administrative port messages.
Definition: Port.cpp:474
void useCallback()
Call own onRead() method – user can override that method to be informed about data as it arrives...
Definition: BufferedPort.h:307
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Definition: BufferedPort.h:204
virtual void unlockCallback()
Unlock callbacks.
Definition: BufferedPort.h:424
virtual bool tryLockCallback()
Try to lock callbacks until unlockCallback() is called.
Definition: BufferedPort.h:420
virtual Property * acquireProperties(bool readOnly)
Access unstructured port properties.
Definition: BufferedPort.h:396
virtual void setInputMode(bool expectInput)
Configure the port to allow or forbid inputs.
Definition: BufferedPort.h:383
A base class for sources of typed data.
void setAdminReader(PortReader &reader)
Set an external reader for unrecognized administrative port messages.
Definition: BufferedPort.h:280
Basic wrapper for mutual exclusion.
Definition: Mutex.h:25