RSB  0.12.2
 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, 2014 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 <string>
31 #include <map>
32 
33 #include <boost/shared_ptr.hpp>
34 #include <boost/enable_shared_from_this.hpp>
35 #include <boost/thread/mutex.hpp>
36 
37 #include <rsc/runtime/TypeStringTools.h>
38 #include <rsc/logging/Logger.h>
39 #include <rsc/threading/Future.h>
40 
41 #include "../Event.h"
42 
43 #include "Server.h"
44 
45 #include "rsb/rsbexports.h"
46 
47 namespace rsb {
48 namespace patterns {
49 
59 class RSB_EXPORT RemoteServer: public Participant,
60  public boost::enable_shared_from_this<RemoteServer> {
61 public:
62  typedef rsc::threading::Future<EventPtr> FutureType;
63  typedef boost::shared_ptr<FutureType> FuturePtr;
64 
65  template <typename O>
66  class DataFuture {
67  public:
69  target(target) {
70  }
71 
72  bool isDone() {
73  return this->target->isDone();
74  }
75 
76  boost::shared_ptr<O> get(double timeout) {
77  return boost::static_pointer_cast<O>(this->target->get(timeout)->getData());
78  }
79  private:
81  };
82 
89  class RemoteMethod: public Method {
90  public:
91  RemoteMethod(const Scope& scope,
92  const std::string& name,
93  const ParticipantConfig& listenerConfig,
94  const ParticipantConfig& informerConfig);
95  virtual ~RemoteMethod();
96 
97  // Overrides method in Participant.
98  virtual std::string getKind() const;
99 
100  FuturePtr call(const std::string& methodName, EventPtr request);
101  private:
102  typedef boost::mutex MutexType;
103 
104  rsc::logging::LoggerPtr logger;
105 
107  std::map<EventId, FuturePtr> inprogress;
108 
109  ListenerPtr makeListener();
110 
111  void handle(EventPtr event);
112  };
113 
114  typedef boost::shared_ptr<RemoteMethod> RemoteMethodPtr;
115 
123  RemoteServer(const Scope& scope,
124  const ParticipantConfig &listenerConfig,
125  const ParticipantConfig &informerConfig);
126  virtual ~RemoteServer();
127 
128  // Overrides method in Participant.
129  virtual std::string getKind() const;
130 
138  template<class I>
139  EventPtr prepareRequestEvent(boost::shared_ptr<I> args) {
140  EventPtr request(new Event);
141  request->setType(rsc::runtime::typeName<I>());
142  request->setData(args);
143  return request;
144  }
145 
158  FuturePtr callAsync(const std::string& methodName, EventPtr data);
159 
174  template <typename O, typename I>
175  DataFuture<O> callAsync(const std::string& methodName,
176  boost::shared_ptr<I> args) {
177  return DataFuture<O>(callAsync(methodName, prepareRequestEvent(args)));
178  }
179 
191  template <typename O>
192  DataFuture<O> callAsync(const std::string& methodName) {
193  EventPtr request(new Event());
194  request->setType(rsc::runtime::typeName<void>());
195  request->setData(VoidPtr());
196  return DataFuture<O>(callAsync(methodName, request));
197  }
198 
219  EventPtr call(const std::string& methodName,
220  EventPtr data,
221  unsigned int maxReplyWaitTime = 25);
222 
244  template <typename O, typename I>
245  boost::shared_ptr<O> call(const std::string& methodName,
246  boost::shared_ptr<I> args,
247  unsigned int maxReplyWaitTime = 25) {
248  return callAsync<O>(methodName, args).get(maxReplyWaitTime);
249  }
250 
269  template <typename O>
270  boost::shared_ptr<O> call(const std::string& methodName,
271  unsigned int maxReplyWaitTime = 25) {
272  return callAsync<O>(methodName).get(maxReplyWaitTime);
273  }
274 private:
275 
276  rsc::logging::LoggerPtr logger;
277 
280 
281  boost::mutex methodsMutex;
282  std::map<std::string, RemoteMethodPtr> methods;
283 
284  RemoteMethodPtr getMethod(const std::string& name);
285 
286 };
287 
288 template <>
290 public:
292  target(target) {
293  }
294 
295  bool isDone() {
296  return this->target->isDone();
297  }
298 
299  boost::shared_ptr<void> get(double timeout) {
300  this->target->get(timeout)->getData();
301  return boost::shared_ptr<void>();
302  }
303 private:
305 };
306 
307 typedef boost::shared_ptr<RemoteServer> RemoteServerPtr;
308 
309 }
310 }
boost::shared_ptr< void > VoidPtr
Definition: Event.h:52
ParticipantConfig listenerConfig
Definition: RemoteServer.h:278
A derived Method class which can be used to invoke methods on a remote LocalServer object...
Definition: RemoteServer.h:89
EventPtr prepareRequestEvent(boost::shared_ptr< I > args)
Prepares an Event which can be used for a request based on typed data.
Definition: RemoteServer.h:139
std::map< std::string, RemoteMethodPtr > methods
Definition: RemoteServer.h:282
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:245
boost::shared_ptr< RemoteMethod > RemoteMethodPtr
Definition: RemoteServer.h:114
std::map< EventId, FuturePtr > inprogress
Definition: RemoteServer.h:107
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
Objects of this class participate in the exchange of notifications on one channel of the bus...
Definition: Participant.h:64
boost::shared_ptr< Listener > ListenerPtr
Definition: Listener.h:151
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:192
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:270
Base class for method classes.
Definition: Server.h:52
ParticipantConfig informerConfig
Definition: RemoteServer.h:279
A class describing the configuration of Participant instances.
boost::shared_ptr< RemoteServer > RemoteServerPtr
Definition: RemoteServer.h:307
rsc::threading::Future< EventPtr > FutureType
Definition: RemoteServer.h:62
rsc::logging::LoggerPtr logger
Definition: RemoteServer.h:276
boost::shared_ptr< Event > EventPtr
Definition: Event.h:254
boost::shared_ptr< FutureType > FuturePtr
Definition: RemoteServer.h:63
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:175
The client side of a request-reply-based communication channel.
Definition: RemoteServer.h:59