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