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