xref: /spdk/test/rpc_client/rpc_client_test.c (revision 8397285b50ba4799100341bd633bd3f638d19673)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/event.h"
36 #include "spdk/jsonrpc.h"
37 
38 #define RPC_MAX_METHODS 200
39 
40 static const char *g_rpcsock_addr = SPDK_DEFAULT_RPC_ADDR;
41 static int g_addr_family = AF_UNIX;
42 
43 #define RPC_MAX_METHODS 200
44 
45 struct get_jsonrpc_methods_resp {
46 	char *method_names[RPC_MAX_METHODS];
47 	size_t method_num;
48 };
49 
50 static int
51 get_jsonrpc_method_json_parser(void *parser_ctx,
52 			       const struct spdk_json_val *result)
53 {
54 	struct get_jsonrpc_methods_resp *resp = parser_ctx;
55 
56 	return spdk_json_decode_array(result, spdk_json_decode_string, resp->method_names,
57 				      RPC_MAX_METHODS, &resp->method_num, sizeof(char *));
58 }
59 
60 static int
61 spdk_jsonrpc_client_check_rpc_method(struct spdk_jsonrpc_client *client, char *method_name)
62 {
63 	int rc, i;
64 	struct get_jsonrpc_methods_resp resp = {};
65 	struct spdk_json_write_ctx *w;
66 	struct spdk_jsonrpc_client_request *request;
67 
68 	request = spdk_jsonrpc_client_create_request();
69 	if (request == NULL) {
70 		return -ENOMEM;
71 	}
72 
73 	w = spdk_jsonrpc_begin_request(request, 1, "get_rpc_methods");
74 	spdk_jsonrpc_end_request(request, w);
75 	spdk_jsonrpc_client_send_request(client, request);
76 	spdk_jsonrpc_client_free_request(request);
77 
78 	rc = spdk_jsonrpc_client_recv_response(client, get_jsonrpc_method_json_parser, &resp);
79 
80 	if (rc) {
81 		goto out;
82 	}
83 
84 	for (i = 0; i < (int)resp.method_num; i++) {
85 		if (strcmp(method_name, resp.method_names[i]) == 0) {
86 			rc = 0;
87 			goto out;
88 		}
89 	}
90 
91 	rc = -1;
92 
93 out:
94 	for (i = 0; i < (int)resp.method_num; i++) {
95 		SPDK_NOTICELOG("%s\n", resp.method_names[i]);
96 		free(resp.method_names[i]);
97 	}
98 
99 	return rc;
100 }
101 
102 int main(int argc, char **argv)
103 {
104 	struct spdk_jsonrpc_client *client;
105 	int rc;
106 	char *method_name = "get_rpc_methods";
107 
108 	client = spdk_jsonrpc_client_connect(g_rpcsock_addr, g_addr_family);
109 	if (!client) {
110 		return EXIT_FAILURE;
111 	}
112 
113 	rc = spdk_jsonrpc_client_check_rpc_method(client, method_name);
114 
115 	spdk_jsonrpc_client_close(client);
116 
117 	return rc ? EXIT_FAILURE : 0;
118 }
119