101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
#ifndef INCLUDE_DI_RPC_MSG_H_
|
|
#define INCLUDE_DI_RPC_MSG_H_
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include <memory>
|
|
#include <functional>
|
|
#include <di/types.h>
|
|
#include <di/Json.h>
|
|
|
|
namespace Di {
|
|
namespace Rpc {
|
|
|
|
#define DiRpcMsgCallbackClass(callback, classCtx) \
|
|
std::bind(&callback, classCtx, std::placeholders::_1, std::placeholders::_2)
|
|
|
|
#define DiRpcMsgCallback(callback) \
|
|
std::bind(reinterpret_cast<di_errno_t(*)(Di::Rpc::Msg *, std::shared_ptr<std::string> *)>(&callback), \
|
|
std::placeholders::_1, std::placeholders::_2)
|
|
|
|
class Msg {
|
|
public:
|
|
enum Type : unsigned int {
|
|
UNKNOWN = 0,
|
|
REQUEST = 1,
|
|
REPLY = 2,
|
|
PUBLISH = 3
|
|
};
|
|
|
|
typedef std::function<di_errno_t(Di::Rpc::Msg *msg, std::shared_ptr<std::string> *data)> Callback;
|
|
|
|
Msg(void);
|
|
explicit Msg(const std::shared_ptr<Msg> &msg);
|
|
virtual ~Msg(void);
|
|
|
|
unsigned int getRpcVersion(); /**< RPC version */
|
|
uint32_t getId(); /**< Message identifier */
|
|
uint64_t getTime(); /**< DI-Net time */
|
|
std::string getDeviceUid(); /**< Device unique identifier */
|
|
uint32_t getProjectId(); /**< Project identifier */
|
|
uint32_t getUserId(); /**< Get user id */
|
|
Type getMsgType(); /**< Message type */
|
|
std::string getClass(); /**< Message class */
|
|
std::string getMethod(); /**< Message method */
|
|
std::string getClassMethod(); /** Method to receive <class>[:<method>] */
|
|
std::shared_ptr<Json> getData(); /**< JSON object of this message */
|
|
|
|
void setQueueReply();
|
|
bool isAsyncQueueRequest();
|
|
bool isAsyncStatusRequest();
|
|
|
|
bool hasError();
|
|
di_errno_t getError();
|
|
|
|
bool hasCallback(void);
|
|
di_errno_t runCallback(void);
|
|
di_errno_t runCallback(std::shared_ptr<std::string> *data);
|
|
void setCallback(const Callback callback);
|
|
|
|
std::string getMsgTypeString();
|
|
|
|
bool decodeHeader(const std::shared_ptr<Json> &msg);
|
|
|
|
virtual std::shared_ptr<Json> serialize();
|
|
|
|
bool hasDecodeError(void);
|
|
|
|
protected:
|
|
enum {
|
|
SYNC,
|
|
ASYNC_QUEUE,
|
|
ASYNC_STATUS
|
|
} _commType;
|
|
|
|
unsigned int _rpcVersion; /**< RPC version */
|
|
uint32_t _id; /**< Message identifier */
|
|
uint64_t _time; /**< DI-Net time */
|
|
std::string _deviceUid; /**< Device unique identifier */
|
|
uint32_t _projectId; /**< Project identifier */
|
|
uint32_t _userId; /**< User identifier */
|
|
Type _msgType; /**< Message type */
|
|
std::string _class; /**< Message class */
|
|
std::string _method; /**< Message method */
|
|
|
|
std::shared_ptr<Json> _data;
|
|
di_errno_t _errorCode;
|
|
bool _decodeError;
|
|
|
|
private:
|
|
Callback _callback;
|
|
|
|
void __decodeType(void);
|
|
bool __decodeTypeInt(std::string type, Type enumType);
|
|
void __decodeClassMethod(std::string classMethod);
|
|
};
|
|
|
|
} // namespace Rpc
|
|
} // namespace Di
|
|
|
|
#endif // INCLUDE_DI_RPC_MSG_H_
|