YARP
Yet Another Robot Platform
BufferedPort.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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 
81  BufferedPort(Port& port);
82 
86  virtual ~BufferedPort();
87 
88  // Documented in Contactable
89  bool open(const std::string& name) override;
90 
91  // Documented in Contactable
92  bool open(const Contact& contact, bool registerName = true) override;
93 
94  // Documented in Contactable
95  bool addOutput(const std::string& name) override;
96 
97  // Documented in Contactable
98  bool addOutput(const std::string& name, const std::string& carrier) override;
99 
100  // Documented in Contactable
101  bool addOutput(const Contact& contact) override;
102 
103  // Documented in Contactable
104  void close() override;
105 
106  // Documented in Contactable
107  void interrupt() override;
108 
109  void resume() override;
110 
114  int getPendingReads() override;
115 
116  // Documented in Contactable
117  Contact where() const override;
118 
119  // Documented in Contactable
120  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  void setStrict(bool strict = true) override;
186 
187  // Documented in TypedReader
188  T* read(bool shouldWait = true) override;
189 
190  // Documented in TypedReader
191  T* lastRead() override;
192 
193  // Documented in TypedReader
194  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  void onRead(T& datum) override;
214 
215  // Documented in TypedReader
216  void useCallback(TypedReaderCallback<T>& callback) override;
217 
223  void useCallback();
224 
225  // Documented in TypedReader
226  void disableCallback() override;
227 
228  // documented in Contactable
229  bool setEnvelope(PortWriter& envelope) override;
230 
231  // documented in Contactable
232  bool getEnvelope(PortReader& envelope) override;
233 
234  // documented in Contactable
235  int getInputCount() override;
236 
237  // documented in Contactable
238  int getOutputCount() override;
239 
240  // documented in Contactable
241  bool isWriting() override;
242 
243  // documented in Contactable
244  void getReport(PortReport& reporter) override;
245 
246  // documented in Contactable
247  void setReporter(PortReport& reporter) override;
248 
249  // documented in Contactable
250  void resetReporter() override;
251 
252  // documented in TypedReader
253  void* acquire() override;
254 
255  // documented in TypedReader
256  void release(void* handle) override;
257 
258  // documented in TypedReader
259  void setTargetPeriod(double period) override;
260 
261  // documented in Contactable
262  Type getType() override;
263 
264  // documented in Contactable
265  void promiseType(const Type& typ) override;
266 
267  // documented in Contactable
268  void setInputMode(bool expectInput) override;
269 
270  // documented in Contactable
271  void setOutputMode(bool expectOutput) override;
272 
273  // documented in Contactable
274  void setRpcMode(bool expectRpc) override;
275 
276  // documented in Contactable
277  Property* acquireProperties(bool readOnly) override;
278 
279  // documented in Contactable
280  void releaseProperties(Property* prop) override;
281 
282  // documented in Contactable
283  void includeNodeInName(bool flag) override;
284 
285 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
288  // documented in Contactable
289  YARP_DEPRECATED_MSG("Use setCallbackLock with std::mutex instead")
290  bool setCallbackLock(yarp::os::Mutex* mutex) override;
292 #endif
293 
294  // documented in Contactable
295  bool setCallbackLock(std::mutex* mutex) override;
296 
297  // documented in Contactable
298  bool removeCallbackLock() override;
299 
300  // documented in Contactable
301  bool lockCallback() override;
302 
303  // documented in Contactable
304  bool tryLockCallback() override;
305 
306  // documented in Contactable
307  void unlockCallback() override;
308 
309 private:
310  PortWriterBuffer<T> writer;
311  Port port;
312  PortReaderBuffer<T> reader;
313  bool interrupted;
314  bool attached;
315 
316  // forbid this
317  BufferedPort(const BufferedPort& alt) = delete;
318  const BufferedPort& operator=(const BufferedPort& alt) = delete;
319 
320  void attachIfNeeded();
321  bool sharedOpen(Port& port);
322 };
323 
324 } // namespace os
325 } // namespace yarp
326 
328 
329 #endif // YARP_OS_BUFFEREDPORT_H
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
Type getType() override
Get the type of data the port has committed to send/receive.
bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
Contact where() const override
Returns information about how this port can be reached.
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
void waitForWrite()
Wait for any pending writes to complete.
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
void * acquire() override
Take control of the last object read.
void resume() override
Put the port back in an operative state after interrupt() has been called.
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
void onRead(T &datum) override
Callback method.
std::string getName() const override
Get name of port.
bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
void close() override
Stop port activity.
void releaseProperties(Property *prop) override
End access unstructured port properties.
void disableCallback() override
Remove a callback set up with useCallback()
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method.
void setReader(PortReader &reader) override
Set an external reader for port data.
BufferedPort()
Constructor.
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
int getPendingReads() override
Get the number of objects ready to be read.
T ContentType
The type of content stored by this BufferedPort.
Definition: BufferedPort.h:71
void interrupt() override
Interrupt any current reads or writes attached to the port.
void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port.
int getInputCount() override
Determine how many connections are arriving into this port.
bool isWriting() override
Report whether the port is currently writing data.
void useCallback()
Use own onRead() method as callback.
bool isClosed() override
Returns whether the port associated with this reader has been closed.
bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
T * read(bool shouldWait=true) override
Read an available object from the port.
T * lastRead() override
Get the last data returned by read()
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
void setTargetPeriod(double period) override
Try to provide data periodically.
bool lockCallback() override
Lock callbacks until unlockCallback() is called.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
int getOutputCount() override
Determine how many output connections this port has.
virtual ~BufferedPort()
Destructor.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
bool unprepare()
Give the last prepared object back to YARP without writing it.
void unlockCallback() override
Unlock callbacks.
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
An abstract port.
Definition: Contactable.h:38
Basic wrapper for mutual exclusion.
Definition: Mutex.h:35
Buffer incoming data to a port.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
A base class for objects that want information about port status changes.
Definition: PortReport.h:31
Buffer outgoing data to a port.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
A mini-server for network communication.
Definition: Port.h:50
A class for storing options and configuration information.
Definition: Property.h:37
A callback for typed data from a port.
A base class for sources of typed data.
Definition: TypedReader.h:26
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
The main, catch-all namespace for YARP.
Definition: environment.h:18
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336