32#include <boost/atomic.hpp>
36#pragma warning(disable : 4996)
37#include <boost/signals2.hpp>
40namespace RobotRaconteur
46class ROBOTRACONTEUR_CORE_API ServerContext_ObjectLock;
47class ROBOTRACONTEUR_CORE_API ServerContext_MonitorObjectSkel;
49class ROBOTRACONTEUR_CORE_API GeneratorServerBase;
77 virtual void RRServiceObjectInit(RR_WEAK_PTR<ServerContext> context,
const std::string& service_path) = 0;
81class ROBOTRACONTEUR_CORE_API ServiceSkel :
public RR_ENABLE_SHARED_FROM_THIS<ServiceSkel>,
private boost::noncopyable
86 virtual ~ServiceSkel() {}
88 virtual void Init(boost::string_ref s,
const RR_SHARED_PTR<RRObject>& o,
const RR_SHARED_PTR<ServerContext>& c);
90 virtual void InitCallbackServers(
const RR_SHARED_PTR<RRObject>& o);
92 std::string GetServicePath()
const;
95 std::string m_ServicePath;
98 RR_SHARED_PTR<ServerContext> GetContext();
101 RR_WEAK_PTR<ServerContext> m_context;
103 RR_SHARED_PTR<RRObject> uncastobj;
105 std::vector<boost::tuple<RobotRaconteurVersion, std::string> > object_type_ver;
107 boost::unordered_map<int32_t, RR_SHARED_PTR<GeneratorServerBase> > generators;
108 boost::mutex generators_lock;
110 int32_t get_new_generator_index();
113 RR_SHARED_PTR<RRObject> GetUncastObject()
const;
115 virtual RR_INTRUSIVE_PTR<MessageEntry> CallGetProperty(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
117 virtual RR_INTRUSIVE_PTR<MessageEntry> CallSetProperty(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
119 virtual RR_INTRUSIVE_PTR<MessageEntry> CallFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
122 static void EndAsyncCallGetProperty(RR_WEAK_PTR<ServiceSkel> skel,
const RR_INTRUSIVE_PTR<MessageElement>& value,
123 const RR_SHARED_PTR<RobotRaconteurException>& err,
124 const RR_INTRUSIVE_PTR<MessageEntry>& m,
125 const RR_SHARED_PTR<ServerEndpoint>& ep);
127 static void EndAsyncCallSetProperty(RR_WEAK_PTR<ServiceSkel> skel,
128 const RR_SHARED_PTR<RobotRaconteurException>& err,
129 const RR_INTRUSIVE_PTR<MessageEntry>& m,
130 const RR_SHARED_PTR<ServerEndpoint>& ep);
132 static void EndAsyncCallFunction(RR_WEAK_PTR<ServiceSkel> skel,
const RR_INTRUSIVE_PTR<MessageElement>& ret,
133 const RR_SHARED_PTR<RobotRaconteurException>& err,
134 const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<ServerEndpoint>& ep);
137 virtual RR_SHARED_PTR<RRObject> GetSubObj(boost::string_ref name, boost::string_ref ind) = 0;
139 RR_SHARED_PTR<RRObject> GetSubObj(boost::string_ref name);
141 virtual void RegisterEvents(
const RR_SHARED_PTR<RRObject>& obj1);
143 virtual void UnregisterEvents(
const RR_SHARED_PTR<RRObject>& obj1);
145 virtual void InitPipeServers(
const RR_SHARED_PTR<RRObject>& obj1);
147 virtual void InitWireServers(
const RR_SHARED_PTR<RRObject>& obj1);
149 void ObjRefChanged(boost::string_ref name);
151 void SendEvent(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
153 void ReleaseObject();
155 virtual void ReleaseCastObject() = 0;
157 void AsyncSendPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e,
bool unreliable,
158 RR_MOVE_ARG(boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>)
161 void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
163 virtual void DispatchPipeMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
165 virtual void DispatchWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
167 virtual RR_INTRUSIVE_PTR<MessageEntry> CallPipeFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
169 virtual RR_INTRUSIVE_PTR<MessageEntry> CallWireFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
171 virtual RR_SHARED_PTR<void> GetCallbackFunction(uint32_t endpoint, boost::string_ref membername);
173 virtual RR_INTRUSIVE_PTR<MessageEntry> CallMemoryFunction(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
174 const RR_SHARED_PTR<Endpoint>& e);
176 RR_WEAK_PTR<ServerContext_ObjectLock> objectlock;
177 boost::mutex objectlock_lock;
179 RR_SHARED_PTR<ServerContext_MonitorObjectSkel> monitorlock;
181 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ServerContext_MonitorObjectSkel> > monitorlocks;
182 boost::mutex monitorlocks_lock;
186 virtual bool IsRequestNoLock(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
188 bool IsMonitorLocked();
190 virtual std::string GetObjectType() = 0;
192 virtual std::string GetObjectType(
const RobotRaconteurVersion& client_version);
194 RR_SHARED_PTR<RobotRaconteurNode> RRGetNode();
195 RR_WEAK_PTR<RobotRaconteurNode> RRGetNodeWeak();
197 virtual void CallGeneratorNext(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<Endpoint>& e);
199 void SendGeneratorResponse(int32_t index,
const RR_INTRUSIVE_PTR<MessageEntry>& m,
200 const RR_SHARED_PTR<ServerEndpoint>& ep);
202 void CleanupGenerators();
205 RR_WEAK_PTR<RobotRaconteurNode> node;
252class ROBOTRACONTEUR_CORE_API
ServerContext :
public RR_ENABLE_SHARED_FROM_THIS<ServerContext>, boost::noncopyable
273 void SetAttributes(
const std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& attr);
276 std::map<std::string, RR_INTRUSIVE_PTR<RRValue> > m_Attributes;
277 boost::mutex m_Attributes_lock;
280 RR_SHARED_PTR<ServiceFactory> GetServiceDef()
const;
287 RR_SHARED_PTR<ServiceFactory> m_ServiceDef;
324 std::vector<std::string> extra_imports;
325 boost::mutex extra_imports_lock;
339 std::string m_ServiceName;
341 RR_UNORDERED_MAP<MessageStringPtr, RR_SHARED_PTR<ServiceSkel> > skels;
342 boost::mutex skels_lock;
344 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ServerEndpoint> > client_endpoints;
345 boost::mutex client_endpoints_lock;
351 std::string m_RootObjectType;
353 RR_WEAK_PTR<RobotRaconteurNode> node;
373 ServerContext(
const RR_SHARED_PTR<ServiceFactory>& f,
const RR_SHARED_PTR<RobotRaconteurNode>& node);
375 RR_SHARED_PTR<RobotRaconteurNode> GetNode();
377 virtual void SendEvent(
const RR_INTRUSIVE_PTR<MessageEntry>& m);
379 virtual void SendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<Endpoint>& e);
381 virtual void SendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
383 virtual void AsyncSendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e,
384 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
386 virtual void AsyncSendMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<Endpoint>& e,
387 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
389 virtual void AsyncSendUnreliableMessage(
390 const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_SHARED_PTR<ServerEndpoint>& e,
391 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
393 virtual void AsyncSendPipeMessage(
394 const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e,
bool unreliable,
395 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
397 virtual void SendWireMessage(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t e);
400 bool base_object_set;
416 virtual void SetBaseObject(
417 boost::string_ref name,
const RR_SHARED_PTR<RRObject>& o,
418 const RR_SHARED_PTR<ServiceSecurityPolicy>& policy = RR_SHARED_PTR<ServiceSecurityPolicy>());
420 virtual RR_SHARED_PTR<ServiceSkel> GetObjectSkel(MessageStringRef servicepath);
422 virtual void ReplaceObject(boost::string_ref path);
424 virtual std::string GetObjectType(MessageStringRef servicepath,
const RobotRaconteurVersion& client_version);
438 static boost::thread_specific_ptr<RR_SHARED_PTR<ServerContext> > m_CurrentServerContext;
454 static boost::thread_specific_ptr<std::string> m_CurrentServicePath;
457 virtual RR_INTRUSIVE_PTR<MessageEntry> ProcessMessageEntry(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
458 const RR_SHARED_PTR<ServerEndpoint>& c);
460 virtual void Close();
462 virtual void MessageReceived(
const RR_INTRUSIVE_PTR<Message>& m,
const RR_SHARED_PTR<ServerEndpoint>& e);
464 virtual void AddClient(
const RR_SHARED_PTR<ServerEndpoint>& cendpoint);
466 virtual void RemoveClient(
const RR_SHARED_PTR<ServerEndpoint>& cendpoint);
478 RR_SHARED_PTR<UserAuthenticator> user_authenticator;
479 std::map<std::string, std::string> security_policies;
481 bool m_RequireValidUser;
482 bool AllowObjectLock;
484 virtual RR_INTRUSIVE_PTR<MessageEntry> ClientSessionOp(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
485 const RR_SHARED_PTR<ServerEndpoint>& e);
499 virtual RR_SHARED_PTR<AuthenticatedUser> AuthenticateUser(
500 boost::string_ref username, std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& credentials,
501 const RR_SHARED_PTR<ServerEndpoint>& ep);
504 boost::mutex ClientLockOp_lockobj;
506 virtual void ClientLockOp(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
const RR_INTRUSIVE_PTR<MessageEntry>& ret);
549 void ReleaseObjectLock(boost::string_ref servicepath, boost::string_ref username,
bool override_);
560 void check_lock(
const RR_SHARED_PTR<ServiceSkel>& skel,
const RR_INTRUSIVE_PTR<MessageEntry>& m);
562 void check_monitor_lock(
const RR_SHARED_PTR<ServiceSkel>& skel);
564 RR_UNORDERED_MAP<std::string, RR_SHARED_PTR<ServerContext_ObjectLock> > active_object_locks;
567 virtual void PeriodicCleanupTask();
570 RR_INTRUSIVE_PTR<MessageEntry> CheckServiceCapability(
const RR_INTRUSIVE_PTR<MessageEntry>& m,
571 const RR_SHARED_PTR<ServerEndpoint>& c);
575 const RR_SHARED_PTR<void>&)>
576 ServerServiceListener;
579 void ReleaseServicePath1(
const std::string& path);
618 void AsyncProcessCallbackRequest(
619 const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t endpoint,
621 boost::function<
void(
const RR_INTRUSIVE_PTR<MessageEntry>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>)
626 void AsyncProcessCallbackRequest_err(
const RR_SHARED_PTR<RobotRaconteurException>& error, uint32_t endpoint,
629 void AsyncProcessCallbackRequest_timeout(
const TimerEvent&, uint32_t endpoint, uint32_t requestid);
631 uint32_t request_number;
633 struct outstanding_request
635 RR_INTRUSIVE_PTR<MessageEntry> ret;
636 RR_SHARED_PTR<AutoResetEvent> evt;
637 boost::function<void(
const RR_INTRUSIVE_PTR<MessageEntry>& ret,
638 const RR_SHARED_PTR<RobotRaconteurException>& error)>
640 RR_SHARED_PTR<Timer> timer;
643 boost::mutex outstanding_requests_lock;
644 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<outstanding_request> > outstanding_requests;
647 RR_INTRUSIVE_PTR<MessageEntry> ProcessCallbackRequest(
const RR_INTRUSIVE_PTR<MessageEntry>& m, uint32_t endpointid);
680 RR_SHARED_PTR<ThreadPool> monitor_thread_pool;
681 boost::mutex monitor_thread_pool_lock;
727class ROBOTRACONTEUR_CORE_API ServerEndpoint :
public Endpoint,
public RR_ENABLE_SHARED_FROM_THIS<ServerEndpoint>
730 RR_SHARED_PTR<ServerContext> service;
733 static boost::thread_specific_ptr<RR_SHARED_PTR<ServerEndpoint> > m_CurrentEndpoint;
750 static boost::thread_specific_ptr<RR_SHARED_PTR<AuthenticatedUser> > m_CurrentAuthenticatedUser;
771 RR_SHARED_PTR<AuthenticatedUser> endpoint_authenticated_user;
774 const std::string GetAuthenticatedUsername()
const;
776 RR_OVIRTUAL
void MessageReceived(
const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
778 void AuthenticateUser(boost::string_ref username, std::map<std::string, RR_INTRUSIVE_PTR<RRValue> >& credentials);
782 RR_OVIRTUAL
void PeriodicCleanupTask() RR_OVERRIDE;
784 RR_OVIRTUAL
void SetTransportConnection(const RR_SHARED_PTR<ITransportConnection>& c) RR_OVERRIDE;
788 boost::mutex this_lock;
844 boost::mutex this_lock;
847#ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
848using ServiceSkelPtr = RR_SHARED_PTR<ServiceSkel>;
RobotRaconteur_LogLevel
Log level enum.
Definition RobotRaconteurConstants.h:608
@ RobotRaconteur_LogLevel_Info
info log level
Definition RobotRaconteurConstants.h:614
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< ServerEndpoint > ServerEndpointPtr
Convenience alias for ServerEndpoint shared_ptr.
Definition Service.h:852
boost::shared_ptr< ServerContext > ServerContextPtr
Convenience alias for ServerContext shared_ptr.
Definition Service.h:850
Class representing an authenticated user.
Definition Security.h:87
Interface for service objects to receive service notifications.
Definition Service.h:66
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:807
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:842
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:253
void RequestClientObjectLock(boost::string_ref servicepath, boost::string_ref username, uint32_t endpoint)
Request a client lock on servicepath for a specific client connection.
std::vector< std::string > GetExtraImports()
Get the current vector of extra service definition imports.
std::map< std::string, boost::intrusive_ptr< RRValue > > GetAttributes()
Get the service attributes.
virtual bool RequireValidUser()
Return if a valid user is required.
void ReleaseObjectLock(boost::string_ref servicepath, boost::string_ref username, bool override_)
Release a client lock on servicepath.
static boost::shared_ptr< ServerContext > GetCurrentServerContext()
Get the current ServerContext.
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.
void SetSecurityPolicy(const boost::shared_ptr< ServiceSecurityPolicy > &policy)
Set the security policy of the service.
std::vector< std::string > GetCandidateConnectionURLs()
Get the candidate connection URLs for this 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 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.
boost::shared_ptr< ThreadPool > GetMonitorThreadPool()
Get the monitor lock thread pool.
virtual void KickUser(boost::string_ref username)
Kicks a user with the specified username.
bool RemoveExtraImport(boost::string_ref import_)
Removes an extra import service definition registered with AddExtraImport().
Server endpoint representing a client connection.
Definition Service.h:728
static boost::shared_ptr< ServerEndpoint > GetCurrentEndpoint()
Returns the current server endpoint.
static boost::shared_ptr< AuthenticatedUser > GetCurrentAuthenticatedUser()
Returns the current authenticated user.
Base class for user authenticators.
Definition Security.h:151
Timer event structure.
Definition Timer.h:55