A mini-server for performing network communication in the background. More...
#include <yarp/os/BufferedPort.h>
Public Types | |
typedef T | ContentType |
The type of content stored by this BufferedPort. More... | |
Public Member Functions | |
BufferedPort () | |
Constructor. More... | |
BufferedPort (Port &port) | |
Wrap an existing unbuffered port. More... | |
virtual | ~BufferedPort () |
Destructor. More... | |
bool | open (const std::string &name) override |
Start port operation, with a specific name, with automatically-chosen network parameters. More... | |
bool | open (const Contact &contact, bool registerName=true) override |
Start port operation with user-chosen network parameters. More... | |
bool | addOutput (const std::string &name) override |
Add an output connection to the specified port. More... | |
bool | addOutput (const std::string &name, const std::string &carrier) override |
Add an output connection to the specified port, using a specified carrier. More... | |
bool | addOutput (const Contact &contact) override |
Add an output connection to the specified port, using specified network parameters. More... | |
void | close () override |
Stop port activity. More... | |
void | interrupt () override |
Interrupt any current reads or writes attached to the port. More... | |
void | resume () override |
Put the port back in an operative state after interrupt() has been called. More... | |
int | getPendingReads () override |
Get the number of objects ready to be read. More... | |
Contact | where () const override |
Returns information about how this port can be reached. More... | |
std::string | getName () const override |
Get name of port. More... | |
T & | prepare () |
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write. More... | |
bool | unprepare () |
Give the last prepared object back to YARP without writing it. More... | |
void | write (bool forceStrict=false) |
Write the current object being returned by BufferedPort::prepare. More... | |
void | writeStrict () |
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends are complete. More... | |
void | waitForWrite () |
Wait for any pending writes to complete. More... | |
void | setStrict (bool strict=true) override |
Call this to strictly keep all messages, or allow old ones to be quietly dropped. More... | |
T * | read (bool shouldWait=true) override |
Read an available object from the port. More... | |
T * | lastRead () override |
Get the last data returned by read() More... | |
bool | isClosed () override |
Returns whether the port associated with this reader has been closed. More... | |
void | setReplier (PortReader &reader) override |
If a message is received that requires a reply, use this handler. More... | |
void | setReader (PortReader &reader) override |
Set an external reader for port data. More... | |
void | setAdminReader (PortReader &reader) override |
Set an external reader for unrecognized administrative port messages. More... | |
void | onRead (T &datum) override |
Callback method. More... | |
void | useCallback (TypedReaderCallback< T > &callback) override |
Set an object whose onRead method will be called when data is available. More... | |
void | useCallback () |
Use own onRead() method as callback. More... | |
void | disableCallback () override |
Remove a callback set up with useCallback() More... | |
bool | setEnvelope (PortWriter &envelope) override |
Set an envelope (e.g., a timestamp) to the next message which will be sent. More... | |
bool | getEnvelope (PortReader &envelope) override |
Get the envelope information (e.g., a timestamp) from the last message received on the port. More... | |
int | getInputCount () override |
Determine how many connections are arriving into this port. More... | |
int | getOutputCount () override |
Determine how many output connections this port has. More... | |
bool | isWriting () override |
Report whether the port is currently writing data. More... | |
void | getReport (PortReport &reporter) override |
Get information on the state of the port - connections etc. More... | |
void | setReporter (PortReport &reporter) override |
Set a callback to be called upon any future connections and disconnections to/from the port. More... | |
void | resetReporter () override |
Remove the callback which is called upon any future connections and disconnections to/from the port. More... | |
void * | acquire () override |
Take control of the last object read. More... | |
void | release (void *handle) override |
Return control to YARP of an object previously taken control of with the acquire() method. More... | |
void | setTargetPeriod (double period) override |
Try to provide data periodically. More... | |
Type | getType () override |
Get the type of data the port has committed to send/receive. More... | |
void | promiseType (const Type &typ) override |
Commit the port to a particular type of data. More... | |
void | setInputMode (bool expectInput) override |
Configure the port to allow or forbid inputs. More... | |
void | setOutputMode (bool expectOutput) override |
Configure the port to allow or forbid outputs. More... | |
void | setRpcMode (bool expectRpc) override |
Configure the port to be RPC only. More... | |
Property * | acquireProperties (bool readOnly) override |
Access unstructured port properties. More... | |
void | releaseProperties (Property *prop) override |
End access unstructured port properties. More... | |
void | includeNodeInName (bool flag) override |
Choose whether to prepend a node name (if one is available) to the port's name. More... | |
bool | setCallbackLock (yarp::os::Mutex *mutex) override |
Add a lock to use when invoking callbacks. More... | |
bool | setCallbackLock (std::mutex *mutex) override |
Add a lock to use when invoking callbacks. More... | |
bool | removeCallbackLock () override |
Remove a lock on callbacks added with setCallbackLock() More... | |
bool | lockCallback () override |
Lock callbacks until unlockCallback() is called. More... | |
bool | tryLockCallback () override |
Try to lock callbacks until unlockCallback() is called. More... | |
void | unlockCallback () override |
Unlock callbacks. More... | |
Public Member Functions inherited from yarp::os::Contactable | |
virtual | ~Contactable () |
Destructor. More... | |
void | setReadOnly () |
Shorthand for setInputMode(true), setOutputMode(false), setRpcMode(false) More... | |
void | setWriteOnly () |
Shorthand for setInputMode(false), setOutputMode(true), setRpcMode(false) More... | |
void | setRpcServer () |
Shorthand for setInputMode(true), setOutputMode(false), setRpcMode(true) More... | |
void | setRpcClient () |
Shorthand for setInputMode(false), setOutputMode(true), setRpcMode(true) More... | |
Public Member Functions inherited from yarp::os::TypedReader< T > | |
virtual | ~TypedReader ()=default |
Destructor. More... | |
Public Member Functions inherited from yarp::os::TypedReaderCallback< T > | |
virtual | ~TypedReaderCallback () |
Destructor. More... | |
virtual void | onRead (T &datum, const yarp::os::TypedReader< T > &reader) |
Callback method. More... | |
A mini-server for performing network communication in the background.
If you are a beginner, you might want to use the yarp::os::Port class first, and then come back to BufferedPort when you want to send and receive messages in the background without having to stop your processing. This is convenient, but requires a little care to understand the life-cycle of objects written to and read from the network (see BufferedPort::read and BufferedPort::write).
By default a BufferedPort attempts to reduce latency between senders and receivers. To do so messages may be dropped by the writer if BufferedPort::write is called too quickly The reader may also drop old messages if BufferedPort::read is not called fast enough, so that new messages can travel with high priority. This policy is sometimes called Oldest Packet Drop (ODP).
If your application cannot afford dropping messages you can change the buffering policy. Use BufferedPort::writeStrict() when writing to a port, this waits for pending transmissions to be finished before writing new data. Call BufferedPort::setStrict() to change the buffering policy to FIFO at the receiver side. In this way all messages will be stored inside the BufferedPort and delivered to the reader. Pay attention that in this case a slow reader may cause increasing latency and memory use.
Methods that can be useful to monitor the status of read and write operations are yarp::os::BufferedPort::getPendingReads() and yarp::os::BufferedPort::isWriting().
Definition at line 63 of file BufferedPort.h.
typedef T yarp::os::BufferedPort< T >::ContentType |
The type of content stored by this BufferedPort.
Definition at line 71 of file BufferedPort.h.
yarp::os::BufferedPort< T >::BufferedPort |
Constructor.
Definition at line 17 of file BufferedPort-inl.h.
yarp::os::BufferedPort< T >::BufferedPort | ( | Port & | port | ) |
Wrap an existing unbuffered port.
Definition at line 27 of file BufferedPort-inl.h.
|
virtual |
Destructor.
Definition at line 35 of file BufferedPort-inl.h.
|
overridevirtual |
Take control of the last object read.
YARP will not reuse that object until it is explicitly released by the user. Be careful - if you acquire objects without releasing, YARP will keep making new ones to store incoming messages. So you need to release all objects you acquire eventually to avoid running out of memory.
Implements yarp::os::TypedReader< T >.
Definition at line 281 of file BufferedPort-inl.h.
|
overridevirtual |
Access unstructured port properties.
readOnly | set this if you won't be modifying the properties. |
Implements yarp::os::Contactable.
Definition at line 331 of file BufferedPort-inl.h.
|
overridevirtual |
Add an output connection to the specified port, using specified network parameters.
contact | information on how to reach the target |
Implements yarp::os::Contactable.
Definition at line 67 of file BufferedPort-inl.h.
|
overridevirtual |
Add an output connection to the specified port.
name | the name of the target port |
Implements yarp::os::Contactable.
Definition at line 55 of file BufferedPort-inl.h.
|
overridevirtual |
Add an output connection to the specified port, using a specified carrier.
name | the name of the target port |
carrier | the carrier (network protocol) to use, e.g. "tcp", "udp", "mcast", "text", ... |
Implements yarp::os::Contactable.
Definition at line 61 of file BufferedPort-inl.h.
|
overridevirtual |
Stop port activity.
Implements yarp::os::Contactable.
Definition at line 73 of file BufferedPort-inl.h.
|
overridevirtual |
Remove a callback set up with useCallback()
Implements yarp::os::TypedReader< T >.
Definition at line 225 of file BufferedPort-inl.h.
|
overridevirtual |
Get the envelope information (e.g., a timestamp) from the last message received on the port.
You must be sure to match the type of your envelope for getEnvelope with whatever is being sent using setEnvelope. The Stamp class is a typical choice for timestamps. The Bottle class also works as an envelope, but it is not specialized to be efficient.
envelope | recipient for envelope information for last message received by port. |
Implements yarp::os::Contactable.
Definition at line 239 of file BufferedPort-inl.h.
|
overridevirtual |
Determine how many connections are arriving into this port.
In other words, how many other ports have this port listed as an output?
Implements yarp::os::Contactable.
Definition at line 245 of file BufferedPort-inl.h.
|
overridevirtual |
Get name of port.
Reimplemented from yarp::os::Contactable.
Definition at line 108 of file BufferedPort-inl.h.
|
overridevirtual |
Determine how many output connections this port has.
Implements yarp::os::Contactable.
Definition at line 251 of file BufferedPort-inl.h.
|
overridevirtual |
Get the number of objects ready to be read.
Implements yarp::os::TypedReader< T >.
Definition at line 96 of file BufferedPort-inl.h.
|
overridevirtual |
Get information on the state of the port - connections etc.
PortReport::report will be called once for each connection to the port that exists right now. To request callbacks for any future connections/disconnections, use the setReporter method instead.
reporter | callback for port event/state information |
Implements yarp::os::Contactable.
Definition at line 263 of file BufferedPort-inl.h.
|
overridevirtual |
Get the type of data the port has committed to send/receive.
Not all ports commit to a particular type of data.
Implements yarp::os::Contactable.
Definition at line 301 of file BufferedPort-inl.h.
|
overridevirtual |
Choose whether to prepend a node name (if one is available) to the port's name.
Node names are set using yarp::os::Node.
flag | true if the node name should be added to port names |
Implements yarp::os::Contactable.
Definition at line 343 of file BufferedPort-inl.h.
|
overridevirtual |
Interrupt any current reads or writes attached to the port.
This is useful prior to calling close(), if there are multiple threads operating on the port. Any reads or writes after the call to interrupt() will fail - unless resume() is called.
Implements yarp::os::Contactable.
Definition at line 82 of file BufferedPort-inl.h.
|
overridevirtual |
Returns whether the port associated with this reader has been closed.
Implements yarp::os::TypedReader< T >.
Definition at line 177 of file BufferedPort-inl.h.
|
overridevirtual |
Report whether the port is currently writing data.
Implements yarp::os::Contactable.
Definition at line 257 of file BufferedPort-inl.h.
|
overridevirtual |
Get the last data returned by read()
Implements yarp::os::TypedReader< T >.
Definition at line 171 of file BufferedPort-inl.h.
|
overridevirtual |
Lock callbacks until unlockCallback() is called.
Has no effect if no lock has been set via a call to setCallbackLock(). Will block if callbacks are already locked.
Implements yarp::os::Contactable.
Definition at line 372 of file BufferedPort-inl.h.
|
overridevirtual |
Callback method.
this method will be called with new data, as long as you've requested this be done by calling useCallback()
datum | data read from a port |
Reimplemented from yarp::os::TypedReaderCallback< T >.
Reimplemented in JoypadControl::JoyPort< T >.
Definition at line 204 of file BufferedPort-inl.h.
|
overridevirtual |
Start port operation with user-chosen network parameters.
Contact information is supplied by the user rather than the name server. If the Contact information is incomplete, the name server is used to complete it (set registerName to false if you don't want name server help).
Implements yarp::os::Contactable.
Definition at line 48 of file BufferedPort-inl.h.
|
overridevirtual |
Start port operation, with a specific name, with automatically-chosen network parameters.
The port is registered with the given name, and allocated network resources, by communicating with the YARP name server.
Implements yarp::os::Contactable.
Definition at line 41 of file BufferedPort-inl.h.
T & yarp::os::BufferedPort< T >::prepare |
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
The object can safely be modified by the user of this class, to prepare it. Extra objects will be created or reused as necessary depending on the state of communication with the output(s) of the port.
Definition at line 114 of file BufferedPort-inl.h.
|
overridevirtual |
Commit the port to a particular type of data.
typ | the type of data the port will send/receive |
Implements yarp::os::Contactable.
Definition at line 307 of file BufferedPort-inl.h.
|
overridevirtual |
Read an available object from the port.
shouldWait | true if the method should wait until an object is available, false if the call should return immediately if no message is available |
Implements yarp::os::TypedReader< T >.
Definition at line 154 of file BufferedPort-inl.h.
|
overridevirtual |
Return control to YARP of an object previously taken control of with the acquire() method.
handle | the pointer returned by acquire() when control of the object was taken by the user. |
Implements yarp::os::TypedReader< T >.
Definition at line 287 of file BufferedPort-inl.h.
|
overridevirtual |
End access unstructured port properties.
prop | the port property object provided by acquireProperties() |
Implements yarp::os::Contactable.
Definition at line 337 of file BufferedPort-inl.h.
|
overridevirtual |
Remove a lock on callbacks added with setCallbackLock()
Implements yarp::os::Contactable.
Definition at line 366 of file BufferedPort-inl.h.
|
overridevirtual |
Remove the callback which is called upon any future connections and disconnections to/from the port.
Implements yarp::os::Contactable.
Definition at line 275 of file BufferedPort-inl.h.
|
overridevirtual |
Put the port back in an operative state after interrupt() has been called.
Implements yarp::os::Contactable.
Definition at line 89 of file BufferedPort-inl.h.
|
overridevirtual |
Set an external reader for unrecognized administrative port messages.
reader | the external reader to use |
Implements yarp::os::Contactable.
Definition at line 197 of file BufferedPort-inl.h.
|
overridevirtual |
Add a lock to use when invoking callbacks.
mutex.lock() will be called before and mutex.unlock() will be called after the callback. This applies at least to callbacks set by setReader and setAdminReader, and in future may apply to other callbacks.
mutex | the lock to use. If nullptr, a mutex will be allocated internally by the port, and destroyed with the port. |
Implements yarp::os::Contactable.
Definition at line 360 of file BufferedPort-inl.h.
|
overridevirtual |
Add a lock to use when invoking callbacks.
mutex.lock() will be called before and mutex.unlock() will be called after the callback. This applies at least to callbacks set by setReader and setAdminReader, and in future may apply to other callbacks.
mutex | the lock to use. If nullptr, a mutex will be allocated internally by the port, and destroyed with the port. |
Implements yarp::os::Contactable.
Definition at line 352 of file BufferedPort-inl.h.
|
overridevirtual |
Set an envelope (e.g., a timestamp) to the next message which will be sent.
You must be sure to match the type of your envelope for setEnvelope with whatever will be read using getEnvelope. The Stamp class is a typical choice for timestamps. The Bottle class also works as an envelope, but it is not specialized to be efficient.
Currently, for proper operation, the envelope must serialize correctly in text-mode (even if you do not explicitly use text-mode connections). Bottle or Stamp are good choices.
envelope | information to add to the next message which will be sent |
Implements yarp::os::Contactable.
Definition at line 232 of file BufferedPort-inl.h.
|
overridevirtual |
Configure the port to allow or forbid inputs.
By default, ports allow anything.
expectInput | set to true if this port will be used for input |
Implements yarp::os::Contactable.
Definition at line 313 of file BufferedPort-inl.h.
|
overridevirtual |
Configure the port to allow or forbid outputs.
By default, ports allow anything.
expectOutput | set to true if this port will be used for output |
Implements yarp::os::Contactable.
Definition at line 319 of file BufferedPort-inl.h.
|
overridevirtual |
Set an external reader for port data.
reader | the external reader to use |
Implements yarp::os::Contactable.
Definition at line 190 of file BufferedPort-inl.h.
|
overridevirtual |
If a message is received that requires a reply, use this handler.
No buffering happens.
reader | the handler to use |
Implements yarp::os::TypedReader< T >.
Definition at line 183 of file BufferedPort-inl.h.
|
overridevirtual |
Set a callback to be called upon any future connections and disconnections to/from the port.
To get information on the current connections that exist, use the getReport method instead.
reporter | callback for port event/state information |
Implements yarp::os::Contactable.
Definition at line 269 of file BufferedPort-inl.h.
|
overridevirtual |
Configure the port to be RPC only.
By default all ports can be used for RPC or streaming communication.
expectRpc | set to true if this port will be used for RPC only |
Implements yarp::os::Contactable.
Definition at line 325 of file BufferedPort-inl.h.
|
overridevirtual |
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
If you don't call this, old messages will be quietly dropped.
strict | True to keep all messages until they are read, false to drop old messages when a new one comes in. |
Implements yarp::os::TypedReader< T >.
Definition at line 147 of file BufferedPort-inl.h.
|
overridevirtual |
Try to provide data periodically.
If no new data arrives in a given period, repeat the last data received (if any). Similarly, the port should not pass on data more frequently than the given period.
period | target period in (fractional) seconds. |
Implements yarp::os::TypedReader< T >.
Definition at line 294 of file BufferedPort-inl.h.
|
overridevirtual |
Try to lock callbacks until unlockCallback() is called.
Has no effect if no lock has been set via a call to setCallbackLock(). Returns immediately.
Implements yarp::os::Contactable.
Definition at line 378 of file BufferedPort-inl.h.
|
overridevirtual |
Unlock callbacks.
Implements yarp::os::Contactable.
Definition at line 384 of file BufferedPort-inl.h.
bool yarp::os::BufferedPort< T >::unprepare |
Give the last prepared object back to YARP without writing it.
Definition at line 120 of file BufferedPort-inl.h.
void yarp::os::BufferedPort< T >::useCallback |
Use own onRead() method as callback.
User can override that method to be informed about data as it arrives.
Definition at line 218 of file BufferedPort-inl.h.
|
overridevirtual |
Set an object whose onRead method will be called when data is available.
callback | the object whose onRead method will be called with data |
Implements yarp::os::TypedReader< T >.
Definition at line 211 of file BufferedPort-inl.h.
void yarp::os::BufferedPort< T >::waitForWrite |
Wait for any pending writes to complete.
Definition at line 141 of file BufferedPort-inl.h.
|
overridevirtual |
Returns information about how this port can be reached.
Implements yarp::os::Contactable.
Definition at line 102 of file BufferedPort-inl.h.
void yarp::os::BufferedPort< T >::write | ( | bool | forceStrict = false | ) |
Write the current object being returned by BufferedPort::prepare.
forceStrict | If this is true, wait until any previous sends are complete. If false, the current object will not be sent on connections that are currently busy. |
Definition at line 126 of file BufferedPort-inl.h.
void yarp::os::BufferedPort< T >::writeStrict |
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends are complete.
Definition at line 135 of file BufferedPort-inl.h.