Add RPC API

This commit is contained in:
Sergey Lyubka committed 2022-07-27 00:46:05 +01:00
1 parent 9cacf443e1
commit 0dabc45780
13 files changed
+332 -75

No files matched your search

+5 -1
View File
@@ -2803,6 +2803,10 @@ static const struct packed_file {
{NULL, NULL, 0, 0}
};
static int scmp(const char *a, const char *b) {
while (*a && (*a == *b)) a++, b++;
return *(const unsigned char *) a - *(const unsigned char *) b;
}
const char *mg_unlist(size_t no);
const char *mg_unlist(size_t no) {
return packed_files[no].name;
@@ -2811,7 +2815,7 @@ const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {
const struct packed_file *p;
for (p = packed_files; p->name != NULL; p++) {
if (strcmp(p->name, name) != 0) continue;
if (scmp(p->name, name) != 0) continue;
if (size != NULL) *size = p->size - 1;
if (mtime != NULL) *mtime = p->mtime;
return (const char *) p->data;
+20 -54
View File
@@ -7,64 +7,20 @@
static const char *s_listen_on = "ws://localhost:8000";
static const char *s_web_root = "web_root";
static void *s_rpc_head = NULL;
// RPC functions. Take string params, return (allocated) string result
static char *sum(struct mg_str params) {
static void rpc_sum(struct mg_rpc_req *r) {
double a = 0.0, b = 0.0;
mg_json_get_num(params, "$[0]", &a);
mg_json_get_num(params, "$[1]", &b);
return mg_mprintf("%g", a + b);
mg_json_get_num(r->frame, "$.params[0]", &a);
mg_json_get_num(r->frame, "$.params[1]", &b);
mg_rpc_ok(r, "%g", a + b);
}
static char *multiply(struct mg_str params) {
static void rpc_mul(struct mg_rpc_req *r) {
double a = 0.0, b = 0.0;
mg_json_get_num(params, "$[0]", &a);
mg_json_get_num(params, "$[1]", &b);
return mg_mprintf("%g", a * b);
}
static void process_json_message(struct mg_connection *c, struct mg_str frame) {
struct mg_str params = mg_str(""), id = mg_str("");
int params_off = 0, params_len = 0, id_off = 0, id_len = 0;
char *response = NULL;
// Parse websocket message, which should be a JSON-RPC frame like this:
// { "id": 3, "method": "sum", "params": [1,2] }
char *method = mg_json_get_str(frame, "$.method");
id_off = mg_json_get(frame.ptr, (int) frame.len, "$.id", &id_len);
params_off = mg_json_get(frame.ptr, (int) frame.len, "$.params", &params_len);
params = mg_str_n(frame.ptr + params_off, params_len);
id = mg_str_n(frame.ptr + id_off, id_len);
if (method == NULL || id_off < 0 || params_off < 0) {
// Invalid frame. Return error and include this frame as error message
response = mg_mprintf("{%Q:{%Q:%d,%Q:%.*Q}", "error", "code", -32700,
"message", (int) frame.len, frame.ptr);
} else if (strcmp(method, "sum") == 0) {
char *result = sum(params);
response = mg_mprintf("{%Q:%.*s, %Q:%s}", "id", (int) id.len, id.ptr,
"result", result);
free(result);
} else if (strcmp(method, "mul") == 0) {
char *result = multiply(params);
response = mg_mprintf("{%Q:%.*s, %Q:%s}", "id", (int) id.len, id.ptr,
"result", result);
free(result);
} else {
response =
mg_mprintf("{%Q:%.*s, %Q:{%Q:%d,%Q:%Q}", "id", (int) id.len, id.ptr,
"error", "code", -32601, "message", "Method not found");
}
// Send the response back to the client
if (response) {
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "%s", response);
MG_INFO(("[%.*s] -> [%s]", (int) frame.len, frame.ptr, response));
}
free(method);
free(response);
mg_json_get_num(r->frame, "$.params[0]", &a);
mg_json_get_num(r->frame, "$.params[1]", &b);
mg_rpc_ok(r, "%g", a * b);
}
// This RESTful server implements the following endpoints:
@@ -89,7 +45,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
process_json_message(c, wm->data);
char *resp = NULL;
mg_rpc_process(&s_rpc_head, wm->data, mg_pfn_realloc, &resp);
mg_ws_send(c, resp, strlen(resp), WEBSOCKET_OP_TEXT);
free(resp);
}
(void) fn_data;
}
@@ -111,9 +70,16 @@ int main(void) {
mg_mgr_init(&mgr); // Init event manager
mg_log_set("3");
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT, timer_fn, &mgr); // Init timer
// Configure JSON-RPC functions we're going to handle
mg_rpc_add(&s_rpc_head, mg_str("sum"), rpc_sum, NULL);
mg_rpc_add(&s_rpc_head, mg_str("mul"), rpc_mul, NULL);
mg_rpc_add(&s_rpc_head, mg_str("rpc.list"), mg_rpc_list, &s_rpc_head);
printf("Starting WS listener on %s/websocket\n", s_listen_on);
mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
mg_mgr_free(&mgr); // Deallocate event manager
mg_rpc_free(&s_rpc_head); // Deallocate RPC handlers
return 0;
}
+5 -1
View File
@@ -1814,6 +1814,10 @@ static const struct packed_file {
{NULL, NULL, 0, 0}
};
static int scmp(const char *a, const char *b) {
while (*a && (*a == *b)) a++, b++;
return *(const unsigned char *) a - *(const unsigned char *) b;
}
const char *mg_unlist(size_t no);
const char *mg_unlist(size_t no) {
return packed_files[no].name;
@@ -1822,7 +1826,7 @@ const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {
const struct packed_file *p;
for (p = packed_files; p->name != NULL; p++) {
if (strcmp(p->name, name) != 0) continue;
if (scmp(p->name, name) != 0) continue;
if (size != NULL) *size = p->size - 1;
if (mtime != NULL) *mtime = p->mtime;
return (const char *) p->data;