32 #include <boost/atomic.hpp>
35 #pragma warning(disable : 4996)
36 #include <boost/signals2.hpp>
38 namespace RobotRaconteur
41 class ROBOTRACONTEUR_CORE_API ServerEndpoint;
42 class ROBOTRACONTEUR_CORE_API AuthenticatedUser;
43 class ROBOTRACONTEUR_CORE_API ServerContext;
44 class ROBOTRACONTEUR_CORE_API ServerContext_ObjectLock;
45 class ROBOTRACONTEUR_CORE_API ServerContext_MonitorObjectSkel;
47 class ROBOTRACONTEUR_CORE_API GeneratorServerBase;
49 class ROBOTRACONTEUR_CORE_API IRobotRaconteurMonitorObject;
75 virtual void RRServiceObjectInit(RR_WEAK_PTR<ServerContext> context,
const std::string& service_path) = 0;
79 class ROBOTRACONTEUR_CORE_API ServiceSkel :
public RR_ENABLE_SHARED_FROM_THIS<ServiceSkel>,
private boost::noncopyable
84 virtual ~ServiceSkel() {}
86 virtual void Init(boost::string_ref s,
const RR_SHARED_PTR<RRObject>& o,
const RR_SHARED_PTR<ServerContext>& c);
88 virtual void InitCallbackServers(
const RR_SHARED_PTR<RRObject>& o);
90 std::string GetServicePath()
const;
93 std::string m_ServicePath;
96 RR_SHARED_PTR<ServerContext> GetContext();
99 RR_WEAK_PTR<ServerContext> m_context;
101 RR_SHARED_PTR<RRObject> uncastobj;
103 std::vector<boost::tuple<RobotRaconteurVersion, std::string> > object_type_ver;
105 boost::unordered_map<int32_t, RR_SHARED_PTR<GeneratorServerBase> > generators;
106 boost::mutex generators_lock;
108 int32_t get_new_generator_index();
111 RR_SHARED_PTR<RRObject> GetUncastObject()
const;
113 virtual RR_INTRUSIVE_PTR<MessageEntry> CallGetProperty(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
115 virtual RR_INTRUSIVE_PTR<MessageEntry> CallSetProperty(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
117 virtual RR_INTRUSIVE_PTR<MessageEntry> CallFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
120 static void EndAsyncCallGetProperty(RR_WEAK_PTR<ServiceSkel> skel,
const RR_INTRUSIVE_PTR<MessageElement>& value,
121 const RR_SHARED_PTR<RobotRaconteurException>& err,
122 const RR_INTRUSIVE_PTR<MessageEntry>& m,
123 const RR_SHARED_PTR<ServerEndpoint>& ep);
125 static void EndAsyncCallSetProperty(RR_WEAK_PTR<ServiceSkel> skel,
126 const RR_SHARED_PTR<RobotRaconteurException>& err,
127 const RR_INTRUSIVE_PTR<MessageEntry>& m,
128 const RR_SHARED_PTR<ServerEndpoint>& ep);
130 static void EndAsyncCallFunction(RR_WEAK_PTR<ServiceSkel> skel,
const RR_INTRUSIVE_PTR<MessageElement>& ret,
131 const RR_SHARED_PTR<RobotRaconteurException>& err,
132 const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<ServerEndpoint>& ep);
135 virtual RR_SHARED_PTR<RRObject> GetSubObj(boost::string_ref name, boost::string_ref ind) = 0;
137 RR_SHARED_PTR<RRObject> GetSubObj(boost::string_ref name);
139 virtual void RegisterEvents(
const RR_SHARED_PTR<RRObject>& obj1);
141 virtual void UnregisterEvents(
const RR_SHARED_PTR<RRObject>& obj1);
143 virtual void InitPipeServers(
const RR_SHARED_PTR<RRObject>& obj1);
145 virtual void InitWireServers(
const RR_SHARED_PTR<RRObject>& obj1);
147 void ObjRefChanged(boost::string_ref name);
149 void SendEvent(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
151 void ReleaseObject();
153 virtual void ReleaseCastObject() = 0;
155 void AsyncSendPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e,
bool unreliable,
156 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>)
159 void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
161 virtual void DispatchPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
163 virtual void DispatchWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
165 virtual RR_INTRUSIVE_PTR<MessageEntry> CallPipeFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
167 virtual RR_INTRUSIVE_PTR<MessageEntry> CallWireFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
169 virtual RR_SHARED_PTR<void> GetCallbackFunction(uint32_t endpoint, boost::string_ref membername);
171 virtual RR_INTRUSIVE_PTR<MessageEntry> CallMemoryFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
172 const RR_SHARED_PTR<Endpoint>& e);
174 RR_WEAK_PTR<ServerContext_ObjectLock> objectlock;
175 boost::mutex objectlock_lock;
177 RR_SHARED_PTR<ServerContext_MonitorObjectSkel> monitorlock;
179 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ServerContext_MonitorObjectSkel> > monitorlocks;
180 boost::mutex monitorlocks_lock;
184 virtual bool IsRequestNoLock(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
186 bool IsMonitorLocked();
188 virtual std::string GetObjectType() = 0;
190 virtual std::string GetObjectType(
const RobotRaconteurVersion& client_version);
192 RR_SHARED_PTR<RobotRaconteurNode> RRGetNode();
193 RR_WEAK_PTR<RobotRaconteurNode> RRGetNodeWeak();
195 virtual void CallGeneratorNext(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<Endpoint>& e);
197 void SendGeneratorResponse(int32_t index,
const RR_INTRUSIVE_PTR<MessageEntry>& m,
198 const RR_SHARED_PTR<ServerEndpoint>& ep);
200 void CleanupGenerators();
203 RR_WEAK_PTR<RobotRaconteurNode> node;
206 class ROBOTRACONTEUR_CORE_API UserAuthenticator;
250 class ROBOTRACONTEUR_CORE_API
ServerContext :
public RR_ENABLE_SHARED_FROM_THIS<ServerContext>, boost::noncopyable
271 void SetAttributes(
const std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& attr);
274 std::map<std::string, RR_INTRUSIVE_PTR<RRValue> > m_Attributes;
275 boost::mutex m_Attributes_lock;
278 RR_SHARED_PTR<ServiceFactory> GetServiceDef()
const;
285 RR_SHARED_PTR<ServiceFactory> m_ServiceDef;
322 std::vector<std::string> extra_imports;
323 boost::mutex extra_imports_lock;
337 std::string m_ServiceName;
339 RR_UNORDERED_MAP<MessageStringPtr, RR_SHARED_PTR<ServiceSkel> > skels;
340 boost::mutex skels_lock;
342 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ServerEndpoint> > client_endpoints;
343 boost::mutex client_endpoints_lock;
349 std::string m_RootObjectType;
351 RR_WEAK_PTR<RobotRaconteurNode> node;
376 ServerContext(
const RR_SHARED_PTR<ServiceFactory>& f,
const RR_SHARED_PTR<RobotRaconteurNode>& node);
378 RR_SHARED_PTR<RobotRaconteurNode> GetNode();
380 virtual void SendEvent(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
382 virtual void SendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<Endpoint>& e);
384 virtual void SendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
386 virtual void AsyncSendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e,
387 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
389 virtual void AsyncSendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<Endpoint>& e,
390 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
392 virtual void AsyncSendUnreliableMessage(
393 const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<ServerEndpoint>& e,
394 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
396 virtual void AsyncSendPipeMessage(
397 const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e,
bool unreliable,
398 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
400 virtual void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
405 bool base_object_set;
421 virtual void SetBaseObject(
422 boost::string_ref name,
const RR_SHARED_PTR<RRObject>& o,
423 const RR_SHARED_PTR<ServiceSecurityPolicy>& policy = RR_SHARED_PTR<ServiceSecurityPolicy>());
425 virtual RR_SHARED_PTR<ServiceSkel> GetObjectSkel(MessageStringRef servicepath);
427 virtual void ReplaceObject(boost::string_ref path);
429 virtual std::string GetObjectType(MessageStringRef servicepath,
const RobotRaconteurVersion& client_version);
443 static boost::thread_specific_ptr<RR_SHARED_PTR<ServerContext> > m_CurrentServerContext;
459 static boost::thread_specific_ptr<std::string> m_CurrentServicePath;
462 virtual RR_INTRUSIVE_PTR<MessageEntry> ProcessMessageEntry(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
463 const RR_SHARED_PTR<ServerEndpoint>& c);
465 virtual void Close();
467 virtual void MessageReceived(
const RR_INTRUSIVE_PTR<Message>& m,
const RR_SHARED_PTR<ServerEndpoint>& e);
469 virtual void AddClient(
const RR_SHARED_PTR<ServerEndpoint>& cendpoint);
471 virtual void RemoveClient(
const RR_SHARED_PTR<ServerEndpoint>& cendpoint);
483 RR_SHARED_PTR<UserAuthenticator> user_authenticator;
484 std::map<std::string, std::string> security_policies;
486 bool m_RequireValidUser;
487 bool AllowObjectLock;
489 virtual RR_INTRUSIVE_PTR<MessageEntry> ClientSessionOp(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
490 const RR_SHARED_PTR<ServerEndpoint>& e);
504 virtual RR_SHARED_PTR<AuthenticatedUser> AuthenticateUser(
505 boost::string_ref username, std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& credentials,
506 const RR_SHARED_PTR<ServerEndpoint>& ep);
509 boost::mutex ClientLockOp_lockobj;
511 virtual void ClientLockOp(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_INTRUSIVE_PTR<MessageEntry>& ret);
554 void ReleaseObjectLock(boost::string_ref servicepath, boost::string_ref username,
bool override_);
565 void check_lock(
const RR_SHARED_PTR<ServiceSkel>& skel,
const RR_INTRUSIVE_PTR<MessageEntry>& m);
567 void check_monitor_lock(
const RR_SHARED_PTR<ServiceSkel>& skel);
569 RR_UNORDERED_MAP<std::string, RR_SHARED_PTR<ServerContext_ObjectLock> > active_object_locks;
572 virtual void PeriodicCleanupTask();
575 RR_INTRUSIVE_PTR<MessageEntry> CheckServiceCapability(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
576 const RR_SHARED_PTR<ServerEndpoint>& c);
580 const RR_SHARED_PTR<void>&)>
581 ServerServiceListener;
584 void ReleaseServicePath1(
const std::string& path);
623 void AsyncProcessCallbackRequest(
624 const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t endpoint,
626 boost::function<
void(
const RR_INTRUSIVE_PTR<MessageEntry>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
631 void AsyncProcessCallbackRequest_err(
const RR_SHARED_PTR<RobotRaconteurException>& error, uint32_t endpoint,
634 void AsyncProcessCallbackRequest_timeout(
const TimerEvent&, uint32_t endpoint, uint32_t requestid);
636 uint32_t request_number;
638 struct outstanding_request
640 RR_INTRUSIVE_PTR<MessageEntry> ret;
641 RR_SHARED_PTR<AutoResetEvent> evt;
642 boost::function<void(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
643 const RR_SHARED_PTR<RobotRaconteurException>& error)>
645 RR_SHARED_PTR<Timer> timer;
648 boost::mutex outstanding_requests_lock;
649 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<outstanding_request> > outstanding_requests;
652 RR_INTRUSIVE_PTR<MessageEntry> ProcessCallbackRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t endpointid);
685 RR_SHARED_PTR<ThreadPool> monitor_thread_pool;
686 boost::mutex monitor_thread_pool_lock;
732 class ROBOTRACONTEUR_CORE_API
ServerEndpoint :
public Endpoint,
public RR_ENABLE_SHARED_FROM_THIS<ServerEndpoint>
735 RR_SHARED_PTR<ServerContext> service;
738 static boost::thread_specific_ptr<RR_SHARED_PTR<ServerEndpoint> > m_CurrentEndpoint;
755 static boost::thread_specific_ptr<RR_SHARED_PTR<AuthenticatedUser> > m_CurrentAuthenticatedUser;
776 RR_SHARED_PTR<AuthenticatedUser> endpoint_authenticated_user;
779 const std::string GetAuthenticatedUsername()
const;
781 RR_OVIRTUAL
void MessageReceived(
const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
783 void AuthenticateUser(boost::string_ref username, std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& credentials);
787 RR_OVIRTUAL
void PeriodicCleanupTask() RR_OVERRIDE;
789 RR_OVIRTUAL
void SetTransportConnection(const RR_SHARED_PTR<ITransportConnection>& c) RR_OVERRIDE;
793 boost::mutex this_lock;
849 boost::mutex this_lock;
852 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
853 using ServiceSkelPtr = RR_SHARED_PTR<ServiceSkel>;
RobotRaconteur_LogLevel
Log level enum.
Definition: RobotRaconteurConstants.h:608
ServerServiceListenerEventType
Enum of service listener events.
Definition: RobotRaconteurConstants.h:518
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition: RobotRaconteurConstants.h:566
boost::shared_ptr< ServerContext > ServerContextPtr
Convenience alias for ServerContext shared_ptr.
Definition: Service.h:855
boost::shared_ptr< ServerEndpoint > ServerEndpointPtr
Convenience alias for ServerEndpoint shared_ptr.
Definition: Service.h:857
Interface for service objects to receive service notifications.
Definition: Service.h:64
virtual void RRServiceObjectInit(RR_WEAK_PTR< ServerContext > context, const std::string &service_path)=0
Function called after service object has been initialized.
Service object monitor lock notification.
Definition: Service.h:812
virtual void RobotRaconteurMonitorExit()=0
Release the thread-exclusive monitor lock.
virtual void RobotRaconteurMonitorEnter(int32_t timeout)=0
Request a thread-exclusive lock with timeout.
virtual void RobotRaconteurMonitorEnter()=0
Request a thread-exclusive lock without timeout.
Base class for default service object implementations.
Definition: Service.h:847
Base class for all Robot Raconteur objects.
Definition: DataTypes.h:227
The central node implementation.
Definition: RobotRaconteurNode.h:132
Robot Raconteur Version storage class.
Definition: ServiceDefinition.h:94
Context for services registered in a node for use by clients.
Definition: Service.h:251
std::map< std::string, boost::intrusive_ptr< RRValue > > GetAttributes()
Get the service attributes.
void RequestClientObjectLock(boost::string_ref servicepath, boost::string_ref username, uint32_t endpoint)
Request a client lock on servicepath for a specific client connection.
virtual bool RequireValidUser()
Return if a valid user is required.
std::vector< std::string > GetExtraImports()
Get the current vector of extra service definition imports.
void ReleaseObjectLock(boost::string_ref servicepath, boost::string_ref username, bool override_)
Release a client lock on servicepath.
int32_t GetMonitorThreadPoolCount()
Get the number of threads used by the monitor thread pool.
void SetAttributes(const std::map< std::string, boost::intrusive_ptr< RRValue > > &attr)
Set the service attributes.
void RequestObjectLock(boost::string_ref servicepath, boost::string_ref username)
Request an object lock on servicepath for user username.
ServerContext(const boost::shared_ptr< ServiceFactory > &f, const boost::shared_ptr< RobotRaconteurNode > &node)
Construct a new ServerContext instance.
void LogCandidateConnectionURLs(RobotRaconteur_LogLevel level=RobotRaconteur_LogLevel_Info)
Log the candidate connection URLs for this service.
boost::shared_ptr< ThreadPool > GetMonitorThreadPool()
Get the monitor lock thread pool.
void SetSecurityPolicy(const boost::shared_ptr< ServiceSecurityPolicy > &policy)
Set the security policy of the service.
void ReleaseServicePath(boost::string_ref path, const std::vector< uint32_t > &endpoints)
Release the specified service path and all sub objects.
std::string GetObjectLockUsername(boost::string_ref servicepath)
Get the name of the user that has locked the specified service path.
void SetMonitorThreadPoolCount(int32_t count)
Set the monitor lock thread pool count.
void ReleaseServicePath(boost::string_ref path)
Release the specified service path and all sub objects.
std::string GetServiceName() const
Get the name of the service.
static boost::shared_ptr< ServerContext > GetCurrentServerContext()
Get the current ServerContext.
static std::string GetCurrentServicePath()
Get the current object service path.
void AddExtraImport(boost::string_ref import_)
Add an extra service definition import.
void PrintCandidateConnectionURLs(std::ostream &out=std::cout)
Print the candidate connection URLs for this service.
virtual void KickUser(boost::string_ref username)
Kicks a user with the specified username.
std::vector< std::string > GetCandidateConnectionURLs()
Get the candidate connection URLs for this service.
bool RemoveExtraImport(boost::string_ref import_)
Removes an extra import service definition registered with AddExtraImport()
Server endpoint representing a client connection.
Definition: Service.h:733
static boost::shared_ptr< ServerEndpoint > GetCurrentEndpoint()
Returns the current server endpoint.
static boost::shared_ptr< AuthenticatedUser > GetCurrentAuthenticatedUser()
Returns the current authenticated user.
Timer event structure.
Definition: Timer.h:46