Code

0397e508d2eeab6f5d0fc58a9d5dbadc4496ec27
[sysdb.git] / src / include / frontend / connection.h
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_connection_server_version:
140  * Send back the backend server version to the connected client.
141  *
142  * Returns:
143  *  - 0 on success
144  *  - a negative value else
145  */
146 int
147 sdb_connection_server_version(sdb_conn_t *conn);
149 /*
150  * sdb_fe_parse:
151  * Parse the query text specified in 'query' of length 'len' and return a list
152  * of parse trees (for each command) to be executed by sdb_fe_exec. The list
153  * has to be freed by the caller. If 'len' is less than zero, parse the whole
154  * (nul-terminated) string. If specified, errbuf will be used to record parse
155  * errors.
156  *
157  * Returns:
158  *  - an sdb_llist_t object of sdb_conn_node_t on success
159  *  - NULL in case of an error
160  */
161 sdb_llist_t *
162 sdb_fe_parse(const char *query, int len, sdb_strbuf_t *errbuf);
164 /*
165  * sdb_fe_exec:
166  * Execute the command identified by 'node' on the specified connection.
167  *
168  * Returns:
169  *  - 0 on success
170  *  - a negative value else
171  */
172 int
173 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node);
175 /*
176  * session handling
177  */
179 /*
180  * sdb_fe_session_start:
181  * Start a new user session on the specified connection.
182  *
183  * Returns:
184  *  - 0 on success
185  *  - a negative value else
186  */
187 int
188 sdb_fe_session_start(sdb_conn_t *conn);
190 /*
191  * store access
192  */
194 /*
195  * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup, sdb_fe_store:
196  * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
197  * SDB_CONNECTION_LOOKUP, and SDB_CONNECTION_STORE commands respectively. It
198  * is expected that the current command has been initialized already.
199  *
200  * Returns:
201  *  - 0 on success
202  *  - a negative value else
203  */
204 int
205 sdb_fe_query(sdb_conn_t *conn);
206 int
207 sdb_fe_fetch(sdb_conn_t *conn);
208 int
209 sdb_fe_list(sdb_conn_t *conn);
210 int
211 sdb_fe_lookup(sdb_conn_t *conn);
212 int
213 sdb_fe_store(sdb_conn_t *conn);
215 /*
216  * sdb_fe_exec_fetch:
217  * Execute the 'FETCH' command. Send the named object of the specified type,
218  * serialized as JSON, to the client. If specified, only objects matching the
219  * filter will be included.
220  *
221  * Returns:
222  *  - 0 on success
223  *  - a negative value else
224  */
225 int
226 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
227                 const char *hostname, const char *name, sdb_store_matcher_t *filter);
229 /*
230  * sdb_fe_exec_list:
231  * Execute the 'LIST' command. Send a complete listing of the store,
232  * serialized as JSON, to the client. The listing includes all hosts and the
233  * specified object type. If specified, only objects matching the filter will
234  * be included.
235  *
236  * Returns:
237  *  - 0 on success
238  *  - a negative value else
239  */
240 int
241 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter);
243 /*
244  * sdb_fe_exec_lookup:
245  * Execute the 'LOOKUP' command. Send a list of objects of the specified type
246  * matching 'm', serialized as JSON, to the client. If specified, only objects
247  * matching the filter will be included.
248  *
249  * Returns:
250  *  - 0 on success
251  *  - a negative value else
252  */
253 int
254 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
255                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter);
257 /*
258  * sdb_fe_exec_timeseries:
259  * Execute the 'TIMESERIES' command. Send the time-series for the specified
260  * host's metric, serialized as JSON, to the client. See
261  * sdb_store_fetch_timeseries for details.
262  *
263  * Returns:
264  *  - 0 on success
265  *  - a negative value else
266  */
267 int
268 sdb_fe_exec_timeseries(sdb_conn_t *conn,
269                 const char *hostname, const char *metric,
270                 sdb_timeseries_opts_t *opts);
272 /*
273  * sdb_fe_store_host, sdb_fe_store_service, sdb_fe_store_metric,
274  * sdb_fe_store_attribute:
275  * Execute the 'STORE' command for the respective object type.
276  *
277  * Returns:
278  *  - 0 on success
279  *  - a negative value else
280  */
281 int
282 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host);
283 int
284 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc);
285 int
286 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric);
287 int
288 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr);
290 #ifdef __cplusplus
291 } /* extern "C" */
292 #endif
294 #endif /* ! SDB_FRONTEND_CONNECTION_H */
296 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */