69 lines
2.2 KiB
C++
69 lines
2.2 KiB
C++
#ifndef INCLUDE_DI_RPC_READER_H_
|
|
#define INCLUDE_DI_RPC_READER_H_
|
|
|
|
#include <string>
|
|
#include <memory>
|
|
#include <vector>
|
|
#include <type_traits>
|
|
#include <di/types.h>
|
|
#include <di/rpc/Msg.h>
|
|
|
|
namespace Di {
|
|
namespace Rpc {
|
|
|
|
/**
|
|
* @class Reader reads RPC messages into objects (factory pattern)
|
|
*/
|
|
class Reader {
|
|
public:
|
|
bool registerCallback(Msg::Callback callback, const char *className);
|
|
bool registerCallback(Msg::Callback callback, const char *className, const char *methodName);
|
|
bool registerCallback(Msg::Callback callback, const char *className, const char *methodName, Msg::Type msgType);
|
|
|
|
bool registerCallback(Msg::Callback callback, std::string className);
|
|
bool registerCallback(Msg::Callback callback, std::string className, std::string methodName);
|
|
bool registerCallback(Msg::Callback callback, std::string className, std::string methodName, Msg::Type msgType);
|
|
|
|
/**
|
|
* Read JSON string and generate Di::Rpc::Msg, always decode full Msg
|
|
* and create derived class.
|
|
*/
|
|
di_errno_t read(const std::string &json, std::shared_ptr<Msg> *msg);
|
|
|
|
/**
|
|
* Read JSON string and generate Di::Rpc::Msg
|
|
* @param json JSON message
|
|
* @param msg Decoded message
|
|
* @param decodeHeaderOnly When set to true only the RPC message head is decoded
|
|
* and the derived class is NOT created!
|
|
*/
|
|
di_errno_t read(const std::string &json, std::shared_ptr<Msg> *msg, bool decodeHeaderOnly);
|
|
di_errno_t read(const std::shared_ptr<std::string> &json, std::shared_ptr<Msg> *msg);
|
|
di_errno_t read(const std::shared_ptr<std::string> &json, std::shared_ptr<Msg> *msg, bool decodeHeaderOnly);
|
|
|
|
private:
|
|
struct callbackItem {
|
|
Msg::Callback callback;
|
|
std::string className;
|
|
std::string methodName;
|
|
Msg::Type msgType;
|
|
};
|
|
std::vector<std::shared_ptr<callbackItem>> callbackList;
|
|
|
|
Msg::Callback getCallback(std::string className, std::string methodName, Msg::Type msgType);
|
|
|
|
/**
|
|
* Check if className exists in DI_RPC_CLASS_LIST
|
|
*/
|
|
bool __classExists(std::string className);
|
|
/**
|
|
* Allocate new message based on className derived from DI_RPC_CLASS_LIST
|
|
*/
|
|
std::shared_ptr<Msg> __classCreate(const std::shared_ptr<Msg> &msgHeader);
|
|
};
|
|
|
|
} // namespace Rpc
|
|
} // namespace Di
|
|
|
|
#endif // INCLUDE_DI_RPC_READER_H_
|