YARP
Yet Another Robot Platform
BufferedPort-inl.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_INL_H
8 #define YARP_OS_BUFFEREDPORT_INL_H
9 
10 #include <yarp/os/BufferedPort.h>
11 #include <yarp/os/Type.h>
12 
13 template <typename T>
15  interrupted(false),
16  attached(false)
17 {
18  T example;
19  port.promiseType(example.getType());
20  port.enableBackgroundWrite(true);
21 }
22 
23 template <typename T>
25  interrupted(false),
26  attached(false)
27 {
28  sharedOpen(port);
29 }
30 
31 template <typename T>
33 {
34  close();
35 }
36 
37 template <typename T>
38 bool yarp::os::BufferedPort<T>::open(const std::string& name)
39 {
40  attachIfNeeded();
41  return port.open(name);
42 }
43 
44 template <typename T>
45 bool yarp::os::BufferedPort<T>::open(const Contact& contact, bool registerName)
46 {
47  attachIfNeeded();
48  return port.open(contact, registerName);
49 }
50 
51 template <typename T>
52 bool yarp::os::BufferedPort<T>::addOutput(const std::string& name)
53 {
54  return port.addOutput(name);
55 }
56 
57 template <typename T>
58 bool yarp::os::BufferedPort<T>::addOutput(const std::string& name, const std::string& carrier)
59 {
60  return port.addOutput(name, carrier);
61 }
62 
63 template <typename T>
65 {
66  return port.addOutput(contact);
67 }
68 
69 template <typename T>
71 {
72  port.close();
73  reader.detach();
74  writer.detach();
75  attached = false;
76 }
77 
78 template <typename T>
80 {
81  interrupted = true;
82  port.interrupt();
83 }
84 
85 template <typename T>
87 {
88  port.resume();
89  interrupted = false;
90 }
91 
92 template <typename T>
94 {
95  return reader.getPendingReads();
96 }
97 
98 template <typename T>
100 {
101  return port.where();
102 }
103 
104 template <typename T>
106 {
107  return where().getName();
108 }
109 
110 template <typename T>
112 {
113  return writer.get();
114 }
115 
116 template <typename T>
118 {
119  return writer.unprepare();
120 }
121 
122 template <typename T>
123 void yarp::os::BufferedPort<T>::write(bool forceStrict)
124 {
125  if (isClosed()) {
126  return;
127  }
128  writer.write(forceStrict);
129 }
130 
131 template <typename T>
133 {
134  write(true);
135 }
136 
137 template <typename T>
139 {
140  writer.waitForWrite();
141 }
142 
143 template <typename T>
145 {
146  attachIfNeeded();
147  reader.setStrict(strict);
148 }
149 
150 template <typename T>
152 {
153  if (!port.isOpen()) {
154  return nullptr;
155  }
156  if (interrupted) {
157  return nullptr;
158  }
159  T* result = reader.read(shouldWait);
160  // in some circs PortReaderBuffer::read(true) may return false
161  while (result == nullptr && shouldWait && !reader.isClosed() && !interrupted) {
162  result = reader.read(shouldWait);
163  }
164  return result;
165 }
166 
167 template <typename T>
169 {
170  return reader.lastRead();
171 }
172 
173 template <typename T>
175 {
176  return !port.isOpen();
177 }
178 
179 template <typename T>
181 {
182  attachIfNeeded();
183  this->reader.setReplier(reader);
184 }
185 
186 template <typename T>
188 {
189  attachIfNeeded();
190  setReplier(reader);
191 }
192 
193 template <typename T>
195 {
196  attachIfNeeded();
197  port.setAdminReader(reader);
198 }
199 
200 template <typename T>
202 {
203  YARP_UNUSED(datum);
204  // override this to do something
205 }
206 
207 template <typename T>
209 {
210  attachIfNeeded();
211  reader.useCallback(callback);
212 }
213 
214 template <typename T>
216 {
217  attachIfNeeded();
218  reader.useCallback(*this);
219 }
220 
221 template <typename T>
223 {
224  attachIfNeeded();
225  reader.disableCallback();
226 }
227 
228 template <typename T>
230 {
231  return port.setEnvelope(envelope);
232 }
233 
234 
235 template <typename T>
237 {
238  return reader.getEnvelope(envelope);
239 }
240 
241 template <typename T>
243 {
244  return port.getInputCount();
245 }
246 
247 template <typename T>
249 {
250  return port.getOutputCount();
251 }
252 
253 template <typename T>
255 {
256  return port.isWriting();
257 }
258 
259 template <typename T>
261 {
262  port.getReport(reporter);
263 }
264 
265 template <typename T>
267 {
268  port.setReporter(reporter);
269 }
270 
271 template <typename T>
273 {
274  port.resetReporter();
275 }
276 
277 template <typename T>
279 {
280  return reader.acquire();
281 }
282 
283 template <typename T>
285 {
286  reader.release(handle);
287 }
288 
289 
290 template <typename T>
292 {
293  attachIfNeeded();
294  reader.setTargetPeriod(period);
295 }
296 
297 template <typename T>
299 {
300  return port.getType();
301 }
302 
303 template <typename T>
305 {
306  port.promiseType(typ);
307 }
308 
309 template <typename T>
311 {
312  port.setInputMode(expectInput);
313 }
314 
315 template <typename T>
317 {
318  port.setOutputMode(expectOutput);
319 }
320 
321 template <typename T>
323 {
324  port.setRpcMode(expectRpc);
325 }
326 
327 template <typename T>
329 {
330  return port.acquireProperties(readOnly);
331 }
332 
333 template <typename T>
335 {
336  port.releaseProperties(prop);
337 }
338 
339 template <typename T>
341 {
342  return port.includeNodeInName(flag);
343 }
344 
345 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
348 template <typename T>
350 {
351  return port.setCallbackLock(mutex);
352 }
354 #endif
355 
356 template <typename T>
358 {
359  return port.setCallbackLock(mutex);
360 }
361 
362 template <typename T>
364 {
365  return port.removeCallbackLock();
366 }
367 
368 template <typename T>
370 {
371  return port.lockCallback();
372 }
373 
374 template <typename T>
376 {
377  return port.tryLockCallback();
378 }
379 
380 template <typename T>
382 {
383  return port.unlockCallback();
384 }
385 
386 template <typename T>
388 {
389  if (!attached) {
390  reader.attach(port);
391  writer.attach(port);
392  attached = true;
393  }
394 }
395 
396 template <typename T>
398 {
399  bool ok = this->port.sharedOpen(port);
400  if (!ok) {
401  return false;
402  }
403  reader.attach(port);
404  writer.attach(port);
405  attached = true;
406  return true;
407 }
408 
409 
410 #endif // YARP_OS_BUFFEREDPORT_INL_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.
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
Basic wrapper for mutual exclusion.
Definition: Mutex.h:32
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
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
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
Definition: Port.cpp:524
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:648
A class for storing options and configuration information.
Definition: Property.h:34
A callback for typed data from a port.
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1099
#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
#define YARP_UNUSED(var)
Definition: api.h:162