Remote Procedure Call (RPC) over Websockets (uses MbedJSONValue)
Dependents: RPC_mbed_client RPC_Wifly_HelloWorld RPC_Ethernet_HelloWorld
MbedJSONRpc.h@2:af408b5cae75, 2011-09-22 (annotated)
- Committer:
- samux
- Date:
- Thu Sep 22 10:38:50 2011 +0000
- Revision:
- 2:af408b5cae75
- Parent:
- 0:a53d1c86196c
- Child:
- 3:4a3bc3a2314f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:a53d1c86196c | 1 | /** |
samux | 0:a53d1c86196c | 2 | * @author Samuel Mokrani |
samux | 0:a53d1c86196c | 3 | * |
samux | 0:a53d1c86196c | 4 | * @section LICENSE |
samux | 0:a53d1c86196c | 5 | * |
samux | 0:a53d1c86196c | 6 | * Copyright (c) 2011 mbed |
samux | 0:a53d1c86196c | 7 | * |
samux | 0:a53d1c86196c | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
samux | 0:a53d1c86196c | 9 | * of this software and associated documentation files (the "Software"), to deal |
samux | 0:a53d1c86196c | 10 | * in the Software without restriction, including without limitation the rights |
samux | 0:a53d1c86196c | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
samux | 0:a53d1c86196c | 12 | * copies of the Software, and to permit persons to whom the Software is |
samux | 0:a53d1c86196c | 13 | * furnished to do so, subject to the following conditions: |
samux | 0:a53d1c86196c | 14 | * |
samux | 0:a53d1c86196c | 15 | * The above copyright notice and this permission notice shall be included in |
samux | 0:a53d1c86196c | 16 | * all copies or substantial portions of the Software. |
samux | 0:a53d1c86196c | 17 | * |
samux | 0:a53d1c86196c | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
samux | 0:a53d1c86196c | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
samux | 0:a53d1c86196c | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
samux | 0:a53d1c86196c | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
samux | 0:a53d1c86196c | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 0:a53d1c86196c | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
samux | 0:a53d1c86196c | 24 | * THE SOFTWARE. |
samux | 0:a53d1c86196c | 25 | * |
samux | 0:a53d1c86196c | 26 | * @section DESCRIPTION |
samux | 0:a53d1c86196c | 27 | * Main part of MbedJSONRpc: you can register methods or procedures, call a registered method or procedure |
samux | 0:a53d1c86196c | 28 | * on a client over a websocket communication, see all methods available on a certain client, listen for |
samux | 0:a53d1c86196c | 29 | * CALL incoming messages to execute a method or procedure. |
samux | 0:a53d1c86196c | 30 | * |
samux | 0:a53d1c86196c | 31 | */ |
samux | 0:a53d1c86196c | 32 | |
samux | 0:a53d1c86196c | 33 | |
samux | 0:a53d1c86196c | 34 | #ifndef _MbedJSONRPC_H_ |
samux | 0:a53d1c86196c | 35 | #define _MbedJSONRPC_H_ |
samux | 0:a53d1c86196c | 36 | |
samux | 0:a53d1c86196c | 37 | #include "Websocket.h" |
samux | 0:a53d1c86196c | 38 | #include "Wifly.h" |
samux | 0:a53d1c86196c | 39 | #include "mbed.h" |
samux | 2:af408b5cae75 | 40 | #include "MbedJSONValue.h" |
samux | 0:a53d1c86196c | 41 | #include <string> |
samux | 0:a53d1c86196c | 42 | |
samux | 0:a53d1c86196c | 43 | #define NB_METH 30 |
samux | 0:a53d1c86196c | 44 | |
samux | 0:a53d1c86196c | 45 | #define MSG_CALL "{\"from\": \"%s\",\"to\": \"%s\",\"msg\": \"CALL\",\"id_msg\": %d,\"method\": \"%s\",\"params\":%s}" |
samux | 0:a53d1c86196c | 46 | /**< Message skeleton which will be use for a call */ |
samux | 0:a53d1c86196c | 47 | #define MSG_RESULT "{\"from\": \"%s\",\"to\": \"%s\",\"msg\": \"RESULT\",\"id_msg\": %d,\"res\":%s}" |
samux | 0:a53d1c86196c | 48 | /**< Message skeleton which will be use for a result */ |
samux | 0:a53d1c86196c | 49 | #define MSG_REGISTER "{\"from\": \"%s\",\"to\": \"gateway\",\"msg\": \"REGISTER\",\"id_msg\": %d,\"fn\":\"%s\"}" |
samux | 0:a53d1c86196c | 50 | /**< Message skeleton which will be use for register a method or a procedure */ |
samux | 0:a53d1c86196c | 51 | #define MSG_INFO_METHODS "{\"from\": \"%s\",\"to\": \"%s\",\"msg\": \"INFO_METHODS\",\"id_msg\": %d}" |
samux | 0:a53d1c86196c | 52 | /**< Message skeleton which will be send to see all methods available of a certain client */ |
samux | 0:a53d1c86196c | 53 | |
samux | 0:a53d1c86196c | 54 | |
samux | 0:a53d1c86196c | 55 | /** |
samux | 0:a53d1c86196c | 56 | * \enum RPC_TYPE |
samux | 0:a53d1c86196c | 57 | * \brief Type of an RPC transaction |
samux | 0:a53d1c86196c | 58 | */ |
samux | 0:a53d1c86196c | 59 | enum RPC_TYPE { |
samux | 0:a53d1c86196c | 60 | JSON_PARSE_ERROR, /*!< Json parse error */ |
samux | 2:af408b5cae75 | 61 | RPC_PARSE_ERROR, /*!< rpc parse error (the message doesn't contain good identifiers in a TypeObject MbedJSONValue)*/ |
samux | 0:a53d1c86196c | 62 | PROC_NOT_FOUND, /*!< The user wants to call an inexisting method or procedure */ |
samux | 0:a53d1c86196c | 63 | CLIENT_NOT_CONNECTED, /*!< The user wants to call a method or procedure on a client not connected (no response from the client within 3s) */ |
samux | 0:a53d1c86196c | 64 | SERVER_NOT_CONNECTED, /*!< No response from the server within 5s */ |
samux | 0:a53d1c86196c | 65 | ERR_ID, /*!< Each messages have an id, if the id of the answer doesn't match with the id of the request, there is an id error */ |
samux | 0:a53d1c86196c | 66 | CALL_OK, /*!< A call has been successfully executed */ |
samux | 0:a53d1c86196c | 67 | REGISTER_OK /*!< A request to register a method or procedure is successful */ |
samux | 0:a53d1c86196c | 68 | }; |
samux | 0:a53d1c86196c | 69 | |
samux | 0:a53d1c86196c | 70 | |
samux | 0:a53d1c86196c | 71 | |
samux | 0:a53d1c86196c | 72 | class ObjBase { |
samux | 0:a53d1c86196c | 73 | public: |
samux | 2:af408b5cae75 | 74 | virtual void execute(MbedJSONValue& val, MbedJSONValue& res) = 0; |
samux | 0:a53d1c86196c | 75 | }; |
samux | 0:a53d1c86196c | 76 | |
samux | 0:a53d1c86196c | 77 | template <class T> |
samux | 0:a53d1c86196c | 78 | class Obj : public ObjBase { |
samux | 0:a53d1c86196c | 79 | public: |
samux | 0:a53d1c86196c | 80 | |
samux | 2:af408b5cae75 | 81 | Obj(T * ptr, void (T::*fn_ptr)(MbedJSONValue& , MbedJSONValue& )) { |
samux | 0:a53d1c86196c | 82 | obj_ptr = ptr; |
samux | 0:a53d1c86196c | 83 | fn = fn_ptr; |
samux | 0:a53d1c86196c | 84 | }; |
samux | 0:a53d1c86196c | 85 | |
samux | 2:af408b5cae75 | 86 | virtual void execute(MbedJSONValue& val, MbedJSONValue& res) { |
samux | 0:a53d1c86196c | 87 | ((*obj_ptr).*fn)(val, res); |
samux | 0:a53d1c86196c | 88 | } |
samux | 0:a53d1c86196c | 89 | |
samux | 0:a53d1c86196c | 90 | //obj |
samux | 0:a53d1c86196c | 91 | T * obj_ptr; |
samux | 0:a53d1c86196c | 92 | |
samux | 0:a53d1c86196c | 93 | //method |
samux | 2:af408b5cae75 | 94 | void (T::*fn)(MbedJSONValue& , MbedJSONValue& ); |
samux | 0:a53d1c86196c | 95 | |
samux | 0:a53d1c86196c | 96 | }; |
samux | 0:a53d1c86196c | 97 | |
samux | 0:a53d1c86196c | 98 | |
samux | 0:a53d1c86196c | 99 | /** MbedJSONRpc class |
samux | 0:a53d1c86196c | 100 | * |
samux | 0:a53d1c86196c | 101 | * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class |
samux | 0:a53d1c86196c | 102 | * |
samux | 0:a53d1c86196c | 103 | * Example (client which registers one method): |
samux | 0:a53d1c86196c | 104 | * @code |
samux | 0:a53d1c86196c | 105 | * #include "mbed.h" |
samux | 0:a53d1c86196c | 106 | * #include "MbedJSONRpc.h" |
samux | 0:a53d1c86196c | 107 | * #include "MbedJSONRpcValue.h" |
samux | 0:a53d1c86196c | 108 | * #include "Websocket.h" |
samux | 0:a53d1c86196c | 109 | * |
samux | 0:a53d1c86196c | 110 | * #define ETHERNET |
samux | 0:a53d1c86196c | 111 | * |
samux | 0:a53d1c86196c | 112 | * #ifdef WIFI |
samux | 0:a53d1c86196c | 113 | * #include "Wifly.h" |
samux | 0:a53d1c86196c | 114 | * #endif |
samux | 0:a53d1c86196c | 115 | * |
samux | 0:a53d1c86196c | 116 | * DigitalOut led1(LED1); |
samux | 0:a53d1c86196c | 117 | * Serial pc(USBTX, USBRX); |
samux | 0:a53d1c86196c | 118 | * |
samux | 0:a53d1c86196c | 119 | * |
samux | 0:a53d1c86196c | 120 | * class Test { |
samux | 0:a53d1c86196c | 121 | * public: |
samux | 0:a53d1c86196c | 122 | * Test() {}; |
samux | 0:a53d1c86196c | 123 | * void fn(JSONRpcValue& val, JSONRpcValue& res) { |
samux | 0:a53d1c86196c | 124 | * printf("first arg: %s\r\n", val[0].get<string>().c_str()); |
samux | 0:a53d1c86196c | 125 | * res[0] = "coucou"; |
samux | 0:a53d1c86196c | 126 | * led1 = 1; |
samux | 0:a53d1c86196c | 127 | * } |
samux | 0:a53d1c86196c | 128 | * }; |
samux | 0:a53d1c86196c | 129 | * |
samux | 0:a53d1c86196c | 130 | * |
samux | 0:a53d1c86196c | 131 | * #ifdef WIFI |
samux | 0:a53d1c86196c | 132 | * //wifi and websocket |
samux | 0:a53d1c86196c | 133 | * Wifly wifly(p9, p10, p30, "mbed", "mbedapm2011", true); |
samux | 0:a53d1c86196c | 134 | * Websocket webs("ws://sockets.mbed.org:888/ws/samux/server",&wifly); |
samux | 0:a53d1c86196c | 135 | * #endif |
samux | 0:a53d1c86196c | 136 | * |
samux | 0:a53d1c86196c | 137 | * #ifdef ETHERNET |
samux | 0:a53d1c86196c | 138 | * Websocket webs("ws://sockets.mbed.org:888/ws/samux/server"); |
samux | 0:a53d1c86196c | 139 | * #endif |
samux | 0:a53d1c86196c | 140 | * |
samux | 0:a53d1c86196c | 141 | * //RPC object identified with the name "server" on the network and attached to the websocket server |
samux | 0:a53d1c86196c | 142 | * MbedJSONRpc rpc("server", &webs); |
samux | 0:a53d1c86196c | 143 | * |
samux | 0:a53d1c86196c | 144 | * Test test; |
samux | 0:a53d1c86196c | 145 | * |
samux | 0:a53d1c86196c | 146 | * int main() { |
samux | 0:a53d1c86196c | 147 | * |
samux | 0:a53d1c86196c | 148 | * RPC_TYPE t; |
samux | 0:a53d1c86196c | 149 | * |
samux | 0:a53d1c86196c | 150 | * |
samux | 0:a53d1c86196c | 151 | * //connection to the router and to the websocket server |
samux | 0:a53d1c86196c | 152 | * #ifdef WIFI |
samux | 0:a53d1c86196c | 153 | * while (1) { |
samux | 0:a53d1c86196c | 154 | * |
samux | 0:a53d1c86196c | 155 | * while (!wifly.Join()) //we connect to the network |
samux | 0:a53d1c86196c | 156 | * wifly.reset(); |
samux | 0:a53d1c86196c | 157 | * |
samux | 0:a53d1c86196c | 158 | * if (!webs.connect()) //we connect to the server |
samux | 0:a53d1c86196c | 159 | * wifly.reset(); |
samux | 0:a53d1c86196c | 160 | * else |
samux | 0:a53d1c86196c | 161 | * break; |
samux | 0:a53d1c86196c | 162 | * } |
samux | 0:a53d1c86196c | 163 | * #endif |
samux | 0:a53d1c86196c | 164 | * |
samux | 0:a53d1c86196c | 165 | * #ifdef ETHERNET |
samux | 0:a53d1c86196c | 166 | * while(!webs.connect()) |
samux | 0:a53d1c86196c | 167 | * pc.printf("cannot connect websocket, retrying\r\n"); |
samux | 0:a53d1c86196c | 168 | * #endif |
samux | 0:a53d1c86196c | 169 | * |
samux | 0:a53d1c86196c | 170 | * //register the method Test::fn as "fn1" |
samux | 0:a53d1c86196c | 171 | * if((t = rpc.registerMethod("fn1", &test, &Test::fn)) == REGISTER_OK) |
samux | 0:a53d1c86196c | 172 | * printf("register ok\r\n"); |
samux | 0:a53d1c86196c | 173 | * else |
samux | 0:a53d1c86196c | 174 | * printType(t); |
samux | 0:a53d1c86196c | 175 | * |
samux | 0:a53d1c86196c | 176 | * //Listen CALL requests |
samux | 0:a53d1c86196c | 177 | * rpc.work(); |
samux | 0:a53d1c86196c | 178 | * } |
samux | 0:a53d1c86196c | 179 | * @endcode |
samux | 0:a53d1c86196c | 180 | * |
samux | 0:a53d1c86196c | 181 | * |
samux | 0:a53d1c86196c | 182 | * |
samux | 0:a53d1c86196c | 183 | * Example (client which calls the previous registered method): |
samux | 0:a53d1c86196c | 184 | * @code |
samux | 0:a53d1c86196c | 185 | * #include "mbed.h" |
samux | 0:a53d1c86196c | 186 | * #include "MbedJSONRpc.h" |
samux | 0:a53d1c86196c | 187 | * #include "MbedJSONRpcValue.h" |
samux | 0:a53d1c86196c | 188 | * #include "Websocket.h" |
samux | 0:a53d1c86196c | 189 | * |
samux | 0:a53d1c86196c | 190 | * #define WIFI |
samux | 0:a53d1c86196c | 191 | * |
samux | 0:a53d1c86196c | 192 | * #ifdef WIFI |
samux | 0:a53d1c86196c | 193 | * #include "Wifly.h" |
samux | 0:a53d1c86196c | 194 | * #endif |
samux | 0:a53d1c86196c | 195 | * |
samux | 0:a53d1c86196c | 196 | * Serial pc(USBTX, USBRX); |
samux | 0:a53d1c86196c | 197 | * |
samux | 0:a53d1c86196c | 198 | * #ifdef WIFI |
samux | 0:a53d1c86196c | 199 | * //wifi and websocket |
samux | 0:a53d1c86196c | 200 | * Wifly wifly(p9, p10, p21, "mbed", "mbedapm2011", true); |
samux | 0:a53d1c86196c | 201 | * Websocket webs("ws://sockets.mbed.org:888/ws/samux/client",&wifly); |
samux | 0:a53d1c86196c | 202 | * #endif |
samux | 0:a53d1c86196c | 203 | * |
samux | 0:a53d1c86196c | 204 | * #ifdef ETHERNET |
samux | 0:a53d1c86196c | 205 | * Websocket webs("ws://sockets.mbed.org:888/ws/samux/client"); |
samux | 0:a53d1c86196c | 206 | * #endif |
samux | 0:a53d1c86196c | 207 | * |
samux | 0:a53d1c86196c | 208 | * //RPC object identified by the name "client" on the network and attached to the websocket server |
samux | 0:a53d1c86196c | 209 | * MbedJSONRpc rpc("client", &webs); |
samux | 0:a53d1c86196c | 210 | * |
samux | 0:a53d1c86196c | 211 | * int main() { |
samux | 0:a53d1c86196c | 212 | * |
samux | 0:a53d1c86196c | 213 | * //connection to the router and to the websocket server |
samux | 0:a53d1c86196c | 214 | * #ifdef WIFI |
samux | 0:a53d1c86196c | 215 | * while (1) { |
samux | 0:a53d1c86196c | 216 | * |
samux | 0:a53d1c86196c | 217 | * while (!wifly.Join()) //we connect to the network |
samux | 0:a53d1c86196c | 218 | * wifly.reset(); |
samux | 0:a53d1c86196c | 219 | * |
samux | 0:a53d1c86196c | 220 | * if (!webs.connect()) //we connect to the server |
samux | 0:a53d1c86196c | 221 | * wifly.reset(); |
samux | 0:a53d1c86196c | 222 | * else |
samux | 0:a53d1c86196c | 223 | * break; |
samux | 0:a53d1c86196c | 224 | * } |
samux | 0:a53d1c86196c | 225 | * #endif |
samux | 0:a53d1c86196c | 226 | * |
samux | 0:a53d1c86196c | 227 | * #ifdef ETHERNET |
samux | 0:a53d1c86196c | 228 | * while(!webs.connect()) |
samux | 0:a53d1c86196c | 229 | * pc.printf("cannot connect websocket, retrying\r\n"); |
samux | 0:a53d1c86196c | 230 | * #endif |
samux | 0:a53d1c86196c | 231 | * |
samux | 0:a53d1c86196c | 232 | * RPC_TYPE t; |
samux | 2:af408b5cae75 | 233 | * MbedJSONValue arg, resp; |
samux | 0:a53d1c86196c | 234 | * arg[0] = "Hello"; |
samux | 0:a53d1c86196c | 235 | * |
samux | 0:a53d1c86196c | 236 | * // print all methods and procedure available on the client "server" |
samux | 0:a53d1c86196c | 237 | * rpc.checkMethods("server"); |
samux | 0:a53d1c86196c | 238 | * |
samux | 0:a53d1c86196c | 239 | * // Try to call the function "fn1" registered by server previously |
samux | 0:a53d1c86196c | 240 | * if((t = rpc.call("fn1", "server", arg, resp)) == CALL_OK) |
samux | 0:a53d1c86196c | 241 | * { |
samux | 0:a53d1c86196c | 242 | * printf("call success\r\n"); |
samux | 0:a53d1c86196c | 243 | * printf("res: %s\r\n", resp[0].get<string>().c_str()); |
samux | 0:a53d1c86196c | 244 | * } |
samux | 0:a53d1c86196c | 245 | * else |
samux | 0:a53d1c86196c | 246 | * printType(t); |
samux | 0:a53d1c86196c | 247 | * } |
samux | 0:a53d1c86196c | 248 | * @endcode |
samux | 0:a53d1c86196c | 249 | */ |
samux | 0:a53d1c86196c | 250 | class MbedJSONRpc { |
samux | 0:a53d1c86196c | 251 | public: |
samux | 0:a53d1c86196c | 252 | |
samux | 0:a53d1c86196c | 253 | /** |
samux | 0:a53d1c86196c | 254 | * Constructor |
samux | 0:a53d1c86196c | 255 | * |
samux | 0:a53d1c86196c | 256 | * @param id Name of the client on the network |
samux | 0:a53d1c86196c | 257 | * @param ws All communication between clients will be established over this websocket |
samux | 0:a53d1c86196c | 258 | */ |
samux | 0:a53d1c86196c | 259 | MbedJSONRpc(Websocket * webs) : webs(webs), index(0), index_proc(0) { |
samux | 0:a53d1c86196c | 260 | std::string path = webs->getPath(); |
samux | 0:a53d1c86196c | 261 | std::string token = "/"; |
samux | 0:a53d1c86196c | 262 | size_t found; |
samux | 0:a53d1c86196c | 263 | found = path.find(token, 3); |
samux | 0:a53d1c86196c | 264 | if (found != std::string::npos) |
samux | 0:a53d1c86196c | 265 | path = path.substr(found + 1, std::string::npos); |
samux | 0:a53d1c86196c | 266 | strcpy(my_id, path.c_str()); |
samux | 0:a53d1c86196c | 267 | }; |
samux | 0:a53d1c86196c | 268 | |
samux | 0:a53d1c86196c | 269 | /** |
samux | 0:a53d1c86196c | 270 | * Register a method of an object |
samux | 0:a53d1c86196c | 271 | * |
samux | 0:a53d1c86196c | 272 | * @param public_name the method will be seen and called by this name |
samux | 0:a53d1c86196c | 273 | * @param obj_ptr a pointeur on the object which contains the method to register |
samux | 2:af408b5cae75 | 274 | * @param fn the method to register (this method must have this signature: void fn(MbedJSONValue& val, MbedJSONValue& res) |
samux | 0:a53d1c86196c | 275 | * @return if REGISTER_OK, the method is registered, otherwise, there is an error |
samux | 0:a53d1c86196c | 276 | * |
samux | 0:a53d1c86196c | 277 | */ |
samux | 2:af408b5cae75 | 278 | template<typename T> RPC_TYPE registerMethod(const char * public_name, T * obj_ptr, void (T::*fn)(MbedJSONValue& val, MbedJSONValue& res)) { |
samux | 0:a53d1c86196c | 279 | char json[100]; |
samux | 0:a53d1c86196c | 280 | RPC_TYPE t; |
samux | 0:a53d1c86196c | 281 | Timer tmr; |
samux | 0:a53d1c86196c | 282 | int id = rand() % 100; |
samux | 2:af408b5cae75 | 283 | MbedJSONValue tmp; |
samux | 0:a53d1c86196c | 284 | |
samux | 0:a53d1c86196c | 285 | sprintf(json, (const char *)MSG_REGISTER, my_id, id, public_name); |
samux | 0:a53d1c86196c | 286 | webs->Send(json); |
samux | 0:a53d1c86196c | 287 | tmr.start(); |
samux | 0:a53d1c86196c | 288 | t = waitAnswer(tmp, id, json); |
samux | 0:a53d1c86196c | 289 | if (t != REGISTER_OK) |
samux | 0:a53d1c86196c | 290 | return t; |
samux | 0:a53d1c86196c | 291 | if( index == NB_METH ) |
samux | 0:a53d1c86196c | 292 | index = NB_METH - 1; |
samux | 0:a53d1c86196c | 293 | obj[index] = new Obj<T>(obj_ptr, fn); |
samux | 0:a53d1c86196c | 294 | name[index++] = public_name; |
samux | 0:a53d1c86196c | 295 | return REGISTER_OK; |
samux | 0:a53d1c86196c | 296 | } |
samux | 0:a53d1c86196c | 297 | |
samux | 0:a53d1c86196c | 298 | |
samux | 0:a53d1c86196c | 299 | /** |
samux | 0:a53d1c86196c | 300 | * Register a procedure |
samux | 0:a53d1c86196c | 301 | * |
samux | 0:a53d1c86196c | 302 | * @param public_name the method will be seen and called by this name |
samux | 2:af408b5cae75 | 303 | * @param fn the procedure to register (this procedure must have this signature: void fn(MbedJSONValue& val, MbedJSONValue& res) |
samux | 0:a53d1c86196c | 304 | * @return if REGISTER_OK, the procedure is registered, otherwise, there is an error |
samux | 0:a53d1c86196c | 305 | * |
samux | 0:a53d1c86196c | 306 | */ |
samux | 2:af408b5cae75 | 307 | RPC_TYPE registerMethod(const char * public_name, void (*fn)(MbedJSONValue& val, MbedJSONValue& res) ); |
samux | 0:a53d1c86196c | 308 | |
samux | 0:a53d1c86196c | 309 | |
samux | 0:a53d1c86196c | 310 | /** |
samux | 0:a53d1c86196c | 311 | * Call a method or procedure on a client |
samux | 0:a53d1c86196c | 312 | * |
samux | 0:a53d1c86196c | 313 | * @param fn name of the method or procedure to be called |
samux | 0:a53d1c86196c | 314 | * @param dest name of the client where will be executed the method or procedure |
samux | 0:a53d1c86196c | 315 | * @param val Input parameters of the method or procedure "fn" |
samux | 0:a53d1c86196c | 316 | * @param resp Once the method or procedure executed, the result will be stored in the "resp" variable |
samux | 0:a53d1c86196c | 317 | * @return If CALL_OK, the method has been executed and you can access the result with the "resp" variable. |
samux | 0:a53d1c86196c | 318 | * If CLIENT_NOT_CONNECTED, means that the client hasn't answered within 3s. |
samux | 0:a53d1c86196c | 319 | * If SERVER_NOT_CONNECTED, means that the server hasn't answered within 5s. |
samux | 0:a53d1c86196c | 320 | * Refer to the RPC_TYPE description |
samux | 0:a53d1c86196c | 321 | * |
samux | 0:a53d1c86196c | 322 | */ |
samux | 2:af408b5cae75 | 323 | RPC_TYPE call(char * fn, char * dest, MbedJSONValue& val, MbedJSONValue& resp); |
samux | 0:a53d1c86196c | 324 | |
samux | 0:a53d1c86196c | 325 | |
samux | 0:a53d1c86196c | 326 | /** |
samux | 0:a53d1c86196c | 327 | * Listen for CALL requests |
samux | 0:a53d1c86196c | 328 | * Warning: infinite loop |
samux | 0:a53d1c86196c | 329 | */ |
samux | 0:a53d1c86196c | 330 | void work(); |
samux | 0:a53d1c86196c | 331 | |
samux | 0:a53d1c86196c | 332 | /** |
samux | 0:a53d1c86196c | 333 | * Print by the usb serial port all methods or procedure available on "dest" client |
samux | 0:a53d1c86196c | 334 | */ |
samux | 0:a53d1c86196c | 335 | void checkMethods(char * dest); |
samux | 0:a53d1c86196c | 336 | |
samux | 0:a53d1c86196c | 337 | private: |
samux | 0:a53d1c86196c | 338 | |
samux | 2:af408b5cae75 | 339 | typedef void (*FNPTR)(MbedJSONValue& , MbedJSONValue& ); |
samux | 0:a53d1c86196c | 340 | |
samux | 0:a53d1c86196c | 341 | int methodAlreadyRegistered(char *); |
samux | 0:a53d1c86196c | 342 | int procAlreadyRegistered(char *); |
samux | 2:af408b5cae75 | 343 | RPC_TYPE decodeMsg(MbedJSONValue& , int); |
samux | 2:af408b5cae75 | 344 | RPC_TYPE waitAnswer(MbedJSONValue& , int, char *); |
samux | 0:a53d1c86196c | 345 | |
samux | 0:a53d1c86196c | 346 | Websocket * webs; |
samux | 0:a53d1c86196c | 347 | |
samux | 0:a53d1c86196c | 348 | ObjBase * obj[NB_METH]; |
samux | 0:a53d1c86196c | 349 | const char * name[NB_METH]; |
samux | 0:a53d1c86196c | 350 | int index; |
samux | 0:a53d1c86196c | 351 | |
samux | 0:a53d1c86196c | 352 | FNPTR proc[NB_METH]; |
samux | 0:a53d1c86196c | 353 | const char * name_proc[NB_METH]; |
samux | 0:a53d1c86196c | 354 | int index_proc; |
samux | 0:a53d1c86196c | 355 | |
samux | 0:a53d1c86196c | 356 | |
samux | 0:a53d1c86196c | 357 | char my_id[20]; |
samux | 0:a53d1c86196c | 358 | |
samux | 0:a53d1c86196c | 359 | }; |
samux | 0:a53d1c86196c | 360 | |
samux | 0:a53d1c86196c | 361 | |
samux | 0:a53d1c86196c | 362 | inline void printType(RPC_TYPE t) |
samux | 0:a53d1c86196c | 363 | { |
samux | 0:a53d1c86196c | 364 | switch(t) |
samux | 0:a53d1c86196c | 365 | { |
samux | 0:a53d1c86196c | 366 | case JSON_PARSE_ERROR: printf("json parse error\r\n"); break; |
samux | 0:a53d1c86196c | 367 | case RPC_PARSE_ERROR: printf("rpc parse error\r\n"); break; |
samux | 0:a53d1c86196c | 368 | case PROC_NOT_FOUND: printf("proc or method not found\r\n"); break; |
samux | 0:a53d1c86196c | 369 | case CLIENT_NOT_CONNECTED: printf("client not connected\r\n"); break; |
samux | 0:a53d1c86196c | 370 | case SERVER_NOT_CONNECTED: printf("server not connected\r\n"); break; |
samux | 0:a53d1c86196c | 371 | case ERR_ID: printf("id error\r\n"); break; |
samux | 0:a53d1c86196c | 372 | case CALL_OK: printf("call ok\r\n"); break; |
samux | 0:a53d1c86196c | 373 | case REGISTER_OK: printf("register ok\r\n"); break; |
samux | 0:a53d1c86196c | 374 | } |
samux | 0:a53d1c86196c | 375 | } |
samux | 0:a53d1c86196c | 376 | |
samux | 0:a53d1c86196c | 377 | #endif |