- check task (could be in the middle of transmission loop and be 0)
[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     GNUNET_TESTBED_operation_done (peers[i].op);
251     GNUNET_MESH_channel_destroy (peers[i].ch);
252     if (NULL != peers[i].incoming_ch)
253       GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
254   }
255   GNUNET_MESH_TEST_cleanup (test_ctx);
256   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
257   {
258     GNUNET_SCHEDULER_cancel (shutdown_handle);
259   }
260   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
261 }
262
263
264 /**
265  * Finish test normally: schedule disconnect and shutdown
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  * Stats callback. Finish the stats testbed operation and when all stats have
282  * been iterated, shutdown the test.
283  *
284  * @param cls closure
285  * @param op the operation that has been finished
286  * @param emsg error message in case the operation has failed; will be NULL if
287  *          operation has executed successfully.
288  */
289 static void
290 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
291 {
292   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
293   GNUNET_TESTBED_operation_done (stats_op);
294
295   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
296     GNUNET_SCHEDULER_cancel (disconnect_task);
297   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
298                                               (void *) __LINE__);
299
300 }
301
302
303 /**
304  * Process statistic values.
305  *
306  * @param cls closure
307  * @param peer the peer the statistic belong to
308  * @param subsystem name of subsystem that created the statistic
309  * @param name the name of the datum
310  * @param value the current value
311  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
312  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
313  */
314 static int
315 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
316                 const char *subsystem, const char *name,
317                 uint64_t value, int is_persistent)
318 {
319   uint32_t i;
320
321   i = GNUNET_TESTBED_get_index (peer);
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
323               i, subsystem, name, value);
324
325   return GNUNET_OK;
326 }
327
328
329 /**
330  * Task check that keepalives were sent and received.
331  *
332  * @param cls Closure (NULL).
333  * @param tc Task Context.
334  */
335 static void
336 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 {
338   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
339     return;
340
341   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
342   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
343   stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
344                                             NULL, NULL,
345                                             stats_iterator, stats_cont, NULL);
346 }
347
348
349 /**
350  * @brief Finish profiler normally. Signal finish and start collecting stats.
351  *
352  * @param cls Closure (unused).
353  * @param tc Task context.
354  */
355 static void
356 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
357 {
358   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
359     return;
360
361   test_finished = GNUNET_YES;
362   show_end_data();
363   GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
364 }
365
366 /**
367  * Set the total number of running peers.
368  *
369  * @param target Desired number of running peers.
370  */
371 static void
372 adjust_running_peers (unsigned int target)
373 {
374   struct GNUNET_TESTBED_Operation *op;
375   unsigned int delta;
376   unsigned int run;
377   unsigned int i;
378   unsigned int r;
379
380   GNUNET_assert (target <= TOTAL_PEERS);
381
382   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
383   if (target > peers_running)
384   {
385     delta = target - peers_running;
386     run = GNUNET_YES;
387   }
388   else
389   {
390     delta = peers_running - target;
391     run = GNUNET_NO;
392   }
393
394   for (i = 0; i < delta; i++)
395   {
396     do {
397       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
398     } while (peers[r].up == run);
399     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
400                 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
401
402     if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
403       GNUNET_SCHEDULER_cancel (peers[r].ping_task);
404     peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
405
406     peers[r].up = GNUNET_NO;
407     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
408                                              "mesh", NULL, NULL, run);
409     GNUNET_break (NULL != op);
410     peers_running += run ? 1 : -1;
411     GNUNET_assert (peers_running > 0);
412   }
413 }
414
415
416 /**
417  * @brief Move to next round.
418  *
419  * @param cls Closure (round #).
420  * @param tc Task context.
421  */
422 static void
423 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
424 {
425   long round = (long) cls;
426
427   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
428     return;
429
430   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
431   if (0.0 == rounds[round])
432   {
433     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
434     GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
435     return;
436   }
437   adjust_running_peers (rounds[round] * TOTAL_PEERS);
438
439   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
440 }
441
442
443 /**
444  * Transmit ready callback.
445  *
446  * @param cls Closure (peer for PING, NULL for PONG).
447  * @param size Size of the tranmist buffer.
448  * @param buf Pointer to the beginning of the buffer.
449  *
450  * @return Number of bytes written to buf.
451  */
452 static size_t
453 tmt_rdy (void *cls, size_t size, void *buf);
454
455
456 /**
457  * @brief Send a ping to destination
458  *
459  * @param cls Closure (peer).
460  * @param tc Task context.
461  */
462 static void
463 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
464 {
465   struct MeshPeer *peer = (struct MeshPeer *) cls;
466
467   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
468
469   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
470       || GNUNET_YES == test_finished
471       || 0 != peer->timestamp.abs_value_us)
472     return;
473
474   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
475               get_index (peer), get_index (peer->dest));
476
477   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
478                                      GNUNET_TIME_UNIT_FOREVER_REL,
479                                      size_payload, &tmt_rdy, peer);
480 }
481
482 /**
483  * @brief Reply with a pong to origin.
484  *
485  * @param cls Closure (peer).
486  * @param tc Task context.
487  */
488 static void
489 pong (struct GNUNET_MESH_Channel *channel)
490 {
491   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
492                                      GNUNET_TIME_UNIT_FOREVER_REL,
493                                      size_payload, &tmt_rdy, NULL);
494 }
495
496
497 /**
498  * Transmit ready callback
499  *
500  * @param cls Closure (peer for PING, NULL for PONG).
501  * @param size Size of the buffer we have.
502  * @param buf Buffer to copy data to.
503  */
504 static size_t
505 tmt_rdy (void *cls, size_t size, void *buf)
506 {
507   struct MeshPeer *peer = (struct MeshPeer *) cls;
508   struct GNUNET_MessageHeader *msg = buf;
509   uint32_t *data;
510
511   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
512   if (size < size_payload || NULL == buf)
513   {
514     GNUNET_break (0);
515     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516                 "size %u, buf %p, data_sent %u, data_received %u\n",
517                 size, buf, peer->data_sent, peer->data_received);
518
519     return 0;
520   }
521   msg->size = htons (size);
522   if (NULL == peer)
523   {
524     msg->type = htons (PONG);
525     return sizeof (*msg);
526   }
527
528   msg->type = htons (PING);
529   data = (uint32_t *) &msg[1];
530   *data = htonl (peer->data_sent);
531   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
532   peer->data_sent++;
533   peer->timestamp = GNUNET_TIME_absolute_get ();
534   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
535                                                   &ping, peer);
536
537   return size_payload;
538 }
539
540
541 /**
542  * Function is called whenever a PING message is received.
543  *
544  * @param cls closure (peer #, set from GNUNET_MESH_connect)
545  * @param channel connection to the other end
546  * @param channel_ctx place to store local state associated with the channel
547  * @param message the actual message
548  * @return GNUNET_OK to keep the connection open,
549  *         GNUNET_SYSERR to close it (signal serious error)
550  */
551 int
552 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
553               void **channel_ctx,
554               const struct GNUNET_MessageHeader *message)
555 {
556   long n = (long) cls;
557
558   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
559   GNUNET_MESH_receive_done (channel);
560   if (GNUNET_NO == test_finished)
561     pong (channel);
562
563   return GNUNET_OK;
564 }
565
566
567 /**
568  * Function is called whenever a PONG message is received.
569  *
570  * @param cls closure (peer #, set from GNUNET_MESH_connect)
571  * @param channel connection to the other end
572  * @param channel_ctx place to store local state associated with the channel
573  * @param message the actual message
574  * @return GNUNET_OK to keep the connection open,
575  *         GNUNET_SYSERR to close it (signal serious error)
576  */
577 int
578 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
579               void **channel_ctx,
580               const struct GNUNET_MessageHeader *message)
581 {
582   long n = (long) cls;
583   struct MeshPeer *peer;
584   struct GNUNET_TIME_Relative latency;
585
586   GNUNET_MESH_receive_done (channel);
587   peer = &peers[n];
588
589   GNUNET_break (0 != peer->timestamp.abs_value_us);
590   latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
591   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
592               get_index (peer), get_index (peer->dest),
593               GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
594
595   if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
596   {
597     peer->timestamp = GNUNET_TIME_absolute_get ();
598     peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
599                                                     &ping, peer);
600   }
601   else
602   {
603     peer->timestamp.abs_value_us = 0;
604   }
605
606   return GNUNET_OK;
607 }
608
609
610 /**
611  * Handlers, for diverse services
612  */
613 static struct GNUNET_MESH_MessageHandler handlers[] = {
614   {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
615   {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
616   {NULL, 0, 0}
617 };
618
619
620 /**
621  * Method called whenever another peer has added us to a channel
622  * the other peer initiated.
623  *
624  * @param cls Closure.
625  * @param channel New handle to the channel.
626  * @param initiator Peer that started the channel.
627  * @param port Port this channel is connected to.
628  * @param options channel option flags
629  * @return Initial channel context for the channel
630  *         (can be NULL -- that's not an error).
631  */
632 static void *
633 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
634                  const struct GNUNET_PeerIdentity *initiator,
635                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
636 {
637   long n = (long) cls;
638   struct MeshPeer *peer;
639
640   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
641   GNUNET_assert (NULL != peer);
642   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u\n", n, get_index (peer));
643   peers[n].incoming_ch = channel;
644
645   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
646   {
647     GNUNET_SCHEDULER_cancel (disconnect_task);
648     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
649                                                     &disconnect_mesh_peers,
650                                                     (void *) __LINE__);
651   }
652
653   return NULL;
654 }
655
656 /**
657  * Function called whenever an inbound channel is destroyed.  Should clean up
658  * any associated state.
659  *
660  * @param cls closure (set from GNUNET_MESH_connect)
661  * @param channel connection to the other end (henceforth invalid)
662  * @param channel_ctx place where local state associated
663  *                   with the channel is stored
664  */
665 static void
666 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
667                  void *channel_ctx)
668 {
669   long n = (long) cls;
670   struct MeshPeer *peer = &peers[n];
671
672   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
673               "Incoming channel disconnected at peer %ld\n", n);
674   if (peer->ch == channel)
675     peer->ch = NULL;
676 }
677
678
679 static struct MeshPeer *
680 select_random_peer (struct MeshPeer *peer)
681 {
682   unsigned int r;
683
684   do
685   {
686     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
687   } while (NULL != peers[r].incoming);
688   peers[r].incoming = peer;
689
690   return &peers[r];
691 }
692
693 /**
694  * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
695  *
696  * Testcase continues when the root receives confirmation of connected peers,
697  * on callback funtion ch.
698  *
699  * @param cls Closure (unsued).
700  * @param tc Task Context.
701  */
702 static void
703 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
704 {
705   enum GNUNET_MESH_ChannelOption flags;
706   unsigned long i;
707
708   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
709     return;
710
711   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
712
713   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
714     GNUNET_SCHEDULER_cancel (disconnect_task);
715   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
716                                                   &disconnect_mesh_peers,
717                                                   (void *) __LINE__);
718
719   flags = GNUNET_MESH_OPTION_DEFAULT;
720   for (i = 0; i < TOTAL_PEERS; i++)
721   {
722
723     peers[i].dest = select_random_peer (&peers[i]);
724     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
725                                               &peers[i].dest->id,
726                                               1, flags);
727     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u\n",
728                 i, get_index (peers[i].dest));
729     peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
730                                                        &ping, &peers[i]);
731   }
732   peers_running = TOTAL_PEERS;
733   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
734 }
735
736
737 /**
738  * Callback to be called when the requested peer information is available
739  *
740  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
741  * @param op the operation this callback corresponds to
742  * @param pinfo the result; will be NULL if the operation has failed
743  * @param emsg error message if the operation has failed;
744  *             NULL if the operation is successfull
745  */
746 static void
747 peer_id_cb (void *cls,
748        struct GNUNET_TESTBED_Operation *op,
749        const struct GNUNET_TESTBED_PeerInformation *pinfo,
750        const char *emsg)
751 {
752   long n = (long) cls;
753
754   if (NULL == pinfo || NULL != emsg)
755   {
756     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
757     abort_test (__LINE__);
758     return;
759   }
760   peers[n].id = *(pinfo->result.id);
761   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u  id: %s\n",
762               n, GNUNET_i2s (&peers[n].id));
763   GNUNET_break (GNUNET_OK ==
764                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
765                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
766   p_ids++;
767   if (p_ids < TOTAL_PEERS)
768     return;
769   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
770   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
771                                             &start_test, NULL);
772 }
773
774 /**
775  * test main: start test when all peers are connected
776  *
777  * @param cls Closure.
778  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
779  * @param num_peers Number of peers that are running.
780  * @param testbed_peers Array of peers.
781  * @param meshes Handle to each of the MESHs of the peers.
782  */
783 static void
784 tmain (void *cls,
785        struct GNUNET_MESH_TEST_Context *ctx,
786        unsigned int num_peers,
787        struct GNUNET_TESTBED_Peer **testbed_peers,
788        struct GNUNET_MESH_Handle **meshes)
789 {
790   unsigned long i;
791
792   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
793   ok = 0;
794   test_ctx = ctx;
795   GNUNET_assert (TOTAL_PEERS == num_peers);
796   peers_running = num_peers;
797   testbed_handles = testbed_peers;
798   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
799                                                   &disconnect_mesh_peers,
800                                                   (void *) __LINE__);
801   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
802                                                   &shutdown_task, NULL);
803   for (i = 0; i < TOTAL_PEERS; i++)
804   {
805     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
806     peers[i].up = GNUNET_YES;
807     peers[i].mesh = meshes[i];
808     peers[i].op =
809       GNUNET_TESTBED_peer_get_information (testbed_handles[i],
810                                            GNUNET_TESTBED_PIT_IDENTITY,
811                                            &peer_id_cb, (void *) i);
812   }
813   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
814   /* Continues from pi_cb -> do_test */
815 }
816
817
818 /**
819  * Main: start profiler.
820  */
821 int
822 main (int argc, char *argv[])
823 {
824   static uint32_t ports[2];
825   const char *config_file;
826
827   config_file = "test_mesh.conf";
828
829   ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
830   GNUNET_assert (NULL != ids);
831   p_ids = 0;
832   test_finished = GNUNET_NO;
833   ports[0] = 1;
834   ports[1] = 0;
835   GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
836                         &tmain, NULL, /* tmain cls */
837                         &incoming_channel, &channel_cleaner,
838                         handlers, ports);
839
840   if (ok_goal > ok)
841   {
842     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
843                 "FAILED! (%d/%d)\n", ok, ok_goal);
844     return 1;
845   }
846   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
847   return 0;
848 }
849
850 /* end of gnunet-mesh-profiler.c */
851