RSB  0.19.0
Informer.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <string>
30 
31 #include <boost/cstdint.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/thread/mutex.hpp>
34 
35 #include <rsc/runtime/TypeStringTools.h>
36 #include <rsc/logging/Logger.h>
37 
38 #include "rsb/rsbexports.h"
39 
40 #include "Event.h"
41 #include "QualityOfServiceSpec.h"
42 #include "Participant.h"
43 
45 
46 #include "transport/Connector.h"
47 
48 namespace rsb {
49 
59 class AnyType {
60 };
61 
62 namespace detail {
63 
64 template<typename T>
65 struct TypeName {
66  std::string operator()() const {
67  return rsc::runtime::typeName<T>();
68  }
69 };
70 
71 template<>
72 struct TypeName<AnyType> {
73  std::string operator()() const {
74  return "";
75  }
76 };
77 
78 }
79 
95 class RSB_EXPORT InformerBase: public Participant {
96 public:
97  template<typename T>
98  struct DataPtr {
99  typedef boost::shared_ptr<T> type;
100  };
101 
102  InformerBase(const std::vector<transport::OutConnectorPtr>& connectors,
103  const Scope& scope, const ParticipantConfig& config,
104  const std::string& defaultType);
105 
106  virtual ~InformerBase();
107 
108  void printContents(std::ostream& stream) const;
109 
110  // Overrides method in Participant.
111  virtual std::string getKind() const;
112 
113  // Overrides method in Participant.
114  virtual const std::set<std::string> getTransportURLs() const;
115 
122  std::string getType() const;
123 
130  void setQualityOfSerivceSpecs(const QualityOfServiceSpec& specs);
131 
151  template<class T1>
152  EventPtr publish(boost::shared_ptr<T1> data,
153  std::string type = rsc::runtime::typeName<T1>()) {
154  VoidPtr p = boost::static_pointer_cast<void>(data);
155  return publish(p, type);
156  }
157 
158  template<class T1>
159  EventPtr uncheckedPublish(boost::shared_ptr<T1> data,
160  const std::string& type = rsc::runtime::typeName<T1>()) {
161  VoidPtr p = boost::static_pointer_cast<void>(data);
162  return uncheckedPublish(p, type);
163  }
164 
170  virtual EventPtr createEvent() const;
171 
184  EventPtr publish(VoidPtr data, const std::string& type);
185  EventPtr uncheckedPublish(VoidPtr data, const std::string& type);
186 
200  EventPtr publish(EventPtr event);
201 
202 protected:
203  void checkedPublish(EventPtr event);
204  void uncheckedPublish(EventPtr event);
205 
206  boost::uint32_t nextSequenceNumber();
207 
208  std::string defaultType;
210 
211 private:
212  rsc::logging::LoggerPtr logger;
213  boost::uint32_t currentSequenceNumber;
214  boost::mutex sequenceNumberMutex;
215 };
216 
217 typedef boost::shared_ptr<InformerBase> InformerBasePtr;
218 
235 template<class T>
236 class Informer: public InformerBase {
237 public:
238 
242  typedef boost::shared_ptr<Informer<T> > Ptr;
243 
247  typedef boost::shared_ptr<T> DataPtr;
248 
263  Informer(const std::vector<transport::OutConnectorPtr>& connectors,
264  const Scope& scope, const ParticipantConfig& config,
265  const std::string& type = detail::TypeName<T>()()) :
266  InformerBase(connectors, scope, config, type),
267  logger(rsc::logging::Logger::getLogger(getClassName())) {
268  }
269 
270  virtual ~Informer() {
271  }
272 
273  std::string getClassName() const {
274  return rsc::runtime::typeName<Informer<T> >();
275  }
276 
287  event->setType(getType());
288  return event;
289  }
290 
298  EventPtr publish(boost::shared_ptr<T> data) {
299  VoidPtr p = boost::static_pointer_cast<void>(data);
300  return InformerBase::publish(p, this->getType());
301  }
302 
303  template<class T1>
304  EventPtr publish(boost::shared_ptr<T1> data,
305  std::string type = rsc::runtime::typeName(typeid(T1))) {
306  return InformerBase::publish(data, type);
307  }
308 
310  return InformerBase::publish(event);
311  }
312 private:
313  rsc::logging::LoggerPtr logger;
314 };
315 
316 }
Specification of desired quality of service settings for sending and receiving events.
virtual ~Informer()
Definition: Informer.h:270
boost::shared_ptr< void > VoidPtr
Definition: Event.h:51
std::string getClassName() const
Definition: Informer.h:273
boost::shared_ptr< OutRouteConfigurator > OutRouteConfiguratorPtr
rsc::logging::LoggerPtr logger
Definition: Informer.h:212
std::string operator()() const
Definition: Informer.h:66
std::string operator()() const
Definition: Informer.h:73
std::string defaultType
Definition: Informer.h:208
EventPtr publish(boost::shared_ptr< T1 > data, std::string type=rsc::runtime::typeName< T1 >())
Published data in the channel in which the informer participates.
Definition: Informer.h:152
Informer(const std::vector< transport::OutConnectorPtr > &connectors, const Scope &scope, const ParticipantConfig &config, const std::string &type=detail::TypeName< T >()())
Constructs a new informer.
Definition: Informer.h:263
EventPtr publish(EventPtr event)
Definition: Informer.h:309
Objects of this class participate in the exchange of notifications on one channel of the bus...
Definition: Participant.h:65
virtual EventPtr createEvent() const
Creates a new Event instance filled with the scope from this informer.
Definition: Informer.cpp:82
boost::shared_ptr< T > DataPtr
Shared pointer type for the default data published by this informer.
Definition: Informer.h:247
boost::mutex sequenceNumberMutex
Definition: Informer.h:214
EventPtr event
Definition: BusImpl.cpp:129
A tag type for constructing Informer instances that can publish data of arbitrary types...
Definition: Informer.h:59
A informer to publish data of a specified type expressed through the template parameter.
Definition: Informer.h:236
A informer to publish data.
Definition: Informer.h:95
rsc::logging::LoggerPtr logger
Definition: Informer.h:313
EventPtr createEvent() const
Creates a new Event instance filled with the scope from this informer.
Definition: Informer.h:285
eventprocessing::OutRouteConfiguratorPtr configurator
Definition: Informer.h:209
boost::shared_ptr< Informer< T > > Ptr
Shared pointer type for this informer.
Definition: Informer.h:242
EventPtr uncheckedPublish(boost::shared_ptr< T1 > data, const std::string &type=rsc::runtime::typeName< T1 >())
Definition: Informer.h:159
boost::shared_ptr< T > type
Definition: Informer.h:99
A class describing the configuration of Participant instances.
EventPtr publish(boost::shared_ptr< T1 > data, std::string type=rsc::runtime::typeName(typeid(T1)))
Definition: Informer.h:304
boost::shared_ptr< InformerBase > InformerBasePtr
Definition: Informer.h:217
boost::shared_ptr< Event > EventPtr
Definition: Event.h:264
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
boost::uint32_t currentSequenceNumber
Definition: Informer.h:213
EventPtr publish(boost::shared_ptr< T > data)
Publishes data to the Informer&#39;s scope.
Definition: Informer.h:298