31 #include <boost/atomic.hpp>
35 #pragma warning(disable : 4996)
36 #include <boost/signals2.hpp>
38 namespace RobotRaconteur
40 class ROBOTRACONTEUR_CORE_API ClientContext;
41 class ROBOTRACONTEUR_CORE_API PipeClientBase;
42 class ROBOTRACONTEUR_CORE_API WireClientBase;
44 class ROBOTRACONTEUR_CORE_API ServiceStub :
public virtual RRObject,
public RR_ENABLE_SHARED_FROM_THIS<ServiceStub>
48 ServiceStub(boost::string_ref path,
const RR_SHARED_PTR<ClientContext>& c);
50 std::string ServicePath;
52 RR_WEAK_PTR<ClientContext> context;
56 RR_SHARED_PTR<boost::recursive_mutex> RRMutex;
58 RR_SHARED_PTR<ClientContext> GetContext();
60 RR_INTRUSIVE_PTR<MessageEntry> ProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
62 void AsyncProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
63 RR_MOVE_ARG(boost::function<
void(
const RR_INTRUSIVE_PTR<MessageEntry>&,
64 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
67 virtual void DispatchEvent(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
69 RR_SHARED_PTR<RRObject> FindObjRef(boost::string_ref n);
71 void AsyncFindObjRef(boost::string_ref n,
72 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
73 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
76 RR_SHARED_PTR<RRObject> FindObjRef(boost::string_ref n, boost::string_ref i);
78 RR_SHARED_PTR<RRObject> FindObjRefTyped(boost::string_ref n, boost::string_ref objecttype);
80 RR_SHARED_PTR<RRObject> FindObjRefTyped(boost::string_ref n, boost::string_ref i, boost::string_ref objecttype);
82 void AsyncFindObjRef(boost::string_ref n, boost::string_ref i,
83 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
84 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
87 void AsyncFindObjRefTyped(boost::string_ref n, boost::string_ref objecttype,
88 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
89 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
92 void AsyncFindObjRefTyped(boost::string_ref n, boost::string_ref i, boost::string_ref objecttype,
93 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
94 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
98 static void EndAsyncFindObjRef(
99 const RR_SHARED_PTR<RRObject>& obj,
const RR_SHARED_PTR<RobotRaconteurException>& err,
100 const boost::function<
void(
const RR_SHARED_PTR<T>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
102 std::string FindObjectType(boost::string_ref n);
104 std::string FindObjectType(boost::string_ref n, boost::string_ref i);
106 void AsyncFindObjectType(boost::string_ref n,
107 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<std::string>&,
108 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
111 void AsyncFindObjectType(boost::string_ref n, boost::string_ref i,
112 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<std::string>&,
113 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
116 void AsyncSendPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
bool unreliable,
117 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
119 void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
121 virtual void DispatchPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
123 virtual void DispatchWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
125 virtual RR_INTRUSIVE_PTR<MessageEntry> CallbackCall(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
127 RR_OVIRTUAL std::string RRType() RR_OVERRIDE {
return "RobotRaconteur.ServiceStub"; }
129 virtual void RRClose();
131 virtual void RRInitStub() = 0;
133 virtual RR_SHARED_PTR<PipeClientBase> RRGetPipeClient(boost::string_ref membername);
135 virtual RR_SHARED_PTR<WireClientBase> RRGetWireClient(boost::string_ref membername);
137 RR_SHARED_PTR<RobotRaconteurNode> RRGetNode();
138 RR_WEAK_PTR<RobotRaconteurNode> RRGetNodeWeak();
141 RR_WEAK_PTR<RobotRaconteurNode> node;
144 struct ROBOTRACONTEUR_CORE_API PullServiceDefinitionReturn
146 RR_SHARED_PTR<ServiceDefinition> def;
147 RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> > attributes;
148 std::set<std::string> extra_imports;
151 struct ROBOTRACONTEUR_CORE_API PullServiceDefinitionAndImportsReturn
153 std::vector<RR_SHARED_PTR<ServiceDefinition> > defs;
154 RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> > attributes;
155 std::set<std::string> extra_imports;
158 class ROBOTRACONTEUR_CORE_API ClientContext :
public Endpoint,
public RR_ENABLE_SHARED_FROM_THIS<ClientContext>
161 RR_UNORDERED_MAP<MessageStringPtr, RR_SHARED_PTR<ServiceStub> > stubs;
162 boost::mutex stubs_lock;
163 std::list<std::string> active_stub_searches;
164 std::list<boost::tuple<std::string, boost::function<void(
const RR_SHARED_PTR<RRObject>&,
165 const RR_SHARED_PTR<RobotRaconteurException>&)> > >
166 active_stub_searches_handlers;
168 RR_SHARED_PTR<ServiceFactory> m_ServiceDef;
171 RR_SHARED_PTR<ServiceFactory> GetServiceDef()
const;
173 ClientContext(
const RR_SHARED_PTR<RobotRaconteurNode>& node);
175 ClientContext(
const RR_SHARED_PTR<ServiceFactory>& service_def,
const RR_SHARED_PTR<RobotRaconteurNode>& node);
177 RR_SHARED_PTR<RRObject> FindObjRef(boost::string_ref path, boost::string_ref objecttype =
"");
179 void AsyncFindObjRef(boost::string_ref path, boost::string_ref objecttype2,
180 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
181 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
184 template <
typename T>
185 static void EndAsyncFindObjRef(
186 const RR_SHARED_PTR<RRObject>& obj,
const RR_SHARED_PTR<RobotRaconteurException>& err,
187 const boost::function<
void(
const RR_SHARED_PTR<T>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler)
191 handler(RR_SHARED_PTR<T>(), err);
196 RR_SHARED_PTR<T> castobj = RR_DYNAMIC_POINTER_CAST<T>(obj);
199 handler(RR_SHARED_PTR<T>(), RR_MAKE_SHARED<DataTypeMismatchException>(
"Invalid object type returned"));
202 handler(castobj, RR_SHARED_PTR<RobotRaconteurException>());
207 void AsyncFindObjRef1(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
208 const std::string& path,
const std::string& objecttype2,
209 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
210 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
213 void AsyncFindObjRef2(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& ret,
214 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& objecttype,
215 const std::string& objectdef,
const std::string& path,
const std::string& objecttype2,
216 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
217 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
220 void AsyncFindObjRef3(
const RR_SHARED_PTR<RRObject>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
221 const std::string& path,
222 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
223 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
226 std::string FindObjectType(boost::string_ref path);
228 void AsyncFindObjectType(boost::string_ref path,
229 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<std::string>&,
230 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
233 bool VerifyObjectImplements(boost::string_ref objecttype, boost::string_ref implementstype);
236 void AsyncFindObjectType1(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
237 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& path,
238 const boost::function<
void(
const RR_SHARED_PTR<std::string>&,
239 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
242 boost::mutex pulled_service_defs_lock;
243 std::map<std::string, RR_SHARED_PTR<ServiceDefinition> > pulled_service_defs;
245 bool VerifyObjectImplements2(boost::string_ref objecttype, boost::string_ref implementstype);
247 struct outstanding_request
249 RR_INTRUSIVE_PTR<MessageEntry> ret;
250 RR_SHARED_PTR<AutoResetEvent> evt;
251 boost::function<void(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>&)>
253 RR_SHARED_PTR<Timer> timer;
256 boost::mutex outstanding_requests_lock;
257 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<outstanding_request> > outstanding_requests;
259 boost::mutex FindObjRef_lock;
262 RR_INTRUSIVE_PTR<MessageEntry> ProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
264 void AsyncProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
265 RR_MOVE_ARG(boost::function<
void(
const RR_INTRUSIVE_PTR<MessageEntry>&,
266 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
269 using Endpoint::SendMessage;
270 void SendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
272 using Endpoint::AsyncSendMessage;
273 void AsyncSendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
274 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
277 void AsyncProcessRequest_err(
const RR_SHARED_PTR<RobotRaconteurException>& error, uint32_t requestid);
279 void AsyncProcessRequest_timeout(
const TimerEvent& error, uint32_t requestid);
281 uint32_t request_number;
283 RR_SHARED_PTR<Timer> connection_test_timer;
285 void connection_test(
const TimerEvent& ev);
288 RR_OVIRTUAL
void MessageReceived(
const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
291 void MessageEntryReceived(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
293 std::string m_ServiceName;
296 std::string GetServiceName()
const;
298 bool GetConnected()
const;
302 boost::mutex m_Connected_lock;
303 boost::mutex close_lock;
306 RR_SHARED_PTR<Transport> connecttransport;
307 std::string connecturl;
310 std::map<std::string, RR_INTRUSIVE_PTR<RRValue> > GetAttributes();
313 std::map<std::string, RR_INTRUSIVE_PTR<RRValue> > m_Attributes;
314 boost::mutex m_Attributes_lock;
317 RR_SHARED_PTR<RRObject> ConnectService(
const RR_SHARED_PTR<Transport>& c, boost::string_ref url,
318 boost::string_ref username =
"",
319 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials =
320 (RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >()),
321 boost::string_ref objecttype =
"");
323 RR_SHARED_PTR<RRObject> ConnectService(
const RR_SHARED_PTR<Transport>& c,
324 const RR_SHARED_PTR<ITransportConnection>& tc, boost::string_ref url,
325 boost::string_ref username =
"",
326 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials =
327 (RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >()),
328 boost::string_ref objecttype =
"");
330 void AsyncConnectService(
331 const RR_SHARED_PTR<Transport>& c, boost::string_ref url, boost::string_ref username,
332 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials, boost::string_ref objecttype,
334 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
338 void AsyncConnectService(
339 const RR_SHARED_PTR<Transport>& c,
const RR_SHARED_PTR<ITransportConnection>& tc, boost::string_ref url,
340 boost::string_ref username,
const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
341 boost::string_ref objecttype,
343 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
349 void AsyncConnectService1(
350 const RR_SHARED_PTR<Transport>& c,
const RR_SHARED_PTR<ITransportConnection>& tc,
351 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& url,
const std::string& username,
352 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
const std::string& objecttype,
353 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
355 void AsyncConnectService2(
356 const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
const RR_SHARED_PTR<RobotRaconteurException>& e,
357 const std::string& username,
const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
358 const std::string& objecttype,
359 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
361 void AsyncConnectService3(
362 const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& e,
363 const std::string& username,
const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
364 const std::string& objecttype,
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
365 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>&
368 void AsyncConnectService4(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
369 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& username,
370 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
371 const std::string& objecttype,
const std::string& type,
372 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
373 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
375 void AsyncConnectService5(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
376 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& username,
377 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
378 const std::string& objecttype,
const std::string& type,
379 const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
380 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
381 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
383 void AsyncConnectService6(
const RR_SHARED_PTR<std::string>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& e,
384 const std::string& type,
const std::string& username,
385 const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
386 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
387 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
389 void AsyncConnectService7(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
390 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& objecttype,
391 const std::string& username,
392 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
393 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
395 boost::recursive_mutex connect_lock;
400 void AsyncClose(RR_MOVE_ARG(boost::function<
void()>) handler);
403 void AsyncClose1(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<RobotRaconteurException>& err,
404 const boost::function<
void()>& handler);
407 void AsyncSendPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
bool unreliable,
408 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
410 void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
412 PullServiceDefinitionReturn PullServiceDefinition(boost::string_ref ServiceType =
"");
414 void AsyncPullServiceDefinition(boost::string_ref ServiceType,
415 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionReturn>&,
416 const RR_SHARED_PTR<RobotRaconteurException>&)>)
421 void AsyncPullServiceDefinition1(
const RR_INTRUSIVE_PTR<MessageEntry>& ret3,
422 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& ServiceType,
423 boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionReturn>&,
424 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
427 PullServiceDefinitionAndImportsReturn PullServiceDefinitionAndImports(boost::string_ref servicetype =
"");
429 void AsyncPullServiceDefinitionAndImports(
430 boost::string_ref servicetype,
431 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>&,
432 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
436 void AsyncPullServiceDefinitionAndImports1(
437 const RR_SHARED_PTR<PullServiceDefinitionReturn>& pull_ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
438 const std::string& servicetype,
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& current,
439 boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>&,
440 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
441 boost::posix_time::ptime timeout_time);
444 std::vector<std::string> GetPulledServiceTypes();
446 RR_SHARED_PTR<ServiceFactory> GetPulledServiceType(boost::string_ref type);
449 boost::mutex pulled_service_types_lock;
450 RR_UNORDERED_MAP<std::string, RR_SHARED_PTR<ServiceFactory> > pulled_service_types;
452 bool use_pulled_types;
454 RR_SHARED_PTR<ITransportConnection> TransportConnection;
456 boost::mutex m_Authentication_lock;
458 bool m_UserAuthenticated;
461 const bool GetUserAuthenticated();
464 std::string m_AuthenticatedUsername;
467 const std::string GetAuthenticatedUsername();
469 std::string AuthenticateUser(boost::string_ref username,
const RR_INTRUSIVE_PTR<RRValue>& credentials);
471 void AsyncAuthenticateUser(
472 boost::string_ref username,
const RR_INTRUSIVE_PTR<RRValue>& credentials,
474 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
479 void AsyncAuthenticateUser2(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
480 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& username,
481 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
482 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
485 std::string LogoutUser();
487 std::string RequestObjectLock(
const RR_SHARED_PTR<RRObject>& obj, RobotRaconteurObjectLockFlags flags);
489 void AsyncRequestObjectLock(
490 const RR_SHARED_PTR<RRObject>& obj, RobotRaconteurObjectLockFlags flags,
492 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
497 void EndAsyncLockOp(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
498 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
499 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
502 std::string ReleaseObjectLock(
const RR_SHARED_PTR<RRObject>& obj);
504 void AsyncReleaseObjectLock(
505 const RR_SHARED_PTR<RRObject>& obj,
507 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
511 std::string MonitorEnter(
const RR_SHARED_PTR<RRObject>& obj, int32_t timeout);
513 void MonitorExit(
const RR_SHARED_PTR<RRObject>& obj);
515 RR_OVIRTUAL
void PeriodicCleanupTask() RR_OVERRIDE;
517 uint32_t CheckServiceCapability(boost::string_ref name);
521 boost::signals2::signal<
void(const RR_SHARED_PTR<ClientContext>&, ClientServiceListenerEventType,
522 const RR_SHARED_PTR<
void>&)>
523 ClientServiceListener;
526 void ProcessCallbackCall(const RR_INTRUSIVE_PTR<MessageEntry>& m);
528 boost::atomic<
bool> use_combined_connection;
531 RR_OVIRTUAL
void TransportConnectionClosed(uint32_t endpoint) RR_OVERRIDE;
533 virtual
void TransportConnectionClosed1();
536 template <typename T>
537 void ServiceStub::EndAsyncFindObjRef(
538 const RR_SHARED_PTR<RRObject>& obj, const RR_SHARED_PTR<RobotRaconteurException>& err,
539 const boost::function<
void(const RR_SHARED_PTR<T>&, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler)
541 ClientContext::EndAsyncFindObjRef(obj, err, handler);
544 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
545 using ServiceStubPtr = RR_SHARED_PTR<ServiceStub>;
546 using ServiceStubConstPtr = RR_SHARED_PTR<const ServiceStub>;
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition: RobotRaconteurConstants.h:566