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
YARP_WARNING_PUSH
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
yarp::os::BufferedPort::disableCallback
void disableCallback() override
Remove a callback set up with useCallback()
Definition: BufferedPort-inl.h:225
yarp::os::BufferedPort::addOutput
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
Definition: BufferedPort-inl.h:55
yarp::os::TypedReader
A base class for sources of typed data.
Definition: TypedReader.h:26
yarp::os::BufferedPort::waitForWrite
void waitForWrite()
Wait for any pending writes to complete.
Definition: BufferedPort-inl.h:141
BufferedPort-inl.h
yarp::os::BufferedPort::lockCallback
bool lockCallback() override
Lock callbacks until unlockCallback() is called.
Definition: BufferedPort-inl.h:372
yarp::os::BufferedPort::read
T * read(bool shouldWait=true) override
Read an available object from the port.
Definition: BufferedPort-inl.h:154
yarp::os::Type
Definition: Type.h:24
Port.h
yarp::os::BufferedPort::release
void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method.
Definition: BufferedPort-inl.h:287
yarp::os::BufferedPort::releaseProperties
void releaseProperties(Property *prop) override
End access unstructured port properties.
Definition: BufferedPort-inl.h:337
yarp::os::BufferedPort::setReplier
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
Definition: BufferedPort-inl.h:183
yarp::os::BufferedPort::getReport
void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
Definition: BufferedPort-inl.h:263
yarp::os::PortReaderBuffer
Buffer incoming data to a port.
Definition: PortReaderBuffer.h:43
yarp::os::PortWriterBuffer
Buffer outgoing data to a port.
Definition: PortWriterBuffer.h:70
yarp::os::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:31
yarp::os::BufferedPort::interrupt
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: BufferedPort-inl.h:82
yarp::os::BufferedPort::unlockCallback
void unlockCallback() override
Unlock callbacks.
Definition: BufferedPort-inl.h:384
yarp::os::BufferedPort::setStrict
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
Definition: BufferedPort-inl.h:147
yarp::os::BufferedPort::setEnvelope
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: BufferedPort-inl.h:232
yarp::os::BufferedPort::~BufferedPort
virtual ~BufferedPort()
Destructor.
Definition: BufferedPort-inl.h:35
PortReaderBuffer.h
PortWriterBuffer.h
yarp::os::BufferedPort::promiseType
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: BufferedPort-inl.h:307
yarp::os::PortReport
A base class for objects that want information about port status changes.
Definition: PortReport.h:31
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
yarp::os::BufferedPort::isClosed
bool isClosed() override
Returns whether the port associated with this reader has been closed.
Definition: BufferedPort-inl.h:177
yarp::os::BufferedPort::setOutputMode
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
Definition: BufferedPort-inl.h:319
yarp::os::BufferedPort::prepare
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
Definition: BufferedPort-inl.h:114
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
yarp::os::BufferedPort::getOutputCount
int getOutputCount() override
Determine how many output connections this port has.
Definition: BufferedPort-inl.h:251
yarp::os::BufferedPort::writeStrict
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
Definition: BufferedPort-inl.h:135
yarp::os::BufferedPort::acquire
void * acquire() override
Take control of the last object read.
Definition: BufferedPort-inl.h:281
yarp::os::BufferedPort::isWriting
bool isWriting() override
Report whether the port is currently writing data.
Definition: BufferedPort-inl.h:257
yarp::os::BufferedPort::resetReporter
void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port.
Definition: BufferedPort-inl.h:275
yarp::os::BufferedPort::getPendingReads
int getPendingReads() override
Get the number of objects ready to be read.
Definition: BufferedPort-inl.h:96
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
yarp::os::BufferedPort::getInputCount
int getInputCount() override
Determine how many connections are arriving into this port.
Definition: BufferedPort-inl.h:245
yarp::os::BufferedPort::acquireProperties
Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
Definition: BufferedPort-inl.h:331
yarp::os::BufferedPort::getType
Type getType() override
Get the type of data the port has committed to send/receive.
Definition: BufferedPort-inl.h:301
yarp::os::BufferedPort::setReader
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: BufferedPort-inl.h:190
yarp::os::BufferedPort::useCallback
void useCallback()
Use own onRead() method as callback.
Definition: BufferedPort-inl.h:218
yarp::os::BufferedPort::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: BufferedPort-inl.h:41
yarp::os::BufferedPort::setReporter
void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port.
Definition: BufferedPort-inl.h:269
yarp::os::BufferedPort::getName
std::string getName() const override
Get name of port.
Definition: BufferedPort-inl.h:108
yarp::os::BufferedPort::setAdminReader
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
Definition: BufferedPort-inl.h:197
yarp::os::BufferedPort::setInputMode
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition: BufferedPort-inl.h:313
YARP_WARNING_POP
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
yarp::os::BufferedPort::where
Contact where() const override
Returns information about how this port can be reached.
Definition: BufferedPort-inl.h:102
yarp::os::BufferedPort::includeNodeInName
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Definition: BufferedPort-inl.h:343
yarp::os::BufferedPort::onRead
void onRead(T &datum) override
Callback method.
Definition: BufferedPort-inl.h:204
yarp::os::BufferedPort::setCallbackLock
bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
Definition: BufferedPort-inl.h:352
yarp::os::BufferedPort::write
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Definition: BufferedPort-inl.h:126
yarp::os::BufferedPort::BufferedPort
BufferedPort()
Constructor.
Definition: BufferedPort-inl.h:17
yarp::os::BufferedPort::lastRead
T * lastRead() override
Get the last data returned by read()
Definition: BufferedPort-inl.h:171
YARP_DEPRECATED_MSG
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
yarp::os::BufferedPort::unprepare
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: BufferedPort-inl.h:120
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::BufferedPort::close
void close() override
Stop port activity.
Definition: BufferedPort-inl.h:73
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::BufferedPort::removeCallbackLock
bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
Definition: BufferedPort-inl.h:366
yarp::os::BufferedPort::getEnvelope
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
Definition: BufferedPort-inl.h:239
yarp::os::Mutex
Basic wrapper for mutual exclusion.
Definition: Mutex.h:35
yarp::os::BufferedPort::tryLockCallback
bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
Definition: BufferedPort-inl.h:378
yarp::os::BufferedPort::setTargetPeriod
void setTargetPeriod(double period) override
Try to provide data periodically.
Definition: BufferedPort-inl.h:294
YARP_DISABLE_DEPRECATED_WARNING
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336
yarp::os::BufferedPort::resume
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: BufferedPort-inl.h:89
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
Contactable.h
yarp::os::BufferedPort::setRpcMode
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: BufferedPort-inl.h:325
yarp::os::Contactable
An abstract port.
Definition: Contactable.h:38
yarp::os::BufferedPort::ContentType
T ContentType
The type of content stored by this BufferedPort.
Definition: BufferedPort.h:71