- fix scheduler clearance
[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 "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include "gnunet_statistics_service.h"
30 #include <gauger.h>
31
32
33 /**
34  * How namy messages to send
35  */
36 #define TOTAL_PACKETS 1000
37
38 /**
39  * How long until we give up on connecting the peers?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
42
43 /**
44  * Time to wait for stuff that should be rather fast
45  */
46 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
47
48 /**
49  * DIFFERENT TESTS TO RUN
50  */
51 #define SETUP 0
52 #define FORWARD 1
53 #define KEEPALIVE 2
54 #define SPEED 3
55 #define SPEED_ACK 4
56 #define SPEED_REL 8
57 #define P2P_SIGNAL 10
58
59 /**
60  * Which test are we running?
61  */
62 static int test;
63
64 /**
65  * String with test name
66  */
67 char *test_name;
68
69 /**
70  * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
71  */
72 static int test_backwards = GNUNET_NO;
73
74 /**
75  * How many events have happened
76  */
77 static int ok;
78
79 /**
80  * Number of events expected to conclude the test successfully.
81  */
82 int ok_goal;
83
84 /**
85  * Size of each test packet
86  */
87 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
88
89 /**
90  * Operation to get peer ids.
91  */
92 struct GNUNET_TESTBED_Operation *t_op[2];
93
94 /**
95  * Peer ids.
96  */
97 struct GNUNET_PeerIdentity *p_id[2];
98
99 /**
100  * Peer ids counter.
101  */
102 unsigned int p_ids;
103
104 /**
105  * Is the setup initialized?
106  */
107 static int initialized;
108
109 /**
110  * Number of payload packes sent
111  */
112 static int data_sent;
113
114 /**
115  * Number of payload packets received
116  */
117 static int data_received;
118
119 /**
120  * Number of payload packed explicitly (app level) acknowledged
121  */
122 static int data_ack;
123
124 /**
125  * Total number of currently running peers.
126  */
127 static unsigned long long peers_running;
128
129 /**
130  * Test context (to shut down).
131  */
132 struct GNUNET_MESH_TEST_Context *test_ctx;
133
134 /**
135  * Task called to disconnect peers.
136  */
137 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
138
139 /**
140  * Task To perform tests
141  */
142 static GNUNET_SCHEDULER_TaskIdentifier test_task;
143
144 /**
145  * Task called to shutdown test.
146  */
147 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
148
149 /**
150  * Mesh handle for the root peer
151  */
152 static struct GNUNET_MESH_Handle *h1;
153
154 /**
155  * Mesh handle for the first leaf peer
156  */
157 static struct GNUNET_MESH_Handle *h2;
158
159 /**
160  * Channel handle for the root peer
161  */
162 static struct GNUNET_MESH_Channel *ch;
163
164 /**
165  * Channel handle for the dest peer
166  */
167 static struct GNUNET_MESH_Channel *incoming_ch;
168
169 /**
170  * Time we started the data transmission (after channel has been established
171  * and initilized).
172  */
173 static struct GNUNET_TIME_Absolute start_time;
174
175 static struct GNUNET_TESTBED_Peer **testbed_peers;
176 static struct GNUNET_STATISTICS_Handle *stats;
177 static struct GNUNET_STATISTICS_GetHandle *stats_get;
178 static struct GNUNET_TESTBED_Operation *stats_op;
179 static unsigned int stats_peer;
180 static unsigned int ka_sent;
181 static unsigned int ka_received;
182
183
184 /**
185  * Show the results of the test (banwidth acheived) and log them to GAUGER
186  */
187 static void
188 show_end_data (void)
189 {
190   static struct GNUNET_TIME_Absolute end_time;
191   static struct GNUNET_TIME_Relative total_time;
192
193   end_time = GNUNET_TIME_absolute_get();
194   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
195   FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
196   FPRINTF (stderr, "Test time %s\n",
197            GNUNET_STRINGS_relative_time_to_string (total_time,
198                                                    GNUNET_YES));
199   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
200            4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
201   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
202            TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
203   GAUGER ("MESH", test_name,
204           TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
205           "packets/s");
206 }
207
208
209 /**
210  * Shut down peergroup, clean up.
211  *
212  * @param cls Closure (unused).
213  * @param tc Task Context.
214  */
215 static void
216 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
219   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
220 }
221
222
223 /**
224  * Disconnect from mesh services af all peers, call shutdown.
225  *
226  * @param cls Closure (unused).
227  * @param tc Task Context.
228  */
229 static void
230 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 {
232   long line = (long) cls;
233   unsigned int i;
234
235   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236               "disconnecting mesh service of peers, called from line %ld\n",
237               line);
238   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
239   for (i = 0; i < 2; i++)
240   {
241     GNUNET_TESTBED_operation_done (t_op[i]);
242   }
243   if (NULL != ch)
244   {
245     GNUNET_MESH_channel_destroy (ch);
246     ch = NULL;
247   }
248   if (NULL != incoming_ch)
249   {
250     GNUNET_MESH_channel_destroy (incoming_ch);
251     incoming_ch = NULL;
252   }
253   GNUNET_MESH_TEST_cleanup (test_ctx);
254   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
255   {
256     GNUNET_SCHEDULER_cancel (shutdown_handle);
257   }
258   if (NULL != stats_get)
259     GNUNET_STATISTICS_get_cancel (stats_get);
260   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
261 }
262
263
264 /**
265  * Abort test: schedule disconnect and shutdown immediately
266  *
267  * @param line Line in the code the abort is requested from (__LINE__).
268  */
269 static void
270 abort_test (long line)
271 {
272   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
273   {
274     GNUNET_SCHEDULER_cancel (disconnect_task);
275     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
276                                                 (void *) line);
277   }
278 }
279
280 /**
281  * Transmit ready callback.
282  *
283  * @param cls Closure (message type).
284  * @param size Size of the tranmist buffer.
285  * @param buf Pointer to the beginning of the buffer.
286  *
287  * @return Number of bytes written to buf.
288  */
289 static size_t
290 tmt_rdy (void *cls, size_t size, void *buf);
291
292
293 /**
294  * Task to schedule a new data transmission.
295  *
296  * @param cls Closure (peer #).
297  * @param tc Task Context.
298  */
299 static void
300 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
301 {
302   struct GNUNET_MESH_TransmitHandle *th;
303   struct GNUNET_MESH_Channel *channel;
304
305   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
306     return;
307
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
309   if (GNUNET_YES == test_backwards)
310   {
311     channel = incoming_ch;
312   }
313   else
314   {
315     channel = ch;
316   }
317   th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
318                                           GNUNET_TIME_UNIT_FOREVER_REL,
319                                           size_payload, &tmt_rdy, (void *) 1L);
320   if (NULL == th)
321   {
322     unsigned long i = (unsigned long) cls;
323
324     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
325     if (0 == i)
326     {
327       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  in 1 ms\n");
328       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
329                                     &data_task, (void *)1UL);
330     }
331     else
332     {
333       i++;
334       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
335       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
336                                       GNUNET_TIME_UNIT_MILLISECONDS,
337                                       i),
338                                     &data_task, (void *)i);
339     }
340   }
341 }
342
343
344 /**
345  * Transmit ready callback
346  *
347  * @param cls Closure (message type).
348  * @param size Size of the buffer we have.
349  * @param buf Buffer to copy data to.
350  */
351 size_t
352 tmt_rdy (void *cls, size_t size, void *buf)
353 {
354   struct GNUNET_MessageHeader *msg = buf;
355   uint32_t *data;
356
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358               "tmt_rdy called, filling buffer\n");
359   if (size < size_payload || NULL == buf)
360   {
361     GNUNET_break (ok >= ok_goal - 2);
362     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363                 "size %u, buf %p, data_sent %u, data_received %u\n",
364                 size, buf, data_sent, data_received);
365     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
366
367     return 0;
368   }
369   msg->size = htons (size);
370   msg->type = htons ((long) cls);
371   data = (uint32_t *) &msg[1];
372   *data = htonl (data_sent);
373   if (GNUNET_NO == initialized)
374   {
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "sending initializer\n");
377   }
378   else if (SPEED == test)
379   {
380     data_sent++;
381     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382               " Sent packet %d\n", data_sent);
383     if (data_sent < TOTAL_PACKETS)
384     {
385       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386               " Scheduling packet %d\n", data_sent + 1);
387       GNUNET_SCHEDULER_add_now (&data_task, NULL);
388     }
389   }
390
391   return size_payload;
392 }
393
394
395 /**
396  * Function is called whenever a message is received.
397  *
398  * @param cls closure (set from GNUNET_MESH_connect)
399  * @param channel connection to the other end
400  * @param channel_ctx place to store local state associated with the channel
401  * @param message the actual message
402  * @return GNUNET_OK to keep the connection open,
403  *         GNUNET_SYSERR to close it (signal serious error)
404  */
405 int
406 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
407                void **channel_ctx,
408                const struct GNUNET_MessageHeader *message)
409 {
410   long client = (long) cls;
411   long expected_target_client;
412   uint32_t *data;
413
414   ok++;
415
416   GNUNET_MESH_receive_done (channel);
417
418   if ((ok % 20) == 0)
419   {
420     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
421     {
422       GNUNET_SCHEDULER_cancel (disconnect_task);
423       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
424                                                       &disconnect_mesh_peers,
425                                                       (void *) __LINE__);
426     }
427   }
428
429   switch (client)
430   {
431   case 0L:
432     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
433     break;
434   case 4L:
435     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
436                 "Leaf client %li got a message.\n",
437                 client);
438     break;
439   default:
440     GNUNET_assert (0);
441     break;
442   }
443   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
444   data = (uint32_t *) &message[1];
445   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
446   if (SPEED == test && GNUNET_YES == test_backwards)
447   {
448     expected_target_client = 0L;
449   }
450   else
451   {
452     expected_target_client = 4L;
453   }
454
455   if (GNUNET_NO == initialized)
456   {
457     initialized = GNUNET_YES;
458     start_time = GNUNET_TIME_absolute_get ();
459     if (SPEED == test)
460     {
461       GNUNET_assert (4L == client);
462       GNUNET_SCHEDULER_add_now (&data_task, NULL);
463       return GNUNET_OK;
464     }
465   }
466
467   if (client == expected_target_client) // Normally 4
468   {
469     data_received++;
470     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
471     if (SPEED != test || (ok_goal - 2) == ok)
472     {
473       GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
474                                          GNUNET_TIME_UNIT_FOREVER_REL,
475                                          size_payload, &tmt_rdy, (void *) 1L);
476       return GNUNET_OK;
477     }
478     else
479     {
480       if (data_received < TOTAL_PACKETS)
481         return GNUNET_OK;
482     }
483   }
484   else // Normally 0
485   {
486     if (test == SPEED_ACK || test == SPEED)
487     {
488       data_ack++;
489       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", data_ack);
490       GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
491                                          GNUNET_TIME_UNIT_FOREVER_REL,
492                                          size_payload, &tmt_rdy, (void *) 1L);
493       if (data_ack < TOTAL_PACKETS && SPEED != test)
494         return GNUNET_OK;
495       if (ok == 2 && SPEED == test)
496         return GNUNET_OK;
497       show_end_data();
498     }
499     if (test == P2P_SIGNAL)
500     {
501       GNUNET_MESH_channel_destroy (incoming_ch);
502       incoming_ch = NULL;
503     }
504     else
505     {
506       GNUNET_MESH_channel_destroy (ch);
507       ch = NULL;
508     }
509   }
510
511   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
512   {
513     GNUNET_SCHEDULER_cancel (disconnect_task);
514     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
515                                                     &disconnect_mesh_peers,
516                                                     (void *) __LINE__);
517   }
518
519   return GNUNET_OK;
520 }
521
522
523 /**
524  * Adapter function called to establish a connection to the statistics service.
525  *
526  * @param cls closure
527  * @param cfg configuration of the peer to connect to; will be available until
528  *          GNUNET_TESTBED_operation_done() is called on the operation returned
529  *          from GNUNET_TESTBED_service_connect()
530  * @return service handle to return in 'op_result', NULL on error
531  */
532 static void *
533 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
534 {
535   return GNUNET_STATISTICS_create ("<test_mesh>", cfg);
536 }
537
538
539 /**
540  * Adapter function called to destroy a connection to
541  * statistics service.
542  *
543  * @param cls Closure (unused).
544  * @param op_result service handle returned from the connect adapter
545  */
546 static void
547 stats_da (void *cls, void *op_result)
548 {
549   GNUNET_assert (op_result == stats);
550   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
551   stats = NULL;
552 }
553
554
555 /**
556  * Function called by testbed once we are connected to stats
557  * service. Get the statistics of interest.
558  *
559  * @param cls Closure (unused).
560  * @param op connect operation handle
561  * @param ca_result handle to stats service
562  * @param emsg error message on failure
563  */
564 static void
565 stats_connect_cb (void *cls,
566                   struct GNUNET_TESTBED_Operation *op,
567                   void *ca_result,
568                   const char *emsg);
569
570 /**
571  * Stats callback. Finish the stats testbed operation and when all stats have
572  * been iterated, shutdown the test.
573  *
574  * @param cls closure
575  * @param success GNUNET_OK if statistics were
576  *        successfully obtained, GNUNET_SYSERR if not.
577  */
578 static void
579 stats_cont (void *cls, int success)
580 {
581   GNUNET_TESTBED_operation_done (stats_op);
582   stats_get = NULL;
583   if (NULL == cls)
584   {
585     stats_op = GNUNET_TESTBED_service_connect (NULL,
586                                                testbed_peers[4],
587                                                "statistics",
588                                                &stats_connect_cb,
589                                                (void *)4,
590                                                &stats_ca,
591                                                &stats_da,
592                                                (void *)4);
593   }
594   else
595   {
596     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
597       GNUNET_SCHEDULER_cancel (disconnect_task);
598     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
599                                                 (void *) __LINE__);
600   }
601 }
602
603
604 /**
605  * Process statistic values.
606  *
607  * @param cls closure
608  * @param subsystem name of subsystem that created the statistic
609  * @param name the name of the datum
610  * @param value the current value
611  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
612  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
613  */
614 static int
615 stats_iterator (void *cls, const char *subsystem, const char *name,
616                 uint64_t value, int is_persistent)
617 {
618   if (0 == strncmp("# keepalives sent", name,
619                    strlen("# keepalives sent"))
620       && 0 == stats_peer)
621     ka_sent = value;
622
623   if (0 == strncmp("# keepalives received", name,
624                    strlen ("# keepalives received"))
625       && 4 == stats_peer)
626   {
627     ka_received = value;
628     if (ka_sent < 2 || ka_sent > ka_received + 1)
629       ok--;
630   }
631
632   return GNUNET_OK;
633 }
634
635
636 /**
637  * Function called by testbed once we are connected to stats
638  * service. Get the statistics of interest.
639  *
640  * @param cls Closure (unused).
641  * @param op connect operation handle
642  * @param ca_result handle to stats service
643  * @param emsg error message on failure
644  */
645 static void
646 stats_connect_cb (void *cls,
647                   struct GNUNET_TESTBED_Operation *op,
648                   void *ca_result,
649                   const char *emsg)
650 {
651   if (NULL == ca_result || NULL != emsg)
652   {
653     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654                 "Failed to connect to statistics service: %s\n", emsg);
655     return;
656   }
657
658   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "stats for peer %u\n", cls);
659   stats = ca_result;
660
661   stats_get = GNUNET_STATISTICS_get (stats, "mesh", NULL,
662                                       GNUNET_TIME_UNIT_FOREVER_REL,
663                                       &stats_cont, &stats_iterator, cls);
664   if (NULL == stats_get)
665   {
666     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667                 "Could not get statistics of peer %u!\n", cls);
668   }
669 }
670
671
672 /**
673  * Task check that keepalives were sent and received.
674  *
675  * @param cls Closure (NULL).
676  * @param tc Task Context.
677  */
678 static void
679 check_keepalives (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
680 {
681   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
682     return;
683
684   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
685   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "check keepalives\n");
686   GNUNET_MESH_channel_destroy (ch);
687   stats_op = GNUNET_TESTBED_service_connect (NULL,
688                                              testbed_peers[0],
689                                              "statistics",
690                                              &stats_connect_cb,
691                                              NULL,
692                                              &stats_ca,
693                                              &stats_da,
694                                              NULL);
695 }
696
697
698 /**
699  * Handlers, for diverse services
700  */
701 static struct GNUNET_MESH_MessageHandler handlers[] = {
702   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
703   {NULL, 0, 0}
704 };
705
706
707 /**
708  * Method called whenever another peer has added us to a channel
709  * the other peer initiated.
710  *
711  * @param cls Closure.
712  * @param channel New handle to the channel.
713  * @param initiator Peer that started the channel.
714  * @param port Port this channel is connected to.
715  * @param options channel option flags
716  * @return Initial channel context for the channel
717  *         (can be NULL -- that's not an error).
718  */
719 static void *
720 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
721                  const struct GNUNET_PeerIdentity *initiator,
722                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
723 {
724   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
725               "Incoming channel from %s to peer %d\n",
726               GNUNET_i2s (initiator), (long) cls);
727   ok++;
728   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
729   if ((long) cls == 4L)
730     incoming_ch = channel;
731   else
732   {
733     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
734                 "Incoming channel for unknown client %lu\n", (long) cls);
735     GNUNET_break(0);
736   }
737   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
738   {
739     GNUNET_SCHEDULER_cancel (disconnect_task);
740     if (KEEPALIVE == test)
741     {
742       struct GNUNET_TIME_Relative delay;
743       delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS , 5);
744       disconnect_task =
745         GNUNET_SCHEDULER_add_delayed (delay, &check_keepalives, NULL);
746     }
747     else
748       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
749                                                       &disconnect_mesh_peers,
750                                                       (void *) __LINE__);
751   }
752
753   return NULL;
754 }
755
756 /**
757  * Function called whenever an inbound channel is destroyed.  Should clean up
758  * any associated state.
759  *
760  * @param cls closure (set from GNUNET_MESH_connect)
761  * @param channel connection to the other end (henceforth invalid)
762  * @param channel_ctx place where local state associated
763  *                   with the channel is stored
764  */
765 static void
766 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
767                  void *channel_ctx)
768 {
769   long i = (long) cls;
770
771   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
772               "Incoming channel disconnected at peer %d\n",
773               i);
774   if (4L == i)
775   {
776     ok++;
777     GNUNET_break (channel == incoming_ch);
778     incoming_ch = NULL;
779   }
780   else if (0L == i)
781   {
782     if (P2P_SIGNAL == test)
783     {
784       ok ++;
785     }
786     GNUNET_break (channel == ch);
787     ch = NULL;
788   }
789   else
790     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
791                 "Unknown peer! %d\n", i);
792   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
793
794   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
795   {
796     GNUNET_SCHEDULER_cancel (disconnect_task);
797     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
798                                                 (void *) __LINE__);
799   }
800
801   return;
802 }
803
804
805 /**
806  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
807  *
808  * Testcase continues when the root receives confirmation of connected peers,
809  * on callback funtion ch.
810  *
811  * @param cls Closure (unsued).
812  * @param tc Task Context.
813  */
814 static void
815 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 {
817   enum GNUNET_MESH_ChannelOption flags;
818
819   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
820     return;
821
822   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
823
824   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
825   {
826     GNUNET_SCHEDULER_cancel (disconnect_task);
827   }
828
829   flags = GNUNET_MESH_OPTION_DEFAULT;
830   if (SPEED_REL == test)
831   {
832     test = SPEED;
833     flags |= GNUNET_MESH_OPTION_RELIABLE;
834   }
835   ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags);
836
837   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
838                                                   &disconnect_mesh_peers,
839                                                   (void *) __LINE__);
840   if (KEEPALIVE == test)
841     return; /* Don't send any data. */
842
843   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844               "Sending data initializer...\n");
845   data_ack = 0;
846   data_received = 0;
847   data_sent = 0;
848   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
849                                      GNUNET_TIME_UNIT_FOREVER_REL,
850                                      size_payload, &tmt_rdy, (void *) 1L);
851 }
852
853 /**
854  * Callback to be called when the requested peer information is available
855  *
856  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
857  * @param op the operation this callback corresponds to
858  * @param pinfo the result; will be NULL if the operation has failed
859  * @param emsg error message if the operation has failed;
860  *             NULL if the operation is successfull
861  */
862 static void
863 pi_cb (void *cls,
864        struct GNUNET_TESTBED_Operation *op,
865        const struct GNUNET_TESTBED_PeerInformation *pinfo,
866        const char *emsg)
867 {
868   long i = (long) cls;
869
870   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
871
872   if (NULL == pinfo || NULL != emsg)
873   {
874     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
875     abort_test (__LINE__);
876     return;
877   }
878   p_id[i] = pinfo->result.id;
879   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
880   p_ids++;
881   if (p_ids < 2)
882     return;
883   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
884   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
885                                             &do_test, NULL);
886 }
887
888 /**
889  * test main: start test when all peers are connected
890  *
891  * @param cls Closure.
892  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
893  * @param num_peers Number of peers that are running.
894  * @param peers Array of peers.
895  * @param meshes Handle to each of the MESHs of the peers.
896  */
897 static void
898 tmain (void *cls,
899        struct GNUNET_MESH_TEST_Context *ctx,
900        unsigned int num_peers,
901        struct GNUNET_TESTBED_Peer **peers,
902        struct GNUNET_MESH_Handle **meshes)
903 {
904   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
905   ok = 0;
906   test_ctx = ctx;
907   peers_running = num_peers;
908   testbed_peers = peers;
909   h1 = meshes[0];
910   h2 = meshes[num_peers - 1];
911   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
912                                                   &disconnect_mesh_peers,
913                                                   (void *) __LINE__);
914   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
915                                                   &shutdown_task, NULL);
916   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
917                                                  GNUNET_TESTBED_PIT_IDENTITY,
918                                                  &pi_cb, (void *) 0L);
919   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
920                                                  GNUNET_TESTBED_PIT_IDENTITY,
921                                                  &pi_cb, (void *) 1L);
922   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
923 }
924
925
926 /**
927  * Main: start test
928  */
929 int
930 main (int argc, char *argv[])
931 {
932   initialized = GNUNET_NO;
933   uint32_t ports[2];
934   const char *config_file;
935
936   GNUNET_log_setup ("test", "DEBUG", NULL);
937   config_file = "test_mesh.conf";
938
939   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
940   if (strstr (argv[0], "_small_forward") != NULL)
941   {
942     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
943     test = FORWARD;
944     test_name = "unicast";
945     ok_goal = 4;
946   }
947   else if (strstr (argv[0], "_small_signal") != NULL)
948   {
949     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
950     test = P2P_SIGNAL;
951     test_name = "signal";
952     ok_goal = 4;
953   }
954   else if (strstr (argv[0], "_small_speed_ack") != NULL)
955   {
956     /* Test is supposed to generate the following callbacks:
957      * 1 incoming channel (@dest)
958      * TOTAL_PACKETS received data packet (@dest)
959      * TOTAL_PACKETS received data packet (@orig)
960      * 1 received channel destroy (@dest)
961      */
962     ok_goal = TOTAL_PACKETS * 2 + 2;
963     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
964     test = SPEED_ACK;
965     test_name = "speed ack";
966   }
967   else if (strstr (argv[0], "_small_speed") != NULL)
968   {
969     /* Test is supposed to generate the following callbacks:
970      * 1 incoming channel (@dest)
971      * 1 initial packet (@dest)
972      * TOTAL_PACKETS received data packet (@dest)
973      * 1 received data packet (@orig)
974      * 1 received channel destroy (@dest)
975      */
976     ok_goal = TOTAL_PACKETS + 4;
977     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
978     if (strstr (argv[0], "_reliable") != NULL)
979     {
980       test = SPEED_REL;
981       test_name = "speed reliable";
982       config_file = "test_mesh_drop.conf";
983     }
984     else
985     {
986       test = SPEED;
987       test_name = "speed";
988     }
989   }
990   else if (strstr (argv[0], "_keepalive") != NULL)
991   {
992     test = KEEPALIVE;
993     /* Test is supposed to generate the following callbacks:
994      * 1 incoming channel (@dest)
995      * [wait]
996      * 1 received channel destroy (@dest)
997      */
998     ok_goal = 2;
999   }
1000   else
1001   {
1002     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1003     test = SETUP;
1004     ok_goal = 0;
1005   }
1006
1007   if (strstr (argv[0], "backwards") != NULL)
1008   {
1009     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1010     test_backwards = GNUNET_YES;
1011     GNUNET_asprintf (&test_name, "backwards %s", test_name);
1012   }
1013
1014   p_ids = 0;
1015   ports[0] = 1;
1016   ports[1] = 0;
1017   GNUNET_MESH_TEST_run ("test_mesh_small",
1018                         config_file,
1019                         5,
1020                         &tmain,
1021                         NULL, /* tmain cls */
1022                         &incoming_channel,
1023                         &channel_cleaner,
1024                         handlers,
1025                         ports);
1026
1027   if (ok_goal > ok)
1028   {
1029     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1030                 "FAILED! (%d/%d)\n", ok, ok_goal);
1031     return 1;
1032   }
1033   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1034   return 0;
1035 }
1036
1037 /* end of test_mesh_small.c */
1038