RSB  0.19.0
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 const std::set<std::string> InformerBase::getTransportURLs() const {
70  return this->configurator->getTransportURLs();
71 }
72 
73 
74 string InformerBase::getType() const {
75  return this->defaultType;
76 }
77 
79  configurator->setQualityOfServiceSpecs(specs);
80 }
81 
83  EventPtr event(new Event());
84  event->setScopePtr(getScope());
85  return event;
86 }
87 
88 EventPtr InformerBase::publish(VoidPtr data, const std::string& type) {
89  EventPtr event = createEvent();
90  event->setData(data);
91  event->setType(type);
93  return event;
94 }
95 
96 EventPtr InformerBase::uncheckedPublish(VoidPtr data, const std::string& type) {
97  EventPtr event = createEvent();
98  event->setData(data);
99  event->setType(type);
101  return event;
102 }
103 
105  checkedPublish(event);
106  return event;
107 }
108 
110  if (event->getType().empty()) {
111  throw invalid_argument(
112  boost::str(
113  boost::format("Event type cannot be empty: %1%")
114  % event));
115  }
116  // Check event type against informer's declared type.
117  if (!getType().empty() && event->getType() != getType()) {
118  throw invalid_argument(
119  boost::str(
120  boost::format(
121  "Specified event type %1% does not match informer type %2%.")
122  % event->getType() % getType()));
123  }
124  // Check event scope against informer's declared scope.
125  if (*event->getScopePtr() != *getScope() && !event->getScopePtr()->isSubScopeOf(
126  *getScope())) {
127  throw invalid_argument(
128  boost::str(
129  boost::format(
130  "Specified event scope %1% does not match informer scope %2%.")
131  % event->getScopePtr() % getScope()));
132  }
133 
134  this->uncheckedPublish(event);
135 }
136 
138  event->setId(getId(), nextSequenceNumber());
139  configurator->publish(event);
140 }
141 
143  boost::mutex::scoped_lock lock(this->sequenceNumberMutex);
144  return this->currentSequenceNumber++;
145 }
146 
147 }
Specification of desired quality of service settings for sending and receiving events.
boost::shared_ptr< void > VoidPtr
Definition: Event.h:51
virtual const std::set< std::string > getTransportURLs() const
TODO.
Definition: Informer.cpp:69
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
Basic message that is exchanged between informers and listeners.
Definition: Event.h:60
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:65
virtual EventPtr createEvent() const
Creates a new Event instance filled with the scope from this informer.
Definition: Informer.cpp:82
STL namespace.
boost::mutex sequenceNumberMutex
Definition: Informer.h:214
EventPtr event
Definition: BusImpl.cpp:129
void setQualityOfSerivceSpecs(const QualityOfServiceSpec &specs)
Defines the desired quality of service settings for this informers.
Definition: Informer.cpp:78
std::string getType() const
Return the event payload type of this Informer.
Definition: Informer.cpp:74
virtual ~InformerBase()
Definition: Informer.cpp:57
eventprocessing::OutRouteConfiguratorPtr configurator
Definition: Informer.h:209
void printContents(std::ostream &stream) const
Definition: Informer.cpp:60
boost::uint32_t nextSequenceNumber()
Definition: Informer.cpp:142
virtual std::string getKind() const
Return the kind of the participant.
Definition: Informer.cpp:65
void checkedPublish(EventPtr event)
Definition: Informer.cpp:109
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:159
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: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