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 static void exchange_hello_last (void *cb_cls,
45 const struct GNUNET_MessageHeader *message);
46 static void exchange_hello (void *cb_cls,
47 const struct GNUNET_MessageHeader *message);
50 notify_connect_internal (void *cls, const struct GNUNET_PeerIdentity *peer,
51 const struct GNUNET_TRANSPORT_ATS_Information *ats,
54 struct ConnectingContext *cc = cls;
56 GNUNET_assert (cc != NULL);
59 memcmp (&(*peer).hashPubKey, &cc->p1->id.hashPubKey,
60 sizeof (GNUNET_HashCode)))
62 if (cc->p1_c == GNUNET_NO)
63 cc->p1_c = GNUNET_YES;
66 memcmp (&(*peer).hashPubKey, &cc->p2->id.hashPubKey,
67 sizeof (GNUNET_HashCode)))
69 if (cc->p2_c == GNUNET_NO)
70 cc->p2_c = GNUNET_YES;
73 if ((cc->p2_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
76 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
77 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
79 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
80 GNUNET_SCHEDULER_cancel (cc->tct);
82 cc->tct = GNUNET_SCHEDULER_NO_TASK;
84 GNUNET_TRANSPORT_disconnect (cc->th_p1);
85 GNUNET_TRANSPORT_disconnect (cc->th_p2);
88 cc->cb (cc->p1, cc->p2, cc->cb_cls);
95 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
96 const struct GNUNET_TRANSPORT_ATS_Information *ats,
99 struct PeerContext *p = cls;
104 p->nc (p->cb_cls, peer, ats, ats_count);
108 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
110 struct PeerContext *p = cls;
115 p->nd (p->cb_cls, peer);
119 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
120 const struct GNUNET_MessageHeader *message,
121 const struct GNUNET_TRANSPORT_ATS_Information *ats,
124 struct PeerContext *p = cls;
129 p->rec (p->cb_cls, peer, message, ats, ats_count);
134 exchange_hello_last (void *cb_cls, const struct GNUNET_MessageHeader *message)
136 struct ConnectingContext *cc = cb_cls;
137 struct PeerContext *me = cc->p2;
139 //struct PeerContext *p1 = cc->p1;
141 GNUNET_assert (message != NULL);
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Exchanging HELLO of size %d with peer (%s)!\n",
144 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
145 message), GNUNET_i2s (&me->id));
146 GNUNET_assert (GNUNET_OK ==
147 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
149 GNUNET_TRANSPORT_offer_hello (cc->th_p1, message, NULL, NULL);
154 exchange_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
156 struct ConnectingContext *cc = cb_cls;
157 struct PeerContext *me = cc->p1;
159 //struct PeerContext *p2 = cc->p2;
161 GNUNET_assert (message != NULL);
162 GNUNET_assert (GNUNET_OK ==
163 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Exchanging HELLO of size %d from peer %s!\n",
167 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
168 message), GNUNET_i2s (&me->id));
169 GNUNET_TRANSPORT_offer_hello (cc->th_p2, message, NULL, NULL);
173 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 struct ConnectingContext *cc = cls;
176 struct PeerContext *p1 = cc->p1;
177 struct PeerContext *p2 = cc->p2;
179 cc->tct = GNUNET_SCHEDULER_NO_TASK;
180 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking peers to connect...\n");
184 /* FIXME: 'pX.id' may still be all-zeros here... */
185 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
186 GNUNET_TRANSPORT_try_connect (cc->th_p2, &p1->id);
189 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
194 * Start a peer with the given configuration
195 * @param rec receive callback
196 * @param nc connect callback
197 * @param nd disconnect callback
198 * @param cb_cls closure for callback
199 * @return the peer context
202 GNUNET_TRANSPORT_TESTING_start_peer (const char *cfgname,
203 GNUNET_TRANSPORT_ReceiveCallback rec,
204 GNUNET_TRANSPORT_NotifyConnect nc,
205 GNUNET_TRANSPORT_NotifyDisconnect nd,
208 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
210 p->cfg = GNUNET_CONFIGURATION_create ();
212 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
213 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
214 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
216 if (NULL != p->servicehome)
217 GNUNET_DISK_directory_remove (p->servicehome);
219 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
220 "gnunet-service-arm", "-c", cfgname,
236 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
237 ¬ify_connect, ¬ify_disconnect);
238 GNUNET_assert (p->th != NULL);
243 * shutdown the given peer
247 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext *p)
250 GNUNET_TRANSPORT_disconnect (p->th);
252 if (NULL != p->arm_proc)
254 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
255 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
256 GNUNET_OS_process_wait (p->arm_proc);
257 GNUNET_OS_process_close (p->arm_proc);
260 GNUNET_CONFIGURATION_destroy (p->cfg);
261 if (p->servicehome != NULL)
263 GNUNET_DISK_directory_remove (p->servicehome);
264 GNUNET_free (p->servicehome);
270 * Connect the two given peers and call the callback when both peers report the
271 * inbound connect. Remarks: start_peer's notify_connect callback can be called
275 * @param cb the callback to call
276 * @param cb_cls callback cls
279 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext *p1,
280 struct PeerContext *p2,
281 GNUNET_TRANSPORT_TESTING_connect_cb cb,
284 struct ConnectingContext *cc =
285 GNUNET_malloc (sizeof (struct ConnectingContext));
287 GNUNET_assert (p1 != NULL);
288 GNUNET_assert (p2 != NULL);
297 GNUNET_TRANSPORT_connect (cc->p1->cfg, NULL, cc, NULL,
298 ¬ify_connect_internal, NULL);
301 GNUNET_TRANSPORT_connect (cc->p2->cfg, NULL, cc, NULL,
302 ¬ify_connect_internal, NULL);
304 GNUNET_assert (cc->th_p1 != NULL);
305 GNUNET_assert (cc->th_p2 != NULL);
307 GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
308 GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
310 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
315 /* end of transport_testing.h */