Remote Procedure Call (RPC) over Websockets (uses MbedJSONValue)
Dependents: RPC_mbed_client RPC_Wifly_HelloWorld RPC_Ethernet_HelloWorld
Revision 7:067cb01b491e, committed 2012-08-23
- Comitter:
- samux
- Date:
- Thu Aug 23 14:23:37 2012 +0000
- Parent:
- 6:0ec91dd0e80e
- Commit message:
- update with new websocket client
Changed in this revision
MbedJSONRpc.cpp | Show annotated file Show diff for this revision Revisions of this file |
MbedJSONRpc.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0ec91dd0e80e -r 067cb01b491e MbedJSONRpc.cpp --- a/MbedJSONRpc.cpp Mon Oct 03 08:40:53 2011 +0000 +++ b/MbedJSONRpc.cpp Thu Aug 23 14:23:37 2012 +0000 @@ -10,7 +10,7 @@ sprintf(json, (const char *)MSG_CALL, my_id, dest, id, fn, str.c_str()); - webs->Send(json); + webs->send(json); t = waitAnswer(tmp, id, json); if (t != CALL_OK) return t; @@ -61,7 +61,7 @@ int id = rand() % 100; sprintf(json, (const char *)MSG_INFO_METHODS, my_id, dest, id); - webs->Send(json); + webs->send(json); waitAnswer(tmp, id, json); printf("methods available on %s: ", dest); for (int i = 0; i < tmp.size() - 1; i++) { @@ -115,7 +115,7 @@ RPC_TYPE t; sprintf(json, (const char *)MSG_REGISTER, my_id, id, public_name); - webs->Send(json); + webs->send(json); t = waitAnswer(tmp, id, json); if (t != REGISTER_OK) return t; @@ -145,14 +145,14 @@ obj[i]->execute(v["params"], r); sprintf(json_recv, (const char *)MSG_RESULT, my_id, v["from"].get<std::string>().c_str(), v["id_msg"].get<int>(), r.serialize().c_str()); - webs->Send(json_recv); + webs->send(json_recv); } else if ((i = procAlreadyRegistered((char *)s.c_str())) != -1) { proc[i](v["params"], r); sprintf(json_recv, (const char *)MSG_RESULT, my_id, v["from"].get<std::string>().c_str(), v["id_msg"].get<int>(), r.serialize().c_str()); - webs->Send(json_recv); + webs->send(json_recv); } }
diff -r 0ec91dd0e80e -r 067cb01b491e MbedJSONRpc.h --- a/MbedJSONRpc.h Mon Oct 03 08:40:53 2011 +0000 +++ b/MbedJSONRpc.h Thu Aug 23 14:23:37 2012 +0000 @@ -35,7 +35,6 @@ #define _MbedJSONRPC_H_ #include "Websocket.h" -#include "Wifly.h" #include "mbed.h" #include "MbedJSONValue.h" #include <string> @@ -98,154 +97,6 @@ /** MbedJSONRpc class * - * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class - * - * Example (client which registers one method): - * @code - * #include "mbed.h" - * #include "MbedJSONRpc.h" - * #include "MbedJSONValue.h" - * #include "Websocket.h" - * - * #define WIFI - * - * #ifdef WIFI - * #include "Wifly.h" - * #endif - * - * DigitalOut led1(LED1); - * Serial pc(USBTX, USBRX); - * - * - * class Test { - * public: - * Test() {}; - * void fn(MbedJSONValue& val, MbedJSONValue& res) { - * printf("first arg: %s\r\n", val[0].get<string>().c_str()); - * res[0] = "coucou"; - * led1 = 1; - * } - * }; - * - * - * #ifdef WIFI - * //wifi and websocket - * Wifly wifly(p9, p10, p30, "NETGEAR", "hello", true); - * Websocket webs("ws://sockets.mbed.org:888/ws/samux/server",&wifly); - * #endif - * - * #ifdef ETHERNET - * Websocket webs("ws://sockets.mbed.org:888/ws/samux/server"); - * #endif - * - * //RPC object identified with the name "server" on the network and attached to the websocket server - * MbedJSONRpc rpc(&webs); - * - * Test test; - * - * int main() { - * - * RPC_TYPE t; - * - * - * //connection to the router and to the websocket server - * #ifdef WIFI - * while (1) { - * - * while (!wifly.Join()) //we connect to the network - * wifly.reset(); - * - * if (!webs.connect()) //we connect to the server - * wifly.reset(); - * else - * break; - * } - * #endif - * - * #ifdef ETHERNET - * while(!webs.connect()) - * pc.printf("cannot connect websocket, retrying\r\n"); - * #endif - * - * //register the method Test::fn as "fn1" - * if((t = rpc.registerMethod("fn1", &test, &Test::fn)) == REGISTER_OK) - * printf("register ok\r\n"); - * else - * printType(t); - * - * //Listen CALL requests - * rpc.work(); - * } - * @endcode - * - * - * - * Example (client which calls the previous registered method): - * @code - * #include "mbed.h" - * #include "MbedJSONRpc.h" - * #include "MbedJSONValue.h" - * #include "Websocket.h" - * - * #define ETHERNET - * - * #ifdef WIFI - * #include "Wifly.h" - * #endif - * - * Serial pc(USBTX, USBRX); - * - * #ifdef WIFI - * //wifi and websocket - * Wifly wifly(p9, p10, p30, "NETGEAR", "hello", true); - * Websocket webs("ws://sockets.mbed.org:888/ws/samux/client",&wifly); - * #endif - * - * #ifdef ETHERNET - * Websocket webs("ws://sockets.mbed.org:888/ws/samux/client"); - * #endif - * - * //RPC object identified by the name "client" on the network and attached to the websocket server - * MbedJSONRpc rpc(&webs); - * - * int main() { - * - * //connection to the router and to the websocket server - * #ifdef WIFI - * while (1) { - * - * while (!wifly.Join()) //we connect to the network - * wifly.reset(); - * - * if (!webs.connect()) //we connect to the server - * wifly.reset(); - * else - * break; - * } - * #endif - * - * #ifdef ETHERNET - * while(!webs.connect()) - * pc.printf("cannot connect websocket, retrying\r\n"); - * #endif - * - * RPC_TYPE t; - * MbedJSONValue arg, resp; - * arg[0] = "Hello"; - * - * // print all methods and procedure available on the client "server" - * rpc.checkMethods("server"); - * - * // Try to call the function "fn1" registered by server previously - * if((t = rpc.call("fn1", "server", arg, resp)) == CALL_OK) - * { - * printf("call success\r\n"); - * printf("res: %s\r\n", resp[0].get<string>().c_str()); - * } - * else - * printType(t); - * } - * @endcode */ class MbedJSONRpc { public: @@ -285,7 +136,7 @@ MbedJSONValue tmp; sprintf(json, (const char *)MSG_REGISTER, my_id, id, public_name); - webs->Send(json); + webs->send(json); tmr.start(); t = waitAnswer(tmp, id, json); if (t != REGISTER_OK)