YARP
Yet Another Robot Platform
BufferedPort.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
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 
16 namespace yarp {
17 namespace os {
18 
59 template <typename T>
60 class BufferedPort : public Contactable, public TypedReader<T>, public TypedReaderCallback<T>
61 {
62 public:
64 
68  typedef T ContentType;
69 
73  BufferedPort();
74 
78  BufferedPort(Port& port);
79 
83  virtual ~BufferedPort();
84 
85  // Documented in Contactable
86  bool open(const std::string& name) override;
87 
88  // Documented in Contactable
89  bool open(const Contact& contact, bool registerName = true) override;
90 
91  // Documented in Contactable
92  bool addOutput(const std::string& name) override;
93 
94  // Documented in Contactable
95  bool addOutput(const std::string& name, const std::string& carrier) override;
96 
97  // Documented in Contactable
98  bool addOutput(const Contact& contact) override;
99 
100  // Documented in Contactable
101  void close() override;
102 
103  // Documented in Contactable
104  void interrupt() override;
105 
106  void resume() override;
107 
111  int getPendingReads() override;
112 
113  // Documented in Contactable
114  Contact where() const override;
115 
116  // Documented in Contactable
117  std::string getName() const override;
118 
119 
139  T& prepare();
140 
146  bool unprepare();
147 
162  void write(bool forceStrict = false);
163 
174  void writeStrict();
175 
179  void waitForWrite();
180 
181  // Documented in TypedReader
182  void setStrict(bool strict = true) override;
183 
184  // Documented in TypedReader
185  T* read(bool shouldWait = true) override;
186 
187  // Documented in TypedReader
188  T* lastRead() override;
189 
190  // Documented in TypedReader
191  bool isClosed() override;
192 
193  // Documented in TypedReader
194  void setReplier(PortReader& reader) override;
195 
196  // Documented in Contactable
197  void setReader(PortReader& reader) override;
198 
199  // Documented in Contactable
200  void setAdminReader(PortReader& reader) override;
201 
210  void onRead(T& datum) override;
211 
212  // Documented in TypedReader
213  void useCallback(TypedReaderCallback<T>& callback) override;
214 
220  void useCallback();
221 
222  // Documented in TypedReader
223  void disableCallback() override;
224 
225  // documented in Contactable
226  bool setEnvelope(PortWriter& envelope) override;
227 
228  // documented in Contactable
229  bool getEnvelope(PortReader& envelope) override;
230 
231  // documented in Contactable
232  int getInputCount() override;
233 
234  // documented in Contactable
235  int getOutputCount() override;
236 
237  // documented in Contactable
238  bool isWriting() override;
239 
240  // documented in Contactable
241  void getReport(PortReport& reporter) override;
242 
243  // documented in Contactable
244  void setReporter(PortReport& reporter) override;
245 
246  // documented in Contactable
247  void resetReporter() override;
248 
249  // documented in TypedReader
250  void* acquire() override;
251 
252  // documented in TypedReader
253  void release(void* handle) override;
254 
255  // documented in TypedReader
256  void setTargetPeriod(double period) override;
257 
258  // documented in Contactable
259  Type getType() override;
260 
261  // documented in Contactable
262  void promiseType(const Type& typ) override;
263 
264  // documented in Contactable
265  void setInputMode(bool expectInput) override;
266 
267  // documented in Contactable
268  void setOutputMode(bool expectOutput) override;
269 
270  // documented in Contactable
271  void setRpcMode(bool expectRpc) override;
272 
273  // documented in Contactable
274  Property* acquireProperties(bool readOnly) override;
275 
276  // documented in Contactable
277  void releaseProperties(Property* prop) override;
278 
279  // documented in Contactable
280  void includeNodeInName(bool flag) override;
281 
282 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
285  // documented in Contactable
286  YARP_DEPRECATED_MSG("Use setCallbackLock with std::mutex instead")
287  bool setCallbackLock(yarp::os::Mutex* mutex) override;
289 #endif
290 
291  // documented in Contactable
292  bool setCallbackLock(std::mutex* mutex) override;
293 
294  // documented in Contactable
295  bool removeCallbackLock() override;
296 
297  // documented in Contactable
298  bool lockCallback() override;
299 
300  // documented in Contactable
301  bool tryLockCallback() override;
302 
303  // documented in Contactable
304  void unlockCallback() override;
305 
306 private:
307  PortWriterBuffer<T> writer;
308  Port port;
309  PortReaderBuffer<T> reader;
310  bool interrupted;
311  bool attached;
312 
313  // forbid this
314  BufferedPort(const BufferedPort& alt) = delete;
315  const BufferedPort& operator=(const BufferedPort& alt) = delete;
316 
317  void attachIfNeeded();
318  bool sharedOpen(Port& port);
319 };
320 
321 } // namespace os
322 } // namespace yarp
323 
325 
326 #endif // YARP_OS_BUFFEREDPORT_H
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:61
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:68
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:36
An abstract port.
Definition: Contactable.h:35
Basic wrapper for mutual exclusion.
Definition: Mutex.h:32
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:25
A base class for objects that want information about port status changes.
Definition: PortReport.h:28
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:24
A mini-server for network communication.
Definition: Port.h:47
A class for storing options and configuration information.
Definition: Property.h:34
A callback for typed data from a port.
A base class for sources of typed data.
Definition: TypedReader.h:23
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:332
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:333