24cf8a829e4f74c5c3e2f2eb61d71d3ade04a554
[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
38 static void
39 exchange_hello_last (void *cls,
40                      const struct GNUNET_MessageHeader *message)
41 {
42   struct ConnectingContext * cc = cls;
43   struct PeerContext *me = cc->p2;
44   struct PeerContext *p1 = cc->p1;
45
46   GNUNET_assert (message != NULL);
47   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
48               "Exchanging HELLO of size %d with peer (%s)!\n",
49               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
50               GNUNET_i2s (&me->id));
51   GNUNET_assert (GNUNET_OK ==
52                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
53                                       message, &me->id));
54   GNUNET_TRANSPORT_offer_hello (p1->th, message, NULL, NULL);
55 }
56
57
58 static void
59 exchange_hello (void *cls,
60                 const struct GNUNET_MessageHeader *message)
61 {
62   struct ConnectingContext * cc = cls;
63   struct PeerContext *me = cc->p1;
64   struct PeerContext *p2 = cc->p2;
65
66   GNUNET_assert (message != NULL);
67   GNUNET_assert (GNUNET_OK ==
68                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
69                                       message, &me->id));
70   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71               "Exchanging HELLO of size %d from peer %s!\n",
72               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
73               GNUNET_i2s (&me->id));
74   GNUNET_TRANSPORT_offer_hello (p2->th, message, NULL, NULL);
75 }
76
77 static void
78 try_connect (void *cls,
79              const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   struct ConnectingContext * cc = cls;
82   struct PeerContext *p1 = cc->p1;
83   struct PeerContext *p2 = cc->p2;
84
85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86               "Asking peers to connect...\n");
87   /* FIXME: 'pX.id' may still be all-zeros here... */
88   GNUNET_TRANSPORT_try_connect (p2->th,
89                                 &p1->id);
90   GNUNET_TRANSPORT_try_connect (p1->th,
91                                 &p2->id);
92   cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
93                                       &try_connect,
94                                       cc);
95 }
96
97 struct PeerContext *
98 GNUNET_TRANSPORT_TESTING_start_peer (const char * cfgname)
99 {
100   struct PeerContext * p = GNUNET_malloc (sizeof (struct PeerContext));
101
102   p->cfg = GNUNET_CONFIGURATION_create ();
103
104   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
105   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
106       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
107   if (NULL != p->servicehome)
108     GNUNET_DISK_directory_remove (p->servicehome);
109   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
110                                         "gnunet-service-arm",
111                                         "-c", cfgname, NULL);
112   return p;
113 }
114
115 void
116 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext * p)
117 {
118   if (NULL != p->arm_proc)
119     {
120       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
121         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
122       GNUNET_OS_process_wait (p->arm_proc);
123       GNUNET_OS_process_close (p->arm_proc);
124       p->arm_proc = NULL;
125     }
126   GNUNET_CONFIGURATION_destroy (p->cfg);
127   if (p->servicehome != NULL)
128     {
129     GNUNET_DISK_directory_remove (p->servicehome);
130     GNUNET_free(p->servicehome);
131     }
132 }
133
134 void
135 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext * p1,
136                                         struct PeerContext * p2,
137                                         GNUNET_TRANSPORT_TESTING_connect_cb * cb,
138                                         void * cls)
139 {
140   struct ConnectingContext * cc = GNUNET_malloc (sizeof (struct ConnectingContext));
141
142   GNUNET_assert (p1 != NULL);
143   GNUNET_assert (p1->th != NULL);
144
145   GNUNET_assert (p2 != NULL);
146   GNUNET_assert (p2->th != NULL);
147
148   cc->p1 = p1;
149   cc->p2 = p2;
150   GNUNET_TRANSPORT_get_hello (p1->th, &exchange_hello, cc);
151   GNUNET_TRANSPORT_get_hello (p2->th, &exchange_hello_last, cc);
152
153   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
154 }
155
156
157
158 /* end of transport_testing.h */