draft
authorimarom <[email protected]>
Mon, 17 Aug 2015 08:32:59 +0000 (11:32 +0300)
committerimarom <[email protected]>
Mon, 17 Aug 2015 08:32:59 +0000 (11:32 +0300)
src/gtest/rpc_test.cpp
src/rpc-server/include/trex_rpc_cmd_api.h
src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h
src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp

index 8cee085..c616b7a 100644 (file)
@@ -187,8 +187,8 @@ TEST_F(RpcTest, batch_rpc_test) {
             {\"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);
@@ -214,5 +214,13 @@ TEST_F(RpcTest, batch_rpc_test) {
     // 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;
 }
index c041610..308e344 100644 (file)
@@ -40,7 +40,8 @@ public:
     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
     };
 
     /**
index 1f26b92..3367ad6 100644 (file)
@@ -26,8 +26,6 @@ limitations under the License.
 #include <vector>
 #include <json/json.h>
 
-class TrexRpcCommand;
-
 /**
  * JSON RPC V2 parsed object
  * 
@@ -49,11 +47,8 @@ protected:
 
     /**
      * 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;
index 6fb210b..c11c603 100644 (file)
@@ -18,6 +18,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 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>
@@ -28,7 +29,8 @@ limitations under the License.
 #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,
@@ -54,7 +56,8 @@ void TrexJsonRpcV2ParsedObject::execute(Json::Value &response) {
         response["id"]      = m_msg_id;
         _execute(response);
     } else {
-        _execute();
+        Json::Value dummy;
+        _execute(dummy);
     }
 }
 
@@ -80,13 +83,13 @@ public:
             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:
@@ -112,20 +115,25 @@ public:
         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;