From 20bed8be2508ca7275b1167077f1aedfbcaa13fd Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 1 Dec 2009 16:32:03 -0800 Subject: [PATCH] jsonrpc: Make it easy to get a new JSON-RPC request's id. --- lib/jsonrpc.c | 12 ++++++++---- lib/jsonrpc.h | 3 ++- ovsdb/jsonrpc-server.c | 2 +- ovsdb/ovsdb-client.c | 7 ++++--- tests/test-jsonrpc.c | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/jsonrpc.c b/lib/jsonrpc.c index 817a35f1..bd019f73 100644 --- a/lib/jsonrpc.c +++ b/lib/jsonrpc.c @@ -402,10 +402,14 @@ jsonrpc_create_id(void) } struct jsonrpc_msg * -jsonrpc_create_request(const char *method, struct json *params) +jsonrpc_create_request(const char *method, struct json *params, + struct json **idp) { - return jsonrpc_create(JSONRPC_REQUEST, method, params, NULL, NULL, - jsonrpc_create_id()); + struct json *id = jsonrpc_create_id(); + if (idp) { + *idp = json_clone(id); + } + return jsonrpc_create(JSONRPC_REQUEST, method, params, NULL, NULL, id); } struct jsonrpc_msg * @@ -722,7 +726,7 @@ jsonrpc_session_run(struct jsonrpc_session *s) struct jsonrpc_msg *request; params = json_array_create_empty(); - request = jsonrpc_create_request("echo", params); + request = jsonrpc_create_request("echo", params, NULL); json_destroy(request->id); request->id = json_string_create("echo"); jsonrpc_send(s->rpc, request); diff --git a/lib/jsonrpc.h b/lib/jsonrpc.h index 5d47ac5e..93ac2e86 100644 --- a/lib/jsonrpc.h +++ b/lib/jsonrpc.h @@ -67,7 +67,8 @@ struct jsonrpc_msg { }; struct jsonrpc_msg *jsonrpc_create_request(const char *method, - struct json *params); + struct json *params, + struct json **idp); struct jsonrpc_msg *jsonrpc_create_notify(const char *method, struct json *params); struct jsonrpc_msg *jsonrpc_create_reply(struct json *result, diff --git a/ovsdb/jsonrpc-server.c b/ovsdb/jsonrpc-server.c index 4644970d..897f9ae5 100644 --- a/ovsdb/jsonrpc-server.c +++ b/ovsdb/jsonrpc-server.c @@ -330,7 +330,7 @@ ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s) struct jsonrpc_msg *request; params = json_array_create_empty(); - request = jsonrpc_create_request("echo", params); + request = jsonrpc_create_request("echo", params, NULL); json_destroy(request->id); request->id = json_string_create("echo"); jsonrpc_send(s->rpc, request); diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c index 6e006812..65d6d81d 100644 --- a/ovsdb/ovsdb-client.c +++ b/ovsdb/ovsdb-client.c @@ -236,7 +236,8 @@ fetch_schema_from_rpc(struct jsonrpc *rpc) struct ovsdb_schema *schema; int error; - request = jsonrpc_create_request("get_schema", json_array_create_empty()); + request = jsonrpc_create_request("get_schema", json_array_create_empty(), + NULL); error = jsonrpc_transact_block(rpc, request, &reply); if (error) { ovs_fatal(error, "transaction failed"); @@ -629,7 +630,7 @@ do_transact(int argc UNUSED, char *argv[]) transaction = parse_json(argv[2]); rpc = open_jsonrpc(argv[1]); - request = jsonrpc_create_request("transact", transaction); + request = jsonrpc_create_request("transact", transaction, NULL); error = jsonrpc_transact_block(rpc, request, &reply); if (error) { ovs_fatal(error, "transaction failed"); @@ -795,7 +796,7 @@ do_monitor(int argc, char *argv[]) json_object_put(monitor_requests, argv[2], monitor_request); monitor = json_array_create_2(json_null_create(), monitor_requests); - request = jsonrpc_create_request("monitor", monitor); + request = jsonrpc_create_request("monitor", monitor, NULL); request_id = json_clone(request->id); jsonrpc_send(rpc, request); for (;;) { diff --git a/tests/test-jsonrpc.c b/tests/test-jsonrpc.c index 7f2166f4..42d2c390 100644 --- a/tests/test-jsonrpc.c +++ b/tests/test-jsonrpc.c @@ -250,7 +250,7 @@ do_request(int argc UNUSED, char *argv[]) method = argv[2]; params = parse_json(argv[3]); - msg = jsonrpc_create_request(method, params); + msg = jsonrpc_create_request(method, params, NULL); string = jsonrpc_msg_is_valid(msg); if (string) { ovs_fatal(0, "not a valid JSON-RPC request: %s", string); -- 2.30.2