RSB  0.7.0
 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  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <set>
30 #include <map>
31 #include <string>
32 
33 #include <boost/noncopyable.hpp>
34 #include <boost/shared_ptr.hpp>
35 
36 #include <rsc/runtime/TypeStringTools.h>
37 #include <rsc/logging/Logger.h>
38 
39 #include "../Informer.h"
40 #include "../Listener.h"
41 #include "../ParticipantConfig.h"
42 #include "../Scope.h"
43 #include "rsb/rsbexports.h"
44 
45 namespace rsb {
46 namespace patterns {
47 
53 class RSB_EXPORT Server: public boost::noncopyable {
54 public:
55 
61  class RSB_EXPORT IntlCallback {
62  public:
63 
64  virtual ~IntlCallback();
65 
66  virtual std::string getRequestType() const = 0;
67 
68  virtual AnnotatedData
69  intlCall(const std::string& methodName,
70  boost::shared_ptr<void> input) = 0;
71 
72  };
73 
79  class RSB_EXPORT CallbackBase : public IntlCallback {
80  public:
81  virtual std::string getRequestType() const;
82  virtual std::string getReplyType() const;
83  protected:
84  CallbackBase(const std::string& requestType,
85  const std::string& replyType);
86 
87  std::string requestType;
88  std::string replyType;
89  };
90 
98  template<class RequestType>
99  class RSB_EXPORT AnyReplyTypeCallback : public CallbackBase {
100  public:
101  // typeid is due to msvc strangeness
102  AnyReplyTypeCallback(const std::string& requestType
103  = rsc::runtime::typeName(typeid(RequestType))) :
104  CallbackBase(requestType, "") {
105  }
106 
117  virtual AnnotatedData call(const std::string& methodName,
118  boost::shared_ptr<RequestType> input) = 0;
119  private:
120  AnnotatedData intlCall(const std::string& methodName,
121  boost::shared_ptr<void> input) {
122  return call(methodName,
123  boost::static_pointer_cast<RequestType>(input));
124  }
125 
126  };
127 
136  template<class RequestType, class ReplyType>
137  class Callback: public CallbackBase {
138  public:
139  // typeid is due to msvc strangeness
140  Callback(const std::string& requestType
141  = rsc::runtime::typeName(typeid(RequestType)),
142  const std::string& replyType
143  = rsc::runtime::typeName(typeid(ReplyType))) :
144  CallbackBase(requestType, replyType) {
145  }
146 
157  virtual boost::shared_ptr<ReplyType> call(const std::string& methodName,
158  boost::shared_ptr<RequestType> input) = 0;
159  private:
160  AnnotatedData intlCall(const std::string& methodName,
161  boost::shared_ptr<void> input) {
162  return std::make_pair(getReplyType(), call(methodName,
163  boost::static_pointer_cast<RequestType>(input)));
164  }
165  };
166 
167  template<class RequestType>
168  class Callback<RequestType, void>: public CallbackBase {
169  public:
170  // typeid is due to msvc strangeness
171  Callback(const std::string& requestType
172  = rsc::runtime::typeName(typeid(RequestType)),
173  const std::string& replyType
174  = rsc::runtime::typeName(typeid(void))) :
175  CallbackBase(requestType, replyType) {
176  }
177 
187  virtual void call(const std::string& methodName,
188  boost::shared_ptr<RequestType> input) = 0;
189  private:
190  AnnotatedData intlCall(const std::string& methodName,
191  boost::shared_ptr<void> input) {
192  call(methodName, boost::static_pointer_cast<RequestType>(input));
193  return make_pair(getReplyType(), boost::shared_ptr<void>());
194  }
195 
196  };
197 
198  template<class ReplyType>
199  class Callback<void, ReplyType>: public CallbackBase {
200  public:
201  // typeid is due to msvc strangeness
202  Callback(const std::string& requestType
203  = rsc::runtime::typeName(typeid(void)),
204  const std::string& replyType
205  = rsc::runtime::typeName(typeid(ReplyType))) :
206  CallbackBase(requestType, replyType) {
207  }
208 
218  virtual boost::shared_ptr<ReplyType> call(
219  const std::string& methodName) = 0;
220  private:
221  AnnotatedData intlCall(const std::string& methodName,
222  boost::shared_ptr<void> input) {
223  return std::make_pair(getReplyType(), call(methodName));
224  }
225 
226  };
227 
228  typedef boost::shared_ptr<IntlCallback> CallbackPtr;
229 
230  Server(const Scope& scope, const ParticipantConfig &listenerConfig,
231  const ParticipantConfig &informerConfig);
232  virtual ~Server();
233 
241  void registerMethod(const std::string& methodName, CallbackPtr callback);
242 
243 private:
244 
248 
249  std::set<ListenerPtr> requestListeners;
250 
251  std::map<std::string, Informer<AnyType>::Ptr> methods;
252 
253 };
254 
255 typedef boost::shared_ptr<Server> ServerPtr;
256 
257 }
258 }