1 /*
2 * SysDB - src/include/frontend/connection.h
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #ifndef SDB_FRONTEND_CONNECTION_H
29 #define SDB_FRONTEND_CONNECTION_H 1
31 #include "frontend/proto.h"
32 #include "core/store.h"
33 #include "utils/llist.h"
34 #include "utils/strbuf.h"
36 #include <inttypes.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 typedef struct sdb_conn sdb_conn_t;
44 /*
45 * sdb_conn_node_t represents an interface for the result of parsing a command
46 * string. The first field of an actual implementation of the interface is a
47 * sdb_conn_state_t in order to fascilitate casting to and from the interface
48 * type to the implementation type.
49 */
50 typedef struct {
51 sdb_object_t super;
52 sdb_conn_state_t cmd;
53 } sdb_conn_node_t;
54 #define SDB_CONN_NODE(obj) ((sdb_conn_node_t *)(obj))
56 /*
57 * sdb_connection_enable_logging:
58 * Enable logging of connection-related messages to the current client
59 * connection. After this function has been called all log messages
60 * originating from the thread handling the current client connection will
61 * also be sent to the client.
62 *
63 * Returns:
64 * - 0 on success
65 * - a negative value else
66 */
67 int
68 sdb_connection_enable_logging(void);
70 /*
71 * sdb_connection_accpet:
72 * Accept a new connection on the specified file-descriptor 'fd' and return a
73 * newly allocated connection object.
74 *
75 * Returns:
76 * - 0 on success
77 * - a negative value else
78 */
79 sdb_conn_t *
80 sdb_connection_accept(int fd);
82 /*
83 * sdb_connection_close:
84 * Close a open connection and deallocate any memory. The connection object is
85 * no longer valid after calling this function.
86 */
87 void
88 sdb_connection_close(sdb_conn_t *conn);
90 /*
91 * sdb_connection_read:
92 * Read from an open connection until reading would block.
93 *
94 * Returns:
95 * - the number of bytes read (0 on EOF)
96 * - a negative value on error
97 */
98 ssize_t
99 sdb_connection_read(sdb_conn_t *conn);
101 /*
102 * sdb_connection_send:
103 * Send to an open connection.
104 *
105 * Returns:
106 * - the number of bytes written
107 * - a negative value on error
108 */
109 ssize_t
110 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
111 uint32_t msg_len, const char *msg);
113 /*
114 * sdb_connection_ping:
115 * Send back a backend status indicator to the connected client.
116 *
117 * Returns:
118 * - 0 on success
119 * - a negative value else
120 */
121 int
122 sdb_connection_ping(sdb_conn_t *conn);
124 /*
125 * sdb_fe_parse:
126 * Parse the query text specified in 'query' of length 'len' and return a list
127 * of parse trees (for each command) to be executed by sdb_fe_exec. The list
128 * has to be freed by the caller. If 'len' is less than zero, parse the whole
129 * (nul-terminated) string.
130 *
131 * Returns:
132 * - an sdb_llist_t object of sdb_conn_node_t on success
133 * - NULL in case of an error
134 */
135 sdb_llist_t *
136 sdb_fe_parse(const char *query, int len);
138 /*
139 * sdb_fe_exec:
140 * Execute the command identified by 'node' on the specified connection.
141 *
142 * Returns:
143 * - 0 on success
144 * - a negative value else
145 */
146 int
147 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node);
149 /*
150 * session handling
151 */
153 /*
154 * sdb_fe_session_start:
155 * Start a new user session on the specified connection.
156 *
157 * Returns:
158 * - 0 on success
159 * - a negative value else
160 */
161 int
162 sdb_fe_session_start(sdb_conn_t *conn);
164 /*
165 * store access
166 */
168 /*
169 * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup:
170 * Handle the CONNECTION_QUERY, CONNECTION_FETCH, CONNECTION_LIST, and
171 * CONNECTION_LOOKUP commands respectively. It is expected that the current
172 * command has been initialized already.
173 *
174 * Returns:
175 * - 0 on success
176 * - a negative value else
177 */
178 int
179 sdb_fe_query(sdb_conn_t *conn);
180 int
181 sdb_fe_fetch(sdb_conn_t *conn);
182 int
183 sdb_fe_list(sdb_conn_t *conn);
184 int
185 sdb_fe_lookup(sdb_conn_t *conn);
187 /*
188 * sdb_fe_exec_fetch:
189 * Execute the 'FETCH' command. Send the named host, serialized as JSON, to
190 * the client. If specified, only objects matching the filter will be
191 * included. See sdb_store_tojson for details.
192 *
193 * Returns:
194 * - 0 on success
195 * - a negative value else
196 */
197 int
198 sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
199 sdb_store_matcher_t *filter);
201 /*
202 * sdb_fe_exec_list:
203 * Execute the 'LIST' command. Send a complete listing of the store,
204 * serialized as JSON, to the client. If specified, only objects matching the
205 * filter will be included. See sdb_store_tojson for details.
206 *
207 * Returns:
208 * - 0 on success
209 * - a negative value else
210 */
211 int
212 sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter);
214 /*
215 * sdb_fe_exec_lookup:
216 * Execute the 'LOOKUP' command. Send a list of hosts matching 'm', serialized
217 * as JSON, to the client. If specified, only objects matching the filter will
218 * be included. See sdb_store_tojson for details.
219 *
220 * Returns:
221 * - 0 on success
222 * - a negative value else
223 */
224 int
225 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
226 sdb_store_matcher_t *filter);
228 /*
229 * sdb_fe_exec_timeseries:
230 * Execute the 'TIMESERIES' command. Send the time-series for the specified
231 * host's metric, serialized as JSON, to the client. See
232 * sdb_store_fetch_timeseries for details.
233 *
234 * Returns:
235 * - 0 on success
236 * - a negative value else
237 */
238 int
239 sdb_fe_exec_timeseries(sdb_conn_t *conn,
240 const char *hostname, const char *metric,
241 sdb_timeseries_opts_t *opts);
243 #ifdef __cplusplus
244 } /* extern "C" */
245 #endif
247 #endif /* ! SDB_FRONTEND_CONNECTION_H */
249 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */