Code

f72f5e267a2787b6470071e0accd1ecfc63d4f39
[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). The list has to be freed by the caller.
153  * If 'len' is less than zero, parse the whole (nul-terminated) string. If
154  * specified, errbuf will be used to record parse errors.
155  *
156  * Returns:
157  *  - an sdb_llist_t object of sdb_conn_node_t on success
158  *  - NULL in case of an error
159  */
160 sdb_llist_t *
161 sdb_fe_parse(const char *query, int len, sdb_strbuf_t *errbuf);
163 /*
164  * session handling
165  */
167 /*
168  * sdb_fe_session_start:
169  * Start a new user session on the specified connection.
170  *
171  * Returns:
172  *  - 0 on success
173  *  - a negative value else
174  */
175 int
176 sdb_fe_session_start(sdb_conn_t *conn);
178 /*
179  * store access
180  */
182 /*
183  * sdb_fe_query, sdb_fe_fetch, sdb_fe_list, sdb_fe_lookup, sdb_fe_store:
184  * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
185  * SDB_CONNECTION_LOOKUP, and SDB_CONNECTION_STORE commands respectively. It
186  * is expected that the current command has been initialized already.
187  *
188  * Returns:
189  *  - 0 on success
190  *  - a negative value else
191  */
192 int
193 sdb_fe_query(sdb_conn_t *conn);
194 int
195 sdb_fe_fetch(sdb_conn_t *conn);
196 int
197 sdb_fe_list(sdb_conn_t *conn);
198 int
199 sdb_fe_lookup(sdb_conn_t *conn);
200 int
201 sdb_fe_store(sdb_conn_t *conn);
203 /*
204  * sdb_fe_exec_fetch:
205  * Execute the 'FETCH' command. Send the named object of the specified type,
206  * serialized as JSON, to the client. If specified, only objects matching the
207  * filter will be included.
208  *
209  * Returns:
210  *  - 0 on success
211  *  - a negative value else
212  */
213 int
214 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
215                 const char *hostname, const char *name, sdb_store_matcher_t *filter);
217 /*
218  * sdb_fe_exec_list:
219  * Execute the 'LIST' command. Send a complete listing of the store,
220  * serialized as JSON, to the client. The listing includes all hosts and the
221  * specified object type. If specified, only objects matching the filter will
222  * be included.
223  *
224  * Returns:
225  *  - 0 on success
226  *  - a negative value else
227  */
228 int
229 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter);
231 /*
232  * sdb_fe_exec_lookup:
233  * Execute the 'LOOKUP' command. Send a list of objects of the specified type
234  * matching 'm', serialized as JSON, to the client. If specified, only objects
235  * matching the filter will be included.
236  *
237  * Returns:
238  *  - 0 on success
239  *  - a negative value else
240  */
241 int
242 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
243                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter);
245 /*
246  * sdb_fe_store_host, sdb_fe_store_service, sdb_fe_store_metric,
247  * sdb_fe_store_attribute:
248  * Execute the 'STORE' command for the respective object type.
249  *
250  * Returns:
251  *  - 0 on success
252  *  - a negative value else
253  */
254 int
255 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host);
256 int
257 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc);
258 int
259 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric);
260 int
261 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr);
263 #ifdef __cplusplus
264 } /* extern "C" */
265 #endif
267 #endif /* ! SDB_FRONTEND_CONNECTION_H */
269 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */