static void do_notify(struct ovs_cmdl_context *ctx) { struct jsonrpc_msg *msg; struct jsonrpc *rpc; struct json *params; struct stream *stream; const char *method; char *string; int error; method = ctx->argv[2]; params = parse_json(ctx->argv[3]); msg = jsonrpc_create_notify(method, params); string = jsonrpc_msg_is_valid(msg); if (string) { ovs_fatal(0, "not a JSON RPC-valid notification: %s", string); } error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, DSCP_DEFAULT), &stream); if (error) { ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]); } rpc = jsonrpc_open(stream); error = jsonrpc_send_block(rpc, msg); if (error) { ovs_fatal(error, "could not send notification"); } jsonrpc_close(rpc); }
char * jsonrpc_msg_from_json(struct json *json, struct jsonrpc_msg **msgp) { struct json *method = NULL; struct jsonrpc_msg *msg = NULL; struct shash *object; char *error; if (json->type != JSON_OBJECT) { error = xstrdup("message is not a JSON object"); goto exit; } object = json_object(json); method = shash_find_and_delete(object, "method"); if (method && method->type != JSON_STRING) { error = xstrdup("method is not a JSON string"); goto exit; } msg = xzalloc(sizeof *msg); msg->method = method ? xstrdup(method->u.string) : NULL; msg->params = null_from_json_null(shash_find_and_delete(object, "params")); msg->result = null_from_json_null(shash_find_and_delete(object, "result")); msg->error = null_from_json_null(shash_find_and_delete(object, "error")); msg->id = null_from_json_null(shash_find_and_delete(object, "id")); msg->type = (msg->result ? JSONRPC_REPLY : msg->error ? JSONRPC_ERROR : msg->id ? JSONRPC_REQUEST : JSONRPC_NOTIFY); if (!shash_is_empty(object)) { error = xasprintf("message has unexpected member \"%s\"", shash_first(object)->name); goto exit; } error = jsonrpc_msg_is_valid(msg); if (error) { goto exit; } exit: json_destroy(method); json_destroy(json); if (error) { jsonrpc_msg_destroy(msg); msg = NULL; } *msgp = msg; return error; }
static void do_request(struct ovs_cmdl_context *ctx) { struct jsonrpc_msg *msg; struct jsonrpc *rpc; struct json *params; struct stream *stream; const char *method; char *string; int error; method = ctx->argv[2]; params = parse_json(ctx->argv[3]); 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); } error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, DSCP_DEFAULT), &stream); if (error) { ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]); } rpc = jsonrpc_open(stream); error = jsonrpc_send(rpc, msg); if (error) { ovs_fatal(error, "could not send request"); } error = jsonrpc_recv_block(rpc, &msg); if (error) { ovs_fatal(error, "error waiting for reply"); } print_and_free_json(jsonrpc_msg_to_json(msg)); jsonrpc_close(rpc); }