25e173b4af88e5ed5f655b99678789a87bebe289
[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 };
36
37 static void
38 notify_connect (void *cls,
39                 const struct GNUNET_PeerIdentity *peer,
40                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
41                 uint32_t ats_count)
42 {
43   struct PeerContext * p = cls;
44   if (p == NULL)
45     return;
46   if (p->nc != NULL)
47     p->nc (p->cb_cls, peer, ats, ats_count);
48 }
49
50 static void
51 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
52 {
53   struct PeerContext * p = cls;
54   if (p == NULL)
55     return;
56   if (p->nd != NULL)
57     p->nd (p->cb_cls, peer);
58 }
59
60 static void
61 notify_receive (void *cls,
62                 const struct GNUNET_PeerIdentity *peer,
63                 const struct GNUNET_MessageHeader *message,
64                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
65                 uint32_t ats_count)
66 {
67   struct PeerContext * p = cls;
68   if (p == NULL)
69     return;
70   if (p->rec != NULL)
71     p->rec (p->cb_cls, peer, message, ats, ats_count);
72 }
73
74
75 static void
76 exchange_hello_last (void *cls,
77                      const struct GNUNET_MessageHeader *message)
78 {
79   struct ConnectingContext * cc = cls;
80   struct PeerContext *me = cc->p2;
81   struct PeerContext *p1 = cc->p1;
82
83   GNUNET_assert (message != NULL);
84   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85               "Exchanging HELLO of size %d with peer (%s)!\n",
86               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
87               GNUNET_i2s (&me->id));
88   GNUNET_assert (GNUNET_OK ==
89                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
90                                       message, &me->id));
91   GNUNET_TRANSPORT_offer_hello (p1->th, message, NULL, NULL);
92   GNUNET_TRANSPORT_get_hello_cancel (me->th, &exchange_hello_last, cc);
93 }
94
95
96 static void
97 exchange_hello (void *cls,
98                 const struct GNUNET_MessageHeader *message)
99 {
100   struct ConnectingContext * cc = cls;
101   struct PeerContext *me = cc->p1;
102   struct PeerContext *p2 = cc->p2;
103
104   GNUNET_assert (message != NULL);
105   GNUNET_assert (GNUNET_OK ==
106                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
107                                       message, &me->id));
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "Exchanging HELLO of size %d from peer %s!\n",
110               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
111               GNUNET_i2s (&me->id));
112   GNUNET_TRANSPORT_offer_hello (p2->th, message, NULL, NULL);
113   GNUNET_TRANSPORT_get_hello_cancel (me->th, &exchange_hello, cc);
114 }
115
116 static void
117 try_connect (void *cls,
118              const struct GNUNET_SCHEDULER_TaskContext *tc)
119 {
120   struct ConnectingContext * cc = cls;
121   struct PeerContext *p1 = cc->p1;
122   struct PeerContext *p2 = cc->p2;
123
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125               "Asking peers to connect...\n");
126   /* FIXME: 'pX.id' may still be all-zeros here... */
127   GNUNET_TRANSPORT_try_connect (p2->th,
128                                 &p1->id);
129   GNUNET_TRANSPORT_try_connect (p1->th,
130                                 &p2->id);
131 //  cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
132 //                                      &try_connect,
133 //                                      cc);
134 }
135
136 struct PeerContext *
137 GNUNET_TRANSPORT_TESTING_start_peer (const char * cfgname,
138     GNUNET_TRANSPORT_ReceiveCallback rec,
139     GNUNET_TRANSPORT_NotifyConnect nc,
140     GNUNET_TRANSPORT_NotifyDisconnect nd,
141     void * cb_cls)
142 {
143   struct PeerContext * p = GNUNET_malloc (sizeof (struct PeerContext));
144
145   p->cfg = GNUNET_CONFIGURATION_create ();
146
147   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
148   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
149       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
150   if (NULL != p->servicehome)
151     GNUNET_DISK_directory_remove (p->servicehome);
152   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
153                                         "gnunet-service-arm",
154                                         "-c", cfgname,
155 #if VERBOSE_PEERS
156                                         "-L", "DEBUG",
157 #else
158                                         "-L", "ERROR",
159 #endif
160                                         NULL);
161   p->nc = nc;
162   p->nd = nd;
163   p->rec = rec;
164   p->cb_cls = cb_cls;
165
166   p->th = GNUNET_TRANSPORT_connect(p->cfg, NULL,
167                             p,
168                             &notify_receive,
169                             &notify_connect,
170                             &notify_disconnect);
171   GNUNET_assert (p->th != NULL);
172   return p;
173 }
174
175 void
176 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext * p)
177 {
178   if (p->th != NULL)
179     GNUNET_TRANSPORT_disconnect(p->th);
180
181   if (NULL != p->arm_proc)
182     {
183       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
184         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
185       GNUNET_OS_process_wait (p->arm_proc);
186       GNUNET_OS_process_close (p->arm_proc);
187       p->arm_proc = NULL;
188     }
189   GNUNET_CONFIGURATION_destroy (p->cfg);
190   if (p->servicehome != NULL)
191     {
192     GNUNET_DISK_directory_remove (p->servicehome);
193     GNUNET_free(p->servicehome);
194     }
195   GNUNET_free (p);
196 }
197
198 void
199 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext * p1,
200                                         struct PeerContext * p2,
201                                         GNUNET_TRANSPORT_TESTING_connect_cb * cb,
202                                         void * cls)
203 {
204   struct ConnectingContext * cc = GNUNET_malloc (sizeof (struct ConnectingContext));
205
206   GNUNET_assert (p1 != NULL);
207   GNUNET_assert (p1->th != NULL);
208
209   GNUNET_assert (p2 != NULL);
210   GNUNET_assert (p2->th != NULL);
211
212   cc->p1 = p1;
213   cc->p2 = p2;
214   GNUNET_TRANSPORT_get_hello (p1->th, &exchange_hello, cc);
215   GNUNET_TRANSPORT_get_hello (p2->th, &exchange_hello_last, cc);
216
217   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
218 }
219
220
221
222 /* end of transport_testing.h */