psyc: ipc messages
[oweals/gnunet.git] / src / include / gnunet_env_lib.h
1 /*
2  * This file is part of GNUnet.
3  * (C) 2013 Christian Grothoff (and other contributing authors)
4  *
5  * GNUnet is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your
8  * option) any later version.
9  *
10  * GNUnet is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with GNUnet; see the file COPYING.  If not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file include/gnunet_env_lib.h
23  * @brief Library providing operations for the @e environment of
24  *        PSYC and Social messages, and for (de)serializing variable values.
25  * @author Gabor X Toth
26  */
27
28
29 #ifndef GNUNET_ENV_LIB_H
30 #define GNUNET_ENV_LIB_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40
41 /**
42  * Possible operations on PSYC state (persistent) and transient variables (per message).
43  */
44 enum GNUNET_ENV_Operator
45 {
46   /**
47    * Set value of a transient variable.
48    */
49   GNUNET_ENV_OP_SET = ':',
50
51   /**
52    * Assign value for a persistent state variable.
53    *
54    * If an assigned value is NULL, the variable is deleted.
55    */
56   GNUNET_ENV_OP_ASSIGN = '=',
57
58   /**
59    * Augment state variable.
60    *
61    * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
62    */
63   GNUNET_ENV_OP_AUGMENT = '+',
64
65   /**
66    * Diminish state variable.
67    *
68    * Used for subtracting numbers, and removing items from a list or dictionary.
69    */
70   GNUNET_ENV_OP_DIMINISH = '-',
71
72   /**
73    * Update state variable.
74    *
75    * Used for modifying a single item of a list or dictionary.
76    */
77   GNUNET_ENV_OP_UPDATE = '@',
78 };
79
80
81 /**
82  * PSYC variable types.
83  */
84 enum GNUNET_ENV_Type
85 {
86   GNUNET_ENV_TYPE_DATA = 0,
87   GNUNET_ENV_TYPE_NUMBER,
88   GNUNET_ENV_TYPE_LIST,
89   GNUNET_ENV_TYPE_DICT
90 };
91
92
93 /**
94  * PSYC state modifier.
95  */
96 struct GNUNET_ENV_Modifier {
97   /**
98    * State operation.
99    */
100   enum GNUNET_ENV_Operator oper;
101
102   /**
103    * Variable name.
104    */
105   const char *name;
106
107   /**
108    * Size of @a value.
109    */
110   size_t value_size;
111
112   /**
113    * Value of variable.
114    */
115   const void *value;
116
117   /**
118    * Next modifier.
119    */
120   struct GNUNET_ENV_Modifier *next;
121
122   /**
123    * Previous modifier.
124    */
125   struct GNUNET_ENV_Modifier *prev;
126 };
127
128
129 /**
130  * Environment for a message.
131  *
132  * Contains modifiers.
133  */
134 struct GNUNET_ENV_Environment;
135
136
137 /**
138  * Create an environment.
139  *
140  * @return A newly allocated environment.
141  */
142 struct GNUNET_ENV_Environment *
143 GNUNET_ENV_environment_create ();
144
145
146 /**
147  * Add a modifier to the environment.
148  *
149  * @param env The environment.
150  * @param oper Operation to perform.
151  * @param name Name of the variable.
152  * @param value Value of the variable.
153  * @param value_size Size of @a value.
154  */
155 void
156 GNUNET_ENV_environment_add_mod (struct GNUNET_ENV_Environment *env,
157                                 enum GNUNET_ENV_Operator oper, const char *name,
158                                 const void *value, size_t value_size);
159
160
161 /**
162  * Iterator for modifiers in the environment.
163  *
164  * @param cls Closure.
165  * @param mod Modifier.
166  *
167  * @return #GNUNET_YES to continue iterating,
168  *         #GNUNET_NO to stop.
169  */
170 typedef int
171 (*GNUNET_ENV_Iterator) (void *cls, enum GNUNET_ENV_Operator oper,
172                         const char *name, const char *value,
173                         uint32_t value_size);
174
175
176 /**
177  * Iterate through all modifiers in the environment.
178  *
179  * @param env The environment.
180  * @param it Iterator.
181  * @param it_cls Closure for iterator.
182  */
183 void
184 GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
185                                 GNUNET_ENV_Iterator it, void *it_cls);
186
187
188 /**
189  * Get the number of modifiers in the environment.
190  *
191  * @param env The environment.
192  *
193  * @return Number of modifiers.
194  */
195 size_t
196 GNUNET_ENV_environment_get_mod_count (const struct GNUNET_ENV_Environment *env);
197
198
199 /**
200  * Destroy an environment.
201  *
202  * @param env The environment to destroy.
203  */
204 void
205 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env);
206
207
208 /**
209  * Get the type of variable.
210  *
211  * @param name Name of the variable.
212  *
213  * @return Variable type.
214  */
215 enum GNUNET_ENV_Type
216 GNUNET_ENV_var_get_type (char *name);
217
218
219 /**
220  * Perform an operation on a variable.
221  *
222  * @param name Name of variable.
223  * @param current_value Current value of variable.
224  * @param current_value_size Size of @a current_value.
225  * @param oper Operator.
226  * @param args Arguments for the operation.
227  * @param args_size Size of @a args.
228  * @param return_value Return value.
229  * @param return_value_size Size of @a return_value.
230  *
231  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
232  */
233 int
234 GNUNET_ENV_operation (char *name, void *current_value, size_t current_value_size,
235                       enum GNUNET_ENV_Operator oper, void *args, size_t args_size,
236                       void **return_value, size_t *return_value_size);
237
238
239 /**
240  * Get the variable's value as an integer.
241  *
242  * @param size Size of value.
243  * @param value Raw value of variable.
244  * @param[out] number Value converted to a 64-bit integer.
245  *
246  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
247  */
248 int
249 GNUNET_ENV_value_to_number (size_t size, const void *value, int64_t *number);
250
251
252 /**
253  * Get the variable's value as a list.
254  *
255  * @param size Size of value.
256  * @param value Raw value of variable.
257  * @param[out] list A newly created list holding the elements.
258  *
259  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
260  */
261 int
262 GNUNET_ENV_value_to_list (size_t size, const void *value, struct GNUNET_CONTAINER_SList **list);
263
264
265 /**
266  * Get the variable's value as a dictionary.
267  *
268  * @param size Size of value.
269  * @param value Raw value of variable.
270  * @param[out] dict A newly created hashmap holding the elements of the dictionary.
271  *
272  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
273  */
274 int
275 GNUNET_ENV_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
276
277
278 /**
279  * Create a PSYC variable value from an integer.
280  *
281  * @param number The number to convert.
282  * @param[out] value_size Size of returned value.
283  *
284  * @return A newly allocated value or NULL on error.
285  */
286 void *
287 GNUNET_ENV_value_from_number (int64_t number, size_t *value_size);
288
289
290 /**
291  * Create a PSYC variable value from a list.
292  *
293  * @param list The list to convert.
294  * @param[out] value_size Size of returned value.
295  *
296  * @return A newly allocated value or NULL on error.
297  */
298 void *
299 GNUNET_ENV_value_from_list (struct GNUNET_CONTAINER_SList *list, size_t *value_size);
300
301
302 /**
303  * Create a PSYC variable value from a dictionary.
304  *
305  * @param dict The dict to convert.
306  * @param[out] value_size Size of returned value.
307  *
308  * @return A newly allocated value or NULL on error.
309  */
310 void *
311 GNUNET_ENV_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
312
313
314 #if 0                           /* keep Emacsens' auto-indent happy */
315 {
316 #endif
317 #ifdef __cplusplus
318 }
319 #endif
320
321 /* ifndef GNUNET_ENV_LIB_H */
322 #endif
323 /* end of gnunet_env_lib.h */