- feedback about abort cause
[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   int line = (int) (long ) cls;
353
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355               "disconnecting mesh service of peers, called from line %d\n",
356               line);
357   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
358   if (NULL != t)
359   {
360     GNUNET_MESH_tunnel_destroy(t);
361     t = NULL;
362   }
363   if (NULL != incoming_t)
364   {
365     GNUNET_MESH_tunnel_destroy(incoming_t);
366     incoming_t = NULL;
367   }
368   if (NULL != incoming_t2)
369   {
370     GNUNET_MESH_tunnel_destroy(incoming_t2);
371     incoming_t2 = NULL;
372   }
373   GNUNET_MESH_disconnect (h1);
374   GNUNET_MESH_disconnect (h2);
375   h1 = h2 = NULL;
376   if (test == MULTICAST)
377   {
378     GNUNET_MESH_disconnect (h3);
379     h3 = NULL;
380   }
381   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
382   {
383     GNUNET_SCHEDULER_cancel (shutdown_handle);
384     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
385   }
386 }
387
388
389 /**
390  * Transmit ready callback.
391  * 
392  * @param cls Closure (message type).
393  * @param size Size of the tranmist buffer.
394  * @param buf Pointer to the beginning of the buffer.
395  * 
396  * @return Number of bytes written to buf.
397  */
398 static size_t
399 tmt_rdy (void *cls, size_t size, void *buf);
400
401
402 /**
403  * Task to schedule a new data transmission.
404  * 
405  * @param cls Closure (peer #).
406  * @param tc Task Context.
407  */
408 static void
409 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
410 {
411   struct GNUNET_MESH_TransmitHandle *th;
412   struct GNUNET_MESH_Tunnel *tunnel;
413   struct GNUNET_PeerIdentity *destination;
414
415   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
416     return;
417
418   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
419   if (GNUNET_YES == test_backwards)
420   {
421     tunnel = incoming_t;
422     destination = &d1->id;
423   }
424   else
425   {
426     tunnel = t;
427     destination = &d2->id;
428   }
429   th = GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
430                                           GNUNET_TIME_UNIT_FOREVER_REL,
431                                           destination,
432                                             size_payload,
433                                           &tmt_rdy, (void *) 1L);
434   if (NULL == th)
435   {
436     unsigned long i = (unsigned long) cls;
437
438     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
439     if (0 == i)
440     {
441       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  in 1 ms\n");
442       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
443                                     &data_task, (void *)1UL);
444     }
445     else
446     {
447       i++;
448       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
449       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
450                                       GNUNET_TIME_UNIT_MILLISECONDS,
451                                       i),
452                                     &data_task, (void *)i);
453     }
454   }
455 }
456
457
458 /**
459  * Transmit ready callback
460  *
461  * @param cls Closure (message type).
462  * @param size Size of the buffer we have.
463  * @param buf Buffer to copy data to.
464  */
465 size_t
466 tmt_rdy (void *cls, size_t size, void *buf)
467 {
468   struct GNUNET_MessageHeader *msg = buf;
469   uint32_t *data;
470
471   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472               " tmt_rdy called\n");
473   if (size < size_payload || NULL == buf)
474   {
475     GNUNET_break (0);
476     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
477                 "size %u, buf %p, data_sent %u, data_received %u\n",
478                 size,
479                 buf,
480                 data_sent,
481                 data_received);
482     return 0;
483   }
484   msg->size = htons (size);
485   msg->type = htons ((long) cls);
486   data = (uint32_t *) &msg[1];
487   *data = htonl (data_sent);
488   if (SPEED == test && GNUNET_YES == initialized)
489   {
490     data_sent++;
491     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492               " Sent packet %d\n", data_sent);
493     if (data_sent < TOTAL_PACKETS)
494     {
495       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496               " Scheduling packet %d\n", data_sent + 1);
497       GNUNET_SCHEDULER_add_now(&data_task, NULL);
498     }
499   }
500   return size_payload;
501 }
502
503
504 /**
505  * Function is called whenever a message is received.
506  *
507  * @param cls closure (set from GNUNET_MESH_connect)
508  * @param tunnel connection to the other end
509  * @param tunnel_ctx place to store local state associated with the tunnel
510  * @param sender who sent the message
511  * @param message the actual message
512  * @param atsi performance data for the connection
513  * @return GNUNET_OK to keep the connection open,
514  *         GNUNET_SYSERR to close it (signal serious error)
515  */
516 int
517 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
518                const struct GNUNET_PeerIdentity *sender,
519                const struct GNUNET_MessageHeader *message,
520                const struct GNUNET_ATS_Information *atsi)
521 {
522   long client = (long) cls;
523   long expected_target_client;
524   uint32_t *data;
525
526   ok++;
527
528   if ((ok % 20) == 0)
529   {
530     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
531     {
532       GNUNET_SCHEDULER_cancel (disconnect_task);
533     }
534     disconnect_task =
535               GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
536                                             (void *) __LINE__);
537   }
538
539   switch (client)
540   {
541   case 1L:
542     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
543     peers_responded++;
544     if (test == MULTICAST && peers_responded < 2)
545       return GNUNET_OK;
546     break;
547   case 2L:
548   case 3L:
549     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
550                 "Leaf client %li got a message.\n",
551                 client);
552     client = 2L;
553     break;
554   default:
555     GNUNET_assert (0);
556     break;
557   }
558   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
559   data = (uint32_t *) &message[1];
560   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
561   if (SPEED == test && GNUNET_YES == test_backwards)
562   {
563     expected_target_client = 1L;
564   }
565   else
566   {
567     expected_target_client = 2L;
568   }
569
570   if (GNUNET_NO == initialized)
571   {
572     initialized = GNUNET_YES;
573     start_time = GNUNET_TIME_absolute_get ();
574     if (SPEED == test)
575     {
576       GNUNET_assert (2L == client);
577       GNUNET_SCHEDULER_add_now (&data_task, NULL);
578       return GNUNET_OK;
579     }
580   }
581
582   if (client == expected_target_client) // Normally 2 or 3
583   {
584     data_received++;
585     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
586                 " received data %u\n", data_received);
587     if (SPEED != test || (ok_goal - 2) == ok)
588     {
589       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
590                                         GNUNET_TIME_UNIT_FOREVER_REL, sender,
591                                                size_payload,
592                                         &tmt_rdy, (void *) 1L);
593       return GNUNET_OK;
594     }
595     else
596     {
597       if (data_received < TOTAL_PACKETS)
598         return GNUNET_OK;
599     }
600   }
601   else // Normally 1
602   {
603     if (test == SPEED_ACK || test == SPEED)
604     {
605       data_ack++;
606       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
607               " received ack %u\n", data_ack);
608       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
609                                         GNUNET_TIME_UNIT_FOREVER_REL, sender,
610                                                size_payload,
611                                         &tmt_rdy, (void *) 1L);
612       if (data_ack < TOTAL_PACKETS && SPEED != test)
613         return GNUNET_OK;
614       if (ok == 2 && SPEED == test)
615         return GNUNET_OK;
616       show_end_data();
617     }
618     GNUNET_MESH_tunnel_destroy (t);
619     t = NULL;
620   }
621
622   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
623   {
624     GNUNET_SCHEDULER_cancel (disconnect_task);
625   }
626   disconnect_task =
627         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
628                                       (void *) __LINE__);
629
630   return GNUNET_OK;
631 }
632
633
634 /**
635  * Handlers, for diverse services
636  */
637 static struct GNUNET_MESH_MessageHandler handlers[] = {
638   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
639   {NULL, 0, 0}
640 };
641
642
643 /**
644  * Method called whenever another peer has added us to a tunnel
645  * the other peer initiated.
646  *
647  * @param cls closure
648  * @param tunnel new handle to the tunnel
649  * @param initiator peer that started the tunnel
650  * @param atsi performance information for the tunnel
651  * @return initial tunnel context for the tunnel
652  *         (can be NULL -- that's not an error)
653  */
654 static void *
655 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
656                  const struct GNUNET_PeerIdentity *initiator,
657                  const struct GNUNET_ATS_Information *atsi)
658 {
659   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
660               "Incoming tunnel from %s to peer %d\n",
661               GNUNET_i2s (initiator), (long) cls);
662   ok++;
663   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
664   if ((long) cls == 2L)
665     incoming_t = tunnel;
666   else if ((long) cls == 3L)
667     incoming_t2 = tunnel;
668   else
669   {
670     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
671                 "Incoming tunnel for unknown client %lu\n", (long) cls);
672     GNUNET_break(0);
673   }
674   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
675   {
676     GNUNET_SCHEDULER_cancel (disconnect_task);
677   }
678   disconnect_task =
679         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
680                                       (void *) __LINE__);
681
682   return NULL;
683 }
684
685 /**
686  * Function called whenever an inbound tunnel is destroyed.  Should clean up
687  * any associated state.
688  *
689  * @param cls closure (set from GNUNET_MESH_connect)
690  * @param tunnel connection to the other end (henceforth invalid)
691  * @param tunnel_ctx place where local state associated
692  *                   with the tunnel is stored
693  */
694 static void
695 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
696                 void *tunnel_ctx)
697 {
698   long i = (long) cls;
699
700   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
701               "Incoming tunnel disconnected at peer %d\n",
702               i);
703   if (2L == i)
704   {
705     ok++;
706     incoming_t = NULL;
707   }
708   else if (3L == i)
709   {
710     ok++;
711     incoming_t2 = NULL;
712   }
713   else
714     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
715                 "Unknown peer! %d\n", i);
716   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
717   peers_in_tunnel--;
718   if (peers_in_tunnel > 0)
719     return;
720
721   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
722   {
723     GNUNET_SCHEDULER_cancel (disconnect_task);
724   }
725   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
726                                               (void *) __LINE__);
727
728   return;
729 }
730
731
732 /**
733  * Method called whenever a tunnel falls apart.
734  *
735  * @param cls closure
736  * @param peer peer identity the tunnel stopped working with
737  */
738 static void
739 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
740 {
741   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742               "peer %s disconnected\n",
743               GNUNET_i2s (peer));
744   return;
745 }
746
747
748 /**
749  * Method called whenever a peer connects to a tunnel.
750  *
751  * @param cls closure
752  * @param peer peer identity the tunnel was created to, NULL on timeout
753  * @param atsi performance data for the connection
754  */
755 static void
756 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
757     const struct GNUNET_ATS_Information *atsi)
758 {
759   struct GNUNET_PeerIdentity *dest;
760
761   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
762               "peer %s connected\n", GNUNET_i2s (peer));
763
764   if (0 == memcmp (&d2->id, peer, sizeof (d2->id)) && (long) cls == 1L)
765   {
766     ok++;
767   }
768   if (test == MULTICAST && 0 == memcmp (&d3->id, peer, sizeof (d3->id)) &&
769       (long) cls == 1L)
770   {
771     ok++;
772   }
773   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
774   switch (test)
775   {
776     case UNICAST:
777     case SPEED:
778     case SPEED_ACK:
779       // incoming_t is NULL unless we send a relevant data packet
780       dest = &d2->id;
781       break;
782     case MULTICAST:
783       peers_in_tunnel++;
784       if (peers_in_tunnel < 2)
785         return;
786       dest = NULL;
787       break;
788     default:
789       GNUNET_assert (0);
790       return;
791   }
792   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
793   {
794     GNUNET_SCHEDULER_cancel (disconnect_task);
795     disconnect_task =
796         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
797                                       (void *) __LINE__);
798     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799                 "Sending data initializer...\n");
800     peers_responded = 0;
801     data_ack = 0;
802     data_received = 0;
803     data_sent = 0;
804     GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
805                                        GNUNET_TIME_UNIT_FOREVER_REL, dest,
806                                            size_payload,
807                                        &tmt_rdy, (void *) 1L);
808   }
809   else
810   {
811     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812                 "Disconnect already run?\n");
813     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814                 "Aborting...\n");
815   }
816   return;
817 }
818
819
820 /**
821  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
822  * 
823  * Testcase continues when the root receives confirmation of connected peers,
824  * on callback funtion ch.
825  * 
826  * @param cls Closure (unsued).
827  * @param tc Task Context.
828  */
829 static void
830 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
831 {
832   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
833   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
834   GNUNET_MESH_peer_request_connect_add (t, &d2->id);
835
836   if (test == MULTICAST)
837   {
838     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839                 "add peer 3\n");
840     GNUNET_MESH_peer_request_connect_add (t, &d3->id);
841   }
842
843   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844               "schedule timeout in TIMEOUT\n");
845   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
846   {
847     GNUNET_SCHEDULER_cancel (disconnect_task);
848     disconnect_task =
849         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_mesh_peers,
850                                       (void *) __LINE__);
851   }
852 }
853
854
855 /**
856  * connect_mesh_service: connect to the mesh service of one of the peers
857  *
858  * @param cls Closure (unsued).
859  * @param tc Task Context.
860  */
861 static void
862 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
863 {
864   GNUNET_MESH_ApplicationType app;
865
866   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
867     return;
868
869   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870               "connect_mesh_service\n");
871
872   d2 = GNUNET_TESTING_daemon_get (pg, num_peers - 1);
873   if (test == MULTICAST)
874   {
875     d3 = GNUNET_TESTING_daemon_get (pg, num_peers - 2);
876   }
877   app = (GNUNET_MESH_ApplicationType) 0;
878
879   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880               "connecting to mesh service of peer %s\n",
881               GNUNET_i2s (&d1->id));
882   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883               "connecting to mesh service of peer %s\n",
884               GNUNET_i2s (&d2->id));
885   if (test == MULTICAST)
886   {
887     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888                 "connecting to mesh service of peer %s\n",
889                 GNUNET_i2s (&d3->id));
890   }
891   h1 = GNUNET_MESH_connect (d1->cfg, (void *) 1L, NULL, &tunnel_cleaner,
892                             handlers, &app);
893   h2 = GNUNET_MESH_connect (d2->cfg, (void *) 2L, &incoming_tunnel,
894                             &tunnel_cleaner, handlers, &app);
895   if (test == MULTICAST)
896   {
897     h3 = GNUNET_MESH_connect (d3->cfg, (void *) 3L, &incoming_tunnel,
898                               &tunnel_cleaner, handlers, &app);
899   }
900   t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
901   if (SPEED_MIN == test)
902   {
903     GNUNET_MESH_tunnel_speed_min(t);
904     test = SPEED;
905   }
906   if (SPEED_NOBUF == test)
907   {
908     GNUNET_MESH_tunnel_buffer(t, GNUNET_NO);
909     test = SPEED;
910   }
911   peers_in_tunnel = 0;
912   test_task =
913       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
914                                     (GNUNET_TIME_UNIT_SECONDS, 1), &do_test,
915                                     NULL);
916 }
917
918
919
920 /**
921  * peergroup_ready: start test when all peers are connected
922  * 
923  * @param cls closure
924  * @param emsg error message
925  */
926 static void
927 peergroup_ready (void *cls, const char *emsg)
928 {
929   char *buf;
930   int buf_len;
931   unsigned int i;
932
933   if (emsg != NULL)
934   {
935     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936                 "Peergroup callback called with error, aborting test!\n");
937     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938                 "Error from testing: `%s'\n", emsg);
939     ok--;
940     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
941     return;
942   }
943   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944               "Peer Group started successfully with %u connections\n",
945               total_connections);
946   if (data_file != NULL)
947   {
948     buf = NULL;
949     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
950     if (buf_len > 0)
951       GNUNET_DISK_file_write (data_file, buf, buf_len);
952     GNUNET_free (buf);
953   }
954   peers_running = GNUNET_TESTING_daemons_running (pg);
955   for (i = 0; i < num_peers; i++)
956   {
957     GNUNET_PEER_Id peer_id;
958
959     d1 = GNUNET_TESTING_daemon_get (pg, i);
960     peer_id = GNUNET_PEER_intern (&d1->id);
961     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u: %s\n",
962                 peer_id, GNUNET_i2s (&d1->id));
963   }
964   d1 = GNUNET_TESTING_daemon_get (pg, 0);
965   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966               "Peer looking: %s\n",
967               GNUNET_i2s (&d1->id));
968
969   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
970   disconnect_task =
971       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers,
972                                     (void *) __LINE__);
973
974 }
975
976
977 /**
978  * Function that will be called whenever two daemons are connected by
979  * the testing library.
980  *
981  * @param cls closure
982  * @param first peer id for first daemon
983  * @param second peer id for the second daemon
984  * @param distance distance between the connected peers
985  * @param first_cfg config for the first daemon
986  * @param second_cfg config for the second daemon
987  * @param first_daemon handle for the first daemon
988  * @param second_daemon handle for the second daemon
989  * @param emsg error message (NULL on success)
990  */
991 static void
992 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
993             const struct GNUNET_PeerIdentity *second, uint32_t distance,
994             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
995             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
996             struct GNUNET_TESTING_Daemon *first_daemon,
997             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
998 {
999   if (emsg == NULL)
1000   {
1001     total_connections++;
1002   }
1003   else
1004   {
1005     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006                 "Problem with new connection (%s)\n",
1007                 emsg);
1008     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (first));
1009     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (second));
1010   }
1011
1012 }
1013
1014
1015 /**
1016  * run: load configuration options and schedule test to run (start peergroup)
1017  * 
1018  * @param cls closure
1019  * @param args argv
1020  * @param cfgfile configuration file name (can be NULL)
1021  * @param cfg configuration handle
1022  */
1023 static void
1024 run (void *cls, char *const *args, const char *cfgfile,
1025      const struct GNUNET_CONFIGURATION_Handle *cfg)
1026 {
1027   char *temp_str;
1028   struct GNUNET_TESTING_Host *hosts;
1029   char *data_filename;
1030
1031   ok = 0;
1032   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
1033
1034   GNUNET_log_setup ("test_mesh_small",
1035                     "WARNING",
1036                     NULL);
1037
1038   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039               "Starting daemons.\n");
1040   if (GNUNET_OK !=
1041       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
1042                                              "num_peers", &num_peers))
1043   {
1044     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1045                 "Option TESTING:NUM_PEERS is required!\n");
1046     return;
1047   }
1048
1049   if (GNUNET_OK !=
1050       GNUNET_CONFIGURATION_get_value_time (testing_cfg, "test_mesh_small",
1051                                            "WAIT_TIME", &wait_time))
1052   {
1053     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1054                 "Option test_mesh_small:wait_time is required!\n");
1055     return;
1056   }
1057
1058   if (GNUNET_OK !=
1059       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing_old",
1060                                              "topology_output_file",
1061                                              &topology_file))
1062   {
1063     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1064                 "Option test_mesh_small:topology_output_file is required!\n");
1065     return;
1066   }
1067
1068   if (GNUNET_OK ==
1069       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
1070                                              "data_output_file",
1071                                              &data_filename))
1072   {
1073     data_file =
1074         GNUNET_DISK_file_open (data_filename,
1075                                GNUNET_DISK_OPEN_READWRITE |
1076                                GNUNET_DISK_OPEN_CREATE,
1077                                GNUNET_DISK_PERM_USER_READ |
1078                                GNUNET_DISK_PERM_USER_WRITE);
1079     if (data_file == NULL)
1080     {
1081       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
1082                   data_filename);
1083       GNUNET_free (data_filename);
1084     }
1085   }
1086
1087   if (GNUNET_YES ==
1088       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
1089                                              "output_file", &temp_str))
1090   {
1091     output_file =
1092         GNUNET_DISK_file_open (temp_str,
1093                                GNUNET_DISK_OPEN_READWRITE |
1094                                GNUNET_DISK_OPEN_CREATE,
1095                                GNUNET_DISK_PERM_USER_READ |
1096                                GNUNET_DISK_PERM_USER_WRITE);
1097     if (output_file == NULL)
1098       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
1099                   temp_str);
1100   }
1101   GNUNET_free_non_null (temp_str);
1102
1103   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
1104
1105   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
1106                                        &connect_cb, &peergroup_ready, NULL,
1107                                        hosts);
1108   GNUNET_assert (pg != NULL);
1109   shutdown_handle =
1110     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1111                                     &shutdown_task, NULL);
1112 }
1113
1114
1115
1116 /**
1117  * test_mesh_small command line options
1118  */
1119 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1120   {'V', "verbose", NULL,
1121    gettext_noop ("be verbose (print progress information)"),
1122    0, &GNUNET_GETOPT_set_one, &verbose},
1123   GNUNET_GETOPT_OPTION_END
1124 };
1125
1126
1127 /**
1128  * Main: start test
1129  */
1130 int
1131 main (int argc, char *argv[])
1132 {
1133   char * argv2[] = {
1134     argv[0],
1135     "-c",
1136     "test_mesh_small.conf",
1137     NULL
1138   };
1139   int argc2 = (sizeof (argv2) / sizeof (char *)) - 1;
1140
1141   initialized = GNUNET_NO;
1142
1143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
1144   if (strstr (argv[0], "test_mesh_small_unicast") != NULL)
1145   {
1146     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNICAST\n");
1147     test = UNICAST;
1148     test_name = "unicast";
1149     ok_goal = 5;
1150   }
1151   else if (strstr (argv[0], "test_mesh_small_multicast") != NULL)
1152   {
1153     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MULTICAST\n");
1154     test = MULTICAST;
1155     test_name = "multicast";
1156     ok_goal = 10;
1157   }
1158   else if (strstr (argv[0], "test_mesh_small_speed_ack") != NULL)
1159   {
1160    /* Each peer is supposed to generate the following callbacks:
1161     * 1 incoming tunnel (@dest)
1162     * 1 connected peer (@orig)
1163     * TOTAL_PACKETS received data packet (@dest)
1164     * TOTAL_PACKETS received data packet (@orig)
1165     * 1 received tunnel destroy (@dest)
1166     * _________________________________
1167     * 5 x ok expected per peer
1168     */
1169     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1170     test = SPEED_ACK;
1171     test_name = "speed ack";
1172     ok_goal = TOTAL_PACKETS * 2 + 3;
1173     argv2 [3] = NULL; // remove -L DEBUG
1174   }
1175   else if (strstr (argv[0], "test_mesh_small_speed") != NULL)
1176   {
1177    /* Each peer is supposed to generate the following callbacks:
1178     * 1 incoming tunnel (@dest)
1179     * 1 connected peer (@orig)
1180     * 1 initial packet (@dest)
1181     * TOTAL_PACKETS received data packet (@dest)
1182     * 1 received data packet (@orig)
1183     * 1 received tunnel destroy (@dest)
1184     * _________________________________
1185     */
1186     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1187     ok_goal = TOTAL_PACKETS + 5;
1188     if (strstr (argv[0], "_min") != NULL)
1189     {
1190       test = SPEED_MIN;
1191       test_name = "speed min";
1192     }
1193     else if (strstr (argv[0], "_nobuf") != NULL)
1194     {
1195       test = SPEED_NOBUF;
1196       test_name = "speed nobuf";
1197     }
1198     else
1199     {
1200       test = SPEED;
1201       test_name = "speed";
1202     }
1203   }
1204   else
1205   {
1206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1207     test = SETUP;
1208     ok_goal = 0;
1209   }
1210
1211   if (strstr (argv[0], "backwards") != NULL)
1212   {
1213     char *aux;
1214
1215     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1216     test_backwards = GNUNET_YES;
1217     aux = malloc (32); // "leaked"
1218     sprintf (aux, "backwards %s", test_name);
1219     test_name = aux;
1220   }
1221
1222   GNUNET_PROGRAM_run (argc2, argv2,
1223                       "test_mesh_small",
1224                       gettext_noop ("Test mesh in a small network."), options,
1225                       &run, NULL);
1226 #if REMOVE_DIR
1227   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
1228 #endif
1229   if (ok_goal > ok)
1230   {
1231     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1232                 "FAILED! (%d/%d)\n", ok, ok_goal);
1233     return 1;
1234   }
1235   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1236   return 0;
1237 }
1238
1239 /* end of test_mesh_small.c */