YARP
Yet Another Robot Platform
Subscriber.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_SUBSCRIBER_H
10 #define YARP_OS_SUBSCRIBER_H
11 
13 #include <yarp/os/BufferedPort.h>
14 #include <yarp/os/Log.h>
15 
16 namespace yarp {
17 namespace os {
18 
24 template <class T>
26 {
27 public:
29 
35  Subscriber(const std::string& name = "")
36  {
37  buffered_port = nullptr;
38  T example;
39  port.promiseType(example.getType());
40  port.setInputMode(true);
41  port.setOutputMode(false);
42  port.setRpcMode(false);
43  if (name != "") {
44  bool ret = topic(name);
45  yAssert(ret);
46  YARP_UNUSED(ret); // FIXME [[maybe-unused]]
47  }
48  isStrict = false;
49  }
50 
54  virtual ~Subscriber()
55  {
56  clear();
57  }
58 
66  bool topic(const std::string& name)
67  {
68  port.includeNodeInName(true);
69  return open(name);
70  }
71 
72  // documentation provided in Contactable
73  bool open(const std::string& name) override
74  {
75  clear();
76  return port.open(name);
77  }
78 
79  // documentation provided in Contactable
80  bool open(const Contact& contact, bool registerName = true) override
81  {
82  clear();
83  return port.open(contact, registerName);
84  }
85 
86  // documentation provided in Contactable
87  void close() override
88  {
89  active().close();
90  }
91 
92  // documentation provided in Contactable
93  void interrupt() override
94  {
95  active().interrupt();
96  }
97 
98  // documentation provided in Contactable
99  void resume() override
100  {
101  active().resume();
102  }
103 
104  // documented in Contactable
105  void setReader(PortReader& reader) override
106  {
107  active().setReader(reader);
108  }
109 
117  T* read(bool shouldWait = true)
118  {
119  return buffer().read(shouldWait);
120  }
121 
122  Port& asPort() override
123  {
124  return port;
125  }
126 
127  const Port& asPort() const override
128  {
129  return port;
130  }
131 
133  void onRead(T& datum) override
134  {
135  YARP_UNUSED(datum);
136  // override this to do something
137  }
138 
140  {
141  buffer().useCallback(callback);
142  }
143 
144  void useCallback()
145  {
146  buffer().useCallback(*this);
147  }
148 
150  {
151  buffer().disableCallback();
152  }
153 
154  void setStrict(bool strict = true)
155  {
156  isStrict = strict;
157  if (buffered_port)
158  buffered_port->setStrict(strict);
159  }
160 
161 private:
162  bool isStrict;
163  Port port;
164  BufferedPort<T>* buffered_port;
165 
166  Contactable& active()
167  {
168  if (buffered_port)
169  return *buffered_port;
170  return port;
171  }
172 
173  BufferedPort<T>& buffer()
174  {
175  if (!buffered_port) {
176  buffered_port = new BufferedPort<T>(port);
177  if (isStrict) {
178  buffered_port->setStrict(isStrict);
179  }
180  }
181  return *buffered_port;
182  }
183 
184  void clear()
185  {
186  if (!buffered_port)
187  return;
188  delete buffered_port;
189  buffered_port = nullptr;
190  }
191 };
192 
193 } // namespace os
194 } // namespace yarp
195 
196 #endif // YARP_OS_SUBSCRIBER_H
yarp::os::Subscriber::resume
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Subscriber.h:99
yarp::os::Subscriber::onRead
void onRead(T &datum) override
Callback method.
Definition: Subscriber.h:133
yarp::os::AbstractContactable::read
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
Definition: AbstractContactable.cpp:162
yarp::os::Port::includeNodeInName
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Definition: Port.cpp:666
yarp::os::Subscriber::Subscriber
Subscriber(const std::string &name="")
Constructor.
Definition: Subscriber.h:35
yarp::os::Subscriber::asPort
Port & asPort() override
Get the concrete Port being used for communication.
Definition: Subscriber.h:122
yarp::os::Subscriber::read
T * read(bool shouldWait=true)
Read a message from the port.
Definition: Subscriber.h:117
yarp::os::Subscriber::interrupt
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Subscriber.h:93
yarp::os::Subscriber::setStrict
void setStrict(bool strict=true)
Definition: Subscriber.h:154
yarp::os::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:31
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
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::Subscriber::useCallback
void useCallback()
Definition: Subscriber.h:144
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:82
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::os::Contactable::interrupt
virtual void interrupt()=0
Interrupt any current reads or writes attached to the port.
yarp::os::Subscriber::asPort
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition: Subscriber.h:127
yarp::os::Subscriber::setReader
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Subscriber.h:105
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
Log.h
yarp::os::Subscriber::close
void close() override
Stop port activity.
Definition: Subscriber.h:87
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
yarp::os::Contactable::setReader
virtual void setReader(PortReader &reader)=0
Set an external reader for port data.
buffer
Definition: V4L_camera.h:75
yarp::os::Contactable::resume
virtual void resume()=0
Put the port back in an operative state after interrupt() has been called.
AbstractContactable.h
BufferedPort.h
yarp::os::Subscriber::open
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition: Subscriber.h:80
yarp::os::Port::setRpcMode
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: Port.cpp:620
yarp::os::Subscriber::disableCallback
void disableCallback()
Definition: Subscriber.h:149
yarp::os::Subscriber::~Subscriber
virtual ~Subscriber()
Destructor.
Definition: Subscriber.h:54
yarp::os::Subscriber::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Subscriber.h:73
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Subscriber
A port specialized for reading data of a constant type published on a topic.
Definition: Subscriber.h:26
yarp::os::Port::setOutputMode
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
Definition: Port.cpp:612
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::Subscriber::useCallback
void useCallback(TypedReaderCallback< T > &callback)
Definition: Subscriber.h:139
yarp::os::Contactable::close
virtual void close()=0
Stop port activity.
yarp::os::Port::setInputMode
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition: Port.cpp:604
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::os::Subscriber::topic
bool topic(const std::string &name)
Set topic to subscribe to.
Definition: Subscriber.h:66
yarp::os::Port::promiseType
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:651
yarp::os::Contactable
An abstract port.
Definition: Contactable.h:38
yarp::os::AbstractContactable
A default implementation of an abstract port.
Definition: AbstractContactable.h:28