- doc
[oweals/gnunet.git] / src / mesh / gnunet-mesh-profiler.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/gnunet-mesh-profiler.c
22  *
23  * @brief Profiler for mesh experiments.
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
31
32 #define PING 1
33 #define PONG 2
34
35 /**
36  * How many peers to run
37  */
38 #define TOTAL_PEERS 10
39
40 /**
41  * How many peers do pinging
42  */
43 #define PING_PEERS 1
44
45
46 /**
47  * Duration of each round.
48  */
49 #define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
50
51 /**
52  * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
53  */
54 #define PING_PERIOD 2000
55
56 /**
57  * How long until we give up on connecting the peers?
58  */
59 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
60
61 /**
62  * Time to wait for stuff that should be rather fast
63  */
64 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
65
66 static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
67
68 struct MeshPeer
69 {
70   /**
71    * Testbed Operation (to get peer id, etc).
72    */
73   struct GNUNET_TESTBED_Operation *op;
74
75   /**
76    * Peer ID.
77    */
78   struct GNUNET_PeerIdentity id;
79
80   /**
81    * Mesh handle for the root peer
82    */
83   struct GNUNET_MESH_Handle *mesh;
84
85   /**
86    * Channel handle for the root peer
87    */
88   struct GNUNET_MESH_Channel *ch;
89
90   /**
91    * Channel handle for the dest peer
92    */
93   struct GNUNET_MESH_Channel *incoming_ch;
94
95   /**
96    * Number of payload packes sent
97    */
98   int data_sent;
99
100   /**
101    * Number of payload packets received
102    */
103   int data_received;
104
105   int up;
106
107   /**
108    * Destinaton to ping.
109    */
110   struct MeshPeer *dest;
111
112   /**
113    * Incoming channel for pings.
114    */
115   struct MeshPeer *incoming;
116
117   /**
118    * Task to do the next ping.
119    */
120   GNUNET_SCHEDULER_TaskIdentifier ping_task;
121
122   /**
123    * Time the last ping was sent.
124    */
125   struct GNUNET_TIME_Absolute timestamp;
126 };
127
128 /**
129  * GNUNET_PeerIdentity -> MeshPeer
130  */
131 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
132
133 /**
134  * Testbed peer handles.
135  */
136 static struct GNUNET_TESTBED_Peer **testbed_handles;
137
138 /**
139  * Testbed Operation (to get stats).
140  */
141 static struct GNUNET_TESTBED_Operation *stats_op;
142
143 /**
144  * How many events have happened
145  */
146 static int ok;
147
148 /**
149  * Number of events expected to conclude the test successfully.
150  */
151 static int ok_goal;
152
153 /**
154  * Size of each test packet
155  */
156 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
157
158 /**
159  * Operation to get peer ids.
160  */
161 struct MeshPeer peers[TOTAL_PEERS];
162
163 /**
164  * Peer ids counter.
165  */
166 static unsigned int p_ids;
167
168 /**
169  * Total number of currently running peers.
170  */
171 static unsigned long long peers_running;
172
173 /**
174  * Test context (to shut down).
175  */
176 static struct GNUNET_MESH_TEST_Context *test_ctx;
177
178 /**
179  * Task called to shutdown test.
180  */
181 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
182
183 /**
184  * Task called to disconnect peers, before shutdown.
185  */
186 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
187
188 /**
189  * Task to perform tests
190  */
191 static GNUNET_SCHEDULER_TaskIdentifier test_task;
192
193
194 /**
195  * Flag to notify callbacks not to generate any new traffic anymore.
196  */
197 static int test_finished;
198
199 /**
200  * Calculate a random delay.
201  *
202  * @param max Exclusive maximum, in ms.
203  *
204  * @return A time between 0 a max-1 ms.
205  */
206 static struct GNUNET_TIME_Relative
207 delay_ms_rnd (unsigned int max)
208 {
209   unsigned int rnd;
210
211   rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
212   return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
213 }
214
215
216 /**
217  * Get the index of a peer in the peers array.
218  *
219  * @param peer Peer whose index to get.
220  *
221  * @return Index of peer in peers.
222  */
223 static unsigned int
224 get_index (struct MeshPeer *peer)
225 {
226   return peer - peers;
227 }
228
229
230 /**
231  * Show the results of the test (banwidth acheived) and log them to GAUGER
232  */
233 static void
234 show_end_data (void)
235 {
236 }
237
238
239 /**
240  * Shut down peergroup, clean up.
241  *
242  * @param cls Closure (unused).
243  * @param tc Task Context.
244  */
245 static void
246 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 {
248   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
249   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
250 }
251
252
253 /**
254  * Disconnect from mesh services af all peers, call shutdown.
255  *
256  * @param cls Closure (unused).
257  * @param tc Task Context.
258  */
259 static void
260 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 {
262   long line = (long) cls;
263   unsigned int i;
264
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266               "disconnecting mesh service, called from line %ld\n", line);
267   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
268   for (i = 0; i < TOTAL_PEERS; i++)
269   {
270     if (NULL != peers[i].op)
271       GNUNET_TESTBED_operation_done (peers[i].op);
272
273     if (peers[i].up != GNUNET_YES)
274       continue;
275
276     if (NULL != peers[i].ch)
277     {
278       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
279       GNUNET_MESH_channel_destroy (peers[i].ch);
280     }
281     if (NULL != peers[i].incoming_ch)
282     {
283       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
284                   i, peers[i].incoming_ch);
285       GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
286     }
287   }
288   GNUNET_MESH_TEST_cleanup (test_ctx);
289   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
290   {
291     GNUNET_SCHEDULER_cancel (shutdown_handle);
292   }
293   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
294 }
295
296
297 /**
298  * Finish test normally: schedule disconnect and shutdown
299  *
300  * @param line Line in the code the abort is requested from (__LINE__).
301  */
302 static void
303 abort_test (long line)
304 {
305   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
306   {
307     GNUNET_SCHEDULER_cancel (disconnect_task);
308     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
309                                                 (void *) line);
310   }
311 }
312
313 /**
314  * Stats callback. Finish the stats testbed operation and when all stats have
315  * been iterated, shutdown the test.
316  *
317  * @param cls closure
318  * @param op the operation that has been finished
319  * @param emsg error message in case the operation has failed; will be NULL if
320  *          operation has executed successfully.
321  */
322 static void
323 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
324 {
325   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
326   GNUNET_TESTBED_operation_done (stats_op);
327
328   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
329     GNUNET_SCHEDULER_cancel (disconnect_task);
330   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
331                                               (void *) __LINE__);
332
333 }
334
335
336 /**
337  * Process statistic values.
338  *
339  * @param cls closure
340  * @param peer the peer the statistic belong to
341  * @param subsystem name of subsystem that created the statistic
342  * @param name the name of the datum
343  * @param value the current value
344  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
345  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
346  */
347 static int
348 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
349                 const char *subsystem, const char *name,
350                 uint64_t value, int is_persistent)
351 {
352   uint32_t i;
353
354   i = GNUNET_TESTBED_get_index (peer);
355   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
356               i, subsystem, name, value);
357
358   return GNUNET_OK;
359 }
360
361
362 /**
363  * Task check that keepalives were sent and received.
364  *
365  * @param cls Closure (NULL).
366  * @param tc Task Context.
367  */
368 static void
369 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
370 {
371   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
372     return;
373
374   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
375   stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
376                                             NULL, NULL,
377                                             stats_iterator, stats_cont, NULL);
378 }
379
380
381 /**
382  * @brief Finish profiler normally. Signal finish and start collecting stats.
383  *
384  * @param cls Closure (unused).
385  * @param tc Task context.
386  */
387 static void
388 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
389 {
390   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
391     return;
392
393   test_finished = GNUNET_YES;
394   show_end_data();
395   GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
396 }
397
398 /**
399  * Set the total number of running peers.
400  *
401  * @param target Desired number of running peers.
402  */
403 static void
404 adjust_running_peers (unsigned int target)
405 {
406   struct GNUNET_TESTBED_Operation *op;
407   unsigned int delta;
408   unsigned int run;
409   unsigned int i;
410   unsigned int r;
411
412   GNUNET_assert (target <= TOTAL_PEERS);
413
414   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
415   if (target > peers_running)
416   {
417     delta = target - peers_running;
418     run = GNUNET_YES;
419   }
420   else
421   {
422     delta = peers_running - target;
423     run = GNUNET_NO;
424   }
425
426   for (i = 0; i < delta; i++)
427   {
428     do {
429       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
430                                     TOTAL_PEERS - PING_PEERS);
431       r += PING_PEERS;
432     } while (peers[r].up == run || NULL != peers[r].incoming);
433     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
434                 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
435
436     if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
437       GNUNET_SCHEDULER_cancel (peers[r].ping_task);
438     peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
439
440     peers[r].up = run;
441
442     if (NULL != peers[r].ch)
443       GNUNET_MESH_channel_destroy (peers[r].ch);
444     peers[r].ch = NULL;
445     if (NULL != peers[r].dest)
446     {
447       if (NULL != peers[r].dest->incoming_ch)
448         GNUNET_MESH_channel_destroy (peers[r].dest->incoming_ch);
449       peers[r].dest->incoming_ch = NULL;
450     }
451
452     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
453                                              "mesh", NULL, NULL, run);
454     GNUNET_break (NULL != op);
455     peers_running += run ? 1 : -1;
456     GNUNET_assert (peers_running > 0);
457   }
458 }
459
460
461 /**
462  * @brief Move to next round.
463  *
464  * @param cls Closure (round #).
465  * @param tc Task context.
466  */
467 static void
468 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
469 {
470   long round = (long) cls;
471
472   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
473     return;
474
475   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
476   if (0.0 == rounds[round])
477   {
478     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
479     GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
480     return;
481   }
482   adjust_running_peers (rounds[round] * TOTAL_PEERS);
483
484   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
485 }
486
487
488 /**
489  * Transmit ready callback.
490  *
491  * @param cls Closure (peer for PING, NULL for PONG).
492  * @param size Size of the tranmist buffer.
493  * @param buf Pointer to the beginning of the buffer.
494  *
495  * @return Number of bytes written to buf.
496  */
497 static size_t
498 tmt_rdy (void *cls, size_t size, void *buf);
499
500
501 /**
502  * @brief Send a ping to destination
503  *
504  * @param cls Closure (peer).
505  * @param tc Task context.
506  */
507 static void
508 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
509 {
510   struct MeshPeer *peer = (struct MeshPeer *) cls;
511
512   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
513
514   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
515       || GNUNET_YES == test_finished
516       || 0 != peer->timestamp.abs_value_us)
517     return;
518
519   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
520               get_index (peer), get_index (peer->dest));
521
522   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
523                                      GNUNET_TIME_UNIT_FOREVER_REL,
524                                      size_payload, &tmt_rdy, peer);
525 }
526
527 /**
528  * @brief Reply with a pong to origin.
529  *
530  * @param cls Closure (peer).
531  * @param tc Task context.
532  */
533 static void
534 pong (struct GNUNET_MESH_Channel *channel)
535 {
536   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
537                                      GNUNET_TIME_UNIT_FOREVER_REL,
538                                      size_payload, &tmt_rdy, NULL);
539 }
540
541
542 /**
543  * Transmit ready callback
544  *
545  * @param cls Closure (peer for PING, NULL for PONG).
546  * @param size Size of the buffer we have.
547  * @param buf Buffer to copy data to.
548  */
549 static size_t
550 tmt_rdy (void *cls, size_t size, void *buf)
551 {
552   struct MeshPeer *peer = (struct MeshPeer *) cls;
553   struct GNUNET_MessageHeader *msg = buf;
554   uint32_t *data;
555
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
557   if (size < size_payload || NULL == buf)
558   {
559     GNUNET_break (0);
560     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561                 "size %u, buf %p, data_sent %u, data_received %u\n",
562                 size, buf, peer->data_sent, peer->data_received);
563
564     return 0;
565   }
566   msg->size = htons (size);
567   if (NULL == peer)
568   {
569     msg->type = htons (PONG);
570     return sizeof (*msg);
571   }
572
573   msg->type = htons (PING);
574   data = (uint32_t *) &msg[1];
575   *data = htonl (peer->data_sent);
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
577   peer->data_sent++;
578   peer->timestamp = GNUNET_TIME_absolute_get ();
579   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
580                                                   &ping, peer);
581
582   return size_payload;
583 }
584
585
586 /**
587  * Function is called whenever a PING message is received.
588  *
589  * @param cls closure (peer #, set from GNUNET_MESH_connect)
590  * @param channel connection to the other end
591  * @param channel_ctx place to store local state associated with the channel
592  * @param message the actual message
593  * @return GNUNET_OK to keep the connection open,
594  *         GNUNET_SYSERR to close it (signal serious error)
595  */
596 int
597 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
598               void **channel_ctx,
599               const struct GNUNET_MessageHeader *message)
600 {
601   long n = (long) cls;
602
603   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
604   GNUNET_MESH_receive_done (channel);
605   if (GNUNET_NO == test_finished)
606     pong (channel);
607
608   return GNUNET_OK;
609 }
610
611
612 /**
613  * Function is called whenever a PONG message is received.
614  *
615  * @param cls closure (peer #, set from GNUNET_MESH_connect)
616  * @param channel connection to the other end
617  * @param channel_ctx place to store local state associated with the channel
618  * @param message the actual message
619  * @return GNUNET_OK to keep the connection open,
620  *         GNUNET_SYSERR to close it (signal serious error)
621  */
622 int
623 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
624               void **channel_ctx,
625               const struct GNUNET_MessageHeader *message)
626 {
627   long n = (long) cls;
628   struct MeshPeer *peer;
629   struct GNUNET_TIME_Relative latency;
630
631   GNUNET_MESH_receive_done (channel);
632   peer = &peers[n];
633
634   GNUNET_break (0 != peer->timestamp.abs_value_us);
635   latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
636   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
637               get_index (peer), get_index (peer->dest),
638               GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
639
640   if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
641   {
642     peer->timestamp = GNUNET_TIME_absolute_get ();
643     peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
644                                                     &ping, peer);
645   }
646   else
647   {
648     peer->timestamp.abs_value_us = 0;
649   }
650
651   return GNUNET_OK;
652 }
653
654
655 /**
656  * Handlers, for diverse services
657  */
658 static struct GNUNET_MESH_MessageHandler handlers[] = {
659   {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
660   {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
661   {NULL, 0, 0}
662 };
663
664
665 /**
666  * Method called whenever another peer has added us to a channel
667  * the other peer initiated.
668  *
669  * @param cls Closure.
670  * @param channel New handle to the channel.
671  * @param initiator Peer that started the channel.
672  * @param port Port this channel is connected to.
673  * @param options channel option flags
674  * @return Initial channel context for the channel
675  *         (can be NULL -- that's not an error).
676  */
677 static void *
678 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
679                  const struct GNUNET_PeerIdentity *initiator,
680                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
681 {
682   long n = (long) cls;
683   struct MeshPeer *peer;
684
685   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
686   GNUNET_assert (NULL != peer);
687   GNUNET_assert (peer == peers[n].incoming);
688   GNUNET_assert (peer->dest == &peers[n]);
689   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
690               n, get_index (peer), channel);
691   peers[n].incoming_ch = channel;
692
693   return NULL;
694 }
695
696 /**
697  * Function called whenever an inbound channel is destroyed.  Should clean up
698  * any associated state.
699  *
700  * @param cls closure (set from GNUNET_MESH_connect)
701  * @param channel connection to the other end (henceforth invalid)
702  * @param channel_ctx place where local state associated
703  *                   with the channel is stored
704  */
705 static void
706 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
707                  void *channel_ctx)
708 {
709   long n = (long) cls;
710   struct MeshPeer *peer = &peers[n];
711
712   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713               "Channel %p disconnected at peer %ld\n", channel, n);
714   if (peer->ch == channel)
715     peer->ch = NULL;
716 }
717
718
719 static struct MeshPeer *
720 select_random_peer (struct MeshPeer *peer)
721 {
722   unsigned int r;
723
724   do
725   {
726     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
727   } while (NULL != peers[r].incoming);
728   peers[r].incoming = peer;
729
730   return &peers[r];
731 }
732
733 /**
734  * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
735  *
736  * Testcase continues when the root receives confirmation of connected peers,
737  * on callback funtion ch.
738  *
739  * @param cls Closure (unsued).
740  * @param tc Task Context.
741  */
742 static void
743 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
744 {
745   enum GNUNET_MESH_ChannelOption flags;
746   unsigned long i;
747
748   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
749     return;
750
751   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
752
753   flags = GNUNET_MESH_OPTION_DEFAULT;
754   for (i = 0; i < PING_PEERS; i++)
755   {
756
757     peers[i].dest = select_random_peer (&peers[i]);
758     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
759                                               &peers[i].dest->id,
760                                               1, flags);
761     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
762                 i, get_index (peers[i].dest), peers[i].ch);
763     peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
764                                                        &ping, &peers[i]);
765   }
766   peers_running = TOTAL_PEERS;
767   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
768 }
769
770
771 /**
772  * Callback to be called when the requested peer information is available
773  *
774  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
775  * @param op the operation this callback corresponds to
776  * @param pinfo the result; will be NULL if the operation has failed
777  * @param emsg error message if the operation has failed;
778  *             NULL if the operation is successfull
779  */
780 static void
781 peer_id_cb (void *cls,
782        struct GNUNET_TESTBED_Operation *op,
783        const struct GNUNET_TESTBED_PeerInformation *pinfo,
784        const char *emsg)
785 {
786   long n = (long) cls;
787
788   if (NULL == pinfo || NULL != emsg)
789   {
790     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
791     abort_test (__LINE__);
792     return;
793   }
794   peers[n].id = *(pinfo->result.id);
795   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u  id: %s\n",
796               n, GNUNET_i2s (&peers[n].id));
797   GNUNET_break (GNUNET_OK ==
798                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
799                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
800
801   GNUNET_TESTBED_operation_done (peers[n].op);
802   peers[n].op = NULL;
803
804   p_ids++;
805   if (p_ids < TOTAL_PEERS)
806     return;
807   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
808   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
809                                             &start_test, NULL);
810 }
811
812 /**
813  * test main: start test when all peers are connected
814  *
815  * @param cls Closure.
816  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
817  * @param num_peers Number of peers that are running.
818  * @param testbed_peers Array of peers.
819  * @param meshes Handle to each of the MESHs of the peers.
820  */
821 static void
822 tmain (void *cls,
823        struct GNUNET_MESH_TEST_Context *ctx,
824        unsigned int num_peers,
825        struct GNUNET_TESTBED_Peer **testbed_peers,
826        struct GNUNET_MESH_Handle **meshes)
827 {
828   unsigned long i;
829
830   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
831   ok = 0;
832   test_ctx = ctx;
833   GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
834   GNUNET_assert (TOTAL_PEERS == num_peers);
835   peers_running = num_peers;
836   testbed_handles = testbed_peers;
837   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
838                                                   &disconnect_mesh_peers,
839                                                   (void *) __LINE__);
840   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
841                                                   &shutdown_task, NULL);
842   for (i = 0; i < TOTAL_PEERS; i++)
843   {
844     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
845     peers[i].up = GNUNET_YES;
846     peers[i].mesh = meshes[i];
847     peers[i].op =
848       GNUNET_TESTBED_peer_get_information (testbed_handles[i],
849                                            GNUNET_TESTBED_PIT_IDENTITY,
850                                            &peer_id_cb, (void *) i);
851   }
852   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
853   /* Continues from pi_cb -> do_test */
854 }
855
856
857 /**
858  * Main: start profiler.
859  */
860 int
861 main (int argc, char *argv[])
862 {
863   static uint32_t ports[2];
864   const char *config_file;
865
866   config_file = "test_mesh.conf";
867
868   ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
869   GNUNET_assert (NULL != ids);
870   p_ids = 0;
871   test_finished = GNUNET_NO;
872   ports[0] = 1;
873   ports[1] = 0;
874   GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
875                         &tmain, NULL, /* tmain cls */
876                         &incoming_channel, &channel_cleaner,
877                         handlers, ports);
878
879   if (ok_goal > ok)
880   {
881     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
882                 "FAILED! (%d/%d)\n", ok, ok_goal);
883     return 1;
884   }
885   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
886   return 0;
887 }
888
889 /* end of gnunet-mesh-profiler.c */
890