YARP
Yet Another Robot Platform
BufferedPort-inl.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_INL_H
11 #define YARP_OS_BUFFEREDPORT_INL_H
12 
13 #include <yarp/os/BufferedPort.h>
14 #include <yarp/os/Type.h>
15 
16 template <typename T>
18  interrupted(false),
19  attached(false)
20 {
21  T example;
22  port.promiseType(example.getType());
23  port.enableBackgroundWrite(true);
24 }
25 
26 template <typename T>
28  interrupted(false),
29  attached(false)
30 {
31  sharedOpen(port);
32 }
33 
34 template <typename T>
36 {
37  close();
38 }
39 
40 template <typename T>
41 bool yarp::os::BufferedPort<T>::open(const std::string& name)
42 {
43  attachIfNeeded();
44  return port.open(name);
45 }
46 
47 template <typename T>
48 bool yarp::os::BufferedPort<T>::open(const Contact& contact, bool registerName)
49 {
50  attachIfNeeded();
51  return port.open(contact, registerName);
52 }
53 
54 template <typename T>
55 bool yarp::os::BufferedPort<T>::addOutput(const std::string& name)
56 {
57  return port.addOutput(name);
58 }
59 
60 template <typename T>
61 bool yarp::os::BufferedPort<T>::addOutput(const std::string& name, const std::string& carrier)
62 {
63  return port.addOutput(name, carrier);
64 }
65 
66 template <typename T>
68 {
69  return port.addOutput(contact);
70 }
71 
72 template <typename T>
74 {
75  port.close();
76  reader.detach();
77  writer.detach();
78  attached = false;
79 }
80 
81 template <typename T>
83 {
84  interrupted = true;
85  port.interrupt();
86 }
87 
88 template <typename T>
90 {
91  port.resume();
92  interrupted = false;
93 }
94 
95 template <typename T>
97 {
98  return reader.getPendingReads();
99 }
100 
101 template <typename T>
103 {
104  return port.where();
105 }
106 
107 template <typename T>
109 {
110  return where().getName();
111 }
112 
113 template <typename T>
115 {
116  return writer.get();
117 }
118 
119 template <typename T>
121 {
122  return writer.unprepare();
123 }
124 
125 template <typename T>
126 void yarp::os::BufferedPort<T>::write(bool forceStrict)
127 {
128  if (isClosed()) {
129  return;
130  }
131  writer.write(forceStrict);
132 }
133 
134 template <typename T>
136 {
137  write(true);
138 }
139 
140 template <typename T>
142 {
143  writer.waitForWrite();
144 }
145 
146 template <typename T>
148 {
149  attachIfNeeded();
150  reader.setStrict(strict);
151 }
152 
153 template <typename T>
155 {
156  if (!port.isOpen()) {
157  return nullptr;
158  }
159  if (interrupted) {
160  return nullptr;
161  }
162  T* result = reader.read(shouldWait);
163  // in some circs PortReaderBuffer::read(true) may return false
164  while (result == nullptr && shouldWait && !reader.isClosed() && !interrupted) {
165  result = reader.read(shouldWait);
166  }
167  return result;
168 }
169 
170 template <typename T>
172 {
173  return reader.lastRead();
174 }
175 
176 template <typename T>
178 {
179  return !port.isOpen();
180 }
181 
182 template <typename T>
184 {
185  attachIfNeeded();
186  this->reader.setReplier(reader);
187 }
188 
189 template <typename T>
191 {
192  attachIfNeeded();
193  setReplier(reader);
194 }
195 
196 template <typename T>
198 {
199  attachIfNeeded();
200  port.setAdminReader(reader);
201 }
202 
203 template <typename T>
205 {
206  YARP_UNUSED(datum);
207  // override this to do something
208 }
209 
210 template <typename T>
212 {
213  attachIfNeeded();
214  reader.useCallback(callback);
215 }
216 
217 template <typename T>
219 {
220  attachIfNeeded();
221  reader.useCallback(*this);
222 }
223 
224 template <typename T>
226 {
227  attachIfNeeded();
228  reader.disableCallback();
229 }
230 
231 template <typename T>
233 {
234  return port.setEnvelope(envelope);
235 }
236 
237 
238 template <typename T>
240 {
241  return reader.getEnvelope(envelope);
242 }
243 
244 template <typename T>
246 {
247  return port.getInputCount();
248 }
249 
250 template <typename T>
252 {
253  return port.getOutputCount();
254 }
255 
256 template <typename T>
258 {
259  return port.isWriting();
260 }
261 
262 template <typename T>
264 {
265  port.getReport(reporter);
266 }
267 
268 template <typename T>
270 {
271  port.setReporter(reporter);
272 }
273 
274 template <typename T>
276 {
277  port.resetReporter();
278 }
279 
280 template <typename T>
282 {
283  return reader.acquire();
284 }
285 
286 template <typename T>
288 {
289  reader.release(handle);
290 }
291 
292 
293 template <typename T>
295 {
296  attachIfNeeded();
297  reader.setTargetPeriod(period);
298 }
299 
300 template <typename T>
302 {
303  return port.getType();
304 }
305 
306 template <typename T>
308 {
309  port.promiseType(typ);
310 }
311 
312 template <typename T>
314 {
315  port.setInputMode(expectInput);
316 }
317 
318 template <typename T>
320 {
321  port.setOutputMode(expectOutput);
322 }
323 
324 template <typename T>
326 {
327  port.setRpcMode(expectRpc);
328 }
329 
330 template <typename T>
332 {
333  return port.acquireProperties(readOnly);
334 }
335 
336 template <typename T>
338 {
339  port.releaseProperties(prop);
340 }
341 
342 template <typename T>
344 {
345  return port.includeNodeInName(flag);
346 }
347 
348 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
351 template <typename T>
353 {
354  return port.setCallbackLock(mutex);
355 }
357 #endif
358 
359 template <typename T>
361 {
362  return port.setCallbackLock(mutex);
363 }
364 
365 template <typename T>
367 {
368  return port.removeCallbackLock();
369 }
370 
371 template <typename T>
373 {
374  return port.lockCallback();
375 }
376 
377 template <typename T>
379 {
380  return port.tryLockCallback();
381 }
382 
383 template <typename T>
385 {
386  return port.unlockCallback();
387 }
388 
389 template <typename T>
391 {
392  if (!attached) {
393  reader.attach(port);
394  writer.attach(port);
395  attached = true;
396  }
397 }
398 
399 template <typename T>
401 {
402  bool ok = this->port.sharedOpen(port);
403  if (!ok) {
404  return false;
405  }
406  reader.attach(port);
407  writer.attach(port);
408  attached = true;
409  return true;
410 }
411 
412 
413 #endif // YARP_OS_BUFFEREDPORT_INL_H
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
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:39
Basic wrapper for mutual exclusion.
Definition: Mutex.h:35
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
A base class for objects that want information about port status changes.
Definition: PortReport.h:31
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
A mini-server for network communication.
Definition: Port.h:50
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
Definition: Port.cpp:527
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:651
A class for storing options and configuration information.
Definition: Property.h:37
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:804
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336
#define YARP_UNUSED(var)
Definition: api.h:159