libqi-api  release-2.5.3-2016-11-18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
session.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2012 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6 
7 #ifndef _QI_MESSAGING_SESSION_HPP_
8 #define _QI_MESSAGING_SESSION_HPP_
9 
10 #include <qi/api.hpp>
14 #include <qi/future.hpp>
15 #include <qi/anyobject.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <vector>
18 #include <string>
19 
20 #ifdef _MSC_VER
21 # pragma warning( push )
22 # pragma warning( disable: 4251 )
23 #endif
24 
25 namespace qi {
26 
27  class SessionPrivate;
28  class AuthProvider;
29  using AuthProviderPtr = boost::shared_ptr<AuthProvider>;
30  using CapabilityMap = std::map<std::string, AnyValue>;
31 
37  class QI_API Session : boost::noncopyable, public ::boost::enable_shared_from_this<Session> {
38  public:
39  Session(bool enforceAuthentication = false);
40  virtual ~Session();
41 
43  ServiceLocality_All = 0,
44  ServiceLocality_Local = 1,
45  ServiceLocality_Remote = 2
46  };
47 
48  //Client
49  qi::FutureSync<void> connect(const char* serviceDirectoryURL);
50  qi::FutureSync<void> connect(const std::string &serviceDirectoryURL);
51  qi::FutureSync<void> connect(const qi::Url &serviceDirectoryURL);
52  bool isConnected() const;
53  qi::Url url() const;
54 
55  qi::FutureSync< std::vector<ServiceInfo> > services(ServiceLocality locality = ServiceLocality_All);
56 
57  qi::FutureSync< qi::AnyObject > service(const std::string &aservice)
58  {
59  return service(aservice, "");
60  }
61 
62  qi::FutureSync< qi::AnyObject > service(const std::string &service,
63  const std::string &protocol);
64 
65  //Server
66  qi::FutureSync<void> listen(const qi::Url &address);
67  std::vector<qi::Url> endpoints() const;
68  bool setIdentity(const std::string& key, const std::string& crt);
69 
70  //close both client and server side
71  qi::FutureSync<void> close();
72 
73  //this create a listen and create a service directory
74  qi::FutureSync<void> listenStandalone(const qi::Url &address);
75 
76  qi::FutureSync<unsigned int> registerService(const std::string &name, AnyObject object);
77  qi::FutureSync<void> unregisterService(unsigned int serviceId);
78 
79 
80  void setAuthProviderFactory(AuthProviderFactoryPtr);
81  void setClientAuthenticatorFactory(ClientAuthenticatorFactoryPtr);
82 
88  void loadService(const std::string& moduleName, const std::string &renameModule = "",
89  const AnyReferenceVector& args = AnyReferenceVector());
90 
96  template <typename T>
97  qi::FutureSync<T> callModule(const std::string& moduleName, const AnyReferenceVector& args = AnyReferenceVector())
98  {
99  qi::Promise<T> promise;
100  qi::Future<qi::AnyValue> future = _callModule(moduleName, args, qi::MetaCallType_Queued);
101  promise.setOnCancel([future](qi::Promise<T>&) mutable {future.cancel();}); // keeps the future alive
102  future.then(qi::bind(qi::detail::futureAdapterVal<T>, future, promise));
103  return promise.future();
104  }
105 
106 #define pushi(z, n, _) params.push_back(p ## n);
107 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
108  void loadService( \
109  const std::string& moduleName, const std::string& renameModule, \
110  qi::AutoAnyReference pp0 comma \
111  QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
112  { \
113  std::vector<qi::AnyReference> params; \
114  params.reserve(n+1); \
115  params.push_back(pp0); \
116  BOOST_PP_REPEAT(n, pushi, _) \
117  loadService(moduleName, renameModule, params); \
118  } \
119  template <typename T> \
120  qi::FutureSync<T> callModule( \
121  const std::string& moduleName, \
122  qi::AutoAnyReference pp0 comma \
123  QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
124  { \
125  std::vector<qi::AnyReference> params; \
126  params.reserve(n+1); \
127  params.push_back(pp0); \
128  BOOST_PP_REPEAT(n, pushi, _) \
129  return callModule<T>(moduleName, params); \
130  }
132 #undef genCall
133 #undef pushi
134 
138  qi::FutureSync<void> waitForService(const std::string& service);
139 
140  public:
143  // C4251
145  // C4251
147 
148  protected:
149  friend class SessionPrivate;
150  boost::shared_ptr<SessionPrivate> _p;
151 
152  private:
153  qi::Future<AnyValue> _callModule(const std::string& moduleName,
154  const AnyReferenceVector& args,
155  qi::MetaCallType metacallType);
156  };
157 
158  using SessionPtr = boost::shared_ptr<Session>;
159 
160  inline SessionPtr makeSession() { return boost::make_shared<qi::Session>(); }
161 }
162 
164 
165 #ifdef _MSC_VER
166 # pragma warning( pop )
167 #endif
168 
169 #endif // _QIMESSAGING_SESSION_HPP_
boost::shared_ptr< Session > SessionPtr
Definition: session.hpp:158
qi::FutureSync< qi::AnyObject > service(const std::string &aservice)
Definition: session.hpp:57
qi::FutureSync< T > callModule(const std::string &moduleName, const AnyReferenceVector &args=AnyReferenceVector())
Definition: session.hpp:97
#define QI_API
Definition: api.hpp:33
SessionPtr makeSession()
Definition: session.hpp:160
qi::Signal< unsigned int, std::string > serviceRegistered
Definition: session.hpp:141
dll import/export and compiler message
qi::Signal< unsigned int, std::string > serviceUnregistered
Definition: session.hpp:142
boost::shared_ptr< AuthProviderFactory > AuthProviderFactoryPtr
boost::shared_ptr< ClientAuthenticatorFactory > ClientAuthenticatorFactoryPtr
Future< T > future() const
Get a future linked to this promise. Can be called multiple times.
Definition: future_fwd.hpp:786
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: session.hpp:107
std::vector< AnyReference > AnyReferenceVector
QI_TYPE_ENUM(qi::Session::ServiceLocality)
qi::Signal connected
Definition: session.hpp:144
boost::shared_ptr< AuthProvider > AuthProviderPtr
std::map< std::string, AnyValue > CapabilityMap
void cancel()
Definition: future_fwd.hpp:314
MetaCallType
Definition: typeobject.hpp:21
auto then(FutureCallbackType type, AF &&func) -> qi::Future< typename detail::DecayAsyncResult< AF, qi::Future< T >>::type >
Execute a callback when the future is finished.
Definition: future_fwd.hpp:394
boost::shared_ptr< SessionPrivate > _p
Definition: session.hpp:150
void setOnCancel(boost::function< void(qi::Promise< T > &)> cancelCallback)
Definition: future_fwd.hpp:802
qi::Signal< std::string > disconnected
Definition: session.hpp:146
Definition: url.hpp:35
Force an asynchronous call in an other thread.
Definition: typeobject.hpp:27
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bind(AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:327
#define QI_GEN(f)
Definition: preproc.hpp:476