YARP
Yet Another Robot Platform
Publisher.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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_PUBLISHER_H
10 #define YARP_OS_PUBLISHER_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 
37  Publisher(const std::string& name = "")
38  {
39  buffered_port = nullptr;
40  T example;
41  port.promiseType(example.getType());
42  port.setInputMode(false);
43  port.setOutputMode(true);
44  port.setRpcMode(false);
45  if (name != "") {
46  bool ret = topic(name);
47  yAssert(ret);
48  }
49  }
50 
54  virtual ~Publisher()
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 
126  T& prepare()
127  {
128  return buffer().prepare();
129  }
130 
136  bool unprepare()
137  {
138  return buffer().unprepare();
139  }
140 
151  void write(bool forceStrict = false)
152  {
153  buffer().write(forceStrict);
154  }
155 
160  {
161  buffer().waitForWrite();
162  }
163 
164  virtual int getPendingReads()
165  {
166  if (buffered_port)
167  return buffered_port->getPendingReads();
168  return 0;
169  }
170 
171  Port& asPort() override
172  {
173  return port;
174  }
175 
176  const Port& asPort() const override
177  {
178  return port;
179  }
180 
181 private:
182  Port port;
183  BufferedPort<T>* buffered_port;
184 
185  Contactable& active()
186  {
187  if (buffered_port)
188  return *buffered_port;
189  return port;
190  }
191 
193  {
194  if (!buffered_port) {
195  buffered_port = new BufferedPort<T>(port);
196  }
197  return *buffered_port;
198  }
199 
200  void clear()
201  {
202  if (!buffered_port)
203  return;
204  delete buffered_port;
205  buffered_port = nullptr;
206  }
207 };
208 
209 } // namespace os
210 } // namespace yarp
211 
212 #endif // YARP_OS_PUBLISHER_H
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::Publisher::write.
Definition: Publisher.h:126
A default implementation of an abstract port.
An abstract port.
Definition: Contactable.h:30
bool ret
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:643
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: Port.cpp:614
A mini-server for network communication.
Definition: Port.h:49
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition: Publisher.h:80
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Publisher.h:93
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
Definition: Port.cpp:606
virtual bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
The main, catch-all namespace for YARP.
Definition: numeric.h:47
virtual ~Publisher()
Destructor.
Definition: Publisher.h:54
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Publisher.h:99
bool topic(const std::string &name)
Set topic to publish to.
Definition: Publisher.h:66
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:78
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition: Port.cpp:598
virtual void interrupt()=0
Interrupt any current reads or writes attached to the port.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:27
void write(bool forceStrict=false)
Write the current object being returned by Publisher::prepare.
Definition: Publisher.h:151
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition: Publisher.h:176
Publisher(const std::string &name="")
Constructor.
Definition: Publisher.h:37
Port & asPort() override
Get the concrete Port being used for communication.
Definition: Publisher.h:171
virtual void close()=0
Stop port activity.
virtual void setReader(PortReader &reader)=0
Set an external reader for port data.
#define yAssert(x)
Definition: Log.h:112
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port&#39;s name.
Definition: Port.cpp:658
virtual int getPendingReads()
Definition: Publisher.h:164
virtual void resume()=0
Put the port back in an operative state after interrupt() has been called.
void waitForWrite()
Wait for any pending writes to complete.
Definition: Publisher.h:159
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Publisher.h:73
Represents how to reach a part of a YARP network.
Definition: Contact.h:38
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:63
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: Publisher.h:136
void close() override
Stop port activity.
Definition: Publisher.h:87
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Publisher.h:105
A port specialized for publishing data of a constant type on a topic.
Definition: Publisher.h:25