Code

connection: Renamed sdb_connection_read to sdb_connection_handle.
[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"
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_handle:
92  * Read from an open connection until reading would block and handle all
93  * incoming commands.
94  *
95  * Returns:
96  *  - the number of bytes read (0 on EOF)
97  *  - a negative value on error
98  */
99 ssize_t
100 sdb_connection_handle(sdb_conn_t *conn);
102 /*
103  * sdb_connection_send:
104  * Send to an open connection.
105  *
106  * Returns:
107  *  - the number of bytes written
108  *  - a negative value on error
109  */
110 ssize_t
111 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
112                 uint32_t msg_len, const char *msg);
114 /*
115  * sdb_connection_ping:
116  * Send back a backend status indicator to the connected client.
117  *
118  * Returns:
119  *  - 0 on success
120  *  - a negative value else
121  */
122 int
123 sdb_connection_ping(sdb_conn_t *conn);
125 /*
126  * sdb_fe_parse:
127  * Parse the query text specified in 'query' of length 'len' and return a list
128  * of parse trees (for each command) to be executed by sdb_fe_exec. The list
129  * has to be freed by the caller. If 'len' is less than zero, parse the whole
130  * (nul-terminated) string. If specified, errbuf will be used to record parse
131  * errors.
132  *
133  * Returns:
134  *  - an sdb_llist_t object of sdb_conn_node_t on success
135  *  - NULL in case of an error
136  */
137 sdb_llist_t *
138 sdb_fe_parse(const char *query, int len, sdb_strbuf_t *errbuf);
140 /*
141  * sdb_fe_exec:
142  * Execute the command identified by 'node' on the specified connection.
143  *
144  * Returns:
145  *  - 0 on success
146  *  - a negative value else
147  */
148 int
149 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node);
151 /*
152  * session handling
153  */
155 /*
156  * sdb_fe_session_start:
157  * Start a new user session on the specified connection.
158  *
159  * Returns:
160  *  - 0 on success
161  *  - a negative value else
162  */
163 int
164 sdb_fe_session_start(sdb_conn_t *conn);
166 /*
167  * store access
168  */
170 /*
171  * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup:
172  * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
173  * and SDB_CONNECTION_LOOKUP commands respectively. It is expected that the
174  * current command has been initialized already.
175  *
176  * Returns:
177  *  - 0 on success
178  *  - a negative value else
179  */
180 int
181 sdb_fe_query(sdb_conn_t *conn);
182 int
183 sdb_fe_fetch(sdb_conn_t *conn);
184 int
185 sdb_fe_list(sdb_conn_t *conn);
186 int
187 sdb_fe_lookup(sdb_conn_t *conn);
189 /*
190  * sdb_fe_exec_fetch:
191  * Execute the 'FETCH' command. Send the named object of the specified type,
192  * serialized as JSON, to the client. If specified, only objects matching the
193  * filter will be included.
194  *
195  * Returns:
196  *  - 0 on success
197  *  - a negative value else
198  */
199 int
200 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
201                 const char *hostname, const char *name, sdb_store_matcher_t *filter);
203 /*
204  * sdb_fe_exec_list:
205  * Execute the 'LIST' command. Send a complete listing of the store,
206  * serialized as JSON, to the client. The listing includes all hosts and the
207  * specified object type. If specified, only objects matching the filter will
208  * be included.
209  *
210  * Returns:
211  *  - 0 on success
212  *  - a negative value else
213  */
214 int
215 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter);
217 /*
218  * sdb_fe_exec_lookup:
219  * Execute the 'LOOKUP' command. Send a list of objects of the specified type
220  * matching 'm', serialized as JSON, to the client. If specified, only objects
221  * matching the filter will be included.
222  *
223  * Returns:
224  *  - 0 on success
225  *  - a negative value else
226  */
227 int
228 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
229                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter);
231 /*
232  * sdb_fe_exec_timeseries:
233  * Execute the 'TIMESERIES' command. Send the time-series for the specified
234  * host's metric, serialized as JSON, to the client. See
235  * sdb_store_fetch_timeseries for details.
236  *
237  * Returns:
238  *  - 0 on success
239  *  - a negative value else
240  */
241 int
242 sdb_fe_exec_timeseries(sdb_conn_t *conn,
243                 const char *hostname, const char *metric,
244                 sdb_timeseries_opts_t *opts);
246 #ifdef __cplusplus
247 } /* extern "C" */
248 #endif
250 #endif /* ! SDB_FRONTEND_CONNECTION_H */
252 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */