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_connection_enable_logging:
63 * Enable logging of connection-related messages to the current client
64 * connection. After this function has been called all log messages
65 * originating from the thread handling the current client connection will
66 * also be sent to the client.
67 *
68 * Returns:
69 * - 0 on success
70 * - a negative value else
71 */
72 int
73 sdb_connection_enable_logging(void);
75 /*
76 * sdb_connection_accpet:
77 * Accept a new connection on the specified file-descriptor 'fd' and return a
78 * newly allocated connection object.
79 *
80 * Returns:
81 * - 0 on success
82 * - a negative value else
83 */
84 sdb_conn_t *
85 sdb_connection_accept(int fd);
87 /*
88 * sdb_connection_close:
89 * Close an open connection. Any subsequent reads from the connection will
90 * fail. Use sdb_object_deref to free the memory used by the object.
91 */
92 void
93 sdb_connection_close(sdb_conn_t *conn);
95 /*
96 * sdb_connection_handle:
97 * Read from an open connection until reading would block and handle all
98 * incoming commands.
99 *
100 * Returns:
101 * - the number of bytes read (0 on EOF)
102 * - a negative value on error
103 */
104 ssize_t
105 sdb_connection_handle(sdb_conn_t *conn);
107 /*
108 * sdb_connection_send:
109 * Send to an open connection.
110 *
111 * Returns:
112 * - the number of bytes written
113 * - a negative value on error
114 */
115 ssize_t
116 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
117 uint32_t msg_len, const char *msg);
119 /*
120 * sdb_connection_ping:
121 * Send back a backend status indicator to the connected client.
122 *
123 * Returns:
124 * - 0 on success
125 * - a negative value else
126 */
127 int
128 sdb_connection_ping(sdb_conn_t *conn);
130 /*
131 * sdb_fe_parse:
132 * Parse the query text specified in 'query' of length 'len' and return a list
133 * of parse trees (for each command) to be executed by sdb_fe_exec. The list
134 * has to be freed by the caller. If 'len' is less than zero, parse the whole
135 * (nul-terminated) string. If specified, errbuf will be used to record parse
136 * errors.
137 *
138 * Returns:
139 * - an sdb_llist_t object of sdb_conn_node_t on success
140 * - NULL in case of an error
141 */
142 sdb_llist_t *
143 sdb_fe_parse(const char *query, int len, sdb_strbuf_t *errbuf);
145 /*
146 * sdb_fe_exec:
147 * Execute the command identified by 'node' on the specified connection.
148 *
149 * Returns:
150 * - 0 on success
151 * - a negative value else
152 */
153 int
154 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node);
156 /*
157 * session handling
158 */
160 /*
161 * sdb_fe_session_start:
162 * Start a new user session on the specified connection.
163 *
164 * Returns:
165 * - 0 on success
166 * - a negative value else
167 */
168 int
169 sdb_fe_session_start(sdb_conn_t *conn);
171 /*
172 * store access
173 */
175 /*
176 * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup, sdb_fe_store:
177 * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
178 * SDB_CONNECTION_LOOKUP, and SDB_CONNECTION_STORE commands respectively. It
179 * is expected that the current command has been initialized already.
180 *
181 * Returns:
182 * - 0 on success
183 * - a negative value else
184 */
185 int
186 sdb_fe_query(sdb_conn_t *conn);
187 int
188 sdb_fe_fetch(sdb_conn_t *conn);
189 int
190 sdb_fe_list(sdb_conn_t *conn);
191 int
192 sdb_fe_lookup(sdb_conn_t *conn);
193 int
194 sdb_fe_store(sdb_conn_t *conn);
196 /*
197 * sdb_fe_exec_fetch:
198 * Execute the 'FETCH' command. Send the named object of the specified type,
199 * serialized as JSON, to the client. If specified, only objects matching the
200 * filter will be included.
201 *
202 * Returns:
203 * - 0 on success
204 * - a negative value else
205 */
206 int
207 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
208 const char *hostname, const char *name, sdb_store_matcher_t *filter);
210 /*
211 * sdb_fe_exec_list:
212 * Execute the 'LIST' command. Send a complete listing of the store,
213 * serialized as JSON, to the client. The listing includes all hosts and the
214 * specified object type. If specified, only objects matching the filter will
215 * be included.
216 *
217 * Returns:
218 * - 0 on success
219 * - a negative value else
220 */
221 int
222 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter);
224 /*
225 * sdb_fe_exec_lookup:
226 * Execute the 'LOOKUP' command. Send a list of objects of the specified type
227 * matching 'm', serialized as JSON, to the client. If specified, only objects
228 * matching the filter will be included.
229 *
230 * Returns:
231 * - 0 on success
232 * - a negative value else
233 */
234 int
235 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
236 sdb_store_matcher_t *m, sdb_store_matcher_t *filter);
238 /*
239 * sdb_fe_exec_timeseries:
240 * Execute the 'TIMESERIES' command. Send the time-series for the specified
241 * host's metric, serialized as JSON, to the client. See
242 * sdb_store_fetch_timeseries for details.
243 *
244 * Returns:
245 * - 0 on success
246 * - a negative value else
247 */
248 int
249 sdb_fe_exec_timeseries(sdb_conn_t *conn,
250 const char *hostname, const char *metric,
251 sdb_timeseries_opts_t *opts);
253 /*
254 * sdb_fe_store_host, sdb_fe_store_service, sdb_fe_store_metric,
255 * sdb_fe_store_attribute:
256 * Execute the 'STORE' command for the respective object type.
257 *
258 * Returns:
259 * - 0 on success
260 * - a negative value else
261 */
262 int
263 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host);
264 int
265 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc);
266 int
267 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric);
268 int
269 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr);
271 #ifdef __cplusplus
272 } /* extern "C" */
273 #endif
275 #endif /* ! SDB_FRONTEND_CONNECTION_H */
277 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */