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);
217 void removeLocalService(
const std::string& name);
222 std::set<std::string> getLocalServices()
const;
230 return mServer.getService(name);
248 uint8 binaryFormatVersion = BinaryBufferSerializer::getSerializerFormatVersion());
253 void removeRemoteService(
const std::string& name);
258 std::set<std::string> getRemoteServices()
const;
265 const RemoteService& getRemoteService(
const std::string& name)
const;
275 void registerCallback(
const std::string& interface,
281 void unregisterCallback(
const std::string& interface,
294 boost::mutex::scoped_lock lock(mServiceManagementMutex);
297 if(mServer.existsService(name))
301 for(
auto it=mRemoteServices.begin(); it!=mRemoteServices.end(); ++it)
302 if(it->second.name==name)
315 bool waitForService(
const std::string& name,
326 std::string waitForServiceInterface(
const std::string& interface,
335 boost::mutex::scoped_lock lock(mServiceManagementMutex);
338 std::list<std::string> res = mServer.queryServicesForInterface(interface);
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);
356 if (mServer.implementsInterface(name, interface))
360 auto it = mRemoteServices.find(name);
361 if (it == mRemoteServices.end())
363 return it->second.interfaces.count(interface) > 0;
375 RemoteFinishHandlerPtr handler = RemoteFinishHandlerPtr());
385 RemoteFinishHandlerPtr handler, uint8 binaryFormatVersion);
398 void handleRemoteResponse(
Buffer<uint8> buffer, uint8 binaryFormatVersion);
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()) {
426 MIRA_THROW(
XRPC,
"The requested service '" << service <<
"' is not known.");
430 if (it->second.binaryFormatVersion == BinaryBufferSerializer::getSerializerFormatVersion()) {
433 forwardRPCParams(std::forward<ARGS>(args))...);
435 else if (it->second.binaryFormatVersion == 0) {
438 request, service, method, forwardRPCParams(std::forward<ARGS>(args))...);
441 MIRA_THROW(
XRPC,
"Requested service expects binary format version " 442 << (
int)it->second.binaryFormatVersion
443 <<
". RPCManager::call() only implemented for versions 0, " 444 << (
int)BinaryBufferSerializer::getSerializerFormatVersion());
446 it->second.handler->onRPCrequested(std::move(buffer));
448 boost::mutex::scoped_lock lock2(mPendingRequestsMutex);
449 mPendingRemoteRequests[future.
callId()] = service;
450 return std::move(future);
499 const std::string& method,
514 const std::string& method,
515 const std::string& params);
533 const std::string& callID,
545 template <
typename Backend>
546 void handleLocalRequest(
typename Backend::ContainerType buffer,
547 const std::string& service);
559 template <
typename Backend>
560 void handleRequest(
typename Backend::ContainerType buffer,
561 RemoteFinishHandlerPtr finishHandler,
bool remote);
570 void handleCallbacks(
const std::string& interface,
571 const std::string& servicename);
585 struct AbstractRequestContext
587 AbstractRequestContext(RemoteFinishHandlerPtr iHandler,
bool iRemote) :
588 handler(iHandler), remote(iRemote){}
590 virtual ~AbstractRequestContext() {}
592 virtual void onFinished(RPCClient& client) = 0;
595 RemoteFinishHandlerPtr handler;
598 typedef boost::shared_ptr<AbstractRequestContext> AbstractRequestContextPtr;
606 template<
typename Backend>
607 struct RequestContext;
617 T&& forwardRPCParams(T&& p) {
return std::forward<T>(p); }
619 std::string forwardRPCParams(
const char* p) {
return std::string(p); }
634 struct RemoteServiceDetail :
public RemoteService
636 RemoteServiceDetail(
const RemoteService& serviceInfo,
637 RemoteRequestHandlerPtr iHandler,
638 uint8 iBinaryFormatVersion = BinaryBufferSerializer::getSerializerFormatVersion()) :
639 RemoteService(serviceInfo), handler(iHandler),
640 binaryFormatVersion(iBinaryFormatVersion) {}
642 RemoteRequestHandlerPtr handler;
643 uint8 binaryFormatVersion;
647 mutable boost::mutex mServiceManagementMutex;
653 std::map<std::string, AbstractRPCHandlerPtr> mLocalServiceMethods;
656 std::set<std::string> mLocalServices;
659 std::map<std::string, RemoteServiceDetail> mRemoteServices;
662 mutable boost::mutex mPendingRequestsMutex;
664 std::map<AbstractDeferredInvoker*, AbstractRequestContextPtr> mPendingRequests;
666 std::map<std::string, std::string> mPendingRemoteRequests;
669 mutable boost::mutex mCallbacksMutex;
671 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:302
Abstract interface for handler(s) that get called upon new services with a special interface get avai...
Server side implementation of RPC calls.
BinaryRPCBackendTempl< 2 > BinaryRPCBackend
Definition: BinaryRPCBackend.h:437
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
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
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:82
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
Provides RPC SFINAE helper to check whether a provided description parameter pack is valid...
An exception that is thrown by the RPCServer if an RPC call fails.
Definition: RPCError.h:76
bool implementsInterface(const std::string &name, const std::string &interface) const
Returns if the given service implements a certain interface.
Definition: RPCManager.h:352
This is a specialization for JSON RPC calls.
Definition: RPCFuture.h:315
Use this class to represent time durations.
Definition: Time.h:106
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:134
std::set< std::string > getLocalServices() const
Get all registered local services.
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:176
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
bool existsService(const std::string &name) const
Returns true, if a service with the given name exists, otherwise false.
Definition: RPCManager.h:292
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.
const std::string & callId()
query call ID
Definition: RPCFuture.h:87
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:68
RPCServer::ServiceInfo< RPCServer::MethodInfoSet > RemoteService
Definition: RPCManager.h:235
Handler that must be implemented by the remote module to send RPC requests to a remote server...
Definition: RPCManager.h:111