From c2b7616c8a80c5de6356b675d55a4ee36415fd7b Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 28 Nov 2013 17:48:48 +0100 Subject: [PATCH] frontend: Added a simple 'LIST' command. This command sends a JSON representation of the whole store to the client. --- src/Makefile.am | 1 + src/frontend/connection.c | 7 ++- src/frontend/query.c | 74 +++++++++++++++++++++++++++++++ src/include/frontend/connection.h | 21 ++++++++- 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/frontend/query.c diff --git a/src/Makefile.am b/src/Makefile.am index 1e809ca..5a15f9b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/frontend/connection.c b/src/frontend/connection.c index eb0ada7..04d1222 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -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 index 0000000..9e3b66a --- /dev/null +++ b/src/frontend/query.c @@ -0,0 +1,74 @@ +/* + * SysDB - src/frontend/query.c + * Copyright (C) 2013 Sebastian 'tokkee' Harl + * 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 + +/* + * 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 : */ + diff --git a/src/include/frontend/connection.h b/src/include/frontend/connection.h index 141248f..cbe4c2a 100644 --- a/src/include/frontend/connection.h +++ b/src/include/frontend/connection.h @@ -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 -- 2.30.2