not yet finished: moving perf_ats to testing
[oweals/gnunet.git] / src / ats-tests / ats-testing.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 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 ats/perf_ats.c
22  * @brief ats benchmark: start peers and modify preferences, monitor change over time
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "ats-testing.h"
32
33
34
35
36 static struct GNUNET_ATS_TEST_Topology *top;
37
38 /**
39  * Shutdown nicely
40  *
41  * @param cls NULL
42  * @param tc the task context
43  */
44 static void
45 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
46 {
47   int c_m;
48   int c_s;
49   int c_op;
50   struct BenchmarkPeer *p;
51 /*
52   if (GNUNET_YES == logging)
53     perf_logging_stop();
54 */
55   top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
56 /*
57   if (GNUNET_SCHEDULER_NO_TASK != progress_task)
58   {
59     fprintf (stderr, "0\n");
60     GNUNET_SCHEDULER_cancel (progress_task);
61   }
62   progress_task = GNUNET_SCHEDULER_NO_TASK;
63 */
64   //evaluate ();
65
66   top->state.benchmarking = GNUNET_NO;
67   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
68
69   for (c_m = 0; c_m < top->num_masters; c_m++)
70   {
71     p = &top->mps[c_m];
72     if (NULL != top->mps[c_m].peer_id_op)
73     {
74       GNUNET_TESTBED_operation_done (p->peer_id_op);
75       p->peer_id_op = NULL;
76     }
77
78     if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
79       GNUNET_SCHEDULER_cancel (p->ats_task);
80     p->ats_task = GNUNET_SCHEDULER_NO_TASK;
81
82     for (c_op = 0; c_op < p->num_partners; c_op++)
83     {
84       if (NULL != p->partners[c_op].cth)
85       {
86         GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
87         p->partners[c_op].cth = NULL;
88       }
89       if (NULL != p->partners[c_op].tth)
90       {
91         GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
92         p->partners[c_op].tth = NULL;
93       }
94       if ( (NULL != p->core_connect_ops) &&
95            (NULL != p->core_connect_ops[c_op].connect_op) )
96       {
97         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
98             _("Failed to connect peer 0 and %u\n"), c_op);
99         GNUNET_TESTBED_operation_done (
100             p->core_connect_ops[c_op].connect_op);
101         p->core_connect_ops[c_op].connect_op = NULL;
102       }
103     }
104
105     if (NULL != p->ats_perf_op)
106     {
107       GNUNET_TESTBED_operation_done (p->ats_perf_op);
108       p->ats_perf_op = NULL;
109     }
110
111     if (NULL != p->comm_op)
112     {
113       GNUNET_TESTBED_operation_done (p->comm_op);
114       p->comm_op = NULL;
115     }
116     GNUNET_free_non_null (p->core_connect_ops);
117     GNUNET_free(p->partners);
118     p->partners = NULL;
119   }
120
121   for (c_s = 0; c_s < top->num_slaves; c_s++)
122   {
123     p = &top->sps[c_s];
124     if (NULL != p->peer_id_op)
125     {
126       GNUNET_TESTBED_operation_done (p->peer_id_op);
127       p->peer_id_op = NULL;
128     }
129
130     for (c_op = 0; c_op < p->num_partners; c_op++)
131     {
132       if (NULL != p->partners[c_op].cth)
133       {
134         GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
135         p->partners[c_op].cth = NULL;
136       }
137       if (NULL != p->partners[c_op].tth)
138       {
139         GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
140         p->partners[c_op].tth = NULL;
141       }
142     }
143     if (NULL != p->ats_perf_op)
144     {
145       GNUNET_TESTBED_operation_done (p->ats_perf_op);
146       p->ats_perf_op = NULL;
147     }
148     if (NULL != p->comm_op)
149     {
150       GNUNET_TESTBED_operation_done (p->comm_op);
151       p->comm_op = NULL;
152     }
153     GNUNET_free(p->partners);
154     p->partners = NULL;
155   }
156
157   GNUNET_SCHEDULER_shutdown ();
158 }
159
160 static struct BenchmarkPartner *
161 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
162 {
163   int c_m;
164   GNUNET_assert (NULL != me);
165   GNUNET_assert (NULL != peer);
166
167   for (c_m = 0; c_m < me->num_partners; c_m++)
168   {
169     /* Find a partner with other as destination */
170     if (0 == memcmp (peer, &me->partners[c_m].dest->id,
171             sizeof(struct GNUNET_PeerIdentity)))
172     {
173       return &me->partners[c_m];
174     }
175   }
176
177   return NULL;
178 }
179
180
181 static struct BenchmarkPeer *
182 find_peer (const struct GNUNET_PeerIdentity * peer)
183 {
184   int c_p;
185
186   for (c_p = 0; c_p < top->num_masters; c_p++)
187   {
188     if (0 == memcmp (&top->mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
189       return &top->mps[c_p];
190   }
191
192   for (c_p = 0; c_p < top->num_slaves; c_p++)
193   {
194     if (0 == memcmp (&top->sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
195       return &top->sps[c_p];
196   }
197   return NULL ;
198 }
199
200
201 /**
202  * Method called whenever a given peer connects.
203  *
204  * @param cls closure
205  * @param peer peer identity this notification is about
206  */
207 static void
208 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
209 {
210   struct BenchmarkPeer *me = cls;
211   struct BenchmarkPeer *remote;
212   char *id;
213   int c;
214   int completed;
215
216   remote = find_peer (peer);
217   if (NULL == remote)
218   {
219     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unknown peer connected: `%s'\n", GNUNET_i2s (peer));
220     GNUNET_break(0);
221     return;
222   }
223
224   id = GNUNET_strdup (GNUNET_i2s (&me->id));
225   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
226       (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
227       (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
228       GNUNET_i2s (peer));
229
230   me->core_connections++;
231   if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
232       && (GNUNET_NO == top->state.connected_CORE))
233   {
234     me->core_slave_connections++;
235
236     if (me->core_slave_connections == top->num_slaves)
237     {
238       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
239           me->no);
240     }
241     completed = GNUNET_YES;
242     for (c = 0; c < top->num_masters; c++)
243     {
244       if (top->mps[c].core_slave_connections != top->num_slaves)
245         completed = GNUNET_NO;
246     }
247     if (GNUNET_YES == completed)
248     {
249       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
250           "All master peers connected all slave peers\n", id,
251           GNUNET_i2s (peer));
252       top->state.connected_CORE = GNUNET_YES;
253     }
254   }
255   GNUNET_free(id);
256 }
257
258 static void
259 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
260 {
261   struct BenchmarkPeer *me = cls;
262   struct BenchmarkPartner *p;
263   char *id;
264
265   if (NULL == (p = find_partner (me, peer)))
266     return;
267
268   id = GNUNET_strdup (GNUNET_i2s (&me->id));
269   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
270       GNUNET_i2s (peer));
271   GNUNET_assert(me->core_connections > 0);
272   me->core_connections--;
273
274   if ((GNUNET_YES == top->state.benchmarking)
275       && ((GNUNET_YES == me->master) || (GNUNET_YES == p->dest->master)))
276   {
277     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
278         "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
279     if (NULL != p->tth)
280     {
281       GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->tth);
282       p->tth = NULL;
283     }
284     if (NULL != p->cth)
285     {
286       GNUNET_CORE_notify_transmit_ready_cancel (p->cth);
287       p->cth = NULL;
288     }
289   }
290   GNUNET_free(id);
291 }
292
293
294 static void *
295 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
296 {
297   struct BenchmarkPeer *me = cls;
298
299   me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
300       comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, top->handlers);
301   if (NULL == me->ch)
302     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
303   return me->ch;
304 }
305
306 static void
307 core_disconnect_adapter (void *cls, void *op_result)
308 {
309   struct BenchmarkPeer *me = cls;
310
311   GNUNET_CORE_disconnect (me->ch);
312   me->ch = NULL;
313 }
314
315
316 static void *
317 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
318 {
319   struct BenchmarkPeer *me = cls;
320
321   me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, top->transport_recv_cb,
322       &comm_connect_cb, &comm_disconnect_cb);
323   if (NULL == me->th)
324     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
325   return me->th;
326 }
327
328 static void
329 transport_disconnect_adapter (void *cls, void *op_result)
330 {
331   struct BenchmarkPeer *me = cls;
332
333   GNUNET_TRANSPORT_disconnect (me->th);
334   me->th = NULL;
335 }
336
337
338 static void
339 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
340     const char *emsg)
341 {
342   struct TestbedConnectOperation *cop = cls;
343   static int ops = 0;
344   int c;
345   if (NULL == emsg)
346   {
347     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
348         _("Connected master [%u] with slave [%u]\n"), cop->master->no,
349         cop->slave->no);
350   }
351   else
352   {
353     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
354         _("Failed to connect master peer [%u] with slave [%u]\n"),
355         cop->master->no, cop->slave->no);
356     GNUNET_break(0);
357     if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
358       GNUNET_SCHEDULER_cancel (top->shutdown_task);
359     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
360   }
361   GNUNET_TESTBED_operation_done (op);
362   ops++;
363   for (c = 0; c < top->num_slaves; c++)
364   {
365     if (cop == &cop->master->core_connect_ops[c])
366       cop->master->core_connect_ops[c].connect_op = NULL;
367   }
368   if (ops == top->num_masters * top->num_slaves)
369   {
370     top->state.connected_PEERS = GNUNET_YES;
371     /* Notify about setup done */
372     if (NULL != top->done_cb)
373       top->done_cb (top->done_cb_cls, top->mps, top->sps);
374   }
375 }
376
377 static void
378 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
379 {
380   int c_m;
381   int c_s;
382   struct BenchmarkPeer *p;
383
384   if ((top->state.connected_ATS_service == GNUNET_NO) ||
385       (top->state.connected_COMM_service == GNUNET_NO))
386     return;
387
388   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
389
390   for (c_m = 0; c_m < top->num_masters; c_m++)
391   {
392     p = &top->mps[c_m];
393     p->core_connect_ops = GNUNET_malloc (top->num_slaves *
394         sizeof (struct TestbedConnectOperation));
395
396     for (c_s = 0; c_s < top->num_slaves; c_s++)
397     {
398       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
399           _("Connecting master [%u] with slave [%u]\n"), p->no, top->sps[c_s].no);
400       p->core_connect_ops[c_s].master = p;
401       p->core_connect_ops[c_s].slave = &top->sps[c_s];
402       p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
403           NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
404           top->sps[c_s].peer, p->peer);
405       if (NULL == p->core_connect_ops[c_s].connect_op)
406       {
407         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
408             _("Could not connect master [%u] and slave [%u]\n"), p->no,
409             top->sps[c_s].no);
410         GNUNET_break(0);
411         if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
412           GNUNET_SCHEDULER_cancel (top->shutdown_task);
413         top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
414         return;
415       }
416     }
417   }
418 }
419
420
421 static void
422 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
423     void *ca_result, const char *emsg)
424 {
425   static int comm_done = 0;
426   if ((NULL != emsg) || (NULL == ca_result))
427   {
428     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
429     GNUNET_break(0);
430     if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
431       GNUNET_SCHEDULER_cancel (top->shutdown_task);
432     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
433     return;
434   }
435   comm_done++;
436
437   if (comm_done == top->num_slaves + top->num_masters)
438   {
439     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
440         (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
441     top->state.connected_COMM_service = GNUNET_YES;
442     GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
443   }
444 }
445
446 static void
447 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
448 {
449   int c_s;
450   int c_m;
451   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
452       (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
453   for (c_m = 0; c_m < top->num_masters; c_m++)
454   {
455     if (GNUNET_YES == top->test_core)
456       top->mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, top->mps[c_m].peer,
457         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
458         &core_disconnect_adapter, &top->mps[c_m]);
459     else
460     {
461       top->mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, top->mps[c_m].peer,
462         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
463         &transport_disconnect_adapter, &top->mps[c_m]);
464     }
465   }
466
467   for (c_s = 0; c_s < top->num_slaves; c_s++)
468   {
469     if (GNUNET_YES == top->test_core)
470       top->sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
471         "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
472         &core_disconnect_adapter, &top->sps[c_s]);
473     else
474     {
475       top->sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
476         "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
477         &transport_disconnect_adapter, &top->sps[c_s]);
478     }
479   }
480 }
481
482
483
484 static void
485 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
486     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
487     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
488     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
489 {
490   struct BenchmarkPeer *me = cls;
491   struct BenchmarkPartner *p;
492   int c_a;
493   int log;
494   char *peer_id;
495
496   p = find_partner (me, &address->peer);
497   if (NULL == p)
498   {
499     /* This is not one of my partners
500      * Will happen since the peers will connect to each other due to gossiping
501      */
502     return;
503   }
504   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
505
506   log = GNUNET_NO;
507   if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
508       (p->bandwidth_out != ntohl (bandwidth_out.value__)))
509       log = GNUNET_YES;
510   p->bandwidth_in = ntohl (bandwidth_in.value__);
511   p->bandwidth_out = ntohl (bandwidth_out.value__);
512
513   for (c_a = 0; c_a < ats_count; c_a++)
514   {
515     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
516         (GNUNET_YES == p->me->master) ? "Master" : "Slave",
517         p->me->no,
518         GNUNET_i2s (&p->dest->id),
519         GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
520         ntohl(ats[c_a].value));
521     switch (ntohl (ats[c_a].type ))
522     {
523       case GNUNET_ATS_ARRAY_TERMINATOR:
524         break;
525       case GNUNET_ATS_UTILIZATION_OUT:
526         if (p->ats_utilization_up != ntohl (ats[c_a].value))
527             log = GNUNET_YES;
528         p->ats_utilization_up = ntohl (ats[c_a].value);
529
530         break;
531       case GNUNET_ATS_UTILIZATION_IN:
532         if (p->ats_utilization_down != ntohl (ats[c_a].value))
533             log = GNUNET_YES;
534         p->ats_utilization_down = ntohl (ats[c_a].value);
535         break;
536       case GNUNET_ATS_NETWORK_TYPE:
537         if (p->ats_network_type != ntohl (ats[c_a].value))
538             log = GNUNET_YES;
539         p->ats_network_type = ntohl (ats[c_a].value);
540         break;
541       case GNUNET_ATS_QUALITY_NET_DELAY:
542         if (p->ats_delay != ntohl (ats[c_a].value))
543             log = GNUNET_YES;
544         p->ats_delay = ntohl (ats[c_a].value);
545         break;
546       case GNUNET_ATS_QUALITY_NET_DISTANCE:
547         if (p->ats_distance != ntohl (ats[c_a].value))
548             log = GNUNET_YES;
549         p->ats_distance = ntohl (ats[c_a].value);
550         GNUNET_break (0);
551         break;
552       case GNUNET_ATS_COST_WAN:
553         if (p->ats_cost_wan != ntohl (ats[c_a].value))
554             log = GNUNET_YES;
555         p->ats_cost_wan = ntohl (ats[c_a].value);
556         break;
557       case GNUNET_ATS_COST_LAN:
558         if (p->ats_cost_lan != ntohl (ats[c_a].value))
559             log = GNUNET_YES;
560         p->ats_cost_lan = ntohl (ats[c_a].value);
561         break;
562       case GNUNET_ATS_COST_WLAN:
563         if (p->ats_cost_wlan != ntohl (ats[c_a].value))
564             log = GNUNET_YES;
565         p->ats_cost_wlan = ntohl (ats[c_a].value);
566         break;
567       default:
568         break;
569     }
570   }
571   if ((GNUNET_YES == top->logging) && (GNUNET_YES == log))
572   {
573 //    collect_log_now();
574   }
575   GNUNET_free(peer_id);
576 }
577
578
579 static void *
580 ats_perf_connect_adapter (void *cls,
581     const struct GNUNET_CONFIGURATION_Handle *cfg)
582 {
583   struct BenchmarkPeer *me = cls;
584
585   me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
586       &ats_performance_info_cb, me);
587   if (NULL == me->ats_perf_handle)
588     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
589         "Failed to create ATS performance handle \n");
590   return me->ats_perf_handle;
591 }
592
593 static void
594 ats_perf_disconnect_adapter (void *cls, void *op_result)
595 {
596   struct BenchmarkPeer *me = cls;
597
598   GNUNET_ATS_performance_done (me->ats_perf_handle);
599   me->ats_perf_handle = NULL;
600 }
601
602 static void
603 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
604     void *ca_result, const char *emsg)
605 {
606   static int op_done = 0;
607
608   if ((NULL != emsg) || (NULL == ca_result))
609   {
610     GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
611     GNUNET_break(0);
612     if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
613       GNUNET_SCHEDULER_cancel (top->shutdown_task);
614     top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
615     return;
616   }
617   op_done++;
618   if (op_done == (top->num_masters + top->num_slaves))
619   {
620     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
621     top->state.connected_ATS_service = GNUNET_YES;
622     GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
623   }
624 }
625
626
627 static void
628 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
629 {
630   int c_m;
631   int c_s;
632
633   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
634   for (c_m = 0; c_m < top->num_masters; c_m++)
635   {
636     top->mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL,
637         top->mps[c_m].peer,
638         "ats", ats_connect_completion_cb, NULL,
639         &ats_perf_connect_adapter,
640         &ats_perf_disconnect_adapter, &top->mps[c_m]);
641   }
642
643   for (c_s = 0; c_s < top->num_slaves; c_s++)
644   {
645     top->sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
646         "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
647         &ats_perf_disconnect_adapter, &top->sps[c_s]);
648   }
649 }
650
651
652
653 static void
654 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
655     const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
656 {
657   struct BenchmarkPeer *p = cb_cls;
658   static int done = 0;
659
660   GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
661
662   p->id = *pinfo->result.id;
663   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
664       (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
665       GNUNET_i2s (&p->id));
666
667   GNUNET_TESTBED_operation_done (op);
668   p->peer_id_op = NULL;
669   done++;
670
671   if (done == top->num_slaves + top->num_masters)
672   {
673     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
674         "Retrieved all peer ID, connect to ATS\n");
675     GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
676   }
677 }
678
679 /**
680  * Signature of a main function for a testcase.
681  *
682  * @param cls closure
683  * @param num_peers number of peers in 'peers'
684  * @param peers_ handle to peers run in the testbed
685  * @param links_succeeded the number of overlay link connection attempts that
686  *          succeeded
687  * @param links_failed the number of overlay link connection attempts that
688  *          failed
689  */
690 static void
691 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
692           unsigned int num_peers,
693           struct GNUNET_TESTBED_Peer **peers_,
694           unsigned int links_succeeded,
695           unsigned int links_failed)
696 {
697   int c_m;
698   int c_s;
699   GNUNET_assert(NULL == cls);
700   GNUNET_assert(top->num_masters + top->num_slaves == num_peers);
701   GNUNET_assert(NULL != peers_);
702
703   top->shutdown_task = GNUNET_SCHEDULER_add_delayed (
704       GNUNET_TIME_relative_multiply (TEST_TIMEOUT, top->num_masters + top->num_slaves),
705       &do_shutdown, top );
706
707   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting up %u masters and %u slaves\n",
708       top->num_masters, top->num_slaves);
709
710   /* Setup master peers */
711   for (c_m = 0; c_m < top->num_masters; c_m++)
712   {
713     GNUNET_assert(NULL != peers_[c_m]);
714     top->mps[c_m].peer = peers_[c_m];
715     top->mps[c_m].no = c_m;
716     top->mps[c_m].master = GNUNET_YES;
717     top->mps[c_m].pref_partner = &top->sps[c_m];
718     top->mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
719     top->mps[c_m].partners =
720         GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
721     top->mps[c_m].num_partners = top->num_slaves;
722     /* Initialize partners */
723     for (c_s = 0; c_s < top->num_slaves; c_s++)
724     {
725       top->mps[c_m].partners[c_s].me = &top->mps[c_m];
726       top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
727     }
728     /* Get configuration */
729     top->mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
730         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &top->mps[c_m]);
731   }
732
733   /* Setup slave peers */
734   for (c_s = 0; c_s < top->num_slaves; c_s++)
735   {
736     GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
737     top->sps[c_s].peer = peers_[c_s + top->num_masters];
738     top->sps[c_s].no = c_s + top->num_masters;
739     top->sps[c_s].master = GNUNET_NO;
740     top->sps[c_s].partners =
741         GNUNET_malloc (top->num_masters * sizeof (struct BenchmarkPartner));
742     top->sps[c_s].num_partners = top->num_masters;
743     /* Initialize partners */
744     for (c_m = 0; c_m < top->num_masters; c_m++)
745     {
746       top->sps[c_s].partners[c_m].me = &top->sps[c_s];
747       top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
748     }
749     /* Get configuration */
750     top->sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
751         GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &top->sps[c_s]);
752   }
753 }
754
755 /**
756  * Controller event callback
757  *
758  * @param cls NULL
759  * @param event the controller event
760  */
761 static void
762 controller_event_cb (void *cls,
763     const struct GNUNET_TESTBED_EventInformation *event)
764 {
765   struct GNUNET_ATS_TEST_Topology *top = cls;
766   switch (event->type)
767   {
768   case GNUNET_TESTBED_ET_CONNECT:
769     break;
770   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
771     break;
772   default:
773     GNUNET_break(0);
774     GNUNET_SCHEDULER_cancel (top->shutdown_task);
775     top->shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
776   }
777 }
778
779 void
780 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
781     unsigned int num_slaves,
782     unsigned int num_masters,
783     int test_core,
784     GNUNET_ATS_TESTING_TopologySetupDoneCallback done_cb,
785     void *done_cb_cls,
786     struct GNUNET_CORE_MessageHandler *handlers,
787     GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb)
788 {
789
790   top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
791   top->num_masters = num_masters;
792   top->num_slaves = num_slaves;
793   top->handlers = handlers;
794   top->done_cb = done_cb;
795   top->done_cb_cls = done_cb_cls;
796   top->test_core = test_core;
797
798   top->mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
799   top->sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
800
801   /* Start topology */
802   uint64_t event_mask;
803   event_mask = 0;
804   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
805   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
806   (void) GNUNET_TESTBED_test_run (name, cfg_file,
807       num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
808       &main_run, NULL);
809 }
810
811 void
812 GNUNET_ATS_TEST_shutdown_topology (void)
813 {
814   GNUNET_SCHEDULER_shutdown();
815 }
816
817 /* end of file perf_ats.c */