diff options
author | Gabor X Toth <*@tg-x.net> | 2014-01-06 00:09:43 +0000 |
---|---|---|
committer | Gabor X Toth <*@tg-x.net> | 2014-01-06 00:09:43 +0000 |
commit | 1a0ffe2288b97b47a5b2bfbda2f9438680429422 (patch) | |
tree | 72db4cd67f06253a60bf3e2966fd0b1bf55eba5c /src/psyc/test_psyc.c | |
parent | 43d497d7c4ebb6efae37ae4bb2f812a68aa64a32 (diff) | |
download | gnunet-1a0ffe2288b97b47a5b2bfbda2f9438680429422.tar.gz gnunet-1a0ffe2288b97b47a5b2bfbda2f9438680429422.zip |
psyc: ipc messages, notify callback for modifiers, tests
Diffstat (limited to 'src/psyc/test_psyc.c')
-rw-r--r-- | src/psyc/test_psyc.c | 224 |
1 files changed, 145 insertions, 79 deletions
diff --git a/src/psyc/test_psyc.c b/src/psyc/test_psyc.c index 704819c50..33684b125 100644 --- a/src/psyc/test_psyc.c +++ b/src/psyc/test_psyc.c | |||
@@ -25,6 +25,8 @@ | |||
25 | * @author Christian Grothoff | 25 | * @author Christian Grothoff |
26 | */ | 26 | */ |
27 | 27 | ||
28 | #include <inttypes.h> | ||
29 | |||
28 | #include "platform.h" | 30 | #include "platform.h" |
29 | #include "gnunet_crypto_lib.h" | 31 | #include "gnunet_crypto_lib.h" |
30 | #include "gnunet_common.h" | 32 | #include "gnunet_common.h" |
@@ -35,7 +37,7 @@ | |||
35 | 37 | ||
36 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) | 38 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) |
37 | 39 | ||
38 | #define DEBUG_SERVICE 1 | 40 | #define DEBUG_SERVICE 0 |
39 | 41 | ||
40 | 42 | ||
41 | /** | 43 | /** |
@@ -62,17 +64,37 @@ static struct GNUNET_CRYPTO_EddsaPublicKey slave_pub_key; | |||
62 | 64 | ||
63 | struct GNUNET_PSYC_MasterTransmitHandle *mth; | 65 | struct GNUNET_PSYC_MasterTransmitHandle *mth; |
64 | 66 | ||
67 | struct TransmitClosure | ||
68 | { | ||
69 | struct GNUNET_PSYC_MasterTransmitHandle *handle; | ||
70 | struct GNUNET_ENV_Environment *env; | ||
71 | char *data[16]; | ||
72 | const char *mod_value; | ||
73 | size_t mod_value_size; | ||
74 | uint8_t data_count; | ||
75 | uint8_t paused; | ||
76 | uint8_t n; | ||
77 | }; | ||
78 | |||
79 | struct TransmitClosure *tmit; | ||
80 | |||
65 | /** | 81 | /** |
66 | * Clean up all resources used. | 82 | * Clean up all resources used. |
67 | */ | 83 | */ |
68 | static void | 84 | static void |
69 | cleanup () | 85 | cleanup () |
70 | { | 86 | { |
71 | if (mst != NULL) | 87 | if (NULL != mst) |
72 | { | 88 | { |
73 | GNUNET_PSYC_master_stop (mst); | 89 | GNUNET_PSYC_master_stop (mst); |
74 | mst = NULL; | 90 | mst = NULL; |
75 | } | 91 | } |
92 | if (NULL != tmit) | ||
93 | { | ||
94 | GNUNET_ENV_environment_destroy (tmit->env); | ||
95 | GNUNET_free (tmit); | ||
96 | tmit = NULL; | ||
97 | } | ||
76 | GNUNET_SCHEDULER_shutdown (); | 98 | GNUNET_SCHEDULER_shutdown (); |
77 | } | 99 | } |
78 | 100 | ||
@@ -121,44 +143,40 @@ end () | |||
121 | } | 143 | } |
122 | 144 | ||
123 | 145 | ||
124 | static int | 146 | static void |
125 | method (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key, | 147 | message (void *cls, uint64_t message_id, uint32_t flags, |
126 | uint64_t message_id, const char *name, | 148 | const struct GNUNET_MessageHeader *msg) |
127 | size_t modifier_count, const struct GNUNET_ENV_Modifier *modifiers, | ||
128 | uint64_t data_offset, const void *data, size_t data_size, | ||
129 | enum GNUNET_PSYC_MessageFlags flags) | ||
130 | { | 149 | { |
131 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 150 | if (NULL == msg) |
132 | "Method: %s, modifiers: %lu, flags: %u\n%.*s\n", | 151 | { |
133 | name, modifier_count, flags, data_size, data); | 152 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
134 | return GNUNET_OK; | 153 | "Error while receiving message %llu\n", message_id); |
135 | } | 154 | return; |
155 | } | ||
136 | 156 | ||
157 | uint16_t type = ntohs (msg->type); | ||
158 | uint16_t size = ntohs (msg->size); | ||
137 | 159 | ||
138 | static int | 160 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
139 | join (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key, | 161 | "Got message part of type %u and size %u " |
140 | const char *method_name, | 162 | "belonging to message ID %llu with flags %u\n", |
141 | size_t variable_count, const struct GNUNET_ENV_Modifier *variables, | 163 | type, size, message_id, flags); |
142 | const void *data, size_t data_size, struct GNUNET_PSYC_JoinHandle *jh) | 164 | |
143 | { | 165 | if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type) |
144 | return GNUNET_OK; | 166 | end (); |
145 | } | 167 | } |
146 | 168 | ||
147 | 169 | ||
148 | struct TransmitClosure | 170 | static void |
171 | join_request (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key, | ||
172 | const char *method_name, | ||
173 | size_t variable_count, const struct GNUNET_ENV_Modifier *variables, | ||
174 | const void *data, size_t data_size, | ||
175 | struct GNUNET_PSYC_JoinHandle *jh) | ||
149 | { | 176 | { |
150 | struct GNUNET_PSYC_MasterTransmitHandle *handle; | 177 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
151 | 178 | "Got join request."); | |
152 | char *mod_names[16]; | 179 | } |
153 | char *mod_values[16]; | ||
154 | char *data[16]; | ||
155 | |||
156 | uint8_t mod_count; | ||
157 | uint8_t data_count; | ||
158 | |||
159 | uint8_t paused; | ||
160 | uint8_t n; | ||
161 | }; | ||
162 | 180 | ||
163 | 181 | ||
164 | static void | 182 | static void |
@@ -172,45 +190,95 @@ transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
172 | 190 | ||
173 | 191 | ||
174 | static int | 192 | static int |
175 | tmit_notify_mod (void *cls, size_t *data_size, void *data) | 193 | tmit_notify_mod (void *cls, uint16_t *data_size, void *data, uint8_t *oper) |
176 | { | 194 | { |
177 | struct TransmitClosure *tmit = cls; | 195 | struct TransmitClosure *tmit = cls; |
178 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 196 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
179 | "Transmit notify modifier: %lu bytes available, " | 197 | "Transmit notify modifier: %lu bytes available, " |
180 | "processing modifier %u/%u.\n", | 198 | "%u modifiers left to process.\n", |
181 | *data_size, tmit->n + 1, tmit->fragment_count); | 199 | *data_size, GNUNET_ENV_environment_get_count (tmit->env)); |
182 | /* FIXME: continuation */ | 200 | |
183 | uint16_t name_size = strlen (tmit->mod_names[tmit->n]); | 201 | enum GNUNET_ENV_Operator op = 0; |
184 | uint16_t value_size = strlen (tmit->mod_values[tmit->n]); | 202 | const char *name = NULL; |
185 | if (name_size + 1 + value_size <= *data_size) | 203 | const char *value = NULL; |
186 | return GNUNET_NO; | 204 | uint16_t name_size = 0; |
187 | 205 | size_t value_size = 0; | |
188 | *data_size = name_size + 1 + value_size; | 206 | |
189 | memcpy (data, tmit->fragments[tmit->n], *data_size); | 207 | if (NULL != tmit->mod_value && 0 < tmit->mod_value_size) |
190 | 208 | { /* Modifier continuation */ | |
191 | if (++tmit->n < tmit->mod_count) | 209 | value = tmit->mod_value; |
192 | { | 210 | if (tmit->mod_value_size <= *data_size) |
193 | return GNUNET_NO; | 211 | { |
212 | value_size = tmit->mod_value_size; | ||
213 | tmit->mod_value = NULL; | ||
214 | } | ||
215 | else | ||
216 | { | ||
217 | value_size = *data_size; | ||
218 | tmit->mod_value += value_size; | ||
219 | } | ||
220 | tmit->mod_value_size -= value_size; | ||
221 | |||
222 | if (*data_size < value_size) | ||
223 | { | ||
224 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
225 | "value larger than buffer: %u < %zu\n", | ||
226 | *data_size, value_size); | ||
227 | *data_size = 0; | ||
228 | return GNUNET_NO; | ||
229 | } | ||
230 | |||
231 | *data_size = value_size; | ||
232 | memcpy (data, value, value_size); | ||
194 | } | 233 | } |
195 | else | 234 | else if (NULL != oper) |
196 | { | 235 | { |
197 | tmit->n = 0; | 236 | if (GNUNET_NO == GNUNET_ENV_environment_shift (tmit->env, &op, &name, |
198 | return GNUNET_YES; | 237 | (void *) &value, &value_size)) |
238 | { /* No more modifiers, continue with data */ | ||
239 | *data_size = 0; | ||
240 | return GNUNET_YES; | ||
241 | } | ||
242 | |||
243 | *oper = op; | ||
244 | name_size = strlen (name); | ||
245 | |||
246 | if (name_size + 1 + value_size <= *data_size) | ||
247 | { | ||
248 | *data_size = name_size + 1 + value_size; | ||
249 | } | ||
250 | else | ||
251 | { | ||
252 | tmit->mod_value_size = value_size; | ||
253 | value_size = *data_size - name_size - 1; | ||
254 | tmit->mod_value_size -= value_size; | ||
255 | tmit->mod_value = value + value_size; | ||
256 | } | ||
257 | |||
258 | memcpy (data, name, name_size); | ||
259 | ((char *)data)[name_size] = '\0'; | ||
260 | memcpy ((char *)data + name_size + 1, value, value_size); | ||
199 | } | 261 | } |
262 | |||
263 | return 0 == tmit->mod_value_size ? GNUNET_YES : GNUNET_NO; | ||
200 | } | 264 | } |
201 | 265 | ||
202 | 266 | ||
203 | static int | 267 | static int |
204 | tmit_notify_data (void *cls, size_t *data_size, void *data) | 268 | tmit_notify_data (void *cls, uint16_t *data_size, void *data) |
205 | { | 269 | { |
206 | struct TransmitClosure *tmit = cls; | 270 | struct TransmitClosure *tmit = cls; |
271 | uint16_t size = strlen (tmit->data[tmit->n]); | ||
207 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 272 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
208 | "Transmit notify data: %lu bytes available, " | 273 | "Transmit notify data: %lu bytes available, " |
209 | "processing fragment %u/%u.\n", | 274 | "processing fragment %u/%u (size %u).\n", |
210 | *data_size, tmit->n + 1, tmit->fragment_count); | 275 | *data_size, tmit->n + 1, tmit->data_count, size); |
211 | uint16_t size = strlen (tmit->data[tmit->n]); | 276 | if (*data_size < size) |
212 | if (size <= *data_size) | 277 | { |
213 | return GNUNET_NO; | 278 | *data_size = 0; |
279 | GNUNET_assert (0); | ||
280 | return GNUNET_SYSERR; | ||
281 | } | ||
214 | 282 | ||
215 | if (GNUNET_YES == tmit->paused && tmit->n == tmit->data_count - 1) | 283 | if (GNUNET_YES == tmit->paused && tmit->n == tmit->data_count - 1) |
216 | { | 284 | { |
@@ -231,19 +299,18 @@ tmit_notify_data (void *cls, size_t *data_size, void *data) | |||
231 | } | 299 | } |
232 | 300 | ||
233 | 301 | ||
234 | void | 302 | static void |
235 | master_started (void *cls, uint64_t max_message_id) | 303 | master_started (void *cls, uint64_t max_message_id) |
236 | { | 304 | { |
237 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 305 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
238 | "Master started: %lu\n", max_message_id); | 306 | "Master started: %" PRIu64 "\n", max_message_id); |
239 | 307 | ||
240 | struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create (); | 308 | tmit = GNUNET_new (struct TransmitClosure); |
241 | GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN, | 309 | tmit->env = GNUNET_ENV_environment_create (); |
242 | "_foo", "bar baz", 7); | 310 | GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN, |
243 | GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN, | 311 | "_foo", "bar baz", 7); |
244 | "_foo_bar", "foo bar baz", 11); | 312 | GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN, |
245 | 313 | "_foo_bar", "foo bar baz", 11); | |
246 | struct TransmitClosure *tmit = GNUNET_new (struct TransmitClosure); | ||
247 | tmit->data[0] = "foo"; | 314 | tmit->data[0] = "foo"; |
248 | tmit->data[1] = "foo bar"; | 315 | tmit->data[1] = "foo bar"; |
249 | tmit->data[2] = "foo bar baz"; | 316 | tmit->data[2] = "foo bar baz"; |
@@ -255,7 +322,7 @@ master_started (void *cls, uint64_t max_message_id) | |||
255 | } | 322 | } |
256 | 323 | ||
257 | 324 | ||
258 | void | 325 | static void |
259 | slave_joined (void *cls, uint64_t max_message_id) | 326 | slave_joined (void *cls, uint64_t max_message_id) |
260 | { | 327 | { |
261 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave joined: %lu\n", max_message_id); | 328 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave joined: %lu\n", max_message_id); |
@@ -288,19 +355,19 @@ run (void *cls, | |||
288 | GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key); | 355 | GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key); |
289 | GNUNET_CRYPTO_eddsa_key_get_public (slave_key, &slave_pub_key); | 356 | GNUNET_CRYPTO_eddsa_key_get_public (slave_key, &slave_pub_key); |
290 | 357 | ||
291 | mst = GNUNET_PSYC_master_start (cfg, channel_key, | 358 | mst = GNUNET_PSYC_master_start (cfg, channel_key, GNUNET_PSYC_CHANNEL_PRIVATE, |
292 | GNUNET_PSYC_CHANNEL_PRIVATE, | 359 | &message, &join_request, &master_started, NULL); |
293 | &method, &join, &master_started, NULL); | 360 | return; /* FIXME: test slave */ |
294 | return; | 361 | |
295 | struct GNUNET_PeerIdentity origin; | 362 | struct GNUNET_PeerIdentity origin; |
296 | struct GNUNET_PeerIdentity relays[16]; | 363 | struct GNUNET_PeerIdentity relays[16]; |
297 | struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create (); | 364 | struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create (); |
298 | GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN, | 365 | GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN, |
299 | "_foo", "bar baz", 7); | 366 | "_foo", "bar baz", 7); |
300 | GNUNET_ENV_environment_add_mod (env, GNUNET_ENV_OP_ASSIGN, | 367 | GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN, |
301 | "_foo_bar", "foo bar baz", 11); | 368 | "_foo_bar", "foo bar baz", 11); |
302 | slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin, | 369 | slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin, |
303 | 16, relays, &method, &join, &slave_joined, | 370 | 16, relays, &message, &join_request, &slave_joined, |
304 | NULL, "_request_join", env, "some data", 9); | 371 | NULL, "_request_join", env, "some data", 9); |
305 | GNUNET_ENV_environment_destroy (env); | 372 | GNUNET_ENV_environment_destroy (env); |
306 | } | 373 | } |
@@ -319,8 +386,7 @@ main (int argc, char *argv[]) | |||
319 | opts, &run, NULL)) | 386 | opts, &run, NULL)) |
320 | return 1; | 387 | return 1; |
321 | #else | 388 | #else |
322 | if (0 != GNUNET_TESTING_service_run ("test-psyc", "psyc", | 389 | if (0 != GNUNET_TESTING_peer_run ("test-psyc", "test_psyc.conf", &run, NULL)) |
323 | "test_psyc.conf", &run, NULL)) | ||
324 | return 1; | 390 | return 1; |
325 | #endif | 391 | #endif |
326 | return res; | 392 | return res; |