]> mj.ucw.cz Git - subauth.git/commitdiff
We have a command table
authorMartin Mares <mj@ucw.cz>
Wed, 19 Jul 2017 10:46:39 +0000 (12:46 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 19 Jul 2017 10:46:39 +0000 (12:46 +0200)
client/subauth.c
server/cmd.c
server/subauthd.c
server/subauthd.h

index e597b768efaa8aa54e67e6e87b8d5d61f42e8904..42b900382a76191eee555d591a6aade6411a0f3d 100644 (file)
@@ -51,6 +51,8 @@ int main(int argc UNUSED, char **argv)
   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);
index 5e8b898125209326982d10a622d39a5af1023663..a53622e8522f498e433f51bf746f6315fb47427e 100644 (file)
@@ -13,4 +13,51 @@ void cmd_error(struct client *c, const char *err)
   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");
+}
index 56d142f4afe17ef0b8736096357da2878381a1d1..cd6376f7e575be792b6f956a4f762f73d8a6f3a4 100644 (file)
@@ -115,7 +115,7 @@ static void received_packet(struct client *c, byte *pkt, int len)
     }
   TRANS_END;
 
-  cmd_error(c, "OK");
+  cmd_dispatch(c);
   send_reply(c);
 }
 
index 4a104ec4e187c45dbd6d2193b8f850ac00274ccb..66d792e92cf568919de27a2d7e7f0f8153867962 100644 (file)
@@ -25,3 +25,4 @@ struct client {
 /* cmd.c */
 
 void cmd_error(struct client *c, const char *err);
+void cmd_dispatch(struct client *c);