Code

frontend: Added a simple 'LIST' command.
authorSebastian Harl <sh@tokkee.org>
Thu, 28 Nov 2013 16:48:48 +0000 (17:48 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 28 Nov 2013 16:48:48 +0000 (17:48 +0100)
This command sends a JSON representation of the whole store to the client.

src/Makefile.am
src/frontend/connection.c
src/frontend/query.c [new file with mode: 0644]
src/include/frontend/connection.h

index 1e809cab4dd9f76aae48e60da39a54c5e093695f..5a15f9bdb2a35dea9382bebcadf3928549fb1f2d 100644 (file)
@@ -42,6 +42,7 @@ libsysdb_la_SOURCES = \
                include/frontend/connection-private.h \
                frontend/sock.c include/frontend/sock.h \
                frontend/session.c \
+               frontend/query.c \
                utils/channel.c include/utils/channel.h \
                utils/llist.c include/utils/llist.h \
                utils/strbuf.c include/utils/strbuf.h \
index eb0ada71e0c6d5c3dc73cc3156396c3bb2690efa..04d12222c89838e8b9ed2a126e3a778773a912bf 100644 (file)
@@ -171,6 +171,11 @@ command_handle(sdb_conn_t *conn)
                case CONNECTION_STARTUP:
                        status = sdb_session_start(conn);
                        break;
+
+               case CONNECTION_LIST:
+                       status = sdb_fe_list(conn);
+                       break;
+
                default:
                {
                        char errbuf[1024];
@@ -281,7 +286,7 @@ sdb_connection_read(sdb_conn_t *conn)
 
 ssize_t
 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
-               uint32_t msg_len, char *msg)
+               uint32_t msg_len, const char *msg)
 {
        size_t len = 2 * sizeof(uint32_t) + msg_len;
        char buffer[len];
diff --git a/src/frontend/query.c b/src/frontend/query.c
new file mode 100644 (file)
index 0000000..9e3b66a
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * SysDB - src/frontend/query.c
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "sysdb.h"
+
+#include "core/error.h"
+#include "core/store.h"
+#include "frontend/connection-private.h"
+#include "utils/strbuf.h"
+
+#include <errno.h>
+
+/*
+ * public API
+ */
+
+int
+sdb_fe_list(sdb_conn_t *conn)
+{
+       sdb_strbuf_t *buf;
+
+       buf = sdb_strbuf_create(1024);
+       if (! buf) {
+               char errbuf[1024];
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
+                               "buffer to handle LIST command: %s",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+
+               /* XXX: send error message */
+               sdb_connection_send(conn, CONNECTION_ERROR, 0, NULL);
+               sdb_strbuf_destroy(buf);
+               return -1;
+       }
+
+       if (sdb_store_tojson(buf)) {
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
+                               "store to JSON");
+               sdb_connection_send(conn, CONNECTION_ERROR, 0, NULL);
+               sdb_strbuf_destroy(buf);
+               return -1;
+       }
+
+       sdb_connection_send(conn, CONNECTION_OK,
+                       (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
+       sdb_strbuf_destroy(buf);
+       return 0;
+} /* session_start */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
index 141248f211a97ffff1b89bc0cb07024092d3cc23..cbe4c2ab233481208e32c815c2923b064be1c3b6 100644 (file)
@@ -44,9 +44,13 @@ typedef enum {
 
 /* accepted commands / state of the connection */
 typedef enum {
+       /* connection handling */
        CONNECTION_IDLE = 0,
        CONNECTION_PING,
        CONNECTION_STARTUP,
+
+       /* querying */
+       CONNECTION_LIST,
 } sdb_conn_state_t;
 
 typedef struct sdb_conn sdb_conn_t;
@@ -92,7 +96,7 @@ sdb_connection_read(sdb_conn_t *conn);
  */
 ssize_t
 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
-               uint32_t msg_len, char *msg);
+               uint32_t msg_len, const char *msg);
 
 /*
  * sdb_connection_ping:
@@ -120,6 +124,21 @@ sdb_connection_ping(sdb_conn_t *conn);
 int
 sdb_session_start(sdb_conn_t *conn);
 
+/*
+ * store access
+ */
+
+/*
+ * sdb_fe_list:
+ * Send a complete listing of the store, serialized as JSON, to the client.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_fe_list(sdb_conn_t *conn);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif