RSB  0.9.6
 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 
34 using namespace std;
35 
36 namespace rsb {
37 
38 InformerBase::InformerBase(const vector<transport::OutConnectorPtr>& connectors,
39  const Scope& scope,
40  const ParticipantConfig& config,
41  const string& defaultType) :
42  Participant(scope, config), defaultType(defaultType),
43  configurator(new eventprocessing::OutRouteConfigurator(scope)),
44  currentSequenceNumber(0) {
45  // TODO evaluate configuration
46  for (vector<transport::OutConnectorPtr>::const_iterator it =
47  connectors.begin(); it != connectors.end(); ++it) {
48  this->configurator->addConnector(*it);
49  }
50 
51  this->configurator->activate();
52 }
53 
55 }
56 
57 void InformerBase::printContents(ostream& stream) const {
59  stream << ", type = " << defaultType;
60 }
61 
62 string InformerBase::getType() const {
63  return this->defaultType;
64 }
65 
67  configurator->setQualityOfServiceSpecs(specs);
68 }
69 
71  EventPtr event(new Event());
72  event->setScopePtr(getScope());
73  return event;
74 }
75 
76 EventPtr InformerBase::publish(VoidPtr data, const std::string& type) {
77  EventPtr event = createEvent();
78  event->setData(data);
79  event->setType(type);
80  checkedPublish(event);
81  return event;
82 }
83 
84 EventPtr InformerBase::uncheckedPublish(VoidPtr data, const std::string& type) {
85  EventPtr event = createEvent();
86  event->setData(data);
87  event->setType(type);
88  uncheckedPublish(event);
89  return event;
90 }
91 
93  checkedPublish(event);
94  return event;
95 }
96 
98  if (event->getType().empty()) {
99  throw invalid_argument(
100  boost::str(
101  boost::format("Event type cannot be empty: %1%")
102  % event));
103  }
104  // Check event type against informer's declared type.
105  if (!getType().empty() && event->getType() != getType()) {
106  throw invalid_argument(
107  boost::str(
108  boost::format(
109  "Specified event type %1% does not match informer type %2%.")
110  % event->getType() % getType()));
111  }
112  // Check event scope against informer's declared scope.
113  if (*event->getScopePtr() != *getScope() && !event->getScopePtr()->isSubScopeOf(
114  *getScope())) {
115  throw invalid_argument(
116  boost::str(
117  boost::format(
118  "Specified event scope %1% does not match informer scope %2%.")
119  % event->getScopePtr() % getScope()));
120  }
121 
122  this->uncheckedPublish(event);
123 }
124 
126  event->setEventId(getId(), nextSequenceNumber());
127  configurator->publish(event);
128 }
129 
131  boost::mutex::scoped_lock lock(this->sequenceNumberMutex);
132  return this->currentSequenceNumber++;
133 }
134 
135 }
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: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
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
void printContents(std::ostream &stream) const
Definition: Participant.cpp:64
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::mutex sequenceNumberMutex
Definition: Informer.h:206
void setQualityOfSerivceSpecs(const QualityOfServiceSpec &specs)
Defines the desired quality of service settings for this informers.
Definition: Informer.cpp:66
std::string getType() const
Return the event payload type of this Informer.
Definition: Informer.cpp:62
virtual ~InformerBase()
Definition: Informer.cpp:54
eventprocessing::OutRouteConfiguratorPtr configurator
Definition: Informer.h:203
void printContents(std::ostream &stream) const
Definition: Informer.cpp:57
boost::uint32_t nextSequenceNumber()
Definition: Informer.cpp:130
void checkedPublish(EventPtr event)
Definition: Informer.cpp:97
EventPtr uncheckedPublish(boost::shared_ptr< T1 > data, const std::string &type=rsc::runtime::typeName< T1 >())
Definition: Informer.h:153
A class describing the configuration of Participant instances.
ScopePtr getScope() const
Returns the scope of this participant.
Definition: Participant.cpp:56
rsc::misc::UUID getId() const
Returns the unique id of the participant.
Definition: Participant.cpp:52
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