struct json_context *js = json_new();
struct json_node *rq = json_new_object(js);
+ json_object_set(rq, "cmd", json_new_string(js, "nop"));
+
bprintf(out, ">>> Request:\n");
json_write(js, out, rq);
bflush(out);
json_object_set(c->reply, "error", json_new_string(c->json, err));
}
+static void cmd_ok(struct client *c)
+{
+ cmd_error(c, "");
+}
+
+static const char *get_string(struct json_node *n, const char *key)
+{
+ struct json_node *s = json_object_get(n, key);
+ if (s && s->type == JSON_STRING)
+ return s->string;
+ else
+ return NULL;
+}
+
+static void cmd_nop(struct client *c)
+{
+ cmd_ok(c);
+}
+struct command {
+ const char *cmd;
+ void (*handler)(struct client *c);
+};
+
+static const struct command command_table[] = {
+ { "nop", cmd_nop },
+};
+
+void cmd_dispatch(struct client *c)
+{
+ struct json_node *rq = c->request;
+ const char *cmd;
+
+ if (rq->type != JSON_OBJECT || !(cmd = get_string(rq, "cmd")))
+ {
+ cmd_error(c, "Malformed request");
+ return;
+ }
+
+ for (uint i=0; i < ARRAY_SIZE(command_table); i++)
+ if (!strcmp(cmd, command_table[i].cmd))
+ {
+ command_table[i].handler(c);
+ return;
+ }
+
+ cmd_error(c, "No such command");
+}