RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Server.h
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, 2012, 2013 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 #pragma once
29 
30 #include <set>
31 #include <map>
32 #include <string>
33 
34 #include <boost/noncopyable.hpp>
35 #include <boost/shared_ptr.hpp>
36 
37 #include <rsc/runtime/TypeStringTools.h>
38 #include <rsc/logging/Logger.h>
39 
40 #include "../Informer.h"
41 #include "../Listener.h"
42 #include "../ParticipantConfig.h"
43 #include "../Scope.h"
44 #include "rsb/rsbexports.h"
45 
46 namespace rsb {
47 namespace patterns {
48 
54 class RSB_EXPORT Server: public boost::noncopyable {
55 public:
56 
62  class RSB_EXPORT IntlCallback {
63  public:
64 
65  virtual ~IntlCallback();
66 
67  virtual const std::string& getRequestType() const = 0;
68 
69  virtual AnnotatedData
70  intlCall(const std::string& methodName,
71  boost::shared_ptr<void> input) = 0;
72 
73  };
74 
80  class RSB_EXPORT CallbackBase : public IntlCallback {
81  public:
82  virtual const std::string& getRequestType() const;
83  virtual const std::string& getReplyType() const;
84  protected:
85  CallbackBase(const std::string& requestType,
86  const std::string& replyType);
87 
88  std::string requestType;
89  std::string replyType;
90  };
91 
99  template<class RequestType>
100  class RSB_EXPORT AnyReplyTypeCallback : public CallbackBase {
101  public:
102  // typeid is due to msvc strangeness
103  AnyReplyTypeCallback(const std::string& requestType
104  = rsc::runtime::typeName(typeid(RequestType))) :
105  CallbackBase(requestType, "") {
106  }
107 
118  virtual AnnotatedData call(const std::string& methodName,
119  boost::shared_ptr<RequestType> input) = 0;
120  private:
121  AnnotatedData intlCall(const std::string& methodName,
122  boost::shared_ptr<void> input) {
123  return call(methodName,
124  boost::static_pointer_cast<RequestType>(input));
125  }
126 
127  };
128 
137  template<class RequestType, class ReplyType>
138  class Callback: public CallbackBase {
139  public:
140  // typeid is due to msvc strangeness
141  Callback(const std::string& requestType
142  = rsc::runtime::typeName(typeid(RequestType)),
143  const std::string& replyType
144  = rsc::runtime::typeName(typeid(ReplyType))) :
145  CallbackBase(requestType, replyType) {
146  }
147 
158  virtual boost::shared_ptr<ReplyType> call(const std::string& methodName,
159  boost::shared_ptr<RequestType> input) = 0;
160  private:
161  AnnotatedData intlCall(const std::string& methodName,
162  boost::shared_ptr<void> input) {
163  return std::make_pair(getReplyType(), call(methodName,
164  boost::static_pointer_cast<RequestType>(input)));
165  }
166  };
167 
168  template<class RequestType>
169  class Callback<RequestType, void>: public CallbackBase {
170  public:
171  // typeid is due to msvc strangeness
172  Callback(const std::string& requestType
173  = rsc::runtime::typeName(typeid(RequestType)),
174  const std::string& replyType
175  = rsc::runtime::typeName(typeid(void))) :
176  CallbackBase(requestType, replyType) {
177  }
178 
188  virtual void call(const std::string& methodName,
189  boost::shared_ptr<RequestType> input) = 0;
190  private:
191  AnnotatedData intlCall(const std::string& methodName,
192  boost::shared_ptr<void> input) {
193  call(methodName, boost::static_pointer_cast<RequestType>(input));
194  return make_pair(getReplyType(), boost::shared_ptr<void>());
195  }
196 
197  };
198 
199  template<class ReplyType>
200  class Callback<void, ReplyType>: public CallbackBase {
201  public:
202  // typeid is due to msvc strangeness
203  Callback(const std::string& requestType
204  = rsc::runtime::typeName(typeid(void)),
205  const std::string& replyType
206  = rsc::runtime::typeName(typeid(ReplyType))) :
207  CallbackBase(requestType, replyType) {
208  }
209 
219  virtual boost::shared_ptr<ReplyType> call(
220  const std::string& methodName) = 0;
221  private:
222  AnnotatedData intlCall(const std::string& methodName,
223  boost::shared_ptr<void> input) {
224  return std::make_pair(getReplyType(), call(methodName));
225  }
226 
227  };
228 
229  typedef boost::shared_ptr<IntlCallback> CallbackPtr;
230 
231  Server(const Scope& scope, const ParticipantConfig &listenerConfig,
232  const ParticipantConfig &informerConfig);
233  virtual ~Server();
234 
242  void registerMethod(const std::string& methodName, CallbackPtr callback);
243 
244 private:
245 
249 
250  std::set<ListenerPtr> requestListeners;
251 
252  std::map<std::string, Informer<AnyType>::Ptr> methods;
253 
254 };
255 
256 // Since this is a complete specialization of Server::Callback, it
257 // cannot be declared inline in Server like the partial
258 // specializations (see C++03, ยง14.7.3/2:).
259 
266 template<>
267 class RSB_EXPORT Server::Callback<void, void>: public Server::CallbackBase {
268  public:
269  // typeid is due to msvc strangeness
271  Server::CallbackBase(rsc::runtime::typeName(typeid(void)),
272  rsc::runtime::typeName(typeid(void))) {
273  }
274 
283  virtual void call(const std::string& methodName) = 0;
284  private:
285  AnnotatedData intlCall(const std::string& methodName,
286  boost::shared_ptr<void> /*input*/) {
287  call(methodName);
288  return std::make_pair(getReplyType(), boost::shared_ptr<void>());
289  }
290 
291 };
292 
293 typedef boost::shared_ptr<Server> ServerPtr;
294 
295 }
296 }
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
Callback object interface to implement for registering callable methods.
Definition: Server.h:62
ParticipantConfig listenerConfig
Definition: Server.h:247
Callback(const std::string &requestType=rsc::runtime::typeName(typeid(RequestType)), const std::string &replyType=rsc::runtime::typeName(typeid(ReplyType)))
Definition: Server.h:141
AnnotatedData intlCall(const std::string &methodName, boost::shared_ptr< void > input)
Definition: Server.h:121
AnnotatedData intlCall(const std::string &methodName, boost::shared_ptr< void > input)
Definition: Server.h:191
Callback(const std::string &requestType=rsc::runtime::typeName(typeid(RequestType)), const std::string &replyType=rsc::runtime::typeName(typeid(void)))
Definition: Server.h:172
boost::shared_ptr< Server > ServerPtr
Definition: Server.h:293
A callback which allows any kind of reply type but is restricted to a single request type...
Definition: Server.h:100
AnnotatedData intlCall(const std::string &methodName, boost::shared_ptr< void > input)
Definition: Server.h:161
A class describing the configuration of Participant instances.
AnyReplyTypeCallback(const std::string &requestType=rsc::runtime::typeName(typeid(RequestType)))
Definition: Server.h:103
Base class for callback classes.
Definition: Server.h:80
AnnotatedData intlCall(const std::string &methodName, boost::shared_ptr< void >)
Definition: Server.h:285
Callback object used to register one method for a server.
Definition: Server.h:138
std::set< ListenerPtr > requestListeners
Definition: Server.h:250
The server side of a request-reply-based communication channel.
Definition: Server.h:54
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
AnnotatedData intlCall(const std::string &methodName, boost::shared_ptr< void > input)
Definition: Server.h:222
Callback(const std::string &requestType=rsc::runtime::typeName(typeid(void)), const std::string &replyType=rsc::runtime::typeName(typeid(ReplyType)))
Definition: Server.h:203