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