Code

frontend: Connection-specific functions now use the sdb_conn_ prefix.
[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  * session handling
151  */
153 /*
154  * sdb_conn_session_start:
155  * Start a new user session on the specified connection.
156  *
157  * Returns:
158  *  - 0 on success
159  *  - a negative value else
160  */
161 int
162 sdb_conn_session_start(sdb_conn_t *conn);
164 /*
165  * store access
166  */
168 /*
169  * sdb_conn_query, sdb_conn_fetch, sdb_conn_list, sdb_conn_lookup,
170  * sdb_conn_store:
171  * Handle the SDB_CONNECTION_QUERY, SDB_CONNECTION_FETCH, SDB_CONNECTION_LIST,
172  * SDB_CONNECTION_LOOKUP, and SDB_CONNECTION_STORE commands respectively. It
173  * is expected that the current command has been initialized already.
174  *
175  * Returns:
176  *  - 0 on success
177  *  - a negative value else
178  */
179 int
180 sdb_conn_query(sdb_conn_t *conn);
181 int
182 sdb_conn_fetch(sdb_conn_t *conn);
183 int
184 sdb_conn_list(sdb_conn_t *conn);
185 int
186 sdb_conn_lookup(sdb_conn_t *conn);
187 int
188 sdb_conn_store(sdb_conn_t *conn);
190 /*
191  * sdb_conn_store_host, sdb_conn_store_service, sdb_conn_store_metric,
192  * sdb_conn_store_attribute:
193  * Execute the 'STORE' command for the respective object type.
194  *
195  * Returns:
196  *  - 0 on success
197  *  - a negative value else
198  */
199 int
200 sdb_conn_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host);
201 int
202 sdb_conn_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc);
203 int
204 sdb_conn_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric);
205 int
206 sdb_conn_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr);
208 #ifdef __cplusplus
209 } /* extern "C" */
210 #endif
212 #endif /* ! SDB_FRONTEND_CONNECTION_H */
214 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */