documentation
[oweals/gnunet.git] / src / transport / transport-testing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport_testing.c
23  * @brief testing lib for transport service
24  *
25  * @author Matthias Wachs
26  */
27
28 #include "transport-testing.h"
29
30 struct ConnectingContext
31 {
32   struct PeerContext * p1;
33   struct PeerContext * p2;
34   GNUNET_SCHEDULER_TaskIdentifier tct;
35   GNUNET_TRANSPORT_TESTING_connect_cb cb;
36   void * cb_cls;
37
38   struct GNUNET_TRANSPORT_Handle *th_p1;
39   struct GNUNET_TRANSPORT_Handle *th_p2;
40   int c;
41 };
42
43 static void
44 exchange_hello_last (void *cb_cls,
45                      const struct GNUNET_MessageHeader *message);
46 static void
47 exchange_hello (void *cb_cls,
48                      const struct GNUNET_MessageHeader *message);
49
50 static void
51 notify_connect_internal (void *cls,
52                 const struct GNUNET_PeerIdentity *peer,
53                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
54                 uint32_t ats_count)
55 {
56   struct ConnectingContext * cc = cls;
57 //  /void * cb_cls = cc->cb_cls;
58
59   GNUNET_assert(cc != NULL);
60   cc->c++;
61
62   if (cc->c == 2)
63   {
64      /* clean up */
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);
67
68     if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
69       GNUNET_SCHEDULER_cancel(cc->tct);
70
71     cc->tct = GNUNET_SCHEDULER_NO_TASK;
72
73     GNUNET_TRANSPORT_disconnect( cc->th_p1);
74     GNUNET_TRANSPORT_disconnect( cc->th_p2);
75
76     if (cc->cb != NULL)
77       cc->cb (cc->p1, cc->p2, cc->cb_cls);
78
79     GNUNET_free(cc);
80   }
81
82 }
83
84 static void
85 notify_connect (void *cls,
86                 const struct GNUNET_PeerIdentity *peer,
87                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
88                 uint32_t ats_count)
89 {
90   struct PeerContext * p = cls;
91   if (p == NULL)
92     return;
93   if (p->nc != NULL)
94     p->nc (p->cb_cls, peer, ats, ats_count);
95 }
96
97 static void
98 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
99 {
100   struct PeerContext * p = cls;
101   if (p == NULL)
102     return;
103   if (p->nd != NULL)
104     p->nd (p->cb_cls, peer);
105 }
106
107 static void
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,
112                 uint32_t ats_count)
113 {
114   struct PeerContext * p = cls;
115   if (p == NULL)
116     return;
117   if (p->rec != NULL)
118     p->rec (p->cb_cls, peer, message, ats, ats_count);
119 }
120
121
122 static void
123 exchange_hello_last (void *cb_cls,
124                      const struct GNUNET_MessageHeader *message)
125 {
126   struct ConnectingContext * cc = cb_cls;
127   struct PeerContext *me = cc->p2;
128   //struct PeerContext *p1 = cc->p1;
129
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 *)
137                                       message, &me->id));
138   GNUNET_TRANSPORT_offer_hello (cc->th_p1, message, NULL, NULL);
139 }
140
141
142 static void
143 exchange_hello (void *cb_cls,
144                 const struct GNUNET_MessageHeader *message)
145 {
146   struct ConnectingContext * cc = cb_cls;
147   struct PeerContext *me = cc->p1;
148   //struct PeerContext *p2 = cc->p2;
149
150   GNUNET_assert (message != NULL);
151   GNUNET_assert (GNUNET_OK ==
152                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
153                                       message, &me->id));
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);
159 }
160
161 static void
162 try_connect (void *cls,
163              const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   struct ConnectingContext * cc = cls;
166   struct PeerContext *p1 = cc->p1;
167   struct PeerContext *p2 = cc->p2;
168
169   cc->tct = GNUNET_SCHEDULER_NO_TASK;
170   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
171     return;
172
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,
177                                 &p2->id);
178   GNUNET_TRANSPORT_try_connect (cc->th_p2,
179                                 &p1->id);
180
181   cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
182                                       &try_connect,
183                                       cc);
184 }
185
186
187 /**
188  * Start a peer with the given configuration
189  * @param rec receive callback
190  * @param nc connect callback
191  * @param nd disconnect callback
192  * @param cb_cls closure for callback
193  * @return the peer context
194  */
195 struct PeerContext *
196 GNUNET_TRANSPORT_TESTING_start_peer (const char * cfgname,
197     GNUNET_TRANSPORT_ReceiveCallback rec,
198     GNUNET_TRANSPORT_NotifyConnect nc,
199     GNUNET_TRANSPORT_NotifyDisconnect nd,
200     void * cb_cls)
201 {
202   struct PeerContext * p = GNUNET_malloc (sizeof (struct PeerContext));
203
204   p->cfg = GNUNET_CONFIGURATION_create ();
205
206   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
207   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
208       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
209   if (NULL != p->servicehome)
210     GNUNET_DISK_directory_remove (p->servicehome);
211   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
212                                         "gnunet-service-arm",
213                                         "-c", cfgname,
214 #if VERBOSE_PEERS
215                                         "-L", "DEBUG",
216 #else
217                                         "-L", "ERROR",
218 #endif
219                                         NULL);
220   p->nc = nc;
221   p->nd = nd;
222   p->rec = rec;
223   p->cb_cls = cb_cls;
224
225   p->th = GNUNET_TRANSPORT_connect(p->cfg, NULL,
226                             p,
227                             &notify_receive,
228                             &notify_connect,
229                             &notify_disconnect);
230   GNUNET_assert (p->th != NULL);
231   return p;
232 }
233
234 /**
235  * shutdown the given peer
236  * @param p the peer
237  */
238 void
239 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext * p)
240 {
241   if (p->th != NULL)
242     GNUNET_TRANSPORT_disconnect(p->th);
243
244   if (NULL != p->arm_proc)
245     {
246       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
247         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
248       GNUNET_OS_process_wait (p->arm_proc);
249       GNUNET_OS_process_close (p->arm_proc);
250       p->arm_proc = NULL;
251     }
252   GNUNET_CONFIGURATION_destroy (p->cfg);
253   if (p->servicehome != NULL)
254     {
255     GNUNET_DISK_directory_remove (p->servicehome);
256     GNUNET_free(p->servicehome);
257     }
258   GNUNET_free (p);
259 }
260
261 /**
262  * Connect the two given peers and call the callback when both peers report the
263  * inbound connect. Remarks: start_peer's notify_connect callback can be called
264  * before.
265  * @param p1 peer 1
266  * @param p2 peer 2
267  * @param cb the callback to call
268  * @param cb_cls callback cls
269  */
270 void
271 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext * p1,
272                                         struct PeerContext * p2,
273                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
274                                         void * cb_cls)
275 {
276   struct ConnectingContext * cc = GNUNET_malloc (sizeof (struct ConnectingContext));
277
278   GNUNET_assert (p1 != NULL);
279   GNUNET_assert (p2 != NULL);
280
281   cc->p1 = p1;
282   cc->p2 = p2;
283
284   cc->cb = cb;
285   cc->cb_cls = cb_cls;
286
287   cc->th_p1 = GNUNET_TRANSPORT_connect(cc->p1->cfg, NULL,
288                             cc,
289                             NULL,
290                             &notify_connect_internal,
291                             NULL);
292
293   cc->th_p2 = GNUNET_TRANSPORT_connect(cc->p2->cfg, NULL,
294                             cc,
295                             NULL,
296                             &notify_connect_internal,
297                             NULL);
298
299   GNUNET_assert (cc->th_p1 != NULL);
300   GNUNET_assert (cc->th_p2 != NULL);
301
302   GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
303   GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
304
305   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
306 }
307
308
309
310 /* end of transport_testing.h */