Line data Source code
1 : #ifndef REGISTER_CMD_PARSER_H_ 2 : #define REGISTER_CMD_PARSER_H_ 3 : 4 : #include <cstdint> 5 : #include <string> 6 : #include <vector> 7 : 8 : #include "simdjson.h" 9 : #include "felix/felix_client_thread.hpp" 10 : 11 : /** 12 : * Data structure to store a command. 13 : */ 14 19 : struct Command { 15 : std::string uuid; 16 : FelixClientThread::Cmd cmd; 17 : std::vector<std::string> cmd_args; 18 : }; 19 : 20 : 21 : /** 22 : * Data structure used by felix-register to parse the command, execute 23 : * the operation and prepare the reply. 24 : */ 25 : struct ReqData { 26 : int status_code{FelixClientThread::OK}; 27 : std::string status_message{"Ok"}; 28 : std::string uuid{""}; 29 : FelixClientThread::Cmd cmd{FelixClientThread::Cmd::UNKNOWN}; 30 : std::string resource_name{""}; 31 : std::string value{""}; 32 : }; 33 : 34 : /** 35 : * RegisterMsgParser de-serialises json-formatted commands (json string to struct) 36 : * and serialises replies (struct to json string) 37 : */ 38 : class RegisterMsgParser 39 : { 40 : public: 41 : /** 42 : * @brief parses a char array containing a command in json format 43 : * @param msg pointer to message C-string 44 : * @param len length of C-string message 45 : * @return a ReqData struct containing the command. 46 : */ 47 : std::vector<ReqData> parse_commands(const char* msg, size_t len); 48 : 49 : /** 50 : * @brief parses a string containing a command in json format 51 : * @param cmd the command string 52 : * @return a ReqData struct containing the command. 53 : */ 54 : std::vector<ReqData> parse_commands(const std::string& cmd); 55 : 56 : /** 57 : * @brief encodes a reply in json 58 : * @param fid the reply fid 59 : * @param cmds vector of ReqData structures containing the command execution data 60 : * @return an std::string containin the reply in serialised json. 61 : */ 62 : std::string encode_replies(uint64_t fid, const std::vector<ReqData>& cmds); 63 : 64 : /** 65 : * @brief decode felix-register replies 66 : * @param r the string containing the reply array in json format 67 : * @return a vector of FelixClientThread::Reply containing the decoded replies. 68 : */ 69 : std::vector<FelixClientThread::Reply> decode_replies(const std::string & r); 70 : 71 : /** 72 : * @brief encodes a vector of commands into a json array 73 : * @param fid the reply fid 74 : * @param cmds vector of Command structures cdescribing commands 75 : * @return an std::string containin the command json array 76 : */ 77 : std::string encode_commands(const std::vector<Command>& cmds); 78 : 79 : private: 80 : simdjson::dom::parser parser; 81 : void parse_cmd_args(ReqData& msg, simdjson::dom::array& arr); 82 : }; 83 : 84 : #endif /* REGISTER_CMD_PARSER_H_ */