RSB  0.12.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Informer.cpp
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  * 2011 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
7  *
8  * This file may be licensed under the terms of the
9  * GNU Lesser General Public License Version 3 (the ``LGPL''),
10  * or (at your option) any later version.
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the LGPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the LGPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The development of this software was supported by:
23  * CoR-Lab, Research Institute for Cognition and Robotics
24  * Bielefeld University
25  *
26  * ============================================================ */
27 
28 #include "Informer.h"
29 
30 #include <boost/format.hpp>
31 
32 #include "Scope.h"
33 #include "ParticipantConfig.h"
34 
35 using namespace std;
36 
37 namespace rsb {
38 
39 InformerBase::InformerBase(const vector<transport::OutConnectorPtr>& connectors,
40  const Scope& scope,
41  const ParticipantConfig& config,
42  const string& defaultType) :
43  Participant(scope, config), defaultType(defaultType),
44  configurator(new eventprocessing::OutRouteConfigurator(scope)),
45  currentSequenceNumber(0) {
46  // TODO evaluate configuration
47  for (vector<transport::OutConnectorPtr>::const_iterator it =
48  connectors.begin(); it != connectors.end(); ++it) {
49  this->configurator->addConnector(*it);
50  }
51 
52  this->configurator->setQualityOfServiceSpecs(config.getQualityOfServiceSpec());
53 
54  this->configurator->activate();
55 }
56 
58 }
59 
60 void InformerBase::printContents(ostream& stream) const {
62  stream << ", type = " << defaultType;
63 }
64 
65 std::string InformerBase::getKind() const {
66  return "informer";
67 }
68 
69 string InformerBase::getType() const {
70  return this->defaultType;
71 }
72 
74  configurator->setQualityOfServiceSpecs(specs);
75 }
76 
78  EventPtr event(new Event());
79  event->setScopePtr(getScope());
80  return event;
81 }
82 
83 EventPtr InformerBase::publish(VoidPtr data, const std::string& type) {
84  EventPtr event = createEvent();
85  event->setData(data);
86  event->setType(type);
87  checkedPublish(event);
88  return event;
89 }
90 
91 EventPtr InformerBase::uncheckedPublish(VoidPtr data, const std::string& type) {
92  EventPtr event = createEvent();
93  event->setData(data);
94  event->setType(type);
95  uncheckedPublish(event);
96  return event;
97 }
98 
100  checkedPublish(event);
101  return event;
102 }
103 
105  if (event->getType().empty()) {
106  throw invalid_argument(
107  boost::str(
108  boost::format("Event type cannot be empty: %1%")
109  % event));
110  }
111  // Check event type against informer's declared type.
112  if (!getType().empty() && event->getType() != getType()) {
113  throw invalid_argument(
114  boost::str(
115  boost::format(
116  "Specified event type %1% does not match informer type %2%.")
117  % event->getType() % getType()));
118  }
119  // Check event scope against informer's declared scope.
120  if (*event->getScopePtr() != *getScope() && !event->getScopePtr()->isSubScopeOf(
121  *getScope())) {
122  throw invalid_argument(
123  boost::str(
124  boost::format(
125  "Specified event scope %1% does not match informer scope %2%.")
126  % event->getScopePtr() % getScope()));
127  }
128 
129  this->uncheckedPublish(event);
130 }
131 
133  event->setEventId(getId(), nextSequenceNumber());
134  configurator->publish(event);
135 }
136 
138  boost::mutex::scoped_lock lock(this->sequenceNumberMutex);
139  return this->currentSequenceNumber++;
140 }
141 
142 }
Specification of desired quality of service settings for sending and receiving events.
boost::shared_ptr< void > VoidPtr
Definition: Event.h:52
std::string defaultType
Definition: Informer.h:205
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:149
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
void printContents(std::ostream &stream) const
Definition: Participant.cpp:72
Objects of this class participate in the exchange of notifications on one channel of the bus...
Definition: Participant.h:64
virtual EventPtr createEvent() const
Creates a new Event instance filled with the scope from this informer.
Definition: Informer.cpp:77
boost::mutex sequenceNumberMutex
Definition: Informer.h:211
void setQualityOfSerivceSpecs(const QualityOfServiceSpec &specs)
Defines the desired quality of service settings for this informers.
Definition: Informer.cpp:73
std::string getType() const
Return the event payload type of this Informer.
Definition: Informer.cpp:69
virtual ~InformerBase()
Definition: Informer.cpp:57
eventprocessing::OutRouteConfiguratorPtr configurator
Definition: Informer.h:206
void printContents(std::ostream &stream) const
Definition: Informer.cpp:60
boost::uint32_t nextSequenceNumber()
Definition: Informer.cpp:137
virtual std::string getKind() const
Return the kind of the participant.
Definition: Informer.cpp:65
void checkedPublish(EventPtr event)
Definition: Informer.cpp:104
QualityOfServiceSpec getQualityOfServiceSpec() const
Returns the current settings for QoS.
EventPtr uncheckedPublish(boost::shared_ptr< T1 > data, const std::string &type=rsc::runtime::typeName< T1 >())
Definition: Informer.h:156
A class describing the configuration of Participant instances.
ScopePtr getScope() const
Returns the scope of this participant.
Definition: Participant.cpp:64
rsc::misc::UUID getId() const
Returns the unique id of the participant.
Definition: Participant.cpp:60
boost::shared_ptr< Event > EventPtr
Definition: Event.h:254
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
boost::uint32_t currentSequenceNumber
Definition: Informer.h:210