-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjsonrpc_server.c
212 lines (181 loc) · 4.74 KB
/
jsonrpc_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <string.h>
#include <assert.h>
#include <zmq.h>
#include "jsonrpc.h"
struct user_context_t
{
int count;
};
static char *json_value_as_string(json_t *value)
{
// caller to free the returned string
char buffer[64];
switch (json_typeof(value)) {
case JSON_OBJECT:
case JSON_ARRAY:
return json_dumps(value, JSON_COMPACT);
case JSON_STRING:
return strdup(json_string_value(value));
case JSON_INTEGER:
snprintf(buffer, sizeof(buffer), "%" JSON_INTEGER_FORMAT, json_integer_value(value));
return strdup(buffer);
case JSON_REAL:
snprintf(buffer, sizeof(buffer), "%f", json_real_value(value));
return strdup(buffer);
case JSON_TRUE:
return strdup("True");
case JSON_FALSE:
return strdup("False");
case JSON_NULL:
return strdup("None");
}
assert(0);
}
static int method_test_foreach(json_t *json_params, json_t **result, void *userdata)
{
if (json_is_array(json_params)) {
#if JANSSON_VERSION_HEX >= 0x020500
size_t index;
json_t *value;
json_array_foreach(json_params, index, value) {
char *str = json_value_as_string(value);
printf("%ld: %s\n", index, str);
free(str);
}
#else
printf("JSON_ARRAY_FOREACH unsupported\n");
#endif
} else if (json_is_object(json_params)) {
const char *key;
json_t *value;
json_object_foreach(json_params, key, value) {
char *str = json_value_as_string(value);
printf("%s: %s\n", key, str);
free(str);
}
} else {
assert(0);
}
return 0;
}
static int method_test_iter(json_t *json_params, json_t **result, void *userdata)
{
if (json_is_array(json_params)) {
size_t len = json_array_size(json_params);
size_t idx;
for (idx = 0; idx < len; idx++) {
json_t *value = json_array_get(json_params, idx);
char *str = json_value_as_string(value);
printf("%ld: %s\n", idx, str);
free(str);
}
} else if (json_is_object(json_params)) {
void *iter = json_object_iter(json_params);
while (iter)
{
const char *key = json_object_iter_key(iter);
json_t *value = json_object_iter_value(iter);
char *str = json_value_as_string(value);
printf("%s: %s\n", key, str);
free(str);
iter = json_object_iter_next(json_params, iter);
}
} else {
assert(0);
}
return 0;
}
static int method_test_apperror(json_t *json_params, json_t **result, void *userdata)
{
/* on error, you can do one of the following:
return -1 and leave result unset
return -1 and set result to a proper jsonrpc_error_object
*/
/* example of how to return an application-defined error */
*result = jsonrpc_error_object(-12345, "application defined error",
json_string("additional information"));
return -1;
}
static int method_echo(json_t *json_params, json_t **result, void *userdata)
{
json_incref(json_params);
*result = json_params;
return 0;
}
static int method_counter(json_t *json_params, json_t **result, void *userdata)
{
struct user_context_t *userctx = (struct user_context_t *)userdata;
userctx->count++;
*result = json_integer(userctx->count);
return 0;
}
static int method_subtract(json_t *json_params, json_t **result, void *userdata)
{
size_t flags = 0;
json_error_t error;
double x, y;
int rc;
if (json_is_array(json_params)) {
rc = json_unpack_ex(json_params, &error, flags, "[FF!]", &x, &y);
} else if (json_is_object(json_params)) {
rc = json_unpack_ex(json_params, &error, flags, "{s:F,s:F}",
"minuend", &x, "subtrahend", &y
);
} else {
assert(0);
}
if (rc==-1) {
*result = jsonrpc_error_object_predefined(JSONRPC_INVALID_PARAMS, json_string(error.text));
return -1;
}
*result = json_real(x - y);
return 0;
}
static int method_sum(json_t *json_params, json_t **result, void *userdata)
{
double total = 0;
size_t len = json_array_size(json_params);
int k;
for (k=0; k < len; k++) {
double value = json_number_value(json_array_get(json_params, k));
total += value;
}
*result = json_real(total);
return 0;
}
static struct jsonrpc_method_entry_t method_table[] = {
{ "foreach", method_test_foreach, "o" },
{ "iterate", method_test_iter, "o" },
{ "apperror", method_test_apperror, "" },
{ "echo", method_echo, "o" },
{ "counter", method_counter, "" },
{ "subtract", method_subtract, "o" },
{ "sum", method_sum, "[]" },
{ NULL },
};
int main()
{
void *ctx = zmq_ctx_new();
void *sock = zmq_socket(ctx, ZMQ_REP);
int rc = zmq_bind(sock, "tcp://127.0.0.1:10000");
assert(rc!=-1);
struct user_context_t userctx = {0};
while (1)
{
zmq_msg_t msg;
zmq_msg_init(&msg);
zmq_msg_recv(&msg, sock, 0);
char *output = jsonrpc_handler((char *)zmq_msg_data(&msg),
zmq_msg_size(&msg), method_table, &userctx);
zmq_msg_close(&msg);
if (output) {
zmq_send(sock, output, strlen(output), 0);
free(output);
} else {
zmq_send(sock, "", 0, 0);
}
}
zmq_close(sock);
zmq_ctx_destroy(ctx);
return 0;
}