fix
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist_learning.c
1 /*
2      This file is part of GNUnet
3      (C) 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  * @file hostlist/test_gnunet_daemon_hostlist_learning.c
22  * @brief test for gnunet_daemon_hostslist.c
23  * @author Christian Grothoff, Matthias Wachs
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_transport_service.h"
29
30
31 #define START_ARM GNUNET_YES
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
37
38 static int ok;
39
40 static struct GNUNET_SCHEDULER_Handle *sched;
41
42 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
43     
44 struct PeerContext
45 {
46   struct GNUNET_CONFIGURATION_Handle *cfg;
47   struct GNUNET_TRANSPORT_Handle *th;
48   struct GNUNET_MessageHeader *hello;
49   struct GNUNET_ARM_Handle *arm;
50 #if START_ARM
51   pid_t arm_pid;
52 #endif
53 };
54
55 static struct PeerContext p1;
56
57 static struct PeerContext p2;
58
59
60 static void
61 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
62 {
63   if (p1.th != NULL)
64     {
65       GNUNET_TRANSPORT_disconnect (p1.th);
66       p1.th = NULL;
67     }
68   if (p2.th != NULL)
69     {
70       GNUNET_TRANSPORT_disconnect (p2.th);
71       p2.th = NULL;
72     }
73   GNUNET_SCHEDULER_shutdown (sched);
74 }
75
76 /**
77  * Timeout, give up.
78  */
79 static void
80 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   timeout_task = GNUNET_SCHEDULER_NO_TASK;
83   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
84               "Timeout trying to connect peers, test failed.\n");
85   clean_up (NULL, tc);
86 }
87
88
89 /**
90  * Function called to notify transport users that another
91  * peer connected to us.
92  *
93  * @param cls closure
94  * @param peer the peer that connected
95  * @param latency current latency of the connection
96  * @param distance in overlay hops, as given by transport plugin
97  */
98 static void
99 notify_connect (void *cls,
100                 const struct GNUNET_PeerIdentity * peer,
101                 struct GNUNET_TIME_Relative latency,
102                 unsigned int distance)
103 {
104   if (peer == NULL)
105     return;
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107               "Peers connected, shutting down.\n");
108   /*
109   ok = 0;
110   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
111     {
112       GNUNET_SCHEDULER_cancel (sched,
113                                timeout_task);
114       timeout_task = GNUNET_SCHEDULER_NO_TASK;
115     }
116   GNUNET_SCHEDULER_add_now (sched,
117                             &clean_up, NULL);
118                             */
119 }
120
121
122 static void
123 process_hello (void *cls,
124                const struct GNUNET_MessageHeader *message)
125 {
126   struct PeerContext *p = cls;
127
128   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Received HELLO, starting hostlist service.\n");
131   GNUNET_ARM_start_services (p->cfg, sched, "hostlist", NULL);
132 }
133
134
135 static void
136 setup_peer (struct PeerContext *p, const char *cfgname)
137 {
138   p->cfg = GNUNET_CONFIGURATION_create ();
139 #if START_ARM
140   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
141                                         "gnunet-service-arm",
142 #if VERBOSE
143                                         "-L", "DEBUG",
144 #endif
145                                         "-c", cfgname, NULL);
146 #endif
147   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
148   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
149   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, 
150                                     &notify_connect, NULL);
151   GNUNET_assert (p->th != NULL);
152   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
153 }
154
155
156 static void
157 waitpid_task (void *cls, 
158               const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   struct PeerContext *p = cls;
161
162 #if START_ARM 
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Killing ARM process.\n");
165   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
166     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
167   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
168     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170               "ARM process %u stopped\n", p->arm_pid);
171 #endif
172   GNUNET_CONFIGURATION_destroy (p->cfg);
173 }
174
175
176 static void
177 stop_cb (void *cls, 
178          int success)
179 {
180   struct PeerContext *p = cls;
181
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183               success 
184               ? "ARM stopped core service\n" 
185               : "ARM failed to stop core service\n");
186   GNUNET_ARM_disconnect (p->arm);
187   p->arm = NULL;
188   /* make sure this runs after all other tasks are done */
189   GNUNET_SCHEDULER_add_delayed (sched,
190                                 GNUNET_TIME_UNIT_SECONDS,
191                                 &waitpid_task, p);
192 }
193
194
195 static void
196 stop_arm (struct PeerContext *p)
197 {
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "Asking ARM to stop core service\n");
200   p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL);
201   GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS,
202                            &stop_cb, p);
203 }
204
205
206 /**
207  * Try again to connect to transport service.
208  */
209 static void
210 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
211 {
212   stop_arm (&p1);
213   stop_arm (&p2);
214 }
215
216
217 static void
218 run (void *cls,
219      struct GNUNET_SCHEDULER_Handle *s,
220      char *const *args,
221      const char *cfgfile, 
222      const struct GNUNET_CONFIGURATION_Handle *cfg)
223 {
224   GNUNET_assert (ok == 1);
225   ok++;
226   sched = s;
227
228   timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
229                                                TIMEOUT,
230                                                &timeout_error,
231                                                NULL);
232   GNUNET_SCHEDULER_add_delayed (sched,
233                                 GNUNET_TIME_UNIT_FOREVER_REL,
234                                 &shutdown_task,
235                                 NULL);
236   setup_peer (&p1, "learning_peer1.conf");
237   setup_peer (&p2, "learning_peer2.conf");
238 }
239
240
241 static int
242 check ()
243 {
244   char *const argv[] = { "test_gnunet_daemon_hostlist_learning",
245     "-c", "learning_data.conf",
246 #if VERBOSE
247     "-L", "DEBUG",
248 #endif
249     NULL
250   };
251   struct GNUNET_GETOPT_CommandLineOption options[] = {
252     GNUNET_GETOPT_OPTION_END
253   };
254   ok = 1;
255   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
256                       argv, "test_gnunet_daemon_hostlist_learning",
257                       "nohelp", options, &run, &ok);
258
259   return ok;
260 }
261
262
263 int
264 main (int argc, char *argv[])
265 {
266   
267   int ret;
268   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
269   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
270   GNUNET_log_setup ("test-gnunet-daemon-hostlist_learning",
271 #if VERBOSE
272                     "DEBUG",
273 #else
274                     "WARNING",
275 #endif
276                     NULL);
277   ret = check ();
278   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
279   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
280   return ret; 
281 }
282
283 /* end of test_gnunet_daemon_hostlist.c */