RSB  0.7.0
 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 
57  return requestType;
58 }
59 
61  return 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 =
139  Factory::getInstance().createInformer<AnyType> (scope.concat(Scope(
140  "/reply")).concat(Scope("/" + methodName)),
141  informerConfig, "");
142 
143  ListenerPtr listener = Factory::getInstance().createListener(scope.concat(
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 }