RSB  0.7.0
 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(
39  const vector<transport::OutConnectorPtr>& connectors,
40  const Scope& scope, const ParticipantConfig& config,
41  const string& defaultType) :
42  Participant(scope, config), defaultType(defaultType),
43  currentSequenceNumber(0) {
44  // 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  return ++this->currentSequenceNumber;
132 }
133 
134 }