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"
35 #include "utils/proto.h"
37 #include <inttypes.h>
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 /*
44 * sdb_conn_t represents an open connection from a client. It inherits from
45 * sdb_object_t and may safely be cast to a generic object.
46 */
47 typedef struct sdb_conn sdb_conn_t;
49 /*
50 * sdb_conn_node_t represents an interface for the result of parsing a command
51 * string. The first field of an actual implementation of the interface is a
52 * sdb_conn_state_t in order to fascilitate casting to and from the interface
53 * type to the implementation type.
54 */
55 typedef struct {
56 sdb_object_t super;
57 sdb_conn_state_t cmd;
58 } sdb_conn_node_t;
59 #define SDB_CONN_NODE(obj) ((sdb_conn_node_t *)(obj))
61 /*
62 * sdb_conn_setup_cb is a callback function used to setup a connection. For
63 * example, it may be used to initialize session information.
64 */
65 typedef int (*sdb_conn_setup_cb)(sdb_conn_t *, void *);
67 /*
68 * sdb_connection_enable_logging:
69 * Enable logging of connection-related messages to the current client
70 * connection. After this function has been called all log messages
71 * originating from the thread handling the current client connection will
72 * also be sent to the client.
73 *
74 * Returns:
75 * - 0 on success
76 * - a negative value else
77 */
78 int
79 sdb_connection_enable_logging(void);
81 /*
82 * sdb_connection_accpet:
83 * Accept a new connection on the specified file-descriptor 'fd' and return a
84 * newly allocated connection object. If specified, the setup callback is used
85 * to setup the newly created connection. It will receive the connection
86 * object and the specified user data as its arguments.
87 *
88 * Returns:
89 * - 0 on success
90 * - a negative value else
91 */
92 sdb_conn_t *
93 sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data);
95 /*
96 * sdb_connection_close:
97 * Close an open connection. Any subsequent reads from the connection will
98 * fail. Use sdb_object_deref to free the memory used by the object.
99 */
100 void
101 sdb_connection_close(sdb_conn_t *conn);
103 /*
104 * sdb_connection_handle:
105 * Read from an open connection until reading would block and handle all
106 * incoming commands.
107 *
108 * Returns:
109 * - the number of bytes read (0 on EOF)
110 * - a negative value on error
111 */
112 ssize_t
113 sdb_connection_handle(sdb_conn_t *conn);
115 /*
116 * sdb_connection_send:
117 * Send to an open connection.
118 *
119 * Returns:
120 * - the number of bytes written
121 * - a negative value on error
122 */
123 ssize_t
124 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
125 uint32_t msg_len, const char *msg);
127 /*
128 * sdb_connection_ping:
129 * Send back a backend status indicator to the connected client.
130 *
131 * Returns:
132 * - 0 on success
133 * - a negative value else
134 */
135 int
136 sdb_connection_ping(sdb_conn_t *conn);
138 /*
139 * sdb_fe_parse:
140 * Parse the query text specified in 'query' of length 'len' and return a list
141 * of parse trees (for each command) to be executed by sdb_fe_exec. The list
142 * has to be freed by the caller. If 'len' is less than zero, parse the whole
143 * (nul-terminated) string. If specified, errbuf will be used to record parse
144 * errors.
145 *
146 * Returns:
147 * - an sdb_llist_t object of sdb_conn_node_t on success
148 * - NULL in case of an error
149 */
150 sdb_llist_t *
151 sdb_fe_parse(const char *query, int len, sdb_strbuf_t *errbuf);
153 /*
154 * sdb_fe_exec:
155 * Execute the command identified by 'node' on the specified connection.
156 *
157 * Returns:
158 * - 0 on success
159 * - a negative value else
160 */
161 int
162 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node);
164 /*
165 * session handling
166 */
168 /*
169 * sdb_fe_session_start:
170 * Start a new user session on the specified connection.
171 *
172 * Returns:
173 * - 0 on success
174 * - a negative value else
175 */
176 int
177 sdb_fe_session_start(sdb_conn_t *conn);
179 /*
180 * store access
181 */
183 /*
184 * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup, sdb_fe_store:
185 * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
186 * SDB_CONNECTION_LOOKUP, and SDB_CONNECTION_STORE commands respectively. It
187 * is expected that the current command has been initialized already.
188 *
189 * Returns:
190 * - 0 on success
191 * - a negative value else
192 */
193 int
194 sdb_fe_query(sdb_conn_t *conn);
195 int
196 sdb_fe_fetch(sdb_conn_t *conn);
197 int
198 sdb_fe_list(sdb_conn_t *conn);
199 int
200 sdb_fe_lookup(sdb_conn_t *conn);
201 int
202 sdb_fe_store(sdb_conn_t *conn);
204 /*
205 * sdb_fe_exec_fetch:
206 * Execute the 'FETCH' command. Send the named object of the specified type,
207 * serialized as JSON, to the client. If specified, only objects matching the
208 * filter will be included.
209 *
210 * Returns:
211 * - 0 on success
212 * - a negative value else
213 */
214 int
215 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
216 const char *hostname, const char *name, sdb_store_matcher_t *filter);
218 /*
219 * sdb_fe_exec_list:
220 * Execute the 'LIST' command. Send a complete listing of the store,
221 * serialized as JSON, to the client. The listing includes all hosts and the
222 * specified object type. If specified, only objects matching the filter will
223 * be included.
224 *
225 * Returns:
226 * - 0 on success
227 * - a negative value else
228 */
229 int
230 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter);
232 /*
233 * sdb_fe_exec_lookup:
234 * Execute the 'LOOKUP' command. Send a list of objects of the specified type
235 * matching 'm', serialized as JSON, to the client. If specified, only objects
236 * matching the filter will be included.
237 *
238 * Returns:
239 * - 0 on success
240 * - a negative value else
241 */
242 int
243 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
244 sdb_store_matcher_t *m, sdb_store_matcher_t *filter);
246 /*
247 * sdb_fe_exec_timeseries:
248 * Execute the 'TIMESERIES' command. Send the time-series for the specified
249 * host's metric, serialized as JSON, to the client. See
250 * sdb_store_fetch_timeseries for details.
251 *
252 * Returns:
253 * - 0 on success
254 * - a negative value else
255 */
256 int
257 sdb_fe_exec_timeseries(sdb_conn_t *conn,
258 const char *hostname, const char *metric,
259 sdb_timeseries_opts_t *opts);
261 /*
262 * sdb_fe_store_host, sdb_fe_store_service, sdb_fe_store_metric,
263 * sdb_fe_store_attribute:
264 * Execute the 'STORE' command for the respective object type.
265 *
266 * Returns:
267 * - 0 on success
268 * - a negative value else
269 */
270 int
271 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host);
272 int
273 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc);
274 int
275 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric);
276 int
277 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr);
279 #ifdef __cplusplus
280 } /* extern "C" */
281 #endif
283 #endif /* ! SDB_FRONTEND_CONNECTION_H */
285 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */