- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

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>
11 #include <qi/clock.hpp>
12 #include <qi/messaging/serviceinfo.hpp>
13 #include <qi/messaging/authproviderfactory.hpp>
14 #include <qi/messaging/clientauthenticatorfactory.hpp>
15 #include <qi/future.hpp>
16 #include <qi/anyobject.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <vector>
19 #include <string>
20
21 #ifdef _MSC_VER
22 # pragma warning( push )
23 # pragma warning( disable: 4251 )
24 #endif
25
26 namespace qi {
27
28 class SessionPrivate;
29 class AuthProvider;
30 using AuthProviderPtr = boost::shared_ptr<AuthProvider>;
31 using CapabilityMap = std::map<std::string, AnyValue>;
32
33 class Session;
34 using SessionPtr = boost::shared_ptr<Session>;
35
36 struct QI_API SessionConfig
37 {
39 static Url defaultConnectUrl();
40 static Url defaultListenUrl();
41
42 // Regular:
43 SessionConfig();
44 KA_GENERATE_FRIEND_REGULAR_OPS_2(SessionConfig, connectUrl, listenUrls)
45
46 // SessionConfig:
47 boost::optional<Url> connectUrl;
48 std::vector<Url> listenUrls;
49 };
50
56 class QI_API Session : boost::noncopyable, public ::boost::enable_shared_from_this<Session> {
57 public:
58 Session(bool enforceAuthentication = false, SessionConfig config = {});
59 explicit Session(SessionConfig defaultConfig);
60
61 virtual ~Session();
62
63 enum ServiceLocality {
64 ServiceLocality_All = 0,
65 ServiceLocality_Local = 1,
66 ServiceLocality_Remote = 2
67 };
68
69 static const char* serviceDirectoryServiceName();
70
71 const SessionConfig& config() const;
72
73 // Client
76 qi::FutureSync<void> connect();
77
79 qi::FutureSync<void> connect(const char* serviceDirectoryURL);
80 qi::FutureSync<void> connect(const std::string &serviceDirectoryURL);
81 qi::FutureSync<void> connect(const qi::Url &serviceDirectoryURL);
82
83 bool isConnected() const;
84 qi::Url url() const;
85
86 qi::FutureSync< std::vector<ServiceInfo> > services(ServiceLocality locality = ServiceLocality_All);
87
88 static qi::MilliSeconds defaultServiceTimeout()
89 {
90 return qi::Minutes{1};
91 }
92
93 qi::FutureSync<qi::AnyObject> service(const std::string& name)
94 {
95 return service(name, "", defaultServiceTimeout());
96 }
97
98 qi::FutureSync<qi::AnyObject> service(const std::string& name, qi::MilliSeconds timeout)
99 {
100 return service(name, "", timeout);
101 }
102
103 qi::FutureSync< qi::AnyObject > service(const std::string& name,
104 const std::string& protocol)
105 {
106 return service(name, protocol, defaultServiceTimeout());
107 }
108
112 qi::FutureSync< qi::AnyObject > service(const std::string& name,
113 const std::string& protocol,
114 qi::MilliSeconds timeout);
115
116 // Server
118 qi::FutureSync<void> listen();
119
121 qi::FutureSync<void> listen(const qi::Url &address);
122
125 qi::FutureSync<void> listen(const std::vector<qi::Url>& addresses);
126
127 std::vector<qi::Url> endpoints() const;
128 bool setIdentity(const std::string& key, const std::string& crt);
129
130 //close both client and server side
131 qi::FutureSync<void> close();
132
133 //this create a listen and create a service directory
136 qi::FutureSync<void> listenStandalone();
137
139 qi::FutureSync<void> listenStandalone(const qi::Url &address);
140
142 qi::FutureSync<void> listenStandalone(const std::vector<qi::Url> &addresses);
143
144 qi::FutureSync<unsigned int> registerService(const std::string &name, AnyObject object);
145 qi::FutureSync<void> unregisterService(unsigned int serviceId);
146
147
148 void setAuthProviderFactory(AuthProviderFactoryPtr);
149 void setClientAuthenticatorFactory(ClientAuthenticatorFactoryPtr);
150
156 qi::FutureSync<unsigned int> loadService(const std::string& moduleName, const std::string &renameModule = "",
157 const AnyReferenceVector& args = AnyReferenceVector());
158
164 template <typename T>
165 qi::FutureSync<T> callModule(const std::string& moduleName, const AnyReferenceVector& args = AnyReferenceVector())
166 {
167 qi::Promise<T> promise;
168 qi::Future<qi::AnyValue> future = _callModule(moduleName, args, qi::MetaCallType_Queued);
169 promise.setOnCancel([future](qi::Promise<T>&) mutable {future.cancel();}); // keeps the future alive
170 future.then(qi::bind(qi::detail::futureAdapterVal<T>, future, promise));
171 return promise.future();
172 }
173
174 #define pushi(z, n, _) params.push_back(p ## n);
175 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
176 void loadService( \
177 const std::string& moduleName, const std::string& renameModule, \
178 qi::AutoAnyReference pp0 comma \
179 QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
180 { \
181 std::vector<qi::AnyReference> params; \
182 params.reserve(n+1); \
183 params.push_back(pp0); \
184 BOOST_PP_REPEAT(n, pushi, _) \
185 loadService(moduleName, renameModule, params); \
186 } \
187 template <typename T> \
188 qi::FutureSync<T> callModule( \
189 const std::string& moduleName, \
190 qi::AutoAnyReference pp0 comma \
191 QI_GEN_ARGSDECLSAMETYPE(n, qi::AutoAnyReference)) \
192 { \
193 std::vector<qi::AnyReference> params; \
194 params.reserve(n+1); \
195 params.push_back(pp0); \
196 BOOST_PP_REPEAT(n, pushi, _) \
197 return callModule<T>(moduleName, params); \
198 }
199 QI_GEN(genCall)
200 #undef genCall
201 #undef pushi
202
207 FutureSync<void> waitForService(const std::string& servicename, MilliSeconds timeout);
208
213 qi::FutureSync<void> waitForService(const std::string& service);
214
215 public:
216 qi::Signal<unsigned int, std::string> serviceRegistered;
217 qi::Signal<unsigned int, std::string> serviceUnregistered;
218 // C4251
219 qi::Signal<> connected;
220 // C4251
221 qi::Signal<std::string> disconnected;
222
223 inline static MilliSeconds defaultWaitForServiceTimeout()
224 {
225 return Minutes{5};
226 }
227
228 protected:
229 friend class SessionPrivate;
230 std::unique_ptr<SessionPrivate> _p;
231
232 private:
233 qi::Future<AnyValue> _callModule(const std::string& moduleName,
234 const AnyReferenceVector& args,
235 qi::MetaCallType metacallType);
236
237 qi::FutureSync<void> waitForServiceImpl(const std::string& service);
238 };
239
240 template <typename... Args>
241 SessionPtr makeSession(Args&&... args)
242 {
243 return boost::make_shared<qi::Session>(std::forward<Args>(args)...);
244 }
245 }
246
247 QI_TYPE_ENUM(qi::Session::ServiceLocality);
248
249 #ifdef _MSC_VER
250 # pragma warning( pop )
251 #endif
252
253 #endif // _QIMESSAGING_SESSION_HPP_
clientauthenticatorfactory.hpp
qi::SessionPtr
boost::shared_ptr< Session > SessionPtr
Definition: session.hpp:34
qi::Session::service
qi::FutureSync< qi::AnyObject > service(const std::string &name)
Definition: session.hpp:93
qi::Session::callModule
qi::FutureSync< T > callModule(const std::string &moduleName, const AnyReferenceVector &args=AnyReferenceVector())
Definition: session.hpp:165
QI_API
#define QI_API
Definition: api.hpp:33
qi::Future
Definition: executioncontext.hpp:20
qi::Session::ServiceLocality
ServiceLocality
Definition: session.hpp:63
qi::Session::defaultServiceTimeout
static qi::MilliSeconds defaultServiceTimeout()
Definition: session.hpp:88
qi::Session::serviceRegistered
qi::Signal< unsigned int, std::string > serviceRegistered
Definition: session.hpp:216
qi::Minutes
DurationType< int64_t, boost::ratio< 60 >> Minutes
Definition: clock.hpp:29
api.hpp
dll import/export and compiler message
qi::Signal< unsigned int, std::string >
qi::Session::serviceUnregistered
qi::Signal< unsigned int, std::string > serviceUnregistered
Definition: session.hpp:217
qi::Future::then
auto then(FutureCallbackType type, F &&func) -> Future< typename std::result_of< F(Future< T >)>::type >
Execute a callback when the future is finished.
Definition: future_fwd.hpp:453
qi::AuthProviderFactoryPtr
boost::shared_ptr< AuthProviderFactory > AuthProviderFactoryPtr
Definition: authproviderfactory.hpp:36
qi::ClientAuthenticatorFactoryPtr
boost::shared_ptr< ClientAuthenticatorFactory > ClientAuthenticatorFactoryPtr
Definition: clientauthenticatorfactory.hpp:27
qi::Session::_p
std::unique_ptr< SessionPrivate > _p
Definition: session.hpp:230
qi::Promise::future
Future< T > future() const
Get a future linked to this promise. Can be called multiple times.
Definition: future_fwd.hpp:881
qi::makeSession
SessionPtr makeSession(Args &&...args)
Definition: session.hpp:241
authproviderfactory.hpp
qi::Session::service
qi::FutureSync< qi::AnyObject > service(const std::string &name, qi::MilliSeconds timeout)
Definition: session.hpp:98
qi::MilliSeconds
DurationType< int64_t, boost::milli > MilliSeconds
Definition: clock.hpp:27
genCall
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: session.hpp:175
qi::AnyReferenceVector
std::vector< AnyReference > AnyReferenceVector
Definition: anyreference.hpp:26
QI_TYPE_ENUM
QI_TYPE_ENUM(qi::Session::ServiceLocality)
qi::Session::connected
qi::Signal connected
Definition: session.hpp:219
qi::AuthProviderPtr
boost::shared_ptr< AuthProvider > AuthProviderPtr
Definition: authprovider.hpp:60
qi::CapabilityMap
std::map< std::string, AnyValue > CapabilityMap
Definition: authprovider.hpp:21
future.hpp
qi::Future::cancel
void cancel()
Definition: future_fwd.hpp:383
qi::Session::service
qi::FutureSync< qi::AnyObject > service(const std::string &name, const std::string &protocol)
Definition: session.hpp:103
qi::MetaCallType
MetaCallType
Definition: typeobject.hpp:24
qi::Object
Definition: object.hxx:243
qi::Session::defaultWaitForServiceTimeout
static MilliSeconds defaultWaitForServiceTimeout()
Definition: session.hpp:223
clock.hpp
qi::SessionConfig
Definition: session.hpp:36
serviceinfo.hpp
qi::Promise::setOnCancel
void setOnCancel(boost::function< void(qi::Promise< T > &)> cancelCallback)
Definition: future_fwd.hpp:897
qi::Session::disconnected
qi::Signal< std::string > disconnected
Definition: session.hpp:221
qi::Url
Definition: url.hpp:35
qi::log::env::QI_DEFAULT_LOGHANDLER::name
char const *const name
qi::FutureSync
Definition: future_fwd.hpp:70
qi::Session
Definition: session.hpp:56
qi::Promise
Definition: future_fwd.hpp:71
qi::MetaCallType_Queued
Force an asynchronous call in an other thread.
Definition: typeobject.hpp:30
anyobject.hpp
qi::bind
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bind(AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:308
QI_GEN
#define QI_GEN(f)
Definition: preproc.hpp:476