YARP
Yet Another Robot Platform
BufferedPort-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 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 reader.isClosed();
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 template <typename T>
350 {
351  return port.setCallbackLock(mutex);
352 }
353 
354 template <typename T>
356 {
357  return port.removeCallbackLock();
358 }
359 
360 template <typename T>
362 {
363  return port.lockCallback();
364 }
365 
366 template <typename T>
368 {
369  return port.tryLockCallback();
370 }
371 
372 template <typename T>
374 {
375  return port.unlockCallback();
376 }
377 
378 template <typename T>
380 {
381  if (!attached) {
382  reader.attach(port);
383  writer.attach(port);
384  attached = true;
385  }
386 }
387 
388 template <typename T>
390 {
391  bool ok = this->port.sharedOpen(port);
392  if (!ok) {
393  return false;
394  }
395  reader.attach(port);
396  writer.attach(port);
397  attached = true;
398  return true;
399 }
400 
401 
402 #endif // YARP_OS_BUFFEREDPORT_INL_H
bool sharedOpen(Port &port)
Open a port wrapping an existing port.
Definition: Port.cpp:63
virtual void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
virtual Contact where() const override
Returns information about how this port can be reached.
void setReader(PortReader &reader) override
Set an external reader for port data.
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:430
virtual void * acquire() override
Take control of the last object read.
virtual bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
virtual void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
virtual void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped. ...
bool isWriting() override
Report whether the port is currently writing data.
virtual int getPendingReads() override
Get the number of objects ready to be read.
virtual void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port...
virtual bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
virtual T * lastRead() override
Get the last data returned by read()
virtual T * read(bool shouldWait=true) override
Read an available object from the port.
virtual void disableCallback() override
Remove a callback set up with useCallback()
A base class for objects that want information about port status changes.
Definition: PortReport.h:29
A class for storing options and configuration information.
Definition: Property.h:35
A callback for typed data from a port.
virtual bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
A mini-server for network communication.
Definition: Port.h:50
virtual int getInputCount() override
Determine how many connections are arriving into this port.
virtual Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
virtual void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
virtual void setTargetPeriod(double period) override
Try to provide data periodically.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:26
virtual void onRead(T &datum) override
Callback method.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
virtual int getOutputCount() override
Determine how many output connections this port has.
virtual void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port...
virtual bool lockCallback() override
Lock callbacks until unlockCallback() is called.
bool unprepare()
Give the last prepared object back to YARP without writing it.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:27
virtual void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
virtual Type getType() override
Get the type of data the port has committed to send/receive.
virtual void close() override
Stop port activity.
virtual void resume() override
Put the port back in an operative state after interrupt() has been called.
virtual bool addOutput(const std::string &name) override
Add an output connection to the specified port.
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
virtual bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
virtual bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port...
virtual void close() override
Definition: TcpRosStream.h:99
void waitForWrite()
Wait for any pending writes to complete.
bool sharedOpen(Port &port)
virtual void interrupt() override
Interrupt any current reads or writes attached to the port.
virtual void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method...
virtual std::string getName() const override
Get name of port.
Represents how to reach a part of a YARP network.
Definition: Contact.h:34
virtual void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
virtual void promiseType(const Type &typ) override
Commit the port to a particular type of data.
virtual bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
BufferedPort()
Constructor.
virtual void releaseProperties(Property *prop) override
End access unstructured port properties.
virtual bool isClosed() override
Returns whether the port associated with this reader has been closed.
void useCallback()
Use own onRead() method as callback.
virtual ~BufferedPort()
Destructor.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
virtual void unlockCallback() override
Unlock callbacks.
Basic wrapper for mutual exclusion.
Definition: Mutex.h:28