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;
45 exchange_hello_last (void *cb_cls, const struct GNUNET_MessageHeader *message);
47 exchange_hello (void *cb_cls, 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)
249 GNUNET_assert (p != NULL);
251 GNUNET_TRANSPORT_disconnect (p->th);
253 if (NULL != p->arm_proc)
255 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
257 GNUNET_OS_process_wait (p->arm_proc);
258 GNUNET_OS_process_close (p->arm_proc);
261 if (p->servicehome != NULL)
263 GNUNET_DISK_directory_remove (p->servicehome);
264 GNUNET_free (p->servicehome);
268 GNUNET_CONFIGURATION_destroy (p->cfg);
274 * Connect the two given peers and call the callback when both peers report the
275 * inbound connect. Remarks: start_peer's notify_connect callback can be called
279 * @param cb the callback to call
280 * @param cb_cls callback cls
281 * @return connect context
283 GNUNET_TRANSPORT_TESTING_ConnectRequest
284 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext *p1,
285 struct PeerContext *p2,
286 GNUNET_TRANSPORT_TESTING_connect_cb cb,
289 struct ConnectingContext *cc =
290 GNUNET_malloc (sizeof (struct ConnectingContext));
292 GNUNET_assert (p1 != NULL);
293 GNUNET_assert (p2 != NULL);
302 GNUNET_TRANSPORT_connect (cc->p1->cfg, NULL, cc, NULL,
303 ¬ify_connect_internal, NULL);
306 GNUNET_TRANSPORT_connect (cc->p2->cfg, NULL, cc, NULL,
307 ¬ify_connect_internal, NULL);
309 GNUNET_assert (cc->th_p1 != NULL);
310 GNUNET_assert (cc->th_p2 != NULL);
312 GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
313 GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
315 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
320 * Cancel the request to connect two peers
321 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
322 * @param cc a connect request handle
324 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
325 (GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
327 struct ConnectingContext *cc = ccr;
330 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
331 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
333 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
334 GNUNET_SCHEDULER_cancel (cc->tct);
336 cc->tct = GNUNET_SCHEDULER_NO_TASK;
338 GNUNET_TRANSPORT_disconnect (cc->th_p1);
339 GNUNET_TRANSPORT_disconnect (cc->th_p2);
345 /* end of transport_testing.h */