YARP
Yet Another Robot Platform
BufferedPort.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_BUFFEREDPORT_H
11 #define YARP_OS_BUFFEREDPORT_H
12 
13 #include <yarp/os/Contactable.h>
14 #include <yarp/os/Port.h>
17 
18 
19 namespace yarp {
20 namespace os {
21 
62 template <typename T>
63 class BufferedPort : public Contactable, public TypedReader<T>, public TypedReaderCallback<T>
64 {
65 public:
67 
71  typedef T ContentType;
72 
76  BufferedPort();
77 
82 
86  virtual ~BufferedPort();
87 
88  // Documented in Contactable
89  virtual bool open(const std::string& name) override;
90 
91  // Documented in Contactable
92  virtual bool open(const Contact& contact, bool registerName = true) override;
93 
94  // Documented in Contactable
95  virtual bool addOutput(const std::string& name) override;
96 
97  // Documented in Contactable
98  virtual bool addOutput(const std::string& name, const std::string& carrier) override;
99 
100  // Documented in Contactable
101  virtual bool addOutput(const Contact& contact) override;
102 
103  // Documented in Contactable
104  virtual void close() override;
105 
106  // Documented in Contactable
107  virtual void interrupt() override;
108 
109  virtual void resume() override;
110 
114  virtual int getPendingReads() override;
115 
116  // Documented in Contactable
117  virtual Contact where() const override;
118 
119  // Documented in Contactable
120  virtual std::string getName() const override;
121 
122 
142  T& prepare();
143 
149  bool unprepare();
150 
165  void write(bool forceStrict = false);
166 
177  void writeStrict();
178 
182  void waitForWrite();
183 
184  // Documented in TypedReader
185  virtual void setStrict(bool strict = true) override;
186 
187  // Documented in TypedReader
188  virtual T* read(bool shouldWait = true) override;
189 
190  // Documented in TypedReader
191  virtual T* lastRead() override;
192 
193  // Documented in TypedReader
194  virtual bool isClosed() override;
195 
196  // Documented in TypedReader
197  void setReplier(PortReader& reader) override;
198 
199  // Documented in Contactable
200  void setReader(PortReader& reader) override;
201 
202  // Documented in Contactable
203  void setAdminReader(PortReader& reader) override;
204 
213  virtual void onRead(T& datum) override;
214 
215  // Documented in TypedReader
216  virtual void useCallback(TypedReaderCallback<T>& callback) override;
217 
223  void useCallback();
224 
225  // Documented in TypedReader
226  virtual void disableCallback() override;
227 
228  // documented in Contactable
229  virtual bool setEnvelope(PortWriter& envelope) override;
230 
231  // documented in Contactable
232  virtual bool getEnvelope(PortReader& envelope) override;
233 
234  // documented in Contactable
235  virtual int getInputCount() override;
236 
237  // documented in Contactable
238  virtual int getOutputCount() override;
239 
240  // documented in Contactable
241  bool isWriting() override;
242 
243  // documented in Contactable
244  virtual void getReport(PortReport& reporter) override;
245 
246  // documented in Contactable
247  virtual void setReporter(PortReport& reporter) override;
248 
249  // documented in Contactable
250  virtual void resetReporter() override;
251 
252  // documented in TypedReader
253  virtual void* acquire() override;
254 
255  // documented in TypedReader
256  virtual void release(void* handle) override;
257 
258  // documented in TypedReader
259  virtual void setTargetPeriod(double period) override;
260 
261  // documented in Contactable
262  virtual Type getType() override;
263 
264  // documented in Contactable
265  virtual void promiseType(const Type& typ) override;
266 
267  // documented in Contactable
268  virtual void setInputMode(bool expectInput) override;
269 
270  // documented in Contactable
271  virtual void setOutputMode(bool expectOutput) override;
272 
273  // documented in Contactable
274  virtual void setRpcMode(bool expectRpc) override;
275 
276  // documented in Contactable
277  virtual Property* acquireProperties(bool readOnly) override;
278 
279  // documented in Contactable
280  virtual void releaseProperties(Property* prop) override;
281 
282  // documented in Contactable
283  virtual void includeNodeInName(bool flag) override;
284 
285  // documented in Contactable
286  virtual bool setCallbackLock(yarp::os::Mutex* mutex) override;
287 
288  // documented in Contactable
289  virtual bool removeCallbackLock() override;
290 
291  // documented in Contactable
292  virtual bool lockCallback() override;
293 
294  // documented in Contactable
295  virtual bool tryLockCallback() override;
296 
297  // documented in Contactable
298  virtual void unlockCallback() override;
299 
300 private:
305  bool attached;
306 
307  // forbid this
308  BufferedPort(const BufferedPort& alt) = delete;
309  const BufferedPort& operator=(const BufferedPort& alt) = delete;
310 
311  void attachIfNeeded();
312  bool sharedOpen(Port& port);
313 };
314 
315 } // namespace os
316 } // namespace yarp
317 
319 
320 #endif // YARP_OS_BUFFEREDPORT_H
virtual void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
virtual Contact where() const override
Returns information about how this port can be reached.
void setReader(PortReader &reader) override
Set an external reader for port data.
virtual void * acquire() override
Take control of the last object read.
virtual bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
virtual void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
virtual void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped. ...
bool isWriting() override
Report whether the port is currently writing data.
virtual int getPendingReads() override
Get the number of objects ready to be read.
virtual void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port...
virtual bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
An abstract port.
Definition: Contactable.h:35
virtual T * lastRead() override
Get the last data returned by read()
virtual T * read(bool shouldWait=true) override
Read an available object from the port.
virtual void disableCallback() override
Remove a callback set up with useCallback()
A base class for objects that want information about port status changes.
Definition: PortReport.h:30
A class for storing options and configuration information.
Definition: Property.h:34
A callback for typed data from a port.
const BufferedPort & operator=(const BufferedPort &alt)=delete
Buffer incoming data to a port.
Buffer outgoing data to a port.
virtual bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
A mini-server for network communication.
Definition: Port.h:49
virtual int getInputCount() override
Determine how many connections are arriving into this port.
virtual Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
virtual void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
virtual void setTargetPeriod(double period) override
Try to provide data periodically.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:26
virtual void onRead(T &datum) override
Callback method.
The main, catch-all namespace for YARP.
Definition: numeric.h:47
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
virtual int getOutputCount() override
Determine how many output connections this port has.
virtual void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port...
virtual bool lockCallback() override
Lock callbacks until unlockCallback() is called.
bool unprepare()
Give the last prepared object back to YARP without writing it.
PortWriterBuffer< T > writer
Definition: BufferedPort.h:301
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:27
T ContentType
The type of content stored by this BufferedPort.
Definition: BufferedPort.h:71
virtual void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
virtual Type getType() override
Get the type of data the port has committed to send/receive.
virtual void close() override
Stop port activity.
virtual void resume() override
Put the port back in an operative state after interrupt() has been called.
virtual bool addOutput(const std::string &name) override
Add an output connection to the specified port.
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
virtual bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
virtual bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port...
void waitForWrite()
Wait for any pending writes to complete.
PortReaderBuffer< T > reader
Definition: BufferedPort.h:303
bool sharedOpen(Port &port)
virtual void interrupt() override
Interrupt any current reads or writes attached to the port.
virtual void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method...
virtual std::string getName() const override
Get name of port.
Represents how to reach a part of a YARP network.
Definition: Contact.h:38
virtual void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
virtual void promiseType(const Type &typ) override
Commit the port to a particular type of data.
virtual bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
BufferedPort()
Constructor.
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:63
virtual void releaseProperties(Property *prop) override
End access unstructured port properties.
virtual bool isClosed() override
Returns whether the port associated with this reader has been closed.
void useCallback()
Use own onRead() method as callback.
virtual ~BufferedPort()
Destructor.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
virtual void unlockCallback() override
Unlock callbacks.
A base class for sources of typed data.
Definition: TypedReader.h:25
Basic wrapper for mutual exclusion.
Definition: Mutex.h:28