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