RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RemoteServer.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 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 <stdexcept>
31 #include <string>
32 
33 #include <boost/noncopyable.hpp>
34 #include <boost/thread/mutex.hpp>
35 
36 #include <rsc/runtime/TypeStringTools.h>
37 #include <rsc/logging/Logger.h>
38 #include <rsc/threading/Future.h>
39 
40 #include "../Event.h"
41 #include "../Exception.h"
42 #include "../Informer.h"
43 #include "../Listener.h"
44 #include "../ParticipantConfig.h"
45 #include "../Scope.h"
46 #include "rsb/rsbexports.h"
47 
48 namespace rsb {
49 namespace patterns {
50 
51 class WaitingEventHandler;
52 
62 class RSB_EXPORT RemoteServer: public boost::noncopyable {
63 public:
64  typedef rsc::threading::Future<EventPtr> FutureType;
65  typedef boost::shared_ptr<FutureType> FuturePtr;
66 
67  template <typename O>
68  class DataFuture {
69  public:
71  target(target) {
72  }
73 
74  bool isDone() {
75  return this->target->isDone();
76  }
77 
78  boost::shared_ptr<O> get(double timeout) {
79  return boost::static_pointer_cast<O>(this->target->get(timeout)->getData());
80  }
81  private:
83  };
84 
92  RemoteServer(const Scope& scope, const ParticipantConfig &listenerConfig,
93  const ParticipantConfig &informerConfig);
94  virtual ~RemoteServer();
95 
103  template<class I>
104  EventPtr prepareRequestEvent(boost::shared_ptr<I> args) {
105  EventPtr request(new Event);
106  request->setType(rsc::runtime::typeName<I>());
107  request->setData(args);
108  return request;
109  }
110 
123  FuturePtr callAsync(const std::string& methodName, EventPtr data);
124 
139  template <typename O, typename I>
140  DataFuture<O> callAsync(const std::string& methodName,
141  boost::shared_ptr<I> args) {
142  return DataFuture<O>(callAsync(methodName, prepareRequestEvent(args)));
143  }
144 
156  template <typename O>
157  DataFuture<O> callAsync(const std::string& methodName) {
158  EventPtr request(new Event());
159  request->setType(rsc::runtime::typeName<void>());
160  request->setData(VoidPtr(malloc(10)));
161  return DataFuture<O>(callAsync(methodName, request));
162  }
163 
184  EventPtr call(const std::string& methodName,
185  EventPtr data,
186  unsigned int maxReplyWaitTime = 25);
187 
209  template <typename O, typename I>
210  boost::shared_ptr<O> call(const std::string& methodName,
211  boost::shared_ptr<I> args,
212  unsigned int maxReplyWaitTime = 25) {
213  return callAsync<O>(methodName, args).get(maxReplyWaitTime);
214  }
215 
234  template <typename O>
235  boost::shared_ptr<O> call(const std::string& methodName,
236  unsigned int maxReplyWaitTime = 25) {
237  return callAsync<O>(methodName).get(maxReplyWaitTime);
238  }
239 private:
240  rsc::logging::LoggerPtr logger;
241 
245 
246  class MethodSet {
247  public:
248  std::string methodName;
249  std::string sendType;
250  boost::shared_ptr<WaitingEventHandler> handler;
253  };
254 
255  boost::mutex methodSetMutex;
256  std::map<std::string, MethodSet> methodSets;
257 
258  MethodSet getMethodSet(const std::string& methodName,
259  const std::string& sendType);
260 
261 };
262 
263 typedef boost::shared_ptr<RemoteServer> RemoteServerPtr;
264 
265 }
266 }