RSB  0.15.1
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, 2015 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 #include <set>
33 
34 #include <boost/shared_ptr.hpp>
35 #include <boost/enable_shared_from_this.hpp>
36 #include <boost/thread/mutex.hpp>
37 
38 #include <rsc/runtime/TypeStringTools.h>
39 #include <rsc/logging/Logger.h>
40 #include <rsc/threading/Future.h>
41 
42 #include "../Event.h"
43 
44 #include "Server.h"
45 
46 #include "rsb/rsbexports.h"
47 
48 namespace rsb {
49 namespace patterns {
50 
60 class RSB_EXPORT RemoteServer: public Participant,
61  public boost::enable_shared_from_this<RemoteServer> {
62 public:
63  typedef rsc::threading::Future<EventPtr> FutureType;
64  typedef boost::shared_ptr<FutureType> FuturePtr;
65 
66  template <typename O>
67  class DataFuture {
68  public:
69  DataFuture(FuturePtr target):
70  target(target) {
71  }
72 
73  bool isDone() {
74  return this->target->isDone();
75  }
76 
77  boost::shared_ptr<O> get(double timeout) {
78  return boost::static_pointer_cast<O>(this->target->get(timeout)->getData());
79  }
80  private:
81  FuturePtr target;
82  };
83 
90  class RemoteMethod: public Method {
91  public:
92  RemoteMethod(const Scope& scope,
93  const std::string& name,
94  const ParticipantConfig& listenerConfig,
95  const ParticipantConfig& informerConfig);
96  virtual ~RemoteMethod();
97 
98  // Overrides method in Participant.
99  virtual std::string getKind() const;
100 
101  FuturePtr call(const std::string& methodName, EventPtr request);
102  private:
103  typedef boost::mutex MutexType;
104 
105  rsc::logging::LoggerPtr logger;
106 
107  MutexType inprogressMutex;
108  std::map<EventId, FuturePtr> inprogress;
109 
110  ListenerPtr makeListener();
111 
112  void handle(EventPtr event);
113  };
114 
115  typedef boost::shared_ptr<RemoteMethod> RemoteMethodPtr;
116 
124  RemoteServer(const Scope& scope,
125  const ParticipantConfig &listenerConfig,
126  const ParticipantConfig &informerConfig);
127  virtual ~RemoteServer();
128 
129  // Overrides method in Participant.
130  virtual std::string getKind() const;
131 
132  // Overrides method in Participant.
133  virtual const std::set<std::string> getTransportURLs() const;
134 
142  template<class I>
143  EventPtr prepareRequestEvent(boost::shared_ptr<I> args) {
144  EventPtr request(new Event);
145  request->setType(rsc::runtime::typeName<I>());
146  request->setData(args);
147  return request;
148  }
149 
162  FuturePtr callAsync(const std::string& methodName, EventPtr data);
163 
178  template <typename O, typename I>
179  DataFuture<O> callAsync(const std::string& methodName,
180  boost::shared_ptr<I> args) {
181  return DataFuture<O>(callAsync(methodName, prepareRequestEvent(args)));
182  }
183 
195  template <typename O>
196  DataFuture<O> callAsync(const std::string& methodName) {
197  EventPtr request(new Event());
198  request->setType(rsc::runtime::typeName<void>());
199  request->setData(VoidPtr());
200  return DataFuture<O>(callAsync(methodName, request));
201  }
202 
223  EventPtr call(const std::string& methodName,
224  EventPtr data,
225  unsigned int maxReplyWaitTime = 25);
226 
248  template <typename O, typename I>
249  boost::shared_ptr<O> call(const std::string& methodName,
250  boost::shared_ptr<I> args,
251  unsigned int maxReplyWaitTime = 25) {
252  return callAsync<O>(methodName, args).get(maxReplyWaitTime);
253  }
254 
273  template <typename O>
274  boost::shared_ptr<O> call(const std::string& methodName,
275  unsigned int maxReplyWaitTime = 25) {
276  return callAsync<O>(methodName).get(maxReplyWaitTime);
277  }
278 private:
279 
280  rsc::logging::LoggerPtr logger;
281 
284 
285  boost::mutex methodsMutex;
286  std::map<std::string, RemoteMethodPtr> methods;
287 
288  RemoteMethodPtr getMethod(const std::string& name);
289 
290 };
291 
292 template <>
294 public:
296  target(target) {
297  }
298 
299  bool isDone() {
300  return this->target->isDone();
301  }
302 
303  boost::shared_ptr<void> get(double timeout) {
304  this->target->get(timeout)->getData();
305  return boost::shared_ptr<void>();
306  }
307 private:
309 };
310 
311 typedef boost::shared_ptr<RemoteServer> RemoteServerPtr;
312 
313 }
314 }
boost::shared_ptr< void > VoidPtr
Definition: Event.h:51
ParticipantConfig listenerConfig
Definition: RemoteServer.h:282
A derived Method class which can be used to invoke methods on a remote LocalServer object...
Definition: RemoteServer.h:90
EventPtr prepareRequestEvent(boost::shared_ptr< I > args)
Prepares an Event which can be used for a request based on typed data.
Definition: RemoteServer.h:143
std::map< std::string, RemoteMethodPtr > methods
Definition: RemoteServer.h:286
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:249
boost::shared_ptr< RemoteMethod > RemoteMethodPtr
Definition: RemoteServer.h:115
std::map< EventId, FuturePtr > inprogress
Definition: RemoteServer.h:108
Basic message that is exchanged between informers and listeners.
Definition: Event.h:60
Objects of this class participate in the exchange of notifications on one channel of the bus...
Definition: Participant.h:65
boost::shared_ptr< Listener > ListenerPtr
Definition: Listener.h:155
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:196
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:274
Base class for method classes.
Definition: Server.h:55
ParticipantConfig informerConfig
Definition: RemoteServer.h:283
A class describing the configuration of Participant instances.
boost::shared_ptr< RemoteServer > RemoteServerPtr
Definition: RemoteServer.h:311
rsc::threading::Future< EventPtr > FutureType
Definition: RemoteServer.h:63
rsc::logging::LoggerPtr logger
Definition: RemoteServer.h:280
boost::shared_ptr< Event > EventPtr
Definition: Event.h:264
boost::shared_ptr< FutureType > FuturePtr
Definition: RemoteServer.h:64
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:179
The client side of a request-reply-based communication channel.
Definition: RemoteServer.h:60