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