RSB  0.17.0
LocalServer.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSB project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  * Copyright (C) 2011, 2014, 2015 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 "LocalServer.h"
29 
30 #include <stdexcept>
31 
32 #include "../EventId.h"
33 #include "../MetaData.h"
34 #include "../Factory.h"
35 
36 #include "../filter/MethodFilter.h"
37 
38 #include "MethodExistsException.h"
39 
40 using namespace std;
41 
42 using namespace rsc::runtime;
43 
44 namespace rsb {
45 namespace patterns {
46 
47 // Callbacks
48 
49 LocalServer::IntlCallback::~IntlCallback() {
50 }
51 
52 LocalServer::CallbackBase::CallbackBase(const string& requestType,
53  const string& replyType)
54  : requestType(requestType), replyType(replyType) {
55 }
56 
58  return this->requestType;
59 }
60 
62  return this->replyType;
63 }
64 
65 EventPtr LocalServer::EventCallback::intlCall(const string& methodName, EventPtr request) {
66  return call(methodName, request);
67 }
68 
69 // LocalMethod
70 
72  const std::string& name,
75  CallbackPtr callback)
76  : Method(scope, name, listenerConfig, informerConfig),
77  logger(rsc::logging::Logger::getLogger(boost::str(boost::format("rsb.patterns.LocalMethod[%1%]")
78  % name))),
79  callback(callback) {
80 }
81 
83 }
84 
85 std::string LocalServer::LocalMethod::getKind() const {
86  return "local-method";
87 }
88 
91  listener->addFilter(filter::FilterPtr(new filter::MethodFilter("REQUEST")));
92  listener->addHandler(HandlerPtr(shared_from_this()));
93  return listener;
94 }
95 
97  LocalServer::CallbackBase* callbackWithReturnType
98  = dynamic_cast<LocalServer::CallbackBase*>(this->callback.get());
99  if (callbackWithReturnType) {
100  if (event->getType() != callbackWithReturnType->getRequestType()) {
101  RSCERROR(this->logger, boost::format("Request type '%1%' "
102  "does not match expected request type '%2%' "
103  "of method '%3%'")
104  % event->getType()
105  % callbackWithReturnType->getRequestType()
106  % getName());
107  return;
108  }
109  }
110 
111  EventPtr reply;
112  try {
113  reply = this->callback->intlCall(getName(), event);
114  assert(reply);
115  } catch (const exception& e) {
116  reply.reset(new Event());
117  reply->setType(typeName<string>());
118  reply->setData(boost::shared_ptr<string>(new string(typeName(e) + ": " + e.what())));
119  reply->mutableMetaData().setUserInfo("rsb:error?", "");
120  }
121  reply->setScopePtr(getInformer()->getScope());
122  reply->setMethod("REPLY");
123  reply->addCause(event->getId());
124  getInformer()->publish(reply);
125 }
126 
127 // LocalServer
128 
132  : Participant(scope, listenerConfig), // TODO do this properly
133  listenerConfig(listenerConfig),
134  informerConfig(informerConfig) {
135 }
136 
138  for (std::map<std::string, LocalMethodPtr>::iterator it
139  = this->methods.begin(); it != this->methods.end(); ++it) {
140  it->second->deactivate();
141  }
142 }
143 
144 std::string LocalServer::getKind() const {
145  return "local-server";
146 }
147 
148 const std::set<std::string> LocalServer::getTransportURLs() const {
149  return std::set<std::string>();
150 }
151 
152 void LocalServer::registerMethod(const std::string& name, CallbackPtr callback) {
153 
154  // TODO locking?
155 
156  // check that method does not exist
157  if (this->methods.find(name) != this->methods.end()) {
158  throw MethodExistsException(name, getScope()->toString());
159  }
160 
161  // TODO check that the reply type is convertible
162  LocalMethodPtr method
163  = getFactory().createLocalMethod(getScope()->concat(Scope("/" + name)),
164  callback,
165  this->listenerConfig, this->informerConfig,
166  shared_from_this());
167  method->activate();
168 
169  this->methods[name] = method;
170 
171 }
172 
173 }
174 }
ListenerPtr listener
Definition: Server.h:120
LocalMethod(const Scope &scope, const std::string &name, const ParticipantConfig &listenerConfig, const ParticipantConfig &informerConfig, CallbackPtr callback)
Definition: LocalServer.cpp:71
EventPtr intlCall(const std::string &methodName, EventPtr request)
Definition: LocalServer.cpp:65
Indicates that a method of a Server already exists.
boost::shared_ptr< LocalMethod > LocalMethodPtr
Definition: LocalServer.h:387
Basic message that is exchanged between informers and listeners.
Definition: Event.h:60
Objects of this class participate in the exchange of notifications on one channel of the bus...
Definition: Participant.h:65
STL namespace.
const std::string & getName() const
Returns the name of the method.
Definition: Server.cpp:59
virtual void call(const std::string &methodName)=0
Implement this method to perform actions.
Base class for callback classes.
Definition: LocalServer.h:90
boost::shared_ptr< Filter > FilterPtr
ParticipantConfig listenerConfig
Definition: Server.h:117
void registerMethod(const std::string &name, CallbackPtr callback)
Register a new method with the given name.
virtual const std::set< std::string > getTransportURLs() const
TODO.
Factory & getFactory()
Returns a factory for client-level RSB objects.
Definition: Factory.cpp:163
This filter matches events based on the value of their method field.
Definition: MethodFilter.h:42
LocalServer(const Scope &scope, const ParticipantConfig &listenerConfig, const ParticipantConfig &informerConfig)
ListenerPtr makeListener()
Creates and returns the Listener participant.
Definition: LocalServer.cpp:89
boost::shared_ptr< Listener > ListenerPtr
Definition: Listener.h:155
std::map< std::string, LocalMethodPtr > methods
Definition: LocalServer.h:413
boost::shared_ptr< IntlCallback > CallbackPtr
Definition: LocalServer.h:71
boost::shared_ptr< Handler > HandlerPtr
Definition: Handler.h:92
InformerBasePtr getInformer()
Returns the Informer participant, creating it if necessary.
Definition: Server.cpp:85
virtual ListenerPtr makeListener()
Creates and returns the Listener participant.
Definition: Server.cpp:80
Base class for method classes.
Definition: Server.h:55
void handle(EventPtr event)
Handle event.
Definition: LocalServer.cpp:96
virtual std::string getKind() const
Return the kind of the participant.
ParticipantConfig informerConfig
Definition: Server.h:118
A class describing the configuration of Participant instances.
virtual const std::string & getReplyType() const
Definition: LocalServer.cpp:61
ScopePtr getScope() const
Returns the scope of this participant.
Definition: Participant.cpp:64
patterns::LocalServer::LocalMethodPtr createLocalMethod(const Scope &scope, patterns::LocalServer::CallbackPtr callback, const ParticipantConfig &listenerConfig=getFactory().getDefaultParticipantConfig(), const ParticipantConfig &informerConfig=getFactory().getDefaultParticipantConfig(), ParticipantPtr parent=ParticipantPtr())
Creates a patterns::LocalServer::LocalMethod.
Definition: Factory.cpp:348
ParticipantConfig listenerConfig
Definition: LocalServer.h:410
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
virtual const std::string & getRequestType() const
Definition: LocalServer.cpp:57
virtual std::string getKind() const
Return the kind of the participant.
Definition: LocalServer.cpp:85
ParticipantConfig informerConfig
Definition: LocalServer.h:411