YARP
Yet Another Robot Platform
BufferedPort.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 RobotCub Consortium
3  * Authors: Paul Fitzpatrick
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
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 // Defined in this file:
17 namespace yarp { namespace os { template <typename T> class BufferedPort; }}
18 
19 
20 namespace yarp {
21 namespace os {
22 
63 template <typename T>
64 class BufferedPort : public Contactable,
65  public TypedReader<T>,
66  public TypedReaderCallback<T>
67 {
68 public:
69 #ifndef YARP_NO_DEPRECATED // since YARP 2.3.72
70  using Contactable::open;
71 #endif // YARP_NO_DEPRECATED
72 
74 
78  typedef T ContentType;
79 
83  BufferedPort();
84 
88  BufferedPort(Port& port);
89 
93  virtual ~BufferedPort();
94 
95  // Documented in Contactable
96  virtual bool open(const ConstString& name) override;
97 
98  // Documented in Contactable
99  virtual bool open(const Contact& contact, bool registerName = true) override;
100 
101  // Documented in Contactable
102  virtual bool addOutput(const ConstString& name) override;
103 
104  // Documented in Contactable
105  virtual bool addOutput(const ConstString& name, const ConstString& carrier) override;
106 
107  // Documented in Contactable
108  virtual bool addOutput(const Contact& contact) override;
109 
110  // Documented in Contactable
111  virtual void close() override;
112 
113  // Documented in Contactable
114  virtual void interrupt() override;
115 
116  virtual void resume() override;
117 
121  virtual int getPendingReads() override;
122 
123  // Documented in Contactable
124  virtual Contact where() const override;
125 
126  // Documented in Contactable
127  virtual ConstString getName() const override;
128 
129 
149  T& prepare();
150 
156  bool unprepare();
157 
172  void write(bool forceStrict = false);
173 
184  void writeStrict();
185 
189  void waitForWrite();
190 
191  // Documented in TypedReader
192  virtual void setStrict(bool strict = true) override;
193 
194  // Documented in TypedReader
195  virtual T* read(bool shouldWait = true) override;
196 
197  // Documented in TypedReader
198  virtual T* lastRead() override;
199 
200  // Documented in TypedReader
201  virtual bool isClosed() override;
202 
203  // Documented in TypedReader
204  void setReplier(PortReader& reader) override;
205 
206  // Documented in Contactable
207  void setReader(PortReader& reader) override;
208 
209  // Documented in Contactable
210  void setAdminReader(PortReader& reader) override;
211 
220  virtual void onRead(T& datum) override;
221 
222  // Documented in TypedReader
223  virtual void useCallback(TypedReaderCallback<T>& callback) override;
224 
230  void useCallback();
231 
232  // Documented in TypedReader
233  virtual void disableCallback() override;
234 
235  // documented in Contactable
236  virtual bool setEnvelope(PortWriter& envelope) override;
237 
238  // documented in Contactable
239  virtual bool getEnvelope(PortReader& envelope) override;
240 
241  // documented in Contactable
242  virtual int getInputCount() override;
243 
244  // documented in Contactable
245  virtual int getOutputCount() override;
246 
247  // documented in Contactable
248  bool isWriting() override;
249 
250  // documented in Contactable
251  virtual void getReport(PortReport& reporter) override;
252 
253  // documented in Contactable
254  virtual void setReporter(PortReport& reporter) override;
255 
256  // documented in Contactable
257  virtual void resetReporter() override ;
258 
259  // documented in TypedReader
260  virtual void* acquire() override ;
261 
262  // documented in TypedReader
263  virtual void release(void* handle) override;
264 
265  // documented in TypedReader
266  virtual void setTargetPeriod(double period) override;
267 
268  // documented in Contactable
269  virtual Type getType() override;
270 
271  // documented in Contactable
272  virtual void promiseType(const Type& typ) override;
273 
274  // documented in Contactable
275  virtual void setInputMode(bool expectInput) override;
276 
277  // documented in Contactable
278  virtual void setOutputMode(bool expectOutput) override;
279 
280  // documented in Contactable
281  virtual void setRpcMode(bool expectRpc) override;
282 
283  // documented in Contactable
284  virtual Property* acquireProperties(bool readOnly) override;
285 
286  // documented in Contactable
287  virtual void releaseProperties(Property* prop) override;
288 
289  // documented in Contactable
290  virtual void includeNodeInName(bool flag) override;
291 
292  // documented in Contactable
293  virtual bool setCallbackLock(yarp::os::Mutex* mutex) override;
294 
295  // documented in Contactable
296  virtual bool removeCallbackLock() override;
297 
298  // documented in Contactable
299  virtual bool lockCallback() override;
300 
301  // documented in Contactable
302  virtual bool tryLockCallback() override;
303 
304  // documented in Contactable
305  virtual void unlockCallback() override;
306 
307 private:
312  bool attached;
313 
314  // forbid this
315  BufferedPort(const BufferedPort& alt) = delete;
316  const BufferedPort& operator=(const BufferedPort& alt) = delete;
317 
318  void attachIfNeeded();
319  bool sharedOpen(Port& port);
320 };
321 
322 } // namespace os
323 } // namespace yarp
324 
326 
327 #endif // YARP_OS_BUFFEREDPORT_H
virtual YARP_SSIZE_T read(const Bytes &b) override
An abstract port.
Definition: Contactable.h:30
A base class for objects that want information about port status changes.
Definition: PortReport.h:26
A class for storing options and configuration information.
Definition: Property.h:32
A callback for typed data from a port.
Buffer incoming data to a port.
Buffer outgoing data to a port.
A mini-server for network communication.
Definition: Port.h:47
bool open()
Start port operation, with automatically-chosen network parameters.
Definition: Contactable.cpp:16
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
The main, catch-all namespace for YARP.
A string with almost the same api as std::string.
Definition: ConstString.h:44
static Semaphore mutex(1)
PortWriterBuffer< T > writer
Definition: BufferedPort.h:308
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:26
T ContentType
The type of content stored by this BufferedPort.
Definition: BufferedPort.h:78
virtual void interrupt() override
Definition: TcpRosStream.h:117
virtual void close() override
Definition: TcpRosStream.h:98
PortReaderBuffer< T > reader
Definition: BufferedPort.h:310
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:17
A base class for sources of typed data.
Definition: TypedReader.h:11
Basic wrapper for mutual exclusion.
Definition: Mutex.h:25
virtual void write(const Bytes &b) override