Code

proto: Define a wire format for host/service/metric and add marshal functions.
[sysdb.git] / src / include / utils / proto.h
1 /*
2  * SysDB - src/include/utils/proto.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_UTILS_PROTO_H
29 #define SDB_UTILS_PROTO_H 1
31 #include "core/data.h"
33 #include <stdint.h>
34 #include <unistd.h>
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 /*
41  * sdb_proto_host, sdb_proto_service, sdb_proto_metric:
42  * Protocol-specific representations of the basic information of stored
43  * objects.
44  */
45 typedef struct {
46         sdb_time_t last_update;
47         const char *name;
48 } sdb_proto_host_t;
50 typedef struct {
51         sdb_time_t last_update;
52         const char *hostname;
53         const char *name;
54 } sdb_proto_service_t;
56 typedef struct {
57         sdb_time_t last_update;
58         const char *hostname;
59         const char *name;
60         const char *store_type; /* optional */
61         const char *store_id;   /* optional */
62 } sdb_proto_metric_t;
64 /*
65  * sdb_proto_marshal:
66  * Encode the message into the wire format by adding an appropriate header.
67  * The encoded message is written to buf which has to be large enough to store
68  * the header (64 bits) and the entire message.
69  *
70  * Returns:
71  *  - The number of bytes of the full encoded message on success. The function
72  *    does not write more than 'buf_len' bytes. If the output was truncated
73  *    then the return value is the number of bytes which would have been
74  *    written if enough space had been available.
75  *  - a negative value on error
76  */
77 ssize_t
78 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
79                 uint32_t msg_len, const char *msg);
81 /*
82  * sdb_proto_marshal_data:
83  * Encode a datum into the wire format and write it to buf.
84  *
85  * Returns:
86  *  - The number of bytes of the full encoded datum on success. The function
87  *    does not write more than 'buf_len' bytes. If the output was truncated
88  *    then the return value is the number of bytes which would have been
89  *    written if enough space had been available.
90  *  - a negative value else
91  */
92 ssize_t
93 sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum);
95 /*
96  * sdb_proto_marshal_host, sdb_proto_marshal_service,
97  * sdb_proto_marshal_metric:
98  * Encode the basic information of a stored object into the wire format and
99  * write it to buf. These functions are similar to the sdb_store_<type>
100  * functions. See their documentation for details about the arguments.
101  *
102  * Returns:
103  *  - The number of bytes of the full encoded datum on success. The function
104  *    does not write more than 'buf_len' bytes. If the output was truncated
105  *    then the return value is the number of bytes which would have been
106  *    written if enough space had been available.
107  *  - a negative value else
108  */
109 ssize_t
110 sdb_proto_marshal_host(char *buf, size_t buf_len,
111                 const sdb_proto_host_t *host);
112 ssize_t
113 sdb_proto_marshal_service(char *buf, size_t buf_len,
114                 const sdb_proto_service_t *svc);
115 ssize_t
116 sdb_proto_marshal_metric(char *buf, size_t buf_len,
117                 const sdb_proto_metric_t *metric);
119 /*
120  * sdb_proto_unmarshal_header:
121  * Read and decode a message header from the specified string.
122  *
123  * Returns:
124  *  - 0 on success
125  *  - a negative value else
126  */
127 int
128 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
129                 uint32_t *code, uint32_t *msg_len);
131 /*
132  * sdb_proto_unmarshal_int32:
133  * Read and decode a 32-bit integer from the specified string.
134  */
135 uint32_t
136 sdb_proto_unmarshal_int32(const char *buf, size_t buf_len);
138 #ifdef __cplusplus
139 } /* extern "C" */
140 #endif
142 #endif /* ! SDB_UTILS_PROTO_H */
144 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */