125 lines
4.1 KiB
C++
125 lines
4.1 KiB
C++
/**
|
|
* @file include/di/RequestHandler.h
|
|
* @brief Request-Reply handler
|
|
* @copyright 2015 Dual Inventive Technology Centre B.V.
|
|
*
|
|
* This class sends requests to the devices when the caller wants to.
|
|
* It also reads the reply and decodes the error-code (if any)
|
|
*/
|
|
#ifndef INCLUDE_DI_REQUESTHANDLER_H_
|
|
#define INCLUDE_DI_REQUESTHANDLER_H_
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <mutex>
|
|
#include <di/Json.h>
|
|
#include <di/Zmq.h>
|
|
#include <di/types.h>
|
|
|
|
namespace Di {
|
|
|
|
/**
|
|
* @class RequestHandler
|
|
* This class sends requests to the devices when the caller wants to.
|
|
* It also reads the reply and decodes the error-code (if any)
|
|
*/
|
|
class RequestHandler {
|
|
public:
|
|
/**
|
|
* constructor for RequestHandler
|
|
*
|
|
* @param uri URI to connect or bind to
|
|
* @param bindToUri Whether to bind or connect (default connect)
|
|
*/
|
|
explicit RequestHandler(const std::string &uri, bool bindToUri = false);
|
|
~RequestHandler();
|
|
|
|
/**
|
|
* do request-reply with the device uid
|
|
* @param uid the uid of the device that made the request
|
|
* @param classMethod the class method
|
|
* @param param possible parameter for requests
|
|
* @return DNOK on success or DNE_PARAM on error
|
|
*/
|
|
di_errno_t doReqRep(const std::string &uid, const std::string &classMethod, const std::shared_ptr<Json> ¶m);
|
|
|
|
/**
|
|
* do request-reply with the device uid
|
|
* @param uid the uid of the device that made the request
|
|
* @param classMethod the class method
|
|
* @param reply message pointer where the reply is written to
|
|
* @return DNOK on success or DNE_PARAM on error
|
|
*/
|
|
di_errno_t doReqRep(const std::string &uid, const std::string &classMethod, std::shared_ptr<Json> *reply);
|
|
|
|
/**
|
|
* do request-reply with the device uid
|
|
* @param uid the uid of the device that made the request
|
|
* @param classMethod the class method
|
|
* @param param possible parameter for requests
|
|
* @param reply message pointer where the reply is written to
|
|
* @return DNOK on success or DNE_PARAM on error
|
|
*/
|
|
di_errno_t doReqRep(const std::string &uid, const std::string &classMethod, const std::shared_ptr<Json> ¶m,
|
|
std::shared_ptr<Json> *reply);
|
|
|
|
|
|
/**
|
|
* send request with the device uid
|
|
* @param uid the uid of the device that made the request
|
|
* @param classMethod the class method
|
|
* @return DNOK on success or DNE_PARAM on error
|
|
*/
|
|
di_errno_t sendRequest(const std::string &uid, const std::string &classMethod);
|
|
|
|
/**
|
|
* send request with the device uid
|
|
* @param uid the uid of the device that made the request
|
|
* @param classMethod the class method
|
|
* @param param possible parameters for requests
|
|
* @return DNOK on success or DNE_PARAM on error
|
|
*/
|
|
di_errno_t sendRequest(const std::string &uid, const std::string &classMethod,
|
|
const std::shared_ptr<Json> ¶m);
|
|
|
|
/**
|
|
* send request with the project id
|
|
* @param projectId the id of the project that made the request
|
|
* @param classMethod the class method
|
|
* @param param possible parameters for requests
|
|
* @param req Request object sent through ZeroMQ
|
|
* @return DNOK on success or DNE_PARAM on error
|
|
*/
|
|
di_errno_t sendRequest(di_project_id_t projectId, const std::string &classMethod,
|
|
const std::shared_ptr<Json> ¶m, std::shared_ptr<Json> *req);
|
|
|
|
/**
|
|
* wait on reply from last request
|
|
* @param reply message pointer where the reply is written to
|
|
* @return return 0 on success and error code on failure
|
|
*/
|
|
di_errno_t receiveReply(std::shared_ptr<Json> *reply);
|
|
|
|
/**
|
|
* Set the timeout for replies
|
|
* @param timeoutMs timeout in miliseconds
|
|
*/
|
|
void setTimeout(time_t timeoutMs);
|
|
|
|
private:
|
|
unsigned int __idCounter; /**< counter for id in requests */
|
|
bool __running;
|
|
bool __mustBind;
|
|
time_t __timeoutMs; /**< timeout for replies in miliseconds, default 11 sec */
|
|
std::string __uri;
|
|
std::shared_ptr<Zmq::Context> __zmqCtx; /** ZeroMQ context */
|
|
std::shared_ptr<Zmq::Socket> __zmqSock; /**< ZeroMQ socket */
|
|
std::mutex _reqrepLock;
|
|
|
|
di_errno_t __reconnect(void);
|
|
};
|
|
|
|
} // namespace Di
|
|
|
|
#endif // INCLUDE_DI_REQUESTHANDLER_H_
|