47 #ifndef _MIRA_RPCCLIENT_H_ 48 #define _MIRA_RPCCLIENT_H_ 51 #include <boost/preprocessor/repetition.hpp> 52 #include <boost/thread/mutex.hpp> 134 template <
typename Backend,
typename R>
148 concreteResponsePtr->getReturn(
promise);
155 promise.set_exception(boost::copy_exception(
XRPC(
"Service removed (unpublished or remote disconnected)")));
173 template<
typename Backend,
typename Head,
typename... Tail>
174 void setParameters(
typename Backend::ClientRequest& request, Head&& head, Tail&&... tail)
176 request.setParameter(std::forward<Head>(head));
177 setParameters<Backend>(request, std::forward<Tail>(tail)...);
180 template<
typename Backend>
181 void setParameters(
typename Backend::ClientRequest& request)
217 template<
typename Backend,
typename R,
typename... ARGS>
219 const std::string& method, ARGS&&... args)
221 std::string callId = request.generateCallID();
222 RPCFuture<R> future = addPendingResponse<Backend, R>(callId);
223 request.setHeader(std::move(callId), service, makeRPCSignature<R, ARGS...>(method));
224 setParameters<Backend>(request, std::forward<ARGS>(args)...);
240 template <
typename Backend,
typename R>
244 boost::mutex::scoped_lock lock(mMutex);
247 mPendingResponses[callId] = ptr;
248 return RPCFuture<R>(ptr->promise.get_future(),
this, callId);
259 template <
typename Backend>
263 response.getHeader(callId);
265 boost::mutex::scoped_lock lock(mMutex);
266 auto it = mPendingResponses.find(callId);
270 if(it==mPendingResponses.end())
274 if(typeId<Backend>()!=it->second->getBackendTypeId())
275 MIRA_THROW(XLogical,
"The expected Response Backend type does not match " 276 "the given Response. Probably you sent an RPC call with a " 277 "different Request Backend that does not match to the received " 281 mPendingResponses.erase(it);
284 r->handleResponse(&response);
295 boost::mutex::scoped_lock lock(mMutex);
296 auto it = mPendingResponses.find(callId);
299 if(it==mPendingResponses.end())
303 mPendingResponses.erase(it);
306 r->handleServiceRemoved();
316 boost::mutex::scoped_lock lock(mMutex);
317 mPendingResponses.erase(callId);
322 typedef std::map<std::string, PendingResponsePtr> PendingResponses;
323 PendingResponses mPendingResponses;
TypeId typeId()
Generates unique IDs for different types.
Definition: TypeId.h:94
The RPCClient is responsible for handling the client-side of an rpc call.
Definition: RPCClient.h:103
virtual void handleServiceRemoved()=0
void handleServiceRemoved(const std::string &callId)
Definition: RPCClient.h:293
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
PendingResponse template class.
Definition: RPCClient.h:135
virtual int getBackendTypeId() const =0
Implementation of RPCFuture.
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
RPCFuture< R > addPendingResponse(const std::string &callId)
Adds a new "PendingResponse" object for a call (with the specified callid) that was either generated ...
Definition: RPCClient.h:241
Contains the base interface of all Reflectors, Serializers, etc.
Abstract interface for RPCClient.
Abstract interface for RPCClient.
Definition: AbstractRPCClient.h:60
An exception that is thrown by the RPCServer if an RPC call fails.
Definition: RPCError.h:76
RPCSignature for storing all information about an RPC method signature.
int backendTypeId
Definition: RPCClient.h:167
virtual ~PendingResponseBase()
Definition: RPCClient.h:112
Interface for PendingResponse.
Definition: RPCClient.h:111
boost::shared_ptr< PendingResponseBase > PendingResponsePtr
Definition: RPCClient.h:170
virtual void onDestructFuture(const std::string &callId)
Is called by the RPCFutures that are created by the call() method upon the destruction of the RPCFutu...
Definition: RPCClient.h:314
virtual void handleResponse(void *responsePtr)
Handles the response by calling getReturn() of the RPCResponse backend.
Definition: RPCClient.h:146
virtual void handleServiceRemoved()
Handles the service being removed before a response is received.
Definition: RPCClient.h:154
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
PendingResponse()
Definition: RPCClient.h:139
Backend::ClientResponse Response
Definition: RPCClient.h:137
virtual void handleResponse(void *responsePtr)=0
virtual int getBackendTypeId() const
Returns the (non portable) type id of the used backend.
Definition: RPCClient.h:163
boost::promise< R > promise
The boost::promise object that is used for generating and setting the RPCFuture.
Definition: RPCClient.h:166
std::string handleResponse(typename Backend::ClientResponse &response)
Is called after the RPCServer has sent the response of the RPC call to handle the response and to set...
Definition: RPCClient.h:260