2 This file is part of GNUnet.
3 (C) 2006, 2009 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
22 * @file transport_testing.c
23 * @brief testing lib for transport service
25 * @author Matthias Wachs
28 #include "transport-testing.h"
30 struct ConnectingContext
32 struct PeerContext * p1;
33 struct PeerContext * p2;
34 GNUNET_SCHEDULER_TaskIdentifier tct;
35 GNUNET_TRANSPORT_TESTING_connect_cb cb;
38 struct GNUNET_TRANSPORT_Handle *th_p1;
39 struct GNUNET_TRANSPORT_Handle *th_p2;
44 exchange_hello_last (void *cls,
45 const struct GNUNET_MessageHeader *message);
47 exchange_hello (void *cls,
48 const struct GNUNET_MessageHeader *message);
51 notify_connect_internal (void *cls,
52 const struct GNUNET_PeerIdentity *peer,
53 const struct GNUNET_TRANSPORT_ATS_Information *ats,
56 struct ConnectingContext * cc = cls;
57 // /void * cb_cls = cc->cb_cls;
59 GNUNET_assert(cc != NULL);
65 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
66 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
68 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
69 GNUNET_SCHEDULER_cancel(cc->tct);
71 cc->tct = GNUNET_SCHEDULER_NO_TASK;
73 GNUNET_TRANSPORT_disconnect( cc->th_p1);
74 GNUNET_TRANSPORT_disconnect( cc->th_p2);
77 cc->cb (cc->p1, cc->p2, cc->cb_cls);
85 notify_connect (void *cls,
86 const struct GNUNET_PeerIdentity *peer,
87 const struct GNUNET_TRANSPORT_ATS_Information *ats,
90 struct PeerContext * p = cls;
94 p->nc (p->cb_cls, peer, ats, ats_count);
98 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
100 struct PeerContext * p = cls;
104 p->nd (p->cb_cls, peer);
108 notify_receive (void *cls,
109 const struct GNUNET_PeerIdentity *peer,
110 const struct GNUNET_MessageHeader *message,
111 const struct GNUNET_TRANSPORT_ATS_Information *ats,
114 struct PeerContext * p = cls;
118 p->rec (p->cb_cls, peer, message, ats, ats_count);
123 exchange_hello_last (void *cls,
124 const struct GNUNET_MessageHeader *message)
126 struct ConnectingContext * cc = cls;
127 struct PeerContext *me = cc->p2;
128 //struct PeerContext *p1 = cc->p1;
130 GNUNET_assert (message != NULL);
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Exchanging HELLO of size %d with peer (%s)!\n",
133 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
134 GNUNET_i2s (&me->id));
135 GNUNET_assert (GNUNET_OK ==
136 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
138 GNUNET_TRANSPORT_offer_hello (cc->th_p1, message, NULL, NULL);
143 exchange_hello (void *cls,
144 const struct GNUNET_MessageHeader *message)
146 struct ConnectingContext * cc = cls;
147 struct PeerContext *me = cc->p1;
148 //struct PeerContext *p2 = cc->p2;
150 GNUNET_assert (message != NULL);
151 GNUNET_assert (GNUNET_OK ==
152 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Exchanging HELLO of size %d from peer %s!\n",
156 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
157 GNUNET_i2s (&me->id));
158 GNUNET_TRANSPORT_offer_hello (cc->th_p2, message, NULL, NULL);
162 try_connect (void *cls,
163 const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct ConnectingContext * cc = cls;
166 struct PeerContext *p1 = cc->p1;
167 struct PeerContext *p2 = cc->p2;
169 cc->tct = GNUNET_SCHEDULER_NO_TASK;
170 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Asking peers to connect...\n");
175 /* FIXME: 'pX.id' may still be all-zeros here... */
176 GNUNET_TRANSPORT_try_connect (cc->th_p1,
178 GNUNET_TRANSPORT_try_connect (cc->th_p2,
181 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
187 GNUNET_TRANSPORT_TESTING_start_peer (const char * cfgname,
188 GNUNET_TRANSPORT_ReceiveCallback rec,
189 GNUNET_TRANSPORT_NotifyConnect nc,
190 GNUNET_TRANSPORT_NotifyDisconnect nd,
193 struct PeerContext * p = GNUNET_malloc (sizeof (struct PeerContext));
195 p->cfg = GNUNET_CONFIGURATION_create ();
197 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
198 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
199 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
200 if (NULL != p->servicehome)
201 GNUNET_DISK_directory_remove (p->servicehome);
202 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
203 "gnunet-service-arm",
216 p->th = GNUNET_TRANSPORT_connect(p->cfg, NULL,
221 GNUNET_assert (p->th != NULL);
226 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext * p)
229 GNUNET_TRANSPORT_disconnect(p->th);
231 if (NULL != p->arm_proc)
233 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
234 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
235 GNUNET_OS_process_wait (p->arm_proc);
236 GNUNET_OS_process_close (p->arm_proc);
239 GNUNET_CONFIGURATION_destroy (p->cfg);
240 if (p->servicehome != NULL)
242 GNUNET_DISK_directory_remove (p->servicehome);
243 GNUNET_free(p->servicehome);
249 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext * p1,
250 struct PeerContext * p2,
251 GNUNET_TRANSPORT_TESTING_connect_cb cb,
254 struct ConnectingContext * cc = GNUNET_malloc (sizeof (struct ConnectingContext));
256 GNUNET_assert (p1 != NULL);
257 GNUNET_assert (p2 != NULL);
265 cc->th_p1 = GNUNET_TRANSPORT_connect(cc->p1->cfg, NULL,
268 ¬ify_connect_internal,
271 cc->th_p2 = GNUNET_TRANSPORT_connect(cc->p2->cfg, NULL,
274 ¬ify_connect_internal,
277 GNUNET_assert (cc->th_p1 != NULL);
278 GNUNET_assert (cc->th_p2 != NULL);
280 GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
281 GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
283 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
288 /* end of transport_testing.h */