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>
211 void yarp::os::BufferedPort<T>::useCallback(TypedReaderCallback<T>& callback)
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
YARP_WARNING_PUSH
#define YARP_WARNING_PUSH
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::BufferedPort::waitForWrite
void waitForWrite()
Wait for any pending writes to complete.
Definition: BufferedPort-inl.h:141
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:23
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
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::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
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
Definition: PortReport.h:30
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:26
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
Definition: Port.h:49
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:63
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
Type.h
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:27
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
BufferedPort.h
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
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::os::BufferedPort::unprepare
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: BufferedPort-inl.h:120
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:38
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:34
yarp::os::BufferedPort::tryLockCallback
bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
Definition: BufferedPort-inl.h:378
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:804
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
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:34
yarp::os::BufferedPort::setRpcMode
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: BufferedPort-inl.h:325