- todo
[oweals/gnunet.git] / src / mesh / test_mesh_small.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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  * @file mesh/test_mesh_small.c
22  *
23  * @brief Test for the mesh service: retransmission of traffic.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include <gauger.h>
30
31
32 #define REMOVE_DIR GNUNET_YES
33
34 struct MeshPeer
35 {
36   struct MeshPeer *prev;
37
38   struct MeshPeer *next;
39
40   struct GNUNET_TESTING_Daemon *daemon;
41
42   struct GNUNET_MESH_Handle *mesh_handle;
43 };
44
45 /**
46  * How namy messages to send
47  */
48 #define TOTAL_PACKETS 1000
49
50 /**
51  * How long until we give up on connecting the peers?
52  */
53 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
54
55 /**
56  * Time to wait for stuff that should be rather fast
57  */
58 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
59
60 /**
61  * DIFFERENT TESTS TO RUN
62  */
63 #define SETUP 0
64 #define UNICAST 1
65 #define MULTICAST 2
66 #define SPEED 3
67 #define SPEED_ACK 4
68 #define SPEED_MIN 5
69 #define SPEED_NOBUF 6
70
71 /**
72  * Which test are we running?
73  */
74 static int test;
75
76 /**
77  * String with test name
78  */
79 char *test_name;
80
81 /**
82  * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
83  */
84 static int test_backwards = GNUNET_NO;
85
86 /**
87  * How many events have happened
88  */
89 static int ok;
90
91  /**
92   * Each peer is supposed to generate the following callbacks:
93   * 1 incoming tunnel (@dest)
94   * 1 connected peer (@orig)
95   * 1 received data packet (@dest)
96   * 1 received data packet (@orig)
97   * 1 received tunnel destroy (@dest)
98   * _________________________________
99   * 5 x ok expected per peer
100   */
101 int ok_goal;
102
103
104 /**
105  * Size of each test packet
106  */
107 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
108
109 /**
110  * Is the setup initialized?
111  */
112 static int initialized;
113
114 /**
115  * Peers that have been connected
116  */
117 static int peers_in_tunnel;
118
119 /**
120  * Peers that have responded
121  */
122 static int peers_responded;
123
124 /**
125  * Number of payload packes sent
126  */
127 static int data_sent;
128
129 /**
130  * Number of payload packets received
131  */
132 static int data_received;
133
134 /**
135  * Number of payload packed explicitly (app level) acknowledged
136  */
137 static int data_ack;
138
139 /**
140  * Be verbose
141  */
142 static int verbose;
143
144 /**
145  * Total number of peers in the test.
146  */
147 static unsigned long long num_peers;
148
149 /**
150  * Global configuration file
151  */
152 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
153
154 /**
155  * Total number of currently running peers.
156  */
157 static unsigned long long peers_running;
158
159 /**
160  * Total number of connections in the whole network.
161  */
162 static unsigned int total_connections;
163
164 /**
165  * The currently running peer group.
166  */
167 static struct GNUNET_TESTING_PeerGroup *pg;
168
169 /**
170  * File to report results to.
171  */
172 static struct GNUNET_DISK_FileHandle *output_file;
173
174 /**
175  * File to log connection info, statistics to.
176  */
177 static struct GNUNET_DISK_FileHandle *data_file;
178
179 /**
180  * Wait time
181  */
182 static struct GNUNET_TIME_Relative wait_time;
183
184 /**
185  * Task called to disconnect peers.
186  */
187 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
188
189 /**
190  * Task To perform tests
191  */
192 static GNUNET_SCHEDULER_TaskIdentifier test_task;
193
194 /**
195  * Task called to shutdown test.
196  */
197 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
198
199 /**
200  * Filename of the file containing the topology.
201  */
202 static char *topology_file;
203
204 /**
205  * Testbed handle for the root peer
206  */
207 static struct GNUNET_TESTING_Daemon *d1;
208
209 /**
210  * Testbed handle for the first leaf peer
211  */
212 static struct GNUNET_TESTING_Daemon *d2;
213
214 /**
215  * Testbed handle for the second leaf peer
216  */
217 static struct GNUNET_TESTING_Daemon *d3;
218
219 /**
220  * Mesh handle for the root peer
221  */
222 static struct GNUNET_MESH_Handle *h1;
223
224 /**
225  * Mesh handle for the first leaf peer
226  */
227 static struct GNUNET_MESH_Handle *h2;
228
229 /**
230  * Mesh handle for the second leaf peer
231  */
232 static struct GNUNET_MESH_Handle *h3;
233
234 /**
235  * Tunnel handle for the root peer
236  */
237 static struct GNUNET_MESH_Tunnel *t;
238
239 /**
240  * Tunnel handle for the first leaf peer
241  */
242 static struct GNUNET_MESH_Tunnel *incoming_t;
243
244 /**
245  * Tunnel handle for the second leaf peer
246  */
247 static struct GNUNET_MESH_Tunnel *incoming_t2;
248
249 /**
250  * Time we started the data transmission (after tunnel has been established
251  * and initilized).
252  */
253 static struct GNUNET_TIME_Absolute start_time;
254
255
256 /**
257  * Show the results of the test (banwidth acheived) and log them to GAUGER
258  */
259 static void
260 show_end_data (void)
261 {
262   static struct GNUNET_TIME_Absolute end_time;
263   static struct GNUNET_TIME_Relative total_time;
264
265   end_time = GNUNET_TIME_absolute_get();
266   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
267   FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
268   FPRINTF (stderr, "Test time %llu ms\n",
269             (unsigned long long) total_time.rel_value);
270   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
271             4 * TOTAL_PACKETS * 1.0 / total_time.rel_value); // 4bytes * ms
272   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
273             TOTAL_PACKETS * 1000.0 / total_time.rel_value); // packets * ms
274   GAUGER ("MESH", test_name,
275           TOTAL_PACKETS * 1000.0 / total_time.rel_value,
276           "packets/s");
277 }
278
279
280 /**
281  * Check whether peers successfully shut down.
282  * 
283  * @param cls Closure (unused).
284  * @param emsg Error message.
285  */
286 static void
287 shutdown_callback (void *cls, const char *emsg)
288 {
289   if (emsg != NULL)
290   {
291     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292                 "Shutdown of peers failed!\n");
293     ok--;
294   }
295   else
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298                 "All peers successfully shut down!\n");
299   }
300   GNUNET_CONFIGURATION_destroy (testing_cfg);
301 }
302
303
304 /**
305  * Shut down peergroup, clean up.
306  * 
307  * @param cls Closure (unused).
308  * @param tc Task Context.
309  */
310 static void
311 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
312 {
313   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314               "Ending test.\n");
315   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
316   {
317     GNUNET_SCHEDULER_cancel (disconnect_task);
318     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
319   }
320
321   if (NULL != h1)
322   {
323     GNUNET_MESH_disconnect (h1);
324     h1 = NULL;
325   }
326   if (NULL != h2)
327   {
328     GNUNET_MESH_disconnect (h2);
329     h2 = NULL;
330   }
331   if (test == MULTICAST && NULL != h3)
332   {
333     GNUNET_MESH_disconnect (h3);
334     h3 = NULL;
335   }
336   
337   if (data_file != NULL)
338     GNUNET_DISK_file_close (data_file);
339   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
340 }
341
342
343 /**
344  * Disconnect from mesh services af all peers, call shutdown.
345  * 
346  * @param cls Closure (unused).
347  * @param tc Task Context.
348  */
349 static void
350 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
351 {
352   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353               "disconnecting mesh service of peers\n");
354   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
355   if (NULL != t)
356   {
357     GNUNET_MESH_tunnel_destroy(t);
358     t = NULL;
359   }
360   if (NULL != incoming_t)
361   {
362     GNUNET_MESH_tunnel_destroy(incoming_t);
363     incoming_t = NULL;
364   }
365   if (NULL != incoming_t2)
366   {
367     GNUNET_MESH_tunnel_destroy(incoming_t2);
368     incoming_t2 = NULL;
369   }
370   GNUNET_MESH_disconnect (h1);
371   GNUNET_MESH_disconnect (h2);
372   h1 = h2 = NULL;
373   if (test == MULTICAST)
374   {
375     GNUNET_MESH_disconnect (h3);
376     h3 = NULL;
377   }
378   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
379   {
380     GNUNET_SCHEDULER_cancel (shutdown_handle);
381     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
382   }
383 }
384
385
386 /**
387  * Transmit ready callback.
388  * 
389  * @param cls Closure (message type).
390  * @param size Size of the tranmist buffer.
391  * @param buf Pointer to the beginning of the buffer.
392  * 
393  * @return Number of bytes written to buf.
394  */
395 static size_t
396 tmt_rdy (void *cls, size_t size, void *buf);
397
398
399 /**
400  * Task to schedule a new data transmission.
401  * 
402  * @param cls Closure (peer #).
403  * @param tc Task Context.
404  */
405 static void
406 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
407 {
408   struct GNUNET_MESH_TransmitHandle *th;
409   struct GNUNET_MESH_Tunnel *tunnel;
410   struct GNUNET_PeerIdentity *destination;
411
412   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
413     return;
414
415   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
416   if (GNUNET_YES == test_backwards)
417   {
418     tunnel = incoming_t;
419     destination = &d1->id;
420   }
421   else
422   {
423     tunnel = t;
424     destination = &d2->id;
425   }
426   th = GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
427                                           GNUNET_TIME_UNIT_FOREVER_REL,
428                                           destination,
429                                             size_payload,
430                                           &tmt_rdy, (void *) 1L);
431   if (NULL == th)
432   {
433     unsigned long i = (unsigned long) cls;
434
435     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
436     if (0 == i)
437     {
438       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  in 1 ms\n");
439       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
440                                     &data_task, (void *)1UL);
441     }
442     else
443     {
444       i++;
445       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
446       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
447                                       GNUNET_TIME_UNIT_MILLISECONDS,
448                                       i),
449                                     &data_task, (void *)i);
450     }
451   }
452 }
453
454
455 /**
456  * Transmit ready callback
457  *
458  * @param cls Closure (message type).
459  * @param size Size of the buffer we have.
460  * @param buf Buffer to copy data to.
461  */
462 size_t
463 tmt_rdy (void *cls, size_t size, void *buf)
464 {
465   struct GNUNET_MessageHeader *msg = buf;
466   uint32_t *data;
467
468   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469               " tmt_rdy called\n");
470   if (size < size_payload || NULL == buf)
471   {
472     GNUNET_break (0);
473     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
474                 "size %u, buf %p, data_sent %u, data_received %u\n",
475                 size,
476                 buf,
477                 data_sent,
478                 data_received);
479     return 0;
480   }
481   msg->size = htons (size);
482   msg->type = htons ((long) cls);
483   data = (uint32_t *) &msg[1];
484   *data = htonl (data_sent);
485   if (SPEED == test && GNUNET_YES == initialized)
486   {
487     data_sent++;
488     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489               " Sent packet %d\n", data_sent);
490     if (data_sent < TOTAL_PACKETS)
491     {
492       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493               " Scheduling packet %d\n", data_sent + 1);
494       GNUNET_SCHEDULER_add_now(&data_task, NULL);
495     }
496   }
497   return size_payload;
498 }
499
500
501 /**
502  * Function is called whenever a message is received.
503  *
504  * @param cls closure (set from GNUNET_MESH_connect)
505  * @param tunnel connection to the other end
506  * @param tunnel_ctx place to store local state associated with the tunnel
507  * @param sender who sent the message
508  * @param message the actual message
509  * @param atsi performance data for the connection
510  * @return GNUNET_OK to keep the connection open,
511  *         GNUNET_SYSERR to close it (signal serious error)
512  */
513 int
514 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
515                const struct GNUNET_PeerIdentity *sender,
516                const struct GNUNET_MessageHeader *message,
517                const struct GNUNET_ATS_Information *atsi)
518 {
519   long client = (long) cls;
520   long expected_target_client;
521   uint32_t *data;
522
523   ok++;
524
525   if ((ok % 20) == 0)
526   {
527     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
528     {
529       GNUNET_SCHEDULER_cancel (disconnect_task);
530       disconnect_task =
531               GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
532                                             NULL);
533     }
534   }
535
536   switch (client)
537   {
538   case 1L:
539     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
540     peers_responded++;
541     if (test == MULTICAST && peers_responded < 2)
542       return GNUNET_OK;
543     break;
544   case 2L:
545   case 3L:
546     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
547                 "Leaf client %li got a message.\n",
548                 client);
549     client = 2L;
550     break;
551   default:
552     GNUNET_assert (0);
553     break;
554   }
555   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
556   data = (uint32_t *) &message[1];
557   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
558   if (SPEED == test && GNUNET_YES == test_backwards)
559   {
560     expected_target_client = 1L;
561   }
562   else
563   {
564     expected_target_client = 2L;
565   }
566
567   if (GNUNET_NO == initialized)
568   {
569     initialized = GNUNET_YES;
570     start_time = GNUNET_TIME_absolute_get ();
571     if (SPEED == test)
572     {
573       GNUNET_assert (2L == client);
574       GNUNET_SCHEDULER_add_now (&data_task, NULL);
575       return GNUNET_OK;
576     }
577   }
578
579   if (client == expected_target_client) // Normally 2 or 3
580   {
581     data_received++;
582     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
583                 " received data %u\n", data_received);
584     if (SPEED != test || (ok_goal - 2) == ok)
585     {
586       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
587                                         GNUNET_TIME_UNIT_FOREVER_REL, sender,
588                                                size_payload,
589                                         &tmt_rdy, (void *) 1L);
590       return GNUNET_OK;
591     }
592     else
593     {
594       if (data_received < TOTAL_PACKETS)
595         return GNUNET_OK;
596     }
597   }
598   else // Normally 1
599   {
600     if (test == SPEED_ACK || test == SPEED)
601     {
602       data_ack++;
603       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
604               " received ack %u\n", data_ack);
605       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
606                                         GNUNET_TIME_UNIT_FOREVER_REL, sender,
607                                                size_payload,
608                                         &tmt_rdy, (void *) 1L);
609       if (data_ack < TOTAL_PACKETS && SPEED != test)
610         return GNUNET_OK;
611       if (ok == 2 && SPEED == test)
612         return GNUNET_OK;
613       show_end_data();
614     }
615     GNUNET_MESH_tunnel_destroy (t);
616     t = NULL;
617   }
618
619   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
620   {
621     GNUNET_SCHEDULER_cancel (disconnect_task);
622     disconnect_task =
623         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
624                                       NULL);
625   }
626
627   return GNUNET_OK;
628 }
629
630
631 /**
632  * Handlers, for diverse services
633  */
634 static struct GNUNET_MESH_MessageHandler handlers[] = {
635   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
636   {NULL, 0, 0}
637 };
638
639
640 /**
641  * Method called whenever another peer has added us to a tunnel
642  * the other peer initiated.
643  *
644  * @param cls closure
645  * @param tunnel new handle to the tunnel
646  * @param initiator peer that started the tunnel
647  * @param atsi performance information for the tunnel
648  * @return initial tunnel context for the tunnel
649  *         (can be NULL -- that's not an error)
650  */
651 static void *
652 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
653                  const struct GNUNET_PeerIdentity *initiator,
654                  const struct GNUNET_ATS_Information *atsi)
655 {
656   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
657               "Incoming tunnel from %s to peer %d\n",
658               GNUNET_i2s (initiator), (long) cls);
659   ok++;
660   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
661   if ((long) cls == 2L)
662     incoming_t = tunnel;
663   else if ((long) cls == 3L)
664     incoming_t2 = tunnel;
665   else
666   {
667     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
668                 "Incoming tunnel for unknown client %lu\n", (long) cls);
669     GNUNET_break(0);
670   }
671   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
672   {
673     GNUNET_SCHEDULER_cancel (disconnect_task);
674     disconnect_task =
675         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
676   }
677
678   return NULL;
679 }
680
681 /**
682  * Function called whenever an inbound tunnel is destroyed.  Should clean up
683  * any associated state.
684  *
685  * @param cls closure (set from GNUNET_MESH_connect)
686  * @param tunnel connection to the other end (henceforth invalid)
687  * @param tunnel_ctx place where local state associated
688  *                   with the tunnel is stored
689  */
690 static void
691 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
692                 void *tunnel_ctx)
693 {
694   long i = (long) cls;
695
696   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
697               "Incoming tunnel disconnected at peer %d\n",
698               i);
699   if (2L == i)
700   {
701     ok++;
702     incoming_t = NULL;
703   }
704   else if (3L == i)
705   {
706     ok++;
707     incoming_t2 = NULL;
708   }
709   else
710     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
711                 "Unknown peer! %d\n", i);
712   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
713   peers_in_tunnel--;
714   if (peers_in_tunnel > 0)
715     return;
716
717   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
718   {
719     GNUNET_SCHEDULER_cancel (disconnect_task);
720     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, NULL);
721   }
722
723   return;
724 }
725
726
727 /**
728  * Method called whenever a tunnel falls apart.
729  *
730  * @param cls closure
731  * @param peer peer identity the tunnel stopped working with
732  */
733 static void
734 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
735 {
736   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737               "peer %s disconnected\n",
738               GNUNET_i2s (peer));
739   return;
740 }
741
742
743 /**
744  * Method called whenever a peer connects to a tunnel.
745  *
746  * @param cls closure
747  * @param peer peer identity the tunnel was created to, NULL on timeout
748  * @param atsi performance data for the connection
749  */
750 static void
751 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
752     const struct GNUNET_ATS_Information *atsi)
753 {
754   struct GNUNET_PeerIdentity *dest;
755
756   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
757               "peer %s connected\n", GNUNET_i2s (peer));
758
759   if (0 == memcmp (&d2->id, peer, sizeof (d2->id)) && (long) cls == 1L)
760   {
761     ok++;
762   }
763   if (test == MULTICAST && 0 == memcmp (&d3->id, peer, sizeof (d3->id)) &&
764       (long) cls == 1L)
765   {
766     ok++;
767   }
768   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
769   switch (test)
770   {
771     case UNICAST:
772     case SPEED:
773     case SPEED_ACK:
774       // incoming_t is NULL unless we send a relevant data packet
775       dest = &d2->id;
776       break;
777     case MULTICAST:
778       peers_in_tunnel++;
779       if (peers_in_tunnel < 2)
780         return;
781       dest = NULL;
782       break;
783     default:
784       GNUNET_assert (0);
785       return;
786   }
787   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
788   {
789     GNUNET_SCHEDULER_cancel (disconnect_task);
790     disconnect_task =
791         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
792     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793                 "Sending data initializer...\n");
794     peers_responded = 0;
795     data_ack = 0;
796     data_received = 0;
797     data_sent = 0;
798     GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
799                                        GNUNET_TIME_UNIT_FOREVER_REL, dest,
800                                            size_payload,
801                                        &tmt_rdy, (void *) 1L);
802   }
803   else
804   {
805     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806                 "Disconnect already run?\n");
807     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808                 "Aborting...\n");
809   }
810   return;
811 }
812
813
814 /**
815  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
816  * 
817  * Testcase continues when the root receives confirmation of connected peers,
818  * on callback funtion ch.
819  * 
820  * @param cls Closure (unsued).
821  * @param tc Task Context.
822  */
823 static void
824 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 {
826   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
827   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
828   GNUNET_MESH_peer_request_connect_add (t, &d2->id);
829
830   if (test == MULTICAST)
831   {
832     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833                 "add peer 3\n");
834     GNUNET_MESH_peer_request_connect_add (t, &d3->id);
835   }
836
837   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838               "schedule timeout in TIMEOUT\n");
839   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
840   {
841     GNUNET_SCHEDULER_cancel (disconnect_task);
842     disconnect_task =
843         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_mesh_peers, NULL);
844   }
845 }
846
847
848 /**
849  * connect_mesh_service: connect to the mesh service of one of the peers
850  *
851  * @param cls Closure (unsued).
852  * @param tc Task Context.
853  */
854 static void
855 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
856 {
857   GNUNET_MESH_ApplicationType app;
858
859   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
860     return;
861
862   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863               "connect_mesh_service\n");
864
865   d2 = GNUNET_TESTING_daemon_get (pg, num_peers - 1);
866   if (test == MULTICAST)
867   {
868     d3 = GNUNET_TESTING_daemon_get (pg, num_peers - 2);
869   }
870   app = (GNUNET_MESH_ApplicationType) 0;
871
872   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873               "connecting to mesh service of peer %s\n",
874               GNUNET_i2s (&d1->id));
875   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876               "connecting to mesh service of peer %s\n",
877               GNUNET_i2s (&d2->id));
878   if (test == MULTICAST)
879   {
880     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881                 "connecting to mesh service of peer %s\n",
882                 GNUNET_i2s (&d3->id));
883   }
884   h1 = GNUNET_MESH_connect (d1->cfg, (void *) 1L, NULL, &tunnel_cleaner,
885                             handlers, &app);
886   h2 = GNUNET_MESH_connect (d2->cfg, (void *) 2L, &incoming_tunnel,
887                             &tunnel_cleaner, handlers, &app);
888   if (test == MULTICAST)
889   {
890     h3 = GNUNET_MESH_connect (d3->cfg, (void *) 3L, &incoming_tunnel,
891                               &tunnel_cleaner, handlers, &app);
892   }
893   t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
894   if (SPEED_MIN == test)
895   {
896     GNUNET_MESH_tunnel_speed_min(t);
897     test = SPEED;
898   }
899   if (SPEED_NOBUF == test)
900   {
901     GNUNET_MESH_tunnel_buffer(t, GNUNET_NO);
902     test = SPEED;
903   }
904   peers_in_tunnel = 0;
905   test_task =
906       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
907                                     (GNUNET_TIME_UNIT_SECONDS, 1), &do_test,
908                                     NULL);
909 }
910
911
912
913 /**
914  * peergroup_ready: start test when all peers are connected
915  * 
916  * @param cls closure
917  * @param emsg error message
918  */
919 static void
920 peergroup_ready (void *cls, const char *emsg)
921 {
922   char *buf;
923   int buf_len;
924   unsigned int i;
925
926   if (emsg != NULL)
927   {
928     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929                 "Peergroup callback called with error, aborting test!\n");
930     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931                 "Error from testing: `%s'\n", emsg);
932     ok--;
933     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
934     return;
935   }
936   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937               "Peer Group started successfully with %u connections\n",
938               total_connections);
939   if (data_file != NULL)
940   {
941     buf = NULL;
942     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
943     if (buf_len > 0)
944       GNUNET_DISK_file_write (data_file, buf, buf_len);
945     GNUNET_free (buf);
946   }
947   peers_running = GNUNET_TESTING_daemons_running (pg);
948   for (i = 0; i < num_peers; i++)
949   {
950     GNUNET_PEER_Id peer_id;
951
952     d1 = GNUNET_TESTING_daemon_get (pg, i);
953     peer_id = GNUNET_PEER_intern (&d1->id);
954     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u: %s\n",
955                 peer_id, GNUNET_i2s (&d1->id));
956   }
957   d1 = GNUNET_TESTING_daemon_get (pg, 0);
958   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959               "Peer looking: %s\n",
960               GNUNET_i2s (&d1->id));
961
962   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
963   disconnect_task =
964       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
965
966 }
967
968
969 /**
970  * Function that will be called whenever two daemons are connected by
971  * the testing library.
972  *
973  * @param cls closure
974  * @param first peer id for first daemon
975  * @param second peer id for the second daemon
976  * @param distance distance between the connected peers
977  * @param first_cfg config for the first daemon
978  * @param second_cfg config for the second daemon
979  * @param first_daemon handle for the first daemon
980  * @param second_daemon handle for the second daemon
981  * @param emsg error message (NULL on success)
982  */
983 static void
984 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
985             const struct GNUNET_PeerIdentity *second, uint32_t distance,
986             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
987             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
988             struct GNUNET_TESTING_Daemon *first_daemon,
989             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
990 {
991   if (emsg == NULL)
992   {
993     total_connections++;
994   }
995   else
996   {
997     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998                 "Problem with new connection (%s)\n",
999                 emsg);
1000     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (first));
1001     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (second));
1002   }
1003
1004 }
1005
1006
1007 /**
1008  * run: load configuration options and schedule test to run (start peergroup)
1009  * 
1010  * @param cls closure
1011  * @param args argv
1012  * @param cfgfile configuration file name (can be NULL)
1013  * @param cfg configuration handle
1014  */
1015 static void
1016 run (void *cls, char *const *args, const char *cfgfile,
1017      const struct GNUNET_CONFIGURATION_Handle *cfg)
1018 {
1019   char *temp_str;
1020   struct GNUNET_TESTING_Host *hosts;
1021   char *data_filename;
1022
1023   ok = 0;
1024   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
1025
1026   GNUNET_log_setup ("test_mesh_small",
1027                     "WARNING",
1028                     NULL);
1029
1030   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031               "Starting daemons.\n");
1032   if (GNUNET_OK !=
1033       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
1034                                              "num_peers", &num_peers))
1035   {
1036     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1037                 "Option TESTING:NUM_PEERS is required!\n");
1038     return;
1039   }
1040
1041   if (GNUNET_OK !=
1042       GNUNET_CONFIGURATION_get_value_time (testing_cfg, "test_mesh_small",
1043                                            "WAIT_TIME", &wait_time))
1044   {
1045     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1046                 "Option test_mesh_small:wait_time is required!\n");
1047     return;
1048   }
1049
1050   if (GNUNET_OK !=
1051       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing_old",
1052                                              "topology_output_file",
1053                                              &topology_file))
1054   {
1055     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1056                 "Option test_mesh_small:topology_output_file is required!\n");
1057     return;
1058   }
1059
1060   if (GNUNET_OK ==
1061       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
1062                                              "data_output_file",
1063                                              &data_filename))
1064   {
1065     data_file =
1066         GNUNET_DISK_file_open (data_filename,
1067                                GNUNET_DISK_OPEN_READWRITE |
1068                                GNUNET_DISK_OPEN_CREATE,
1069                                GNUNET_DISK_PERM_USER_READ |
1070                                GNUNET_DISK_PERM_USER_WRITE);
1071     if (data_file == NULL)
1072     {
1073       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
1074                   data_filename);
1075       GNUNET_free (data_filename);
1076     }
1077   }
1078
1079   if (GNUNET_YES ==
1080       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
1081                                              "output_file", &temp_str))
1082   {
1083     output_file =
1084         GNUNET_DISK_file_open (temp_str,
1085                                GNUNET_DISK_OPEN_READWRITE |
1086                                GNUNET_DISK_OPEN_CREATE,
1087                                GNUNET_DISK_PERM_USER_READ |
1088                                GNUNET_DISK_PERM_USER_WRITE);
1089     if (output_file == NULL)
1090       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
1091                   temp_str);
1092   }
1093   GNUNET_free_non_null (temp_str);
1094
1095   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
1096
1097   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
1098                                        &connect_cb, &peergroup_ready, NULL,
1099                                        hosts);
1100   GNUNET_assert (pg != NULL);
1101   shutdown_handle =
1102     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1103                                     &shutdown_task, NULL);
1104 }
1105
1106
1107
1108 /**
1109  * test_mesh_small command line options
1110  */
1111 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1112   {'V', "verbose", NULL,
1113    gettext_noop ("be verbose (print progress information)"),
1114    0, &GNUNET_GETOPT_set_one, &verbose},
1115   GNUNET_GETOPT_OPTION_END
1116 };
1117
1118
1119 /**
1120  * Main: start test
1121  */
1122 int
1123 main (int argc, char *argv[])
1124 {
1125   char * argv2[] = {
1126     argv[0],
1127     "-c",
1128     "test_mesh_small.conf",
1129     NULL
1130   };
1131   int argc2 = (sizeof (argv2) / sizeof (char *)) - 1;
1132
1133   initialized = GNUNET_NO;
1134
1135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
1136   if (strstr (argv[0], "test_mesh_small_unicast") != NULL)
1137   {
1138     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNICAST\n");
1139     test = UNICAST;
1140     test_name = "unicast";
1141     ok_goal = 5;
1142   }
1143   else if (strstr (argv[0], "test_mesh_small_multicast") != NULL)
1144   {
1145     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MULTICAST\n");
1146     test = MULTICAST;
1147     test_name = "multicast";
1148     ok_goal = 10;
1149   }
1150   else if (strstr (argv[0], "test_mesh_small_speed_ack") != NULL)
1151   {
1152    /* Each peer is supposed to generate the following callbacks:
1153     * 1 incoming tunnel (@dest)
1154     * 1 connected peer (@orig)
1155     * TOTAL_PACKETS received data packet (@dest)
1156     * TOTAL_PACKETS received data packet (@orig)
1157     * 1 received tunnel destroy (@dest)
1158     * _________________________________
1159     * 5 x ok expected per peer
1160     */
1161     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1162     test = SPEED_ACK;
1163     test_name = "speed ack";
1164     ok_goal = TOTAL_PACKETS * 2 + 3;
1165     argv2 [3] = NULL; // remove -L DEBUG
1166   }
1167   else if (strstr (argv[0], "test_mesh_small_speed") != NULL)
1168   {
1169    /* Each peer is supposed to generate the following callbacks:
1170     * 1 incoming tunnel (@dest)
1171     * 1 connected peer (@orig)
1172     * 1 initial packet (@dest)
1173     * TOTAL_PACKETS received data packet (@dest)
1174     * 1 received data packet (@orig)
1175     * 1 received tunnel destroy (@dest)
1176     * _________________________________
1177     */
1178     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1179     ok_goal = TOTAL_PACKETS + 5;
1180     if (strstr (argv[0], "_min") != NULL)
1181     {
1182       test = SPEED_MIN;
1183       test_name = "speed min";
1184     }
1185     else if (strstr (argv[0], "_nobuf") != NULL)
1186     {
1187       test = SPEED_NOBUF;
1188       test_name = "speed nobuf";
1189     }
1190     else
1191     {
1192       test = SPEED;
1193       test_name = "speed";
1194     }
1195   }
1196   else
1197   {
1198     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1199     test = SETUP;
1200     ok_goal = 0;
1201   }
1202
1203   if (strstr (argv[0], "backwards") != NULL)
1204   {
1205     char *aux;
1206
1207     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1208     test_backwards = GNUNET_YES;
1209     aux = malloc (32); // "leaked"
1210     sprintf (aux, "backwards %s", test_name);
1211     test_name = aux;
1212   }
1213
1214   GNUNET_PROGRAM_run (argc2, argv2,
1215                       "test_mesh_small",
1216                       gettext_noop ("Test mesh in a small network."), options,
1217                       &run, NULL);
1218 #if REMOVE_DIR
1219   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
1220 #endif
1221   if (ok_goal > ok)
1222   {
1223     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1224                 "FAILED! (%d/%d)\n", ok, ok_goal);
1225     return 1;
1226   }
1227   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1228   return 0;
1229 }
1230
1231 /* end of test_mesh_small.c */