RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParallelEventReceivingStrategy.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 
29 
30 #include <rsc/debug/DebugTools.h>
31 #include <rsc/runtime/ContainerIO.h>
32 #include <rsc/misc/langutils.h>
33 
34 #include "../MetaData.h"
35 #include "../filter/Filter.h"
36 
37 using namespace std;
38 
39 using namespace boost;
40 
41 using namespace rsc::runtime;
42 using namespace rsc::logging;
43 using namespace rsc::debug;
44 
45 namespace rsb {
46 namespace eventprocessing {
47 
48 EventReceivingStrategy* ParallelEventReceivingStrategy::create(const Properties& props) {
49  return new ParallelEventReceivingStrategy(props.getAs<unsigned int>("threads", 5));
50 }
51 
52 ParallelEventReceivingStrategy::ParallelEventReceivingStrategy(unsigned int numThreads) :
53  logger(Logger::getLogger("rsb.eventprocessing.ParallelEventReceivingStrategy")),
54  pool(numThreads,
55  boost::bind(&ParallelEventReceivingStrategy::deliver, this, _1, _2),
56  boost::bind(&ParallelEventReceivingStrategy::filter, this, _1, _2)),
57  errorStrategy(ParticipantConfig::ERROR_STRATEGY_LOG) {
58  pool.start();
59 }
60 
62  pool.stop();
63 }
64 
66  return "ParallelEventReceivingStrategy";
67 }
68 
69 void ParallelEventReceivingStrategy::printContents(ostream& stream) const {
70  shared_lock<shared_mutex> filtersLock(filtersMutex);
71  recursive_mutex::scoped_lock errorLock(errorStrategyMutex);
72  stream << "filters = " << filters << ", errorStrategy = " << errorStrategy;
73 }
74 
76  const ParticipantConfig::ErrorStrategy& strategy) {
77  recursive_mutex::scoped_lock lock(errorStrategyMutex);
78  this->errorStrategy = strategy;
79 }
80 
82  RSCDEBUG(logger, "Matching event " << e << " for handler " << handler);
83 
84  // match event
85  try {
86 
87  if (!handler->acceptsMethod(e->getMethod())) {
88  return false;
89  }
90 
91  shared_lock<shared_mutex> lock(filtersMutex);
92  for (set<filter::FilterPtr>::const_iterator filterIt = filters.begin(); filterIt
93  != filters.end(); ++filterIt) {
94  if (!(*filterIt)->match(e)) {
95  return false;
96  }
97  }
98 
99  return true;
100 
101  } catch (const std::exception& ex) {
102 
103  stringstream s;
104  s << "Exception matching event " << e << " for handler " << handler
105  << ":" << endl;
106  s << ex.what() << endl;
107  s << DebugTools::newInstance()->exceptionInfo(ex);
108 
109  handleDispatchError(s.str());
110 
111  } catch (...) {
112 
113  stringstream s;
114  s << "Catch-all handler called matching event " << e << " for handler "
115  << handler << endl;
116  DebugToolsPtr tool = DebugTools::newInstance();
117  vector<string> trace = tool->createBacktrace();
118  s << tool->formatBacktrace(trace);
119 
120  handleDispatchError(s.str());
121 
122  }
123 
124  return false;
125 
126 }
127 
129 
130  recursive_mutex::scoped_lock strategyLock(errorStrategyMutex);
131  switch (errorStrategy) {
133  RSCERROR(logger, message)
134  ;
135  break;
137  cerr << message << endl;
138  break;
140  cerr << message << endl;
141  exit(1);
142  break;
143  default:
144  RSCWARN(logger, "Unknown error strategy: " << errorStrategy)
145  ;
146  RSCERROR(logger, message)
147  ;
148  break;
149  }
150 
151 }
152 
154  RSCDEBUG(logger, "Delivering event " << e << " to handler " << handler);
155 
156  try {
157 
158  handler->handle(e);
159 
160  } catch (const std::exception& ex) {
161 
162  stringstream s;
163  s << "Exception dispatching event " << e << " to handler " << handler
164  << ":" << endl;
165  s << ex.what() << endl;
166  s << DebugTools::newInstance()->exceptionInfo(ex);
167 
168  handleDispatchError(s.str());
169 
170  } catch (...) {
171 
172  stringstream s;
173  s << "Catch-all handler called dispatching event " << e
174  << " to handler " << handler << endl;
175  DebugToolsPtr tool = DebugTools::newInstance();
176  vector<string> trace = tool->createBacktrace();
177  s << tool->formatBacktrace(trace);
178 
179  handleDispatchError(s.str());
180 
181  }
182 
183 }
184 
186  event->mutableMetaData().setDeliverTime(rsc::misc::currentTimeMicros());
187  pool.push(event);
188 }
189 
191  const bool& /*wait*/) {
192  // wait can be ignored because the pool always ensures this
193  pool.registerReceiver(handler);
194 }
195 
197  const bool& /*wait*/) {
198  // wait can be ignored because the pool always ensures this
199  pool.unregisterReceiver(handler);
200 }
201 
203  unique_lock<shared_mutex> lock(filtersMutex);
204  filters.insert(filter);
205 }
206 
208  unique_lock<shared_mutex> lock(filtersMutex);
209  filters.erase(filter);
210 }
211 
212 }
213 }
Uses stderr for printing a message.
Implementations of this interface organize the receiving of events via rsb::transport::InConnector s...
rsc::threading::OrderedQueueDispatcherPool< EventPtr, rsb::Handler > pool
boost::shared_ptr< Filter > FilterPtr
virtual void addHandler(rsb::HandlerPtr handler, const bool &wait)
Adds a new handler that will be notified about new events.
void setHandlerErrorStrategy(const ParticipantConfig::ErrorStrategy &strategy)
Defines the strategy to use for handling dispatching errors to the client handler.
This push-style event receiving strategy uses one or more threads to filter rsb::Event s and dispatch...
boost::shared_ptr< Handler > HandlerPtr
Definition: Handler.h:92
ErrorStrategy
Possible error handling strategies in user-provided code like event handlers.
Logs a message using the logging mechanism.
virtual void removeHandler(rsb::HandlerPtr handler, const bool &wait)
Removes a handler that will will then not be notified anymore.
A class describing the configuration of Participant instances.
boost::shared_ptr< Event > EventPtr
Definition: Event.h:251
void handle(EventPtr e)
Dispatches the event to the listener.