RSB  0.9.6
 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());
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 template <>
265 public:
267  target(target) {
268  }
269 
270  bool isDone() {
271  return this->target->isDone();
272  }
273 
274  boost::shared_ptr<void> get(double timeout) {
275  this->target->get(timeout)->getData();
276  return boost::shared_ptr<void>();
277  }
278 private:
280 };
281 
282 typedef boost::shared_ptr<RemoteServer> RemoteServerPtr;
283 
284 }
285 }
boost::shared_ptr< WaitingEventHandler > handler
Definition: RemoteServer.h:250
boost::shared_ptr< void > VoidPtr
Definition: Event.h:52
Informer< void >::Ptr requestInformer
Definition: RemoteServer.h:252
ParticipantConfig listenerConfig
Definition: RemoteServer.h:243
EventPtr prepareRequestEvent(boost::shared_ptr< I > args)
Prepares an Event which can be used for a request based on typed data.
Definition: RemoteServer.h:104
boost::shared_ptr< O > call(const std::string &methodName, boost::shared_ptr< I > args, unsigned int maxReplyWaitTime=25)
Call the method named methodName on the remote server, passing it the argument object args and return...
Definition: RemoteServer.h:210
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
std::map< std::string, MethodSet > methodSets
Definition: RemoteServer.h:256
A informer to publish data of a specified type expressed through the template parameter.
Definition: Informer.h:230
boost::shared_ptr< Listener > ListenerPtr
Definition: Listener.h:150
DataFuture< O > callAsync(const std::string &methodName)
Call the method named methodName on the remote server, without arguments and returning the value retu...
Definition: RemoteServer.h:157
boost::shared_ptr< O > call(const std::string &methodName, unsigned int maxReplyWaitTime=25)
Call the method named methodName on the remote server, without arguments and return the value returne...
Definition: RemoteServer.h:235
ParticipantConfig informerConfig
Definition: RemoteServer.h:244
A class describing the configuration of Participant instances.
boost::shared_ptr< RemoteServer > RemoteServerPtr
Definition: RemoteServer.h:282
rsc::threading::Future< EventPtr > FutureType
Definition: RemoteServer.h:64
rsc::logging::LoggerPtr logger
Definition: RemoteServer.h:240
boost::shared_ptr< Event > EventPtr
Definition: Event.h:251
boost::shared_ptr< FutureType > FuturePtr
Definition: RemoteServer.h:65
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
DataFuture< O > callAsync(const std::string &methodName, boost::shared_ptr< I > args)
Call the method named methodName on the remote server, passing it the argument object args and return...
Definition: RemoteServer.h:140
The client side of a request-reply-based communication channel.
Definition: RemoteServer.h:62