summaryrefslogtreecommitdiff
path: root/cethcan/rpc.c
blob: 57cb117ba94c2d85af19097db89da8105104fecb (plain)
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
#include "cethcan.h"

#include <event2/buffer.h>
#include "jsonrpc/jsonrpc.h"

static int rpc_ping(void *apparg, json_t *json_params, json_t **result)
{
	*result = json_string("pong");
	return 0;
}

struct jsonrpc_method_entry_t method_table[] = {
	{ "ping", rpc_ping, "" },
	{ NULL, NULL, NULL },
};

void rpc_perform(struct evbuffer *request,
	void (*response_handler)(void *arg, struct evbuffer *data),
	void *handler_arg)
{
	size_t len = evbuffer_get_length(request);
	char *data = (char *)evbuffer_pullup(request, len);
	struct evbuffer *outbuf = evbuffer_new();

	/* TODO: asynchronous calls */
	char *output = jsonrpc_handler(NULL, data, len, method_table);
	if (output)
		evbuffer_add(outbuf, output, strlen(output));
	response_handler(handler_arg, outbuf);
	evbuffer_free(outbuf);
}