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,
46 const struct GNUNET_MessageHeader *message);
48 exchange_hello (void *cb_cls,
49 const struct GNUNET_MessageHeader *message);
52 notify_connect_internal (void *cls,
53 const struct GNUNET_PeerIdentity *peer,
54 const struct GNUNET_TRANSPORT_ATS_Information *ats,
57 struct ConnectingContext * cc = cls;
59 GNUNET_assert(cc != NULL);
61 if (0 == memcmp (&(*peer).hashPubKey, &cc->p1->id.hashPubKey, sizeof (GNUNET_HashCode)))
63 if (cc->p1_c == GNUNET_NO)
64 cc->p1_c = GNUNET_YES;
66 if (0 == memcmp (&(*peer).hashPubKey, &cc->p2->id.hashPubKey, sizeof (GNUNET_HashCode)))
68 if (cc->p2_c == GNUNET_NO)
69 cc->p2_c = GNUNET_YES;
72 if ((cc->p2_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
75 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
76 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
78 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
79 GNUNET_SCHEDULER_cancel(cc->tct);
81 cc->tct = GNUNET_SCHEDULER_NO_TASK;
83 GNUNET_TRANSPORT_disconnect (cc->th_p1);
84 GNUNET_TRANSPORT_disconnect (cc->th_p2);
87 cc->cb (cc->p1, cc->p2, cc->cb_cls);
94 notify_connect (void *cls,
95 const struct GNUNET_PeerIdentity *peer,
96 const struct GNUNET_TRANSPORT_ATS_Information *ats,
99 struct PeerContext * p = cls;
103 p->nc (p->cb_cls, peer, ats, ats_count);
107 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
109 struct PeerContext * p = cls;
113 p->nd (p->cb_cls, peer);
117 notify_receive (void *cls,
118 const struct GNUNET_PeerIdentity *peer,
119 const struct GNUNET_MessageHeader *message,
120 const struct GNUNET_TRANSPORT_ATS_Information *ats,
123 struct PeerContext * p = cls;
127 p->rec (p->cb_cls, peer, message, ats, ats_count);
132 exchange_hello_last (void *cb_cls,
133 const struct GNUNET_MessageHeader *message)
135 struct ConnectingContext * cc = cb_cls;
136 struct PeerContext *me = cc->p2;
137 //struct PeerContext *p1 = cc->p1;
139 GNUNET_assert (message != NULL);
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "Exchanging HELLO of size %d with peer (%s)!\n",
142 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
143 GNUNET_i2s (&me->id));
144 GNUNET_assert (GNUNET_OK ==
145 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
147 GNUNET_TRANSPORT_offer_hello (cc->th_p1, message, NULL, NULL);
152 exchange_hello (void *cb_cls,
153 const struct GNUNET_MessageHeader *message)
155 struct ConnectingContext * cc = cb_cls;
156 struct PeerContext *me = cc->p1;
157 //struct PeerContext *p2 = cc->p2;
159 GNUNET_assert (message != NULL);
160 GNUNET_assert (GNUNET_OK ==
161 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Exchanging HELLO of size %d from peer %s!\n",
165 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
166 GNUNET_i2s (&me->id));
167 GNUNET_TRANSPORT_offer_hello (cc->th_p2, message, NULL, NULL);
171 try_connect (void *cls,
172 const struct GNUNET_SCHEDULER_TaskContext *tc)
174 struct ConnectingContext * cc = cls;
175 struct PeerContext *p1 = cc->p1;
176 struct PeerContext *p2 = cc->p2;
178 cc->tct = GNUNET_SCHEDULER_NO_TASK;
179 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Asking peers to connect...\n");
184 /* FIXME: 'pX.id' may still be all-zeros here... */
185 GNUNET_TRANSPORT_try_connect (cc->th_p1,
187 GNUNET_TRANSPORT_try_connect (cc->th_p2,
190 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", &p->servicehome);
218 if (NULL != p->servicehome)
219 GNUNET_DISK_directory_remove (p->servicehome);
220 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
221 "gnunet-service-arm",
234 p->th = GNUNET_TRANSPORT_connect(p->cfg, NULL,
239 GNUNET_assert (p->th != NULL);
244 * shutdown the given peer
248 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext * p)
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 GNUNET_CONFIGURATION_destroy (p->cfg);
262 if (p->servicehome != NULL)
264 GNUNET_DISK_directory_remove (p->servicehome);
265 GNUNET_free(p->servicehome);
271 * Connect the two given peers and call the callback when both peers report the
272 * inbound connect. Remarks: start_peer's notify_connect callback can be called
276 * @param cb the callback to call
277 * @param cb_cls callback cls
280 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext * p1,
281 struct PeerContext * p2,
282 GNUNET_TRANSPORT_TESTING_connect_cb cb,
285 struct ConnectingContext * cc = GNUNET_malloc (sizeof (struct ConnectingContext));
287 GNUNET_assert (p1 != NULL);
288 GNUNET_assert (p2 != NULL);
296 cc->th_p1 = GNUNET_TRANSPORT_connect(cc->p1->cfg, NULL,
299 ¬ify_connect_internal,
302 cc->th_p2 = GNUNET_TRANSPORT_connect(cc->p2->cfg, NULL,
305 ¬ify_connect_internal,
308 GNUNET_assert (cc->th_p1 != NULL);
309 GNUNET_assert (cc->th_p2 != NULL);
311 GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
312 GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
314 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
319 /* end of transport_testing.h */