Code

frontend: Connection-specific functions now use the sdb_conn_ prefix.
[sysdb.git] / src / include / frontend / connection.h
index a5ea092c425d65f570af2b838f2febbdf9e64611..349993224ebfe336ad7033d312274ee60ff9b99c 100644 (file)
 #ifndef SDB_FRONTEND_CONNECTION_H
 #define SDB_FRONTEND_CONNECTION_H 1
 
+#include "frontend/proto.h"
+#include "core/store.h"
+#include "utils/llist.h"
 #include "utils/strbuf.h"
+#include "utils/proto.h"
 
 #include <inttypes.h>
 
 extern "C" {
 #endif
 
-/* status codes returned to a client */
-typedef enum {
-       CONNECTION_OK = 0,
-       CONNECTION_ERROR
-} sdb_conn_status_t;
-
-/* accepted commands / state of the connection */
-typedef enum {
-       CONNECTION_IDLE = 0,
-       CONNECTION_PING,
-       CONNECTION_STARTUP,
-} sdb_conn_state_t;
+/*
+ * sdb_conn_t represents an open connection from a client. It inherits from
+ * sdb_object_t and may safely be cast to a generic object.
+ */
+typedef struct sdb_conn sdb_conn_t;
 
-/* a generic connection object */
+/*
+ * sdb_conn_node_t represents an interface for the result of parsing a command
+ * string. The first field of an actual implementation of the interface is a
+ * sdb_conn_state_t in order to fascilitate casting to and from the interface
+ * type to the implementation type.
+ */
 typedef struct {
-       /* file-descriptor of the open connection */
-       int fd;
-
-       /* read buffer */
-       sdb_strbuf_t *buf;
-
-       /* connection / protocol state information */
-       uint32_t cmd;
-       uint32_t cmd_len;
+       sdb_object_t super;
+       sdb_conn_state_t cmd;
+} sdb_conn_node_t;
+#define SDB_CONN_NODE(obj) ((sdb_conn_node_t *)(obj))
 
-       /* user information */
-       char *username; /* NULL if the user has not been authenticated */
-} sdb_conn_t;
+/*
+ * sdb_conn_setup_cb is a callback function used to setup a connection. For
+ * example, it may be used to initialize session information.
+ */
+typedef int (*sdb_conn_setup_cb)(sdb_conn_t *, void *);
 
 /*
- * sdb_connection_init:
- * Initialize an (already allocated) connection. This function allocates and
- * initialized any attributes. It's an error to call init on an already
- * initialized object.
+ * sdb_connection_enable_logging:
+ * Enable logging of connection-related messages to the current client
+ * connection. After this function has been called all log messages
+ * originating from the thread handling the current client connection will
+ * also be sent to the client.
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_connection_init(sdb_conn_t *conn);
+sdb_connection_enable_logging(void);
+
+/*
+ * sdb_connection_accpet:
+ * Accept a new connection on the specified file-descriptor 'fd' and return a
+ * newly allocated connection object. If specified, the setup callback is used
+ * to setup the newly created connection. It will receive the connection
+ * object and the specified user data as its arguments.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+sdb_conn_t *
+sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data);
 
 /*
  * sdb_connection_close:
- * Close a open connection and deallocate any memory used by its attributes.
+ * Close an open connection. Any subsequent reads from the connection will
+ * fail. Use sdb_object_deref to free the memory used by the object.
  */
 void
 sdb_connection_close(sdb_conn_t *conn);
 
 /*
- * sdb_connection_read:
- * Read from an open connection until reading would block.
+ * sdb_connection_handle:
+ * Read from an open connection until reading would block and handle all
+ * incoming commands.
  *
  * Returns:
  *  - the number of bytes read (0 on EOF)
  *  - a negative value on error
  */
 ssize_t
-sdb_connection_read(sdb_conn_t *conn);
+sdb_connection_handle(sdb_conn_t *conn);
 
 /*
  * sdb_connection_send:
@@ -106,7 +122,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:
@@ -119,12 +135,23 @@ sdb_connection_send(sdb_conn_t *conn, uint32_t code,
 int
 sdb_connection_ping(sdb_conn_t *conn);
 
+/*
+ * sdb_connection_server_version:
+ * Send back the backend server version to the connected client.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_connection_server_version(sdb_conn_t *conn);
+
 /*
  * session handling
  */
 
 /*
- * sdb_session_start:
+ * sdb_conn_session_start:
  * Start a new user session on the specified connection.
  *
  * Returns:
@@ -132,7 +159,51 @@ sdb_connection_ping(sdb_conn_t *conn);
  *  - a negative value else
  */
 int
-sdb_session_start(sdb_conn_t *conn);
+sdb_conn_session_start(sdb_conn_t *conn);
+
+/*
+ * store access
+ */
+
+/*
+ * sdb_conn_query, sdb_conn_fetch, sdb_conn_list, sdb_conn_lookup,
+ * sdb_conn_store:
+ * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
+ * SDB_CONNECTION_LOOKUP, and SDB_CONNECTION_STORE commands respectively. It
+ * is expected that the current command has been initialized already.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_conn_query(sdb_conn_t *conn);
+int
+sdb_conn_fetch(sdb_conn_t *conn);
+int
+sdb_conn_list(sdb_conn_t *conn);
+int
+sdb_conn_lookup(sdb_conn_t *conn);
+int
+sdb_conn_store(sdb_conn_t *conn);
+
+/*
+ * sdb_conn_store_host, sdb_conn_store_service, sdb_conn_store_metric,
+ * sdb_conn_store_attribute:
+ * Execute the 'STORE' command for the respective object type.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_conn_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host);
+int
+sdb_conn_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc);
+int
+sdb_conn_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric);
+int
+sdb_conn_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr);
 
 #ifdef __cplusplus
 } /* extern "C" */