RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Server.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  * 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 "Server.h"
29 
30 #include <stdexcept>
31 
32 #include <rsc/runtime/TypeStringTools.h>
33 #include <rsc/logging/Logger.h>
34 
35 #include "../EventId.h"
36 #include "../Factory.h"
37 #include "../MetaData.h"
38 #include "../Handler.h"
39 #include "MethodExistsException.h"
40 
41 using namespace std;
42 
43 using namespace rsc::runtime;
44 
45 namespace rsb {
46 namespace patterns {
47 
48 Server::IntlCallback::~IntlCallback() {
49 }
50 
51 Server::CallbackBase::CallbackBase(const string& requestType,
52  const string& replyType)
53  : requestType(requestType), replyType(replyType) {
54 }
55 
56 const string& Server::CallbackBase::getRequestType() const {
57  return this->requestType;
58 }
59 
60 const string& Server::CallbackBase::getReplyType() const {
61  return this->replyType;
62 }
63 
64 class RequestHandler: public Handler {
65 private:
66 
67  rsc::logging::LoggerPtr logger;
68 
69  string methodName;
72 
73 public:
74 
75  RequestHandler(const string& methodName, Server::CallbackPtr callback,
76  Informer<AnyType>::Ptr informer) :
77  logger(rsc::logging::Logger::getLogger("rsb.patterns.RequestHandler."
78  + methodName)), methodName(methodName), callback(callback),
79  informer(informer) {
80  }
81 
82  string getClassName() const {
83  return "RequestHandler";
84  }
85 
86  void printContents(ostream& stream) const {
87  stream << "methodName = " << methodName;
88  }
89 
90  void handle(EventPtr event) {
91  if (event->getMethod() != "REQUEST") {
92  return;
93  }
94  if (event->getType() != callback->getRequestType()) {
95  RSCERROR(logger, "Request type '" << event->getType()
96  << "' does not match expected request type '"
97  << callback->getRequestType() << "' of method '"
98  << methodName << "'");
99  return;
100  }
101 
102  EventPtr reply(new Event());
103  reply->setScopePtr(informer->getScope());
104  reply->setMethod("REPLY");
105  reply->addCause(event->getEventId());
106  try {
107  AnnotatedData returnData
108  = callback->intlCall(methodName, event->getData());
109  reply->setType(returnData.first);
110  reply->setData(returnData.second);
111  } catch (const exception& e) {
112  reply->setType(typeName<string>());
113  reply->setData(boost::shared_ptr<string>(new string(typeName(e) + ": " + e.what())));
114  reply->mutableMetaData().setUserInfo("rsb:error?", "");
115  }
116  informer->publish(reply);
117  }
118 
119 };
120 
123  scope(scope), listenerConfig(listenerConfig), informerConfig(
124  informerConfig) {
125 }
126 
128 }
129 
130 void Server::registerMethod(const std::string& methodName, CallbackPtr callback) {
131 
132  // check that method does not exist
133  if (methods.count(methodName)) {
134  throw MethodExistsException(methodName, scope.toString());
135  }
136 
137  // TODO check that the reply type is convertible
138  Informer<AnyType>::Ptr informer =
140  "/reply")).concat(Scope("/" + methodName)),
141  informerConfig, "");
142 
144  Scope("/request")).concat(Scope("/" + methodName)), listenerConfig);
145  listener->addHandler(HandlerPtr(new RequestHandler(methodName, callback,
146  informer)));
147  this->requestListeners.insert(listener);
148 
149  methods[methodName] = informer;
150 
151 }
152 
153 }
154 }
boost::shared_ptr< IntlCallback > CallbackPtr
Definition: Server.h:229
std::map< std::string, Informer< AnyType >::Ptr > methods
Definition: Server.h:252
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:256
ParticipantConfig informerConfig
Definition: Server.h:248
virtual const std::string & getRequestType() const
Definition: Server.cpp:56
Indicates that a method of a Server already exists.
RequestHandler(const string &methodName, Server::CallbackPtr callback, Informer< AnyType >::Ptr informer)
Definition: Server.cpp:75
void handle(EventPtr event)
Handle event.
Definition: Server.cpp:90
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
ParticipantConfig listenerConfig
Definition: Server.h:247
Asynchronously called handler interface on the client level.
Definition: Handler.h:53
A tag type for constructing Informer instances that can publish data of arbitrary types...
Definition: Informer.h:59
const std::string & toString() const
Reconstructs a fully formal string representation of the scope with leading an trailing slashes...
Definition: Scope.cpp:143
void registerMethod(const std::string &methodName, CallbackPtr callback)
Register a new method with the given name.
Definition: Server.cpp:130
virtual const std::string & getReplyType() const
Definition: Server.cpp:60
Factory & getFactory()
Returns a factory for client-level RSB objects.
Definition: Factory.cpp:89
Informer< DataType >::Ptr createInformer(const Scope &scope, const ParticipantConfig &config=getFactory().getDefaultParticipantConfig(), const std::string &dataType=detail::TypeName< DataType >()())
Creates and returns a new Informer that publishes Event s under the Scope scope.
Definition: Factory.h:99
boost::shared_ptr< Listener > ListenerPtr
Definition: Listener.h:150
Informer< AnyType >::Ptr informer
Definition: Server.cpp:71
Server(const Scope &scope, const ParticipantConfig &listenerConfig, const ParticipantConfig &informerConfig)
Definition: Server.cpp:121
void printContents(ostream &stream) const
Definition: Server.cpp:86
boost::shared_ptr< Handler > HandlerPtr
Definition: Handler.h:92
ListenerPtr createListener(const Scope &scope, const ParticipantConfig &config=getFactory().getDefaultParticipantConfig())
Creates a new listener for the specified scope.
Definition: Factory.cpp:206
boost::shared_ptr< Informer< T > > Ptr
Shared pointer type for this informer.
Definition: Informer.h:236
rsc::logging::LoggerPtr logger
Definition: Server.cpp:67
Scope concat(const Scope &childScope) const
Creates a new scope that is a sub-scope of this one with the subordinated scope described by the give...
Definition: Scope.cpp:147
A class describing the configuration of Participant instances.
string getClassName() const
Definition: Server.cpp:82
std::set< ListenerPtr > requestListeners
Definition: Server.h:250
Server::CallbackPtr callback
Definition: Server.cpp:70
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