Code

utils dbi + unixsock: Pass user-data object to data processing callbacks.
[sysdb.git] / src / include / utils / unixsock.h
1 /*
2  * syscollector - src/include/utils/unixsock.h
3  * Copyright (C) 2012 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 SC_UTILS_UNIXSOCK_H
29 #define SC_UTILS_UNIXSOCK_H 1
31 #include "core/object.h"
32 #include "utils/data.h"
34 #include <sys/socket.h>
36 #include <stddef.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 struct sc_unixsock_client;
43 typedef struct sc_unixsock_client sc_unixsock_client_t;
45 typedef int (*sc_unixsock_client_data_cb)(sc_unixsock_client_t *,
46                 size_t, sc_data_t *, sc_object_t *);
48 sc_unixsock_client_t *
49 sc_unixsock_client_create(const char *path);
51 int
52 sc_unixsock_client_connect(sc_unixsock_client_t *client);
54 int
55 sc_unixsock_client_send(sc_unixsock_client_t *client, const char *msg);
57 char *
58 sc_unixsock_client_recv(sc_unixsock_client_t *client, char *buffer, size_t buflen);
60 /*
61  * sc_unixsock_client_process_lines:
62  * Reads up to 'max_lines' lines from the socket, splits each line at the
63  * specified 'delim' and passes the data on to the specified 'callback'. If
64  * 'max_lines' is less than zero, the function will read until EOF or an error
65  * is encountered. If 'n_cols' is greater than zero, the function will expect
66  * that number of columns to appear in each line. Also, it will expect that
67  * number of further arguments, specifying the data-type to be returned for
68  * the respective column (see sc_data_t). The content of each column will then
69  * be converted accordingly.
70  *
71  * Returns:
72  *  - 0 on success
73  *  - a negative value else
74  */
75 int
76 sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
77                 sc_unixsock_client_data_cb callback, sc_object_t *user_data,
78                 long int max_lines, const char *delim, int n_cols, ...);
80 /*
81  * sc_unixsock_client_shutdown:
82  * Shut down the client's send and/or receive operations. If appropriate, the
83  * client will automatically re-connect on the next send / receive operation
84  * after that.
85  *
86  * See shutdown(3) for details.
87  */
88 int
89 sc_unixsock_client_shutdown(sc_unixsock_client_t *client, int how);
91 /*
92  * sc_unixsock_client_clearerr, sc_unixsock_client_eof,
93  * sc_unixsock_client_error:
94  * Check and reset the client status. See the clearerr(3), feof(3), and
95  * ferror(3) manpages for details.
96  */
97 void
98 sc_unixsock_client_clearerr(sc_unixsock_client_t *client);
99 int
100 sc_unixsock_client_eof(sc_unixsock_client_t *client);
101 int
102 sc_unixsock_client_error(sc_unixsock_client_t *client);
104 void
105 sc_unixsock_client_destroy(sc_unixsock_client_t *client);
107 const char *
108 sc_unixsock_client_path(sc_unixsock_client_t *client);
110 #ifdef __cplusplus
111 } /* extern "C" */
112 #endif
114 #endif /* ! SC_UTILS_UNIXSOCK_H */
116 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */