47 #ifndef _MIRA_RPCMANAGER_H_ 48 #define _MIRA_RPCMANAGER_H_ 144 template<
typename Reflector>
147 r.interface(
"IRPCManager");
149 "Get all registered local services");
151 "Returns a reference to the local service object with the given name",
152 "name",
"name of the service",
"MyService");
154 "Get all registered remote services");
156 "Returns a reference to the remote service object with the given name",
157 "name",
"name of the service",
"MyService");
159 "Returns true, if a service with the given name exists, otherwise false",
160 "name",
"name of the service",
"MyService");
162 "Returns a string list with the names of all registered services (local" 163 "and remote), that implement the specified interface",
164 "name",
"name of the interface",
"MyInterface");
166 "Returns if the given service implements a certain interface",
167 "name",
"name of the service",
"MyService",
168 "interface",
"name of the interface",
"MyInterface");
179 template <
typename Service>
183 boost::mutex::scoped_lock lock(mServiceManagementMutex);
187 std::set<RPCSignature> addedMethods;
188 std::set<std::string> addedInterfaces;
190 service, &addedMethods, &addedInterfaces);
196 std::string method = name +
"." + s.
name;
197 auto it = mLocalServiceMethods.find(method);
198 if(it == mLocalServiceMethods.end())
199 mLocalServiceMethods.insert(std::make_pair(method, handler));
201 if(it->second != handler)
202 MIRA_THROW(XLogical,
"The service method '" << method <<
203 "' was already registered with a different handler");
208 foreach(
const std::string& ifc, addedInterfaces)
209 handleCallbacks(ifc, name);
211 mLocalServices.insert(name);
248 uint8 binaryFormatVersion = BinaryBufferSerializer::getSerializerFormatVersion());
294 boost::mutex::scoped_lock lock(mServiceManagementMutex);
301 for(
auto it=mRemoteServices.begin(); it!=mRemoteServices.end(); ++it)
302 if(it->second.name==name)
335 boost::mutex::scoped_lock lock(mServiceManagementMutex);
341 for(
auto it=mRemoteServices.begin(); it!=mRemoteServices.end(); ++it)
343 if(it->second.interfaces.count(interface)!=0)
344 res.push_back(it->second.name);
354 boost::mutex::scoped_lock lock(mServiceManagementMutex);
360 auto it = mRemoteServices.find(name);
361 if (it == mRemoteServices.end())
363 return it->second.interfaces.count(interface) > 0;
410 template<
typename R,
typename... ARGS>
414 boost::mutex::scoped_lock lock(mServiceManagementMutex);
415 if (mLocalServices.count(service) > 0) {
419 request, service, method, forwardRPCParams(std::forward<ARGS>(args))...);
420 handleLocalRequest<BinaryRPCBackend>(std::move(buffer), service);
421 return std::move(future);
424 auto it = mRemoteServices.find(service);
425 if (it == mRemoteServices.end()) {
427 "Unable to call method '" << method <<
"' for service '" << service
428 <<
"'. The requested service is not known.");
432 if (it->second.binaryFormatVersion == BinaryBufferSerializer::getSerializerFormatVersion()) {
435 forwardRPCParams(std::forward<ARGS>(args))...);
437 else if (it->second.binaryFormatVersion == 0) {
440 request, service, method, forwardRPCParams(std::forward<ARGS>(args))...);
443 MIRA_THROW(
XRPC,
"Requested service expects binary format version " 444 << (
int)it->second.binaryFormatVersion
445 <<
". RPCManager::call() only implemented for versions 0, " 446 << (
int)BinaryBufferSerializer::getSerializerFormatVersion());
448 it->second.handler->onRPCrequested(std::move(buffer));
450 boost::mutex::scoped_lock lock2(mPendingRequestsMutex);
451 mPendingRemoteRequests[future.
callId()] = service;
452 return std::move(future);
501 const std::string& method,
516 const std::string& method,
517 const std::string& params);
535 const std::string& callID,
547 template <
typename Backend>
549 const std::string& service);
561 template <
typename Backend>
572 void handleCallbacks(
const std::string& interface,
573 const std::string& servicename);
587 struct AbstractRequestContext
590 handler(iHandler), remote(iRemote){}
592 virtual ~AbstractRequestContext() {}
594 virtual void onFinished(RPCClient& client) = 0;
600 typedef boost::shared_ptr<AbstractRequestContext> AbstractRequestContextPtr;
608 template<
typename Backend>
609 struct RequestContext;
619 T&& forwardRPCParams(T&& p) {
return std::forward<T>(p); }
621 std::string forwardRPCParams(
const char* p) {
return std::string(p); }
640 uint8 iBinaryFormatVersion = BinaryBufferSerializer::getSerializerFormatVersion()) :
642 binaryFormatVersion(iBinaryFormatVersion) {}
645 uint8 binaryFormatVersion;
649 mutable boost::mutex mServiceManagementMutex;
655 std::map<std::string, AbstractRPCHandlerPtr> mLocalServiceMethods;
658 std::set<std::string> mLocalServices;
661 std::map<std::string, RemoteServiceDetail> mRemoteServices;
664 mutable boost::mutex mPendingRequestsMutex;
666 std::map<AbstractDeferredInvoker*, AbstractRequestContextPtr> mPendingRequests;
668 std::map<std::string, std::string> mPendingRemoteRequests;
671 mutable boost::mutex mCallbacksMutex;
673 std::multimap<std::string, AbstractInterfaceCallbackHandlerPtr> mInterfaceCallbacks;
boost::shared_ptr< RemoteFinishHandler > RemoteFinishHandlerPtr
Definition: RPCManager.h:139
static Duration invalid()
Returns an invalid duration.
Definition: Time.h:249
boost::shared_ptr< AbstractRPCHandler > AbstractRPCHandlerPtr
Definition: AbstractRPCHandler.h:92
Contains all available information about a single RPC service, including the service' name...
Definition: RPCServer.h:301
Abstract interface for handler(s) that get called upon new services with a special interface get avai...
bool implementsInterface(const std::string &name, const std::string &interface) const
Returns if the given service implements a certain interface.
Definition: RPCServer.h:648
void handleRemoteResponse(Buffer< uint8 > buffer)
Handles the response on a previous remote request, that was sent through a RemoteRequestHandler.
Server side implementation of RPC calls.
BinaryRPCBackendTempl< 2 > BinaryRPCBackend
Definition: BinaryRPCBackend.h:437
virtual void onRPCrequested(Buffer< uint8 > &&request)=0
Implementation of the client side of an RPC call.
Handler that must be implemented by the remote module to send RPC responses to a remote server which ...
Definition: RPCManager.h:133
bool existsService(const std::string &name) const
Returns true, if a service with the specified name exists, otherwise false.
Definition: RPCServer.h:622
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void removeLocalService(const std::string &name)
Remove the local service.
virtual ~RemoteRequestHandler()
Definition: RPCManager.h:114
Abstract interface for DeferredInvoker which is a class to support different RPC backends.
Definition: AbstractDeferredInvoker.h:80
RPCCallDefinition for storing all information required to call a service method.
void reflect(Reflector &r)
Reflect method for serialization.
Definition: RPCManager.h:145
Binary client-side request.
Definition: BinaryRPCBackend.h:228
BinaryRPCBackendTempl< 0 > BinaryRPCBackendLegacy
Definition: BinaryRPCBackend.h:436
An RPCFuture is a proxy for the result of an asynchronous RPC call.
Definition: RPCFuture.h:173
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
std::list< std::string > queryServicesForInterface(const std::string &interface) const
Returns a string list with the names of all registered services (local and remote), that implement the specified interface.
Definition: RPCManager.h:333
const Service & registerServiceObject(const std::string &serviceName, T &serviceObject, std::set< RPCSignature > *addedMethods=NULL, std::set< std::string > *addedInterfaces=NULL)
Registers the methods of the specified service object under the specified service name...
Definition: RPCServer.h:551
Provides RPC SFINAE helper to check whether a provided description parameter pack is valid...
std::string waitForServiceInterface(const std::string &interface, Duration timeout=Duration::invalid()) const
Waits until a service with the specified interface becomes available.
An exception that is thrown by the RPCServer if an RPC call fails.
Definition: RPCError.h:76
RPCFuture< JSONRPCResponse > callJSON(const json::Value &jsonCall)
Invokes a JSON RPC call.
bool implementsInterface(const std::string &name, const std::string &interface) const
Returns if the given service implements a certain interface.
Definition: RPCManager.h:352
bool waitForService(const std::string &name, Duration timeout=Duration::invalid()) const
Waits until a service with the specified name becomes available.
This is a specialization for JSON RPC calls.
Definition: RPCFuture.h:315
Use this class to represent time durations.
Definition: Time.h:106
void addRemoteService(const RemoteService &serviceInfo, RemoteRequestHandlerPtr handler, uint8 binaryFormatVersion=BinaryBufferSerializer::getSerializerFormatVersion())
Adds a remote service.
void handleRemoteRequest(Buffer< uint8 > buffer, RemoteFinishHandlerPtr handler=RemoteFinishHandlerPtr())
Request to handle the given remote request, whose content is stored in the specified buffer...
RPCFuture< R > call(const std::string &service, const std::string &method, ARGS &&... args)
Method that is used by local C++ clients for requesting an rpc call.
Definition: RPCManager.h:411
Abstract interface for DeferredInvoker.
Abstract interface for RPCHandler(s).
std::string name
The method's name.
Definition: RPCSignature.h:130
std::set< std::string > getLocalServices() const
Get all registered local services.
void handleRequest(typename Backend::ContainerType buffer, RemoteFinishHandlerPtr finishHandler, bool remote)
Internally used, to handle incoming rpc requests.
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:174
virtual void onRPCfinished(AbstractDeferredInvoker *invoker)
called upon finish of the RPC call, the ID of the call is passed as parameter.
void handleLocalRequest(typename Backend::ContainerType buffer, const std::string &service)
Internally used, to handle rpc requests for services that are also maintained by this local RPCManage...
void unregisterCallback(const std::string &interface, AbstractInterfaceCallbackHandlerPtr callback)
Unregisters a callback handler for a specific interface.
std::list< std::string > queryServicesForInterface(const std::string &interface) const
Returns a string list with the names of all registered services, that implement the specified interfa...
Definition: RPCServer.h:633
boost::shared_ptr< RemoteRequestHandler > RemoteRequestHandlerPtr
Definition: RPCManager.h:117
std::set< std::string > getRemoteServices() const
Get all registered remote services.
void addLocalService(const std::string &name, Service &service, AbstractRPCHandlerPtr handler)
Adds a local service.
Definition: RPCManager.h:180
void removeRemoteService(const std::string &name)
Remove a remote service.
bool existsService(const std::string &name) const
Returns true, if a service with the given name exists, otherwise false.
Definition: RPCManager.h:292
RPCFuture< R > call(typename Backend::ClientRequest &request, const std::string &service, const std::string &method, ARGS &&... args)
Generates an RPCRequest for an RPC call.
Definition: RPCClient.h:218
virtual ~RemoteFinishHandler()
Definition: RPCManager.h:136
const RemoteService & getRemoteService(const std::string &name) const
Returns a reference to the remote service info object with the given name.
virtual void onRPCfinished(Buffer< uint8 > &&response)=0
const std::string & callId()
query call ID
Definition: RPCFuture.h:87
void registerCallback(const std::string &interface, AbstractInterfaceCallbackHandlerPtr callback)
Registers a callback handler that is called whenever a service registers the interface, which is specified as first parameter.
const RPCServer::Service & getLocalService(const std::string &name) const
Returns a reference to the local service object with the given name.
Definition: RPCManager.h:229
Handler that is called when a deferred RPC call was executed and finished and therefore when the resp...
Definition: AbstractDeferredInvoker.h:67
boost::shared_ptr< AbstractInterfaceCallbackHandler > AbstractInterfaceCallbackHandlerPtr
Definition: AbstractInterfaceCallbackHandler.h:76
Stores info required to call an RPC method - service name, method name, arguments.
Definition: RPCCallDefinition.h:62
This class is for internal use only.
Definition: RPCManager.h:96
Stores the signature of an RPC method including the methods name and its parameter types...
Definition: RPCSignature.h:64
RPCServer::ServiceInfo< RPCServer::MethodInfoSet > RemoteService
Definition: RPCManager.h:235
const Service & getService(const std::string &name) const
Returns a reference to the service with the given name.
Definition: RPCServer.h:609
RPCFuture< JSONRPCResponse > callJSONIntern(const std::string &service, const std::string &callID, const json::Value &request)
Internally doing a JSON RPC call.
Handler that must be implemented by the remote module to send RPC requests to a remote server...
Definition: RPCManager.h:111