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,
51 const struct GNUNET_PeerIdentity *peer,
52 const struct GNUNET_TRANSPORT_ATS_Information *ats,
55 struct ConnectingContext *cc = cls;
57 GNUNET_assert (cc != NULL);
60 memcmp (&(*peer).hashPubKey, &cc->p1->id.hashPubKey,
61 sizeof (GNUNET_HashCode)))
63 if (cc->p1_c == GNUNET_NO)
64 cc->p1_c = GNUNET_YES;
67 memcmp (&(*peer).hashPubKey, &cc->p2->id.hashPubKey,
68 sizeof (GNUNET_HashCode)))
70 if (cc->p2_c == GNUNET_NO)
71 cc->p2_c = GNUNET_YES;
74 if ((cc->p2_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
77 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
78 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
80 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
81 GNUNET_SCHEDULER_cancel (cc->tct);
83 cc->tct = GNUNET_SCHEDULER_NO_TASK;
85 GNUNET_TRANSPORT_disconnect (cc->th_p1);
86 GNUNET_TRANSPORT_disconnect (cc->th_p2);
89 cc->cb (cc->p1, cc->p2, cc->cb_cls);
96 notify_connect (void *cls,
97 const struct GNUNET_PeerIdentity *peer,
98 const struct GNUNET_TRANSPORT_ATS_Information *ats,
101 struct PeerContext *p = cls;
106 p->nc (p->cb_cls, peer, ats, ats_count);
110 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
112 struct PeerContext *p = cls;
117 p->nd (p->cb_cls, peer);
121 notify_receive (void *cls,
122 const struct GNUNET_PeerIdentity *peer,
123 const struct GNUNET_MessageHeader *message,
124 const struct GNUNET_TRANSPORT_ATS_Information *ats,
127 struct PeerContext *p = cls;
132 p->rec (p->cb_cls, peer, message, ats, ats_count);
137 exchange_hello_last (void *cb_cls, const struct GNUNET_MessageHeader *message)
139 struct ConnectingContext *cc = cb_cls;
140 struct PeerContext *me = cc->p2;
142 //struct PeerContext *p1 = cc->p1;
144 GNUNET_assert (message != NULL);
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Exchanging HELLO of size %d with peer (%s)!\n",
147 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
148 message), GNUNET_i2s (&me->id));
149 GNUNET_assert (GNUNET_OK ==
150 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
152 GNUNET_TRANSPORT_offer_hello (cc->th_p1, message, NULL, NULL);
157 exchange_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
159 struct ConnectingContext *cc = cb_cls;
160 struct PeerContext *me = cc->p1;
162 //struct PeerContext *p2 = cc->p2;
164 GNUNET_assert (message != NULL);
165 GNUNET_assert (GNUNET_OK ==
166 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Exchanging HELLO of size %d from peer %s!\n",
170 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
171 message), GNUNET_i2s (&me->id));
172 GNUNET_TRANSPORT_offer_hello (cc->th_p2, message, NULL, NULL);
176 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 struct ConnectingContext *cc = cls;
179 struct PeerContext *p1 = cc->p1;
180 struct PeerContext *p2 = cc->p2;
182 cc->tct = GNUNET_SCHEDULER_NO_TASK;
183 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking peers to connect...\n");
187 /* FIXME: 'pX.id' may still be all-zeros here... */
188 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
189 GNUNET_TRANSPORT_try_connect (cc->th_p2, &p1->id);
191 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
197 * Start a peer with the given configuration
198 * @param rec receive callback
199 * @param nc connect callback
200 * @param nd disconnect callback
201 * @param cb_cls closure for callback
202 * @return the peer context
205 GNUNET_TRANSPORT_TESTING_start_peer (const char *cfgname,
206 GNUNET_TRANSPORT_ReceiveCallback rec,
207 GNUNET_TRANSPORT_NotifyConnect nc,
208 GNUNET_TRANSPORT_NotifyDisconnect nd,
211 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
213 p->cfg = GNUNET_CONFIGURATION_create ();
215 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
216 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
217 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
219 if (NULL != p->servicehome)
220 GNUNET_DISK_directory_remove (p->servicehome);
221 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
222 "gnunet-service-arm", "-c", cfgname,
237 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
240 ¬ify_connect, ¬ify_disconnect);
241 GNUNET_assert (p->th != NULL);
246 * shutdown the given peer
250 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext *p)
253 GNUNET_TRANSPORT_disconnect (p->th);
255 if (NULL != p->arm_proc)
257 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
259 GNUNET_OS_process_wait (p->arm_proc);
260 GNUNET_OS_process_close (p->arm_proc);
263 GNUNET_CONFIGURATION_destroy (p->cfg);
264 if (p->servicehome != NULL)
266 GNUNET_DISK_directory_remove (p->servicehome);
267 GNUNET_free (p->servicehome);
273 * Connect the two given peers and call the callback when both peers report the
274 * inbound connect. Remarks: start_peer's notify_connect callback can be called
278 * @param cb the callback to call
279 * @param cb_cls callback cls
282 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext *p1,
283 struct PeerContext *p2,
284 GNUNET_TRANSPORT_TESTING_connect_cb cb,
287 struct ConnectingContext *cc =
288 GNUNET_malloc (sizeof (struct ConnectingContext));
290 GNUNET_assert (p1 != NULL);
291 GNUNET_assert (p2 != NULL);
299 cc->th_p1 = GNUNET_TRANSPORT_connect (cc->p1->cfg, NULL,
301 NULL, ¬ify_connect_internal, NULL);
303 cc->th_p2 = GNUNET_TRANSPORT_connect (cc->p2->cfg, NULL,
305 NULL, ¬ify_connect_internal, NULL);
307 GNUNET_assert (cc->th_p1 != NULL);
308 GNUNET_assert (cc->th_p2 != NULL);
310 GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
311 GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
313 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
318 /* end of transport_testing.h */