RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
116  std::string getType() const;
117 
124  void setQualityOfSerivceSpecs(const QualityOfServiceSpec& specs);
125 
145  template<class T1>
146  EventPtr publish(boost::shared_ptr<T1> data,
147  std::string type = rsc::runtime::typeName<T1>()) {
148  VoidPtr p = boost::static_pointer_cast<void>(data);
149  return publish(p, type);
150  }
151 
152  template<class T1>
153  EventPtr uncheckedPublish(boost::shared_ptr<T1> data,
154  const std::string& type = rsc::runtime::typeName<T1>()) {
155  VoidPtr p = boost::static_pointer_cast<void>(data);
156  return uncheckedPublish(p, type);
157  }
158 
164  virtual EventPtr createEvent() const;
165 
178  EventPtr publish(VoidPtr data, const std::string& type);
179  EventPtr uncheckedPublish(VoidPtr data, const std::string& type);
180 
194  EventPtr publish(EventPtr event);
195 
196 protected:
197  void checkedPublish(EventPtr event);
198  void uncheckedPublish(EventPtr event);
199 
200  boost::uint32_t nextSequenceNumber();
201 
202  std::string defaultType;
204 
205  boost::uint32_t currentSequenceNumber;
206  boost::mutex sequenceNumberMutex;
207 private:
208  rsc::logging::LoggerPtr logger;
209 };
210 
211 typedef boost::shared_ptr<InformerBase> InformerBasePtr;
212 
229 template<class T>
230 class Informer: public InformerBase {
231 public:
232 
236  typedef boost::shared_ptr<Informer<T> > Ptr;
237 
241  typedef boost::shared_ptr<T> DataPtr;
242 
257  Informer(const std::vector<transport::OutConnectorPtr>& connectors,
258  const Scope& scope, const ParticipantConfig& config,
259  const std::string& type = detail::TypeName<T>()()) :
260  InformerBase(connectors, scope, config, type),
261  logger(rsc::logging::Logger::getLogger(getClassName())) {
262  }
263 
264  virtual ~Informer() {
265  }
266 
267  std::string getClassName() const {
268  return rsc::runtime::typeName<Informer<T> >();
269  }
270 
281  event->setType(getType());
282  return event;
283  }
284 
292  EventPtr publish(boost::shared_ptr<T> data) {
293  VoidPtr p = boost::static_pointer_cast<void>(data);
294  return InformerBase::publish(p, this->getType());
295  }
296 
297  template<class T1>
298  EventPtr publish(boost::shared_ptr<T1> data,
299  std::string type = rsc::runtime::typeName(typeid(T1))) {
300  return InformerBase::publish(data, type);
301  }
302 
304  return InformerBase::publish(event);
305  }
306 private:
307  rsc::logging::LoggerPtr logger;
308 };
309 
310 }
Specification of desired quality of service settings for sending and receiving events.
virtual ~Informer()
Definition: Informer.h:264
boost::shared_ptr< void > VoidPtr
Definition: Event.h:52
std::string getClassName() const
Definition: Informer.h:267
boost::shared_ptr< OutRouteConfigurator > OutRouteConfiguratorPtr
rsc::logging::LoggerPtr logger
Definition: Informer.h:208
std::string operator()() const
Definition: Informer.h:66
std::string operator()() const
Definition: Informer.h:73
std::string defaultType
Definition: Informer.h:202
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:146
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:257
EventPtr publish(EventPtr event)
Definition: Informer.h:303
Objects of this class participate in the exchange of notifications on one channel of the bus...
Definition: Participant.h:57
virtual EventPtr createEvent() const
Creates a new Event instance filled with the scope from this informer.
Definition: Informer.cpp:70
boost::shared_ptr< T > DataPtr
Shared pointer type for the default data published by this informer.
Definition: Informer.h:241
boost::mutex sequenceNumberMutex
Definition: Informer.h:206
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:230
A informer to publish data.
Definition: Informer.h:95
std::string getType() const
Return the event payload type of this Informer.
Definition: Informer.cpp:62
rsc::logging::LoggerPtr logger
Definition: Informer.h:307
EventPtr createEvent() const
Creates a new Event instance filled with the scope from this informer.
Definition: Informer.h:279
eventprocessing::OutRouteConfiguratorPtr configurator
Definition: Informer.h:203
boost::shared_ptr< Informer< T > > Ptr
Shared pointer type for this informer.
Definition: Informer.h:236
EventPtr uncheckedPublish(boost::shared_ptr< T1 > data, const std::string &type=rsc::runtime::typeName< T1 >())
Definition: Informer.h:153
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:298
boost::shared_ptr< InformerBase > InformerBasePtr
Definition: Informer.h:211
boost::shared_ptr< Event > EventPtr
Definition: Event.h:251
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
boost::uint32_t currentSequenceNumber
Definition: Informer.h:205
EventPtr publish(boost::shared_ptr< T > data)
Publishes data to the Informer's scope.
Definition: Informer.h:292