aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_restart_2peers.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-11-06 23:28:30 +0000
committerChristian Grothoff <christian@grothoff.org>2015-11-06 23:28:30 +0000
commit17bca33cca3c06668aa07c2750000a40a229c4a3 (patch)
tree70a1ad9c2506b4ca50b19fa0b387b46ab0b9b2e5 /src/transport/test_transport_api_restart_2peers.c
parentbfe1f56580a066da971887c9beea44c890d26dd6 (diff)
downloadgnunet-17bca33cca3c06668aa07c2750000a40a229c4a3.tar.gz
gnunet-17bca33cca3c06668aa07c2750000a40a229c4a3.zip
-removing last uses of TRANSPORT_try_connect API
Diffstat (limited to 'src/transport/test_transport_api_restart_2peers.c')
-rw-r--r--src/transport/test_transport_api_restart_2peers.c337
1 files changed, 199 insertions, 138 deletions
diff --git a/src/transport/test_transport_api_restart_2peers.c b/src/transport/test_transport_api_restart_2peers.c
index 5b47ee1d3..8d30ba15d 100644
--- a/src/transport/test_transport_api_restart_2peers.c
+++ b/src/transport/test_transport_api_restart_2peers.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2009, 2010, 2015 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
@@ -44,17 +44,17 @@ static char *test_name;
44 44
45static int ok; 45static int ok;
46 46
47static struct GNUNET_SCHEDULER_Task * die_task; 47static struct GNUNET_SCHEDULER_Task *die_task;
48 48
49static struct GNUNET_SCHEDULER_Task * send_task; 49static struct GNUNET_SCHEDULER_Task *send_task;
50 50
51static struct GNUNET_SCHEDULER_Task * reconnect_task; 51static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
52 52
53static struct PeerContext *p1; 53static struct PeerContext *p1;
54 54
55static struct PeerContext *p2; 55static struct PeerContext *p2;
56 56
57static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc; 57static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
58 58
59static struct GNUNET_TRANSPORT_TransmitHandle *th; 59static struct GNUNET_TRANSPORT_TransmitHandle *th;
60 60
@@ -71,80 +71,67 @@ static void
71end () 71end ()
72{ 72{
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n"); 73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
74 if (send_task != NULL) 74 if (NULL != send_task)
75 {
75 GNUNET_SCHEDULER_cancel (send_task); 76 GNUNET_SCHEDULER_cancel (send_task);
76 send_task = NULL; 77 send_task = NULL;
77 78 }
78 if (reconnect_task != NULL) 79 if (NULL != ats_sh)
79 GNUNET_SCHEDULER_cancel (reconnect_task); 80 {
80 reconnect_task = NULL; 81 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
81 82 ats_sh = NULL;
82 if (die_task != NULL) 83 }
84 if (NULL != die_task)
85 {
83 GNUNET_SCHEDULER_cancel (die_task); 86 GNUNET_SCHEDULER_cancel (die_task);
84 die_task = NULL; 87 die_task = NULL;
85 88 }
86 if (th != NULL) 89 if (NULL != th)
90 {
87 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 91 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
88 th = NULL; 92 th = NULL;
89 93 }
90 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1); 94 if (NULL != p1)
91 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2); 95 {
96 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
97 p1 = NULL;
98 }
99 if (NULL != p2)
100 {
101 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
102 p2 = NULL;
103 }
92} 104}
93 105
94 106
95static void 107static void
96end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 108end_badly (void *cls,
109 const struct GNUNET_SCHEDULER_TaskContext *tc)
97{ 110{
98 die_task = NULL; 111 die_task = NULL;
99 112
100 if (restarted == GNUNET_YES) 113 if (restarted == GNUNET_YES)
101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted, but communication did not resume\n"); 114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 "Peer was restarted, but communication did not resume\n");
102 116
103 if (restarted == GNUNET_NO) 117 if (restarted == GNUNET_NO)
104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT (even) restarted\n"); 118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105 119 "Peer was NOT (even) restarted\n");
106 if (reconnect_task != NULL)
107 GNUNET_SCHEDULER_cancel (reconnect_task);
108 reconnect_task = NULL;
109
110 if (send_task != NULL)
111 GNUNET_SCHEDULER_cancel (send_task);
112 send_task = NULL;
113
114 if (cc != NULL) 120 if (cc != NULL)
115 { 121 {
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n")); 122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
123 _("Fail! Could not connect peers\n"));
117 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc); 124 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
118 cc = NULL; 125 cc = NULL;
119 } 126 }
120 127 end ();
121 if (th != NULL)
122 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
123 th = NULL;
124
125 if (p1 != NULL)
126 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
127 if (p2 != NULL)
128 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
129
130 ok = GNUNET_SYSERR; 128 ok = GNUNET_SYSERR;
131} 129}
132 130
133 131
134static void 132static void
135reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 133restart_cb (struct PeerContext *p,
136{ 134 void *cls)
137 struct PeerContext *p = cls;
138
139 reconnect_task = NULL;
140 GNUNET_TRANSPORT_try_connect (p1->th, &p2->id, NULL, NULL); /*FIXME TRY_CONNECT change */
141 reconnect_task =
142 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
143}
144
145
146static void
147restart_cb (struct PeerContext *p, void *cls)
148{ 135{
149 static int c; 136 static int c;
150 137
@@ -152,26 +139,34 @@ restart_cb (struct PeerContext *p, void *cls)
152 if (c != 2) 139 if (c != 2)
153 return; 140 return;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Restarted peer %u (`%4s'), issuing reconnect\n", p->no, 142 "Restarted peer %u (`%4s'), issuing reconnect\n",
143 p->no,
156 GNUNET_i2s (&p->id)); 144 GNUNET_i2s (&p->id));
157 reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p); 145 ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
146 &p2->id,
147 1);
158} 148}
159 149
160 150
161static void 151static void
162restart (struct PeerContext *p, char *cfg_file) 152restart (struct PeerContext *p,
153 const char *cfg_file)
163{ 154{
164 GNUNET_assert (p != NULL); 155 GNUNET_assert (NULL != p);
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Restarting peer %u (`%4s')\n", 157 "Restarting peer %u (`%4s')\n",
167 p->no, 158 p->no,
168 GNUNET_i2s (&p->id)); 159 GNUNET_i2s (&p->id));
169 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p); 160 GNUNET_TRANSPORT_TESTING_restart_peer (p,
161 cfg_file,
162 &restart_cb,
163 p);
170} 164}
171 165
172 166
173static void 167static void
174notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, 168notify_receive (void *cls,
169 const struct GNUNET_PeerIdentity *peer,
175 const struct GNUNET_MessageHeader *message) 170 const struct GNUNET_MessageHeader *message)
176{ 171{
177 struct PeerContext *p = cls; 172 struct PeerContext *p = cls;
@@ -183,13 +178,19 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
183 t = p2; 178 t = p2;
184 GNUNET_assert (t != NULL); 179 GNUNET_assert (t != NULL);
185 180
186 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); 181 {
187 182 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 183
189 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n", 184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 p->no, ps, ntohs (message->type), ntohs (message->size), t->no, 185 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
191 GNUNET_i2s (&t->id)); 186 p->no,
192 GNUNET_free (ps); 187 ps,
188 ntohs (message->type),
189 ntohs (message->size),
190 t->no,
191 GNUNET_i2s (&t->id));
192 GNUNET_free (ps);
193 }
193 194
194 if ((MTYPE == ntohs (message->type)) && 195 if ((MTYPE == ntohs (message->type)) &&
195 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) 196 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
@@ -221,13 +222,15 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
221 222
222 223
223static size_t 224static size_t
224notify_ready (void *cls, size_t size, void *buf) 225notify_ready (void *cls,
226 size_t size,
227 void *buf)
225{ 228{
226 struct PeerContext *p = cls; 229 struct PeerContext *p = cls;
227 struct GNUNET_MessageHeader *hdr; 230 struct GNUNET_MessageHeader *hdr;
228 231
229 th = NULL; 232 th = NULL;
230 if (buf == NULL) 233 if (NULL == buf)
231 { 234 {
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 "Timeout occurred while waiting for transmit_ready\n"); 236 "Timeout occurred while waiting for transmit_ready\n");
@@ -239,40 +242,54 @@ notify_ready (void *cls, size_t size, void *buf)
239 } 242 }
240 243
241 GNUNET_assert (size >= 256); 244 GNUNET_assert (size >= 256);
245 hdr = buf;
246 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
247 hdr->type = htons (MTYPE);
242 248
243 if (buf != NULL)
244 { 249 {
245 hdr = buf; 250 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
246 hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); 251
247 hdr->type = htons (MTYPE); 252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
254 p2->no,
255 ps,
256 ntohs (hdr->type),
257 ntohs (hdr->size),
258 p->no,
259 GNUNET_i2s (&p->id));
260 GNUNET_free (ps);
248 } 261 }
249 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
250 262
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
253 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
254 GNUNET_i2s (&p->id));
255 GNUNET_free (ps);
256 return sizeof (struct GNUNET_MessageHeader); 263 return sizeof (struct GNUNET_MessageHeader);
257} 264}
258 265
259 266
260static void 267static void
261sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 268sendtask (void *cls,
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
262{ 270{
263 send_task = NULL; 271 send_task = NULL;
264 272
265 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 273 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
266 return; 274 return;
267 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
268 275
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 276 {
270 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", 277 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
271 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s); 278
272 GNUNET_free (receiver_s); 279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
281 p2->no,
282 GNUNET_i2s (&p2->id),
283 p1->no,
284 receiver_s);
285 GNUNET_free (receiver_s);
286 }
273 287
274 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 288 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
275 TIMEOUT_TRANSMIT, &notify_ready, 289 &p1->id,
290 256,
291 TIMEOUT_TRANSMIT,
292 &notify_ready,
276 p1); 293 p1);
277} 294}
278 295
@@ -281,64 +298,90 @@ static void
281notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer) 298notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
282{ 299{
283 static int c; 300 static int c;
284
285 c++;
286 struct PeerContext *p = cls; 301 struct PeerContext *p = cls;
287 struct PeerContext *t = NULL; 302 struct PeerContext *t = NULL;
288 303
289 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) 304 c++;
305 if (0 == memcmp (peer,
306 &p1->id,
307 sizeof (struct GNUNET_PeerIdentity)))
290 t = p1; 308 t = p1;
291 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) 309 if (0 == memcmp (peer,
310 &p2->id,
311 sizeof (struct GNUNET_PeerIdentity)))
292 t = p2; 312 t = p2;
293 GNUNET_assert (t != NULL); 313 GNUNET_assert (t != NULL);
294 314
295 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); 315 {
296 316 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 317
298 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps, 318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 t->no, GNUNET_i2s (peer)); 319 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
300 GNUNET_free (ps); 320 p->no,
321 ps,
322 t->no,
323 GNUNET_i2s (peer));
324 GNUNET_free (ps);
325 }
301 326
302 if ((restarted == GNUNET_YES) && (c == 4)) 327 if ((restarted == GNUNET_YES) && (c == 4))
303 { 328 {
304 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL); 329 send_task = GNUNET_SCHEDULER_add_now (&sendtask,
330 NULL);
305 } 331 }
306} 332}
307 333
308 334
309static void 335static void
310notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) 336notify_disconnect (void *cls,
337 const struct GNUNET_PeerIdentity *peer)
311{ 338{
312 struct PeerContext *p = cls; 339 struct PeerContext *p = cls;
313 340
314 341 {
315 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); 342 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
316 343
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps, 345 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
319 GNUNET_i2s (peer)); 346 p->no,
320 GNUNET_free (ps); 347 ps,
348 GNUNET_i2s (peer));
349 GNUNET_free (ps);
350 }
321 351
322 if (th != NULL) 352 if (th != NULL)
353 {
323 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 354 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
324 th = NULL; 355 th = NULL;
325 if (NULL != send_task) 356 }
357 if (NULL != send_task)
358 {
326 GNUNET_SCHEDULER_cancel (send_task); 359 GNUNET_SCHEDULER_cancel (send_task);
327 send_task = NULL; 360 send_task = NULL;
361 }
328} 362}
329 363
330 364
331static void 365static void
332testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls) 366testing_connect_cb (struct PeerContext *p1,
367 struct PeerContext *p2,
368 void *cls)
333{ 369{
334 cc = NULL; 370 cc = NULL;
335 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
336 371
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n", 372 {
338 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id)); 373 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
339 GNUNET_free (p1_c); 374
340 375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL); 376 "Peers connected: %u (%s) <-> %u (%s)\n",
377 p1->no,
378 p1_c,
379 p2->no,
380 GNUNET_i2s (&p2->id));
381 GNUNET_free (p1_c);
382 }
383 send_task = GNUNET_SCHEDULER_add_now (&sendtask,
384 NULL);
342} 385}
343 386
344 387
@@ -348,40 +391,58 @@ start_cb (struct PeerContext *p, void *cls)
348 static int started; 391 static int started;
349 392
350 started++; 393 started++;
351 394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no, 395 "Peer %u (`%s') started\n",
396 p->no,
353 GNUNET_i2s (&p->id)); 397 GNUNET_i2s (&p->id));
354
355 if (started != 2) 398 if (started != 2)
356 return; 399 return;
357 400
358 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); 401 {
359 402 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 403
361 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", 404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id)); 405 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
363 GNUNET_free (sender_c); 406 p1->no,
407 sender_c,
408 p2->no,
409 GNUNET_i2s (&p2->id));
410 GNUNET_free (sender_c);
411 }
364 412
365 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, 413 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
414 p1,
415 p2,
416 &testing_connect_cb,
366 NULL); 417 NULL);
367
368} 418}
369 419
370 420
371static void 421static void
372run (void *cls, char *const *args, const char *cfgfile, 422run (void *cls,
423 char *const *args,
424 const char *cfgfile,
373 const struct GNUNET_CONFIGURATION_Handle *cfg) 425 const struct GNUNET_CONFIGURATION_Handle *cfg)
374{ 426{
375 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); 427 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
376 428 &end_badly,
377 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1, 429 NULL);
378 &notify_receive, &notify_connect, 430 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
379 &notify_disconnect, &start_cb, 431 cfg_file_p1,
432 1,
433 &notify_receive,
434 &notify_connect,
435 &notify_disconnect,
436 &start_cb,
380 NULL); 437 NULL);
381 438
382 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2, 439 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
383 &notify_receive, &notify_connect, 440 cfg_file_p2,
384 &notify_disconnect, &start_cb, 441 2,
442 &notify_receive,
443 &notify_connect,
444 &notify_disconnect,
445 &start_cb,
385 NULL); 446 NULL);
386 447
387 if ((p1 == NULL) || (p2 == NULL)) 448 if ((p1 == NULL) || (p2 == NULL))