{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_sub\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"2\"}, \
{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 22, \"y\": \"itay\"}, \"id\": \"2\"}, \
{\"foo\": \"boo\"}, \
- {\"jsonrpc\": \"2.0\", \"method\": \"foo.get\", \"params\": {\"name\": \"myself\"}, \"id\": \"5\"}, \
- {\"jsonrpc\": \"2.0\", \"method\": \"get_data\", \"id\": \"9\"} \
+ {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_sheker\", \"params\": {\"name\": \"myself\"}, \"id\": 5}, \
+ {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 22, \"y\": 17} } \
]";
resp_str = send_msg(req_str);
// message 4
EXPECT_TRUE(response[3] == Json::Value::null);
+ // message 5
+ EXPECT_TRUE(response[4]["jsonrpc"] == "2.0");
+ EXPECT_TRUE(response[4]["id"] == 5);
+ EXPECT_TRUE(response[4]["error"]["code"] == -32601);
+
+ // message 6 - no ID but a valid command
+ EXPECT_TRUE(response[5] == Json::Value::null);
+
return;
}
enum rpc_cmd_rc_e {
RPC_CMD_OK,
RPC_CMD_PARAM_COUNT_ERR = 1,
- RPC_CMD_PARAM_PARSE_ERR
+ RPC_CMD_PARAM_PARSE_ERR,
+ RPC_CMD_INTERNAL_ERR
};
/**
#include <vector>
#include <json/json.h>
-class TrexRpcCommand;
-
/**
* JSON RPC V2 parsed object
*
/**
* instance private implementation
- * should provide implementation with response
- * and without response value
*/
virtual void _execute(Json::Value &response) = 0;
- virtual void _execute() = 0;
Json::Value m_msg_id;
bool m_respond;
See the License for the specific language governing permissions and
limitations under the License.
*/
+
#include <trex_rpc_exception_api.h>
#include <trex_rpc_jsonrpc_v2_parser.h>
#include <trex_rpc_cmd_api.h>
#include <iostream>
/**
- * error as described in the RFC
+ * error as described in the RFC
+ * http://www.jsonrpc.org/specification
*/
enum {
JSONRPC_V2_ERR_PARSE = -32700,
response["id"] = m_msg_id;
_execute(response);
} else {
- _execute();
+ Json::Value dummy;
+ _execute(dummy);
}
}
response["error"]["code"] = JSONRPC_V2_ERR_INVALID_PARAMS;
response["error"]["message"] = "Bad paramters for method";
break;
- }
- }
+ case TrexRpcCommand::RPC_CMD_INTERNAL_ERR:
+ response["error"]["code"] = JSONRPC_V2_ERR_INTERNAL_ERROR;
+ response["error"]["message"] = "Internal Server Error";
+ break;
+ }
- virtual void _execute() {
- Json::Value result;
- m_cmd->run(m_params, result);
}
private:
response["error"]["message"] = m_msg;
}
- virtual void _execute() {
- /* nothing to do with no response */
- }
-
private:
int m_code;
std::string m_msg;
};
+/************** JSON RPC V2 parser implementation *************/
+
TrexJsonRpcV2Parser::TrexJsonRpcV2Parser(const std::string &msg) : m_msg(msg) {
}
+/**
+ * parse a batch of commands
+ *
+ * @author imarom (17-Aug-15)
+ *
+ * @param commands
+ */
void TrexJsonRpcV2Parser::parse(std::vector<TrexJsonRpcV2ParsedObject *> &commands) {
Json::Reader reader;