Code

frontend: Improved parser error reporting.
[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_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. If specified, errbuf will be used to record parse
130  * errors.
131  *
132  * Returns:
133  *  - an sdb_llist_t object of sdb_conn_node_t on success
134  *  - NULL in case of an error
135  */
136 sdb_llist_t *
137 sdb_fe_parse(const char *query, int len, sdb_strbuf_t *errbuf);
139 /*
140  * sdb_fe_exec:
141  * Execute the command identified by 'node' on the specified connection.
142  *
143  * Returns:
144  *  - 0 on success
145  *  - a negative value else
146  */
147 int
148 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node);
150 /*
151  * session handling
152  */
154 /*
155  * sdb_fe_session_start:
156  * Start a new user session on the specified connection.
157  *
158  * Returns:
159  *  - 0 on success
160  *  - a negative value else
161  */
162 int
163 sdb_fe_session_start(sdb_conn_t *conn);
165 /*
166  * store access
167  */
169 /*
170  * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup:
171  * Handle the CONNECTION_QUERY, CONNECTION_FETCH, CONNECTION_LIST, and
172  * CONNECTION_LOOKUP commands respectively. It is expected that the current
173  * command has been initialized already.
174  *
175  * Returns:
176  *  - 0 on success
177  *  - a negative value else
178  */
179 int
180 sdb_fe_query(sdb_conn_t *conn);
181 int
182 sdb_fe_fetch(sdb_conn_t *conn);
183 int
184 sdb_fe_list(sdb_conn_t *conn);
185 int
186 sdb_fe_lookup(sdb_conn_t *conn);
188 /*
189  * sdb_fe_exec_fetch:
190  * Execute the 'FETCH' command. Send the named object of the specified type,
191  * serialized as JSON, to the client. If specified, only objects matching the
192  * filter will be included.
193  *
194  * Returns:
195  *  - 0 on success
196  *  - a negative value else
197  */
198 int
199 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
200                 const char *hostname, const char *name, sdb_store_matcher_t *filter);
202 /*
203  * sdb_fe_exec_list:
204  * Execute the 'LIST' command. Send a complete listing of the store,
205  * serialized as JSON, to the client. The listing includes all hosts and the
206  * specified object type. If specified, only objects matching the filter will
207  * be included.
208  *
209  * Returns:
210  *  - 0 on success
211  *  - a negative value else
212  */
213 int
214 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter);
216 /*
217  * sdb_fe_exec_lookup:
218  * Execute the 'LOOKUP' command. Send a list of objects of the specified type
219  * matching 'm', serialized as JSON, to the client. If specified, only objects
220  * matching the filter will be included.
221  *
222  * Returns:
223  *  - 0 on success
224  *  - a negative value else
225  */
226 int
227 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
228                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter);
230 /*
231  * sdb_fe_exec_timeseries:
232  * Execute the 'TIMESERIES' command. Send the time-series for the specified
233  * host's metric, serialized as JSON, to the client. See
234  * sdb_store_fetch_timeseries for details.
235  *
236  * Returns:
237  *  - 0 on success
238  *  - a negative value else
239  */
240 int
241 sdb_fe_exec_timeseries(sdb_conn_t *conn,
242                 const char *hostname, const char *metric,
243                 sdb_timeseries_opts_t *opts);
245 #ifdef __cplusplus
246 } /* extern "C" */
247 #endif
249 #endif /* ! SDB_FRONTEND_CONNECTION_H */
251 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */