diff options
author | Gabor X Toth <*@tg-x.net> | 2013-09-25 17:45:59 +0000 |
---|---|---|
committer | Gabor X Toth <*@tg-x.net> | 2013-09-25 17:45:59 +0000 |
commit | ffc11bb1c2c09cda9e7bed84e56cedb8ed49d46c (patch) | |
tree | 8003be1feb88db1f585e5c480b11b2a01cf3109b | |
parent | 38f919aee243423fdc49828687c5735ab77b9128 (diff) | |
download | gnunet-ffc11bb1c2c09cda9e7bed84e56cedb8ed49d46c.tar.gz gnunet-ffc11bb1c2c09cda9e7bed84e56cedb8ed49d46c.zip |
env lib
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/env/Makefile.am | 47 | ||||
-rw-r--r-- | src/env/env.c | 131 | ||||
-rw-r--r-- | src/env/test_env.c | 85 | ||||
-rw-r--r-- | src/include/gnunet_env_lib.h | 102 |
6 files changed, 322 insertions, 45 deletions
diff --git a/configure.ac b/configure.ac index 055d55334..71832fcdf 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1305,6 +1305,7 @@ src/dns/Makefile | |||
1305 | src/dns/dns.conf | 1305 | src/dns/dns.conf |
1306 | src/dv/Makefile | 1306 | src/dv/Makefile |
1307 | src/dv/dv.conf | 1307 | src/dv/dv.conf |
1308 | src/env/Makefile | ||
1308 | src/exit/Makefile | 1309 | src/exit/Makefile |
1309 | src/experimentation/Makefile | 1310 | src/experimentation/Makefile |
1310 | src/experimentation/experimentation.conf | 1311 | src/experimentation/experimentation.conf |
diff --git a/src/Makefile.am b/src/Makefile.am index 1ef92db41..8c107bf83 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -14,6 +14,7 @@ if HAVE_EXPERIMENTAL | |||
14 | EXP_DIR = \ | 14 | EXP_DIR = \ |
15 | dv \ | 15 | dv \ |
16 | multicast \ | 16 | multicast \ |
17 | env \ | ||
17 | psyc \ | 18 | psyc \ |
18 | $(CONSENSUS) \ | 19 | $(CONSENSUS) \ |
19 | $(EXPERIMENTATION) | 20 | $(EXPERIMENTATION) |
diff --git a/src/env/Makefile.am b/src/env/Makefile.am new file mode 100644 index 000000000..67e9c6425 --- /dev/null +++ b/src/env/Makefile.am | |||
@@ -0,0 +1,47 @@ | |||
1 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
2 | |||
3 | pkgcfgdir= $(pkgdatadir)/config.d/ | ||
4 | |||
5 | libexecdir= $(pkglibdir)/libexec/ | ||
6 | |||
7 | if MINGW | ||
8 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
9 | endif | ||
10 | |||
11 | if USE_COVERAGE | ||
12 | AM_CFLAGS = --coverage -O0 | ||
13 | XLIB = -lgcov | ||
14 | endif | ||
15 | |||
16 | lib_LTLIBRARIES = libgnunetenv.la | ||
17 | |||
18 | libgnunetenv_la_SOURCES = \ | ||
19 | env.c | ||
20 | libgnunetenv_la_LIBADD = \ | ||
21 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
22 | $(GN_LIBINTL) $(XLIB) | ||
23 | libgnunetenv_la_LDFLAGS = \ | ||
24 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | ||
25 | -version-info 0:0:0 | ||
26 | libgnunetenv_la_DEPENDENCIES = \ | ||
27 | $(top_builddir)/src/util/libgnunetutil.la | ||
28 | |||
29 | if HAVE_TESTING | ||
30 | check_PROGRAMS = \ | ||
31 | test_env | ||
32 | endif | ||
33 | |||
34 | if ENABLE_TEST_RUN | ||
35 | TESTS = $(check_PROGRAMS) | ||
36 | endif | ||
37 | |||
38 | test_env_SOURCES = \ | ||
39 | test_env.c | ||
40 | test_env_LDADD = \ | ||
41 | libgnunetenv.la \ | ||
42 | $(top_builddir)/src/testing/libgnunettesting.la \ | ||
43 | $(top_builddir)/src/util/libgnunetutil.la | ||
44 | test_env_DEPENDENCIES = \ | ||
45 | libgnunetenv.la \ | ||
46 | $(top_builddir)/src/testing/libgnunettesting.la \ | ||
47 | $(top_builddir)/src/util/libgnunetutil.la | ||
diff --git a/src/env/env.c b/src/env/env.c new file mode 100644 index 000000000..c977572ab --- /dev/null +++ b/src/env/env.c | |||
@@ -0,0 +1,131 @@ | |||
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 env/env.c | ||
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 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_env_lib.h" | ||
31 | |||
32 | /** | ||
33 | * Environment for a message. | ||
34 | * | ||
35 | * Contains modifiers. | ||
36 | */ | ||
37 | struct GNUNET_ENV_Environment | ||
38 | { | ||
39 | struct GNUNET_ENV_Modifier *mod_head; | ||
40 | struct GNUNET_ENV_Modifier *mod_tail; | ||
41 | size_t mod_count; | ||
42 | }; | ||
43 | |||
44 | |||
45 | /** | ||
46 | * Create an environment. | ||
47 | * | ||
48 | * @return A newly allocated environment. | ||
49 | */ | ||
50 | struct GNUNET_ENV_Environment * | ||
51 | GNUNET_ENV_environment_create () | ||
52 | { | ||
53 | return GNUNET_new (struct GNUNET_ENV_Environment); | ||
54 | } | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Add a modifier to the environment. | ||
59 | * | ||
60 | * @param env The environment. | ||
61 | * @param oper Operation to perform. | ||
62 | * @param name Name of the variable. | ||
63 | * @param value Value of the variable. | ||
64 | * @param value_size Size of @a value. | ||
65 | */ | ||
66 | void | ||
67 | GNUNET_ENV_environment_add_mod (struct GNUNET_ENV_Environment *env, | ||
68 | enum GNUNET_ENV_Operator oper, const char *name, | ||
69 | const void *value, size_t value_size) | ||
70 | { | ||
71 | struct GNUNET_ENV_Modifier *mod = GNUNET_malloc (sizeof (*mod)); | ||
72 | mod->oper = oper; | ||
73 | mod->name = name; | ||
74 | mod->value = value; | ||
75 | mod->value_size = value_size; | ||
76 | GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod); | ||
77 | env->mod_count++; | ||
78 | } | ||
79 | |||
80 | |||
81 | /** | ||
82 | * Iterate through all modifiers in the environment. | ||
83 | * | ||
84 | * @param env The environment. | ||
85 | * @param it Iterator. | ||
86 | * @param it_cls Closure for iterator. | ||
87 | */ | ||
88 | void | ||
89 | GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env, | ||
90 | GNUNET_ENV_Iterator it, void *it_cls) | ||
91 | { | ||
92 | struct GNUNET_ENV_Modifier *mod; | ||
93 | for (mod = env->mod_head; NULL != mod; mod = mod->next) | ||
94 | it (it_cls, mod); | ||
95 | } | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Get the number of modifiers in the environment. | ||
100 | * | ||
101 | * @param env The environment. | ||
102 | * | ||
103 | * @return Number of modifiers. | ||
104 | */ | ||
105 | size_t | ||
106 | GNUNET_ENV_environment_get_mod_count (const struct GNUNET_ENV_Environment *env) | ||
107 | { | ||
108 | return env->mod_count; | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Destroy an environment. | ||
114 | * | ||
115 | * @param env The environment to destroy. | ||
116 | */ | ||
117 | void | ||
118 | GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env) | ||
119 | { | ||
120 | struct GNUNET_ENV_Modifier *mod, *prev = NULL; | ||
121 | for (mod = env->mod_head; NULL != mod; mod = mod->next) | ||
122 | { | ||
123 | if (NULL != prev) | ||
124 | GNUNET_free (prev); | ||
125 | prev = mod; | ||
126 | } | ||
127 | if (NULL != prev) | ||
128 | GNUNET_free (prev); | ||
129 | |||
130 | GNUNET_free (env); | ||
131 | } | ||
diff --git a/src/env/test_env.c b/src/env/test_env.c new file mode 100644 index 000000000..11ec9662b --- /dev/null +++ b/src/env/test_env.c | |||
@@ -0,0 +1,85 @@ | |||
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 env/test_env.c | ||
23 | * @brief Tests for the environment library. | ||
24 | * @author Gabor X Toth | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_common.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_testing_lib.h" | ||
31 | #include "gnunet_env_lib.h" | ||
32 | |||
33 | struct GNUNET_ENV_Modifier mods[] = { | ||
34 | { .oper = GNUNET_ENV_OP_SET, | ||
35 | .name = "_foo", .value = "foo", .value_size = 3 }, | ||
36 | |||
37 | { .oper = GNUNET_ENV_OP_ASSIGN, | ||
38 | .name = "_foo_bar", .value = "foo bar", .value_size = 7 }, | ||
39 | |||
40 | { .oper = GNUNET_ENV_OP_AUGMENT, | ||
41 | .name = "_foo_bar_baz", .value = "foo bar baz", .value_size = 11 } | ||
42 | }; | ||
43 | |||
44 | struct ItCls | ||
45 | { | ||
46 | size_t n; | ||
47 | }; | ||
48 | |||
49 | int | ||
50 | iterator (void *cls, struct GNUNET_ENV_Modifier *mod) | ||
51 | { | ||
52 | struct ItCls *it_cls = cls; | ||
53 | struct GNUNET_ENV_Modifier *m = &mods[it_cls->n++]; | ||
54 | |||
55 | GNUNET_assert (mod->oper == m->oper); | ||
56 | GNUNET_assert (mod->value_size == m->value_size); | ||
57 | GNUNET_assert (0 == memcmp (mod->name, m->name, strlen (m->name))); | ||
58 | GNUNET_assert (0 == memcmp (mod->value, m->value, m->value_size)); | ||
59 | |||
60 | return GNUNET_YES; | ||
61 | } | ||
62 | |||
63 | int | ||
64 | main (int argc, char *argv[]) | ||
65 | { | ||
66 | GNUNET_log_setup ("test-env", "WARNING", NULL); | ||
67 | |||
68 | struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create (); | ||
69 | GNUNET_assert (NULL != env); | ||
70 | int i, len = 3; | ||
71 | |||
72 | for (i = 0; i < len; i++) | ||
73 | { | ||
74 | GNUNET_ENV_environment_add_mod (env, mods[i].oper, mods[i].name, | ||
75 | mods[i].value, mods[i].value_size); | ||
76 | } | ||
77 | |||
78 | struct ItCls it_cls = { .n = 0 }; | ||
79 | GNUNET_ENV_environment_iterate (env, iterator, &it_cls); | ||
80 | GNUNET_assert (len == it_cls.n); | ||
81 | |||
82 | GNUNET_ENV_environment_destroy (env); | ||
83 | |||
84 | return 0; | ||
85 | } | ||
diff --git a/src/include/gnunet_env_lib.h b/src/include/gnunet_env_lib.h index 0a7e195be..89c41d2c1 100644 --- a/src/include/gnunet_env_lib.h +++ b/src/include/gnunet_env_lib.h | |||
@@ -1,22 +1,22 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | * This file is part of GNUnet. |
3 | (C) 2013 Christian Grothoff (and other contributing authors) | 3 | * (C) 2013 Christian Grothoff (and other contributing authors) |
4 | 4 | * | |
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 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 | 7 | * by the Free Software Foundation; either version 3, or (at your |
8 | option) any later version. | 8 | * option) any later version. |
9 | 9 | * | |
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | * GNUnet is distributed in the hope that it will be useful, but |
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | General Public License for more details. | 13 | * General Public License for more details. |
14 | 14 | * | |
15 | You should have received a copy of the GNU General Public License | 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 | 16 | * along with GNUnet; see the file COPYING. If not, write to the |
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | * Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file include/gnunet_env_lib.h | 22 | * @file include/gnunet_env_lib.h |
@@ -113,6 +113,16 @@ struct GNUNET_ENV_Modifier { | |||
113 | * Value of variable. | 113 | * Value of variable. |
114 | */ | 114 | */ |
115 | const void *value; | 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; | ||
116 | }; | 126 | }; |
117 | 127 | ||
118 | 128 | ||
@@ -134,52 +144,54 @@ GNUNET_ENV_environment_create (); | |||
134 | 144 | ||
135 | 145 | ||
136 | /** | 146 | /** |
137 | * Add an operation on a variable to the environment. | 147 | * Add a modifier to the environment. |
138 | * | 148 | * |
139 | * @param env The environment. | 149 | * @param env The environment. |
140 | * @param oper Operation to perform. | 150 | * @param oper Operation to perform. |
141 | * @param name Name of the variable. | 151 | * @param name Name of the variable. |
142 | * @param value_size Size of @a value. | ||
143 | * @param value Value of the variable. | 152 | * @param value Value of the variable. |
144 | * | 153 | * @param value_size Size of @a value. |
145 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error. | ||
146 | */ | 154 | */ |
147 | int | 155 | void |
148 | GNUNET_ENV_environment_operation (struct GNUNET_ENV_Environment *env, | 156 | GNUNET_ENV_environment_add_mod (struct GNUNET_ENV_Environment *env, |
149 | enum GNUNET_ENV_Operator oper, | 157 | enum GNUNET_ENV_Operator oper, const char *name, |
150 | const char *name, | 158 | const void *value, size_t value_size); |
151 | size_t value_size, const void *value); | ||
152 | 159 | ||
153 | 160 | ||
154 | /** | 161 | /** |
155 | * Get all modifiers in the environment. | 162 | * Iterator for modifiers in the environment. |
156 | * | 163 | * |
157 | * FIXME: use an iterator instead, as we'll likely use a SList to store the | 164 | * @param cls Closure. |
158 | * modifiers in the environment. | 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, struct GNUNET_ENV_Modifier *mod); | ||
172 | |||
173 | |||
174 | /** | ||
175 | * Iterate through all modifiers in the environment. | ||
159 | * | 176 | * |
160 | * @param env The environment. | 177 | * @param env The environment. |
161 | * @param[out] modifier_count Set to the number of returned modifiers. | 178 | * @param it Iterator. |
162 | * | 179 | * @param it_cls Closure for iterator. |
163 | * @return Array of modifiers. | ||
164 | */ | 180 | */ |
165 | const struct GNUNET_ENV_Modifier * | 181 | void |
166 | GNUNET_ENV_environment_get_modifiers (const struct GNUNET_ENV_Environment *env, | 182 | GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env, |
167 | size_t *modifier_count); | 183 | GNUNET_ENV_Iterator it, void *it_cls); |
168 | 184 | ||
169 | 185 | ||
170 | /** | 186 | /** |
171 | * Add list of modifiers to the environment. | 187 | * Get the number of modifiers in the environment. |
172 | * | 188 | * |
173 | * @param env The environment. | 189 | * @param env The environment. |
174 | * @param modifier_count Number of @a modifiers. | 190 | * |
175 | * @param modifiers Array of modifiers to add. | 191 | * @return Number of modifiers. |
176 | * | ||
177 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error. | ||
178 | */ | 192 | */ |
179 | int | 193 | size_t |
180 | GNUNET_ENV_environment_set_modifiers (const struct GNUNET_ENV_Environment *env, | 194 | GNUNET_ENV_environment_get_mod_count (const struct GNUNET_ENV_Environment *env); |
181 | size_t modifier_count, | ||
182 | const struct GNUNET_ENV_Modifier *modifiers); | ||
183 | 195 | ||
184 | 196 | ||
185 | /** | 197 | /** |