31#include <boost/atomic.hpp>
36#pragma warning(disable : 4996)
38#include <boost/signals2.hpp>
40namespace RobotRaconteur
42class ROBOTRACONTEUR_CORE_API ClientContext;
43class ROBOTRACONTEUR_CORE_API PipeClientBase;
44class ROBOTRACONTEUR_CORE_API WireClientBase;
46class ROBOTRACONTEUR_CORE_API ServiceStub :
public virtual RRObject,
public RR_ENABLE_SHARED_FROM_THIS<ServiceStub>
50 ServiceStub(boost::string_ref path,
const RR_SHARED_PTR<ClientContext>& c);
52 std::string ServicePath;
54 RR_WEAK_PTR<ClientContext> context;
58 RR_SHARED_PTR<boost::recursive_mutex> RRMutex;
60 RR_SHARED_PTR<ClientContext> GetContext();
62 RR_INTRUSIVE_PTR<MessageEntry> ProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
64 void AsyncProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
65 RR_MOVE_ARG(boost::function<
void(
const RR_INTRUSIVE_PTR<MessageEntry>&,
66 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
69 virtual void DispatchEvent(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
71 RR_SHARED_PTR<RRObject> FindObjRef(boost::string_ref n);
73 void AsyncFindObjRef(boost::string_ref n,
74 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
75 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
78 RR_SHARED_PTR<RRObject> FindObjRef(boost::string_ref n, boost::string_ref i);
80 RR_SHARED_PTR<RRObject> FindObjRefTyped(boost::string_ref n, boost::string_ref objecttype);
82 RR_SHARED_PTR<RRObject> FindObjRefTyped(boost::string_ref n, boost::string_ref i, boost::string_ref objecttype);
84 void AsyncFindObjRef(boost::string_ref n, boost::string_ref i,
85 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
86 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
89 void AsyncFindObjRefTyped(boost::string_ref n, boost::string_ref objecttype,
90 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
91 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
94 void AsyncFindObjRefTyped(boost::string_ref n, boost::string_ref i, boost::string_ref objecttype,
95 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
96 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
100 static void EndAsyncFindObjRef(
101 const RR_SHARED_PTR<RRObject>& obj,
const RR_SHARED_PTR<RobotRaconteurException>& err,
102 const boost::function<
void(
const RR_SHARED_PTR<T>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
104 std::string FindObjectType(boost::string_ref n);
106 std::string FindObjectType(boost::string_ref n, boost::string_ref i);
108 void AsyncFindObjectType(boost::string_ref n,
109 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<std::string>&,
110 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
113 void AsyncFindObjectType(boost::string_ref n, boost::string_ref i,
114 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<std::string>&,
115 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
118 void AsyncSendPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
bool unreliable,
119 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
121 void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
123 virtual void DispatchPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
125 virtual void DispatchWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
127 virtual RR_INTRUSIVE_PTR<MessageEntry> CallbackCall(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
129 RR_OVIRTUAL std::string RRType() RR_OVERRIDE {
return "RobotRaconteur.ServiceStub"; }
131 virtual void RRClose();
133 virtual void RRInitStub() = 0;
135 virtual RR_SHARED_PTR<PipeClientBase> RRGetPipeClient(boost::string_ref membername);
137 virtual RR_SHARED_PTR<WireClientBase> RRGetWireClient(boost::string_ref membername);
139 RR_SHARED_PTR<RobotRaconteurNode> RRGetNode();
140 RR_WEAK_PTR<RobotRaconteurNode> RRGetNodeWeak();
143 RR_WEAK_PTR<RobotRaconteurNode> node;
146struct ROBOTRACONTEUR_CORE_API PullServiceDefinitionReturn
148 RR_SHARED_PTR<ServiceDefinition> def;
149 RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> > attributes;
150 std::set<std::string> extra_imports;
153struct ROBOTRACONTEUR_CORE_API PullServiceDefinitionAndImportsReturn
155 std::vector<RR_SHARED_PTR<ServiceDefinition> > defs;
156 RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> > attributes;
157 std::set<std::string> extra_imports;
160class ROBOTRACONTEUR_CORE_API ClientContext :
public Endpoint,
public RR_ENABLE_SHARED_FROM_THIS<ClientContext>
163 RR_UNORDERED_MAP<MessageStringPtr, RR_SHARED_PTR<ServiceStub> > stubs;
164 boost::mutex stubs_lock;
165 std::list<std::string> active_stub_searches;
166 std::list<boost::tuple<std::string, boost::function<void(
const RR_SHARED_PTR<RRObject>&,
167 const RR_SHARED_PTR<RobotRaconteurException>&)> > >
168 active_stub_searches_handlers;
170 RR_SHARED_PTR<ServiceFactory> m_ServiceDef;
173 RR_SHARED_PTR<ServiceFactory> GetServiceDef()
const;
175 ClientContext(
const RR_SHARED_PTR<RobotRaconteurNode>& node);
177 ClientContext(
const RR_SHARED_PTR<ServiceFactory>& service_def,
const RR_SHARED_PTR<RobotRaconteurNode>& node);
179 RR_SHARED_PTR<RRObject> FindObjRef(boost::string_ref path, boost::string_ref objecttype =
"");
181 void AsyncFindObjRef(boost::string_ref path, boost::string_ref objecttype2,
182 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
183 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
186 template <
typename T>
187 static void EndAsyncFindObjRef(
188 const RR_SHARED_PTR<RRObject>& obj,
const RR_SHARED_PTR<RobotRaconteurException>& err,
189 const boost::function<
void(
const RR_SHARED_PTR<T>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler)
193 handler(RR_SHARED_PTR<T>(), err);
198 RR_SHARED_PTR<T> castobj = RR_DYNAMIC_POINTER_CAST<T>(obj);
201 handler(RR_SHARED_PTR<T>(), RR_MAKE_SHARED<DataTypeMismatchException>(
"Invalid object type returned"));
204 handler(castobj, RR_SHARED_PTR<RobotRaconteurException>());
209 void AsyncFindObjRef1(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
210 const std::string& path,
const std::string& objecttype2,
211 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
212 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
215 void AsyncFindObjRef2(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& ret,
216 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& objecttype,
217 const std::string& objectdef,
const std::string& path,
const std::string& objecttype2,
218 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
219 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
222 void AsyncFindObjRef3(
const RR_SHARED_PTR<RRObject>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
223 const std::string& path,
224 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
225 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
228 std::string FindObjectType(boost::string_ref path);
230 void AsyncFindObjectType(boost::string_ref path,
231 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<std::string>&,
232 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
235 bool VerifyObjectImplements(boost::string_ref objecttype, boost::string_ref implementstype);
238 void AsyncFindObjectType1(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
239 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& path,
240 const boost::function<
void(
const RR_SHARED_PTR<std::string>&,
241 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
244 boost::mutex pulled_service_defs_lock;
245 std::map<std::string, RR_SHARED_PTR<ServiceDefinition> > pulled_service_defs;
247 bool VerifyObjectImplements2(boost::string_ref objecttype, boost::string_ref implementstype);
249 struct outstanding_request
251 RR_INTRUSIVE_PTR<MessageEntry> ret;
252 RR_SHARED_PTR<AutoResetEvent> evt;
253 boost::function<void(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>&)>
255 RR_SHARED_PTR<Timer> timer;
258 boost::mutex outstanding_requests_lock;
259 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<outstanding_request> > outstanding_requests;
261 boost::mutex FindObjRef_lock;
264 RR_INTRUSIVE_PTR<MessageEntry> ProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
266 void AsyncProcessRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
267 RR_MOVE_ARG(boost::function<
void(
const RR_INTRUSIVE_PTR<MessageEntry>&,
268 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
271 using Endpoint::SendMessage;
272 void SendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
274 using Endpoint::AsyncSendMessage;
275 void AsyncSendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
276 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
279 void AsyncProcessRequest_err(
const RR_SHARED_PTR<RobotRaconteurException>& error, uint32_t requestid);
281 void AsyncProcessRequest_timeout(
const TimerEvent& error, uint32_t requestid);
283 uint32_t request_number;
285 RR_SHARED_PTR<Timer> connection_test_timer;
287 void connection_test(
const TimerEvent& ev);
290 RR_OVIRTUAL
void MessageReceived(
const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
293 void MessageEntryReceived(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
295 std::string m_ServiceName;
298 std::string GetServiceName()
const;
300 bool GetConnected()
const;
304 boost::mutex m_Connected_lock;
305 boost::mutex close_lock;
308 RR_SHARED_PTR<Transport> connecttransport;
309 std::string connecturl;
312 std::map<std::string, RR_INTRUSIVE_PTR<RRValue> > GetAttributes();
315 std::map<std::string, RR_INTRUSIVE_PTR<RRValue> > m_Attributes;
316 boost::mutex m_Attributes_lock;
319 RR_SHARED_PTR<RRObject> ConnectService(
const RR_SHARED_PTR<Transport>& c, boost::string_ref url,
320 boost::string_ref username =
"",
321 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials =
322 (RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >()),
323 boost::string_ref objecttype =
"");
325 RR_SHARED_PTR<RRObject> ConnectService(
const RR_SHARED_PTR<Transport>& c,
326 const RR_SHARED_PTR<ITransportConnection>& tc, boost::string_ref url,
327 boost::string_ref username =
"",
328 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials =
329 (RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >()),
330 boost::string_ref objecttype =
"");
332 void AsyncConnectService(
333 const RR_SHARED_PTR<Transport>& c, boost::string_ref url, boost::string_ref username,
334 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials, boost::string_ref objecttype,
336 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
340 void AsyncConnectService(
341 const RR_SHARED_PTR<Transport>& c,
const RR_SHARED_PTR<ITransportConnection>& tc, boost::string_ref url,
342 boost::string_ref username,
const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
343 boost::string_ref objecttype,
345 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
351 void AsyncConnectService1(
352 const RR_SHARED_PTR<Transport>& c,
const RR_SHARED_PTR<ITransportConnection>& tc,
353 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& url,
const std::string& username,
354 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
const std::string& objecttype,
355 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
357 void AsyncConnectService2(
358 const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
const RR_SHARED_PTR<RobotRaconteurException>& e,
359 const std::string& username,
const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
360 const std::string& objecttype,
361 boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
363 void AsyncConnectService3(
364 const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& e,
365 const std::string& username,
const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
366 const std::string& objecttype,
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
367 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>&
370 void AsyncConnectService4(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
371 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& username,
372 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
373 const std::string& objecttype,
const std::string& type,
374 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
375 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
377 void AsyncConnectService5(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
378 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& username,
379 const RR_INTRUSIVE_PTR<RRMap<std::string, RRValue> >& credentials,
380 const std::string& objecttype,
const std::string& type,
381 const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
382 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
383 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
385 void AsyncConnectService6(
const RR_SHARED_PTR<std::string>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& e,
386 const std::string& type,
const std::string& username,
387 const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& d,
388 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
389 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
391 void AsyncConnectService7(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
392 const RR_SHARED_PTR<RobotRaconteurException>& e,
const std::string& objecttype,
393 const std::string& username,
394 const boost::function<
void(
const RR_SHARED_PTR<RRObject>&,
395 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
397 boost::recursive_mutex connect_lock;
402 void AsyncClose(RR_MOVE_ARG(boost::function<
void()>) handler);
405 void AsyncClose1(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<RobotRaconteurException>& err,
406 const boost::function<
void()>& handler);
409 void AsyncSendPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
bool unreliable,
410 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
412 void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
414 PullServiceDefinitionReturn PullServiceDefinition(boost::string_ref ServiceType =
"");
416 void AsyncPullServiceDefinition(boost::string_ref ServiceType,
417 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionReturn>&,
418 const RR_SHARED_PTR<RobotRaconteurException>&)>)
423 void AsyncPullServiceDefinition1(
const RR_INTRUSIVE_PTR<MessageEntry>& ret3,
424 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& ServiceType,
425 boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionReturn>&,
426 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
429 PullServiceDefinitionAndImportsReturn PullServiceDefinitionAndImports(boost::string_ref servicetype =
"");
431 void AsyncPullServiceDefinitionAndImports(
432 boost::string_ref servicetype,
433 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>&,
434 const RR_SHARED_PTR<RobotRaconteurException>&)>) handler,
438 void AsyncPullServiceDefinitionAndImports1(
439 const RR_SHARED_PTR<PullServiceDefinitionReturn>& pull_ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
440 const std::string& servicetype,
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>& current,
441 boost::function<
void(
const RR_SHARED_PTR<PullServiceDefinitionAndImportsReturn>&,
442 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
443 boost::posix_time::ptime timeout_time);
446 std::vector<std::string> GetPulledServiceTypes();
448 RR_SHARED_PTR<ServiceFactory> GetPulledServiceType(boost::string_ref type);
451 boost::mutex pulled_service_types_lock;
452 RR_UNORDERED_MAP<std::string, RR_SHARED_PTR<ServiceFactory> > pulled_service_types;
454 bool use_pulled_types;
456 RR_SHARED_PTR<ITransportConnection> TransportConnection;
458 boost::mutex m_Authentication_lock;
460 bool m_UserAuthenticated;
463 const bool GetUserAuthenticated();
466 std::string m_AuthenticatedUsername;
469 const std::string GetAuthenticatedUsername();
471 std::string AuthenticateUser(boost::string_ref username,
const RR_INTRUSIVE_PTR<RRValue>& credentials);
473 void AsyncAuthenticateUser(
474 boost::string_ref username,
const RR_INTRUSIVE_PTR<RRValue>& credentials,
476 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
481 void AsyncAuthenticateUser2(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
482 const RR_SHARED_PTR<RobotRaconteurException>& err,
const std::string& username,
483 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
484 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
487 std::string LogoutUser();
489 std::string RequestObjectLock(
const RR_SHARED_PTR<RRObject>& obj, RobotRaconteurObjectLockFlags flags);
491 void AsyncRequestObjectLock(
492 const RR_SHARED_PTR<RRObject>& obj, RobotRaconteurObjectLockFlags flags,
494 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
499 void EndAsyncLockOp(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
const RR_SHARED_PTR<RobotRaconteurException>& err,
500 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
501 const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
504 std::string ReleaseObjectLock(
const RR_SHARED_PTR<RRObject>& obj);
506 void AsyncReleaseObjectLock(
507 const RR_SHARED_PTR<RRObject>& obj,
509 boost::function<
void(
const RR_SHARED_PTR<std::string>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
513 std::string MonitorEnter(
const RR_SHARED_PTR<RRObject>& obj, int32_t timeout);
515 void MonitorExit(
const RR_SHARED_PTR<RRObject>& obj);
517 RR_OVIRTUAL
void PeriodicCleanupTask() RR_OVERRIDE;
519 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();
537void 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
545using ServiceStubPtr = RR_SHARED_PTR<ServiceStub>;
546using ServiceStubConstPtr = RR_SHARED_PTR<const ServiceStub>;
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition RobotRaconteurConstants.h:566
Base class for all Robot Raconteur objects.
Definition DataTypes.h:228