plugin datastore mysql
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist_reconnect.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file hostlist/test_gnunet_daemon_hostlist_reconnect.c
22  * @brief test for gnunet_daemon_hostslist.c; tries to re-start the peers
23  *        and connect a second time
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_transport_service.h"
30
31 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
35
36 static int ok;
37
38 static struct GNUNET_SCHEDULER_Task * timeout_task;
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43   struct GNUNET_TRANSPORT_Handle *th;
44   struct GNUNET_MessageHeader *hello;
45   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
46   struct GNUNET_OS_Process *arm_proc;
47 };
48
49 static struct PeerContext p1;
50
51 static struct PeerContext p2;
52
53
54 /**
55  * Timeout, give up.
56  */
57 static void
58 timeout_error (void *cls)
59 {
60   timeout_task = NULL;
61   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
62               "Timeout trying to connect peers, test failed.\n");
63   GNUNET_SCHEDULER_shutdown ();
64 }
65
66
67 /**
68  * Function called to notify transport users that another
69  * peer connected to us.
70  *
71  * @param cls closure
72  * @param peer the peer that connected
73  * @param latency current latency of the connection
74  * @param distance in overlay hops, as given by transport plugin
75  */
76 static void
77 notify_connect (void *cls,
78                 const struct GNUNET_PeerIdentity *peer)
79 {
80   if (peer == NULL)
81     return;
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83               "Peers connected, shutting down.\n");
84   ok = 0;
85   GNUNET_SCHEDULER_shutdown ();
86 }
87
88
89 static void
90 process_hello (void *cls,
91                const struct GNUNET_MessageHeader *message)
92 {
93   struct PeerContext *p = cls;
94
95   GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
96   p->ghh = NULL;
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98               "Received HELLO, starting hostlist service.\n");
99 }
100
101
102 static void
103 setup_peer (struct PeerContext *p, const char *cfgname)
104 {
105   char *binary;
106
107   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
108   p->cfg = GNUNET_CONFIGURATION_create ();
109   p->arm_proc =
110     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
111                              NULL, NULL, NULL,
112                              binary,
113                              "gnunet-service-arm",
114                              "-c", cfgname, NULL);
115   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
116   p->th =
117       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
118   GNUNET_assert (p->th != NULL);
119   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
120   GNUNET_free (binary);
121 }
122
123
124 static void
125 waitpid_task (void *cls)
126 {
127   struct PeerContext *p = cls;
128
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
130   if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
131     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
132   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
133     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
135               GNUNET_OS_process_get_pid (p->arm_proc));
136   GNUNET_OS_process_destroy (p->arm_proc);
137   p->arm_proc = NULL;
138   GNUNET_CONFIGURATION_destroy (p->cfg);
139 }
140
141
142 static void
143 stop_arm (struct PeerContext *p)
144 {
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
146   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
147                                 &waitpid_task, p);
148 }
149
150
151 /**
152  * Try again to connect to transport service.
153  */
154 static void
155 shutdown_task (void *cls)
156 {
157   if (NULL != timeout_task)
158   {
159     GNUNET_SCHEDULER_cancel (timeout_task);
160     timeout_task = NULL;
161   }
162   if (NULL != p1.ghh)
163   {
164     GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
165     p1.ghh = NULL;
166   }
167   if (p1.th != NULL)
168   {
169     GNUNET_TRANSPORT_disconnect (p1.th);
170     p1.th = NULL;
171   }
172   if (NULL != p2.ghh)
173   {
174     GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
175     p2.ghh = NULL;
176   }
177   if (p2.th != NULL)
178   {
179     GNUNET_TRANSPORT_disconnect (p2.th);
180     p2.th = NULL;
181   }
182   stop_arm (&p1);
183   stop_arm (&p2);
184 }
185
186
187 static void
188 run (void *cls, char *const *args, const char *cfgfile,
189      const struct GNUNET_CONFIGURATION_Handle *cfg)
190 {
191   GNUNET_assert (ok == 1);
192   ok++;
193   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
194                                                &timeout_error, NULL);
195   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
196                                  NULL);
197   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
198   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
199 }
200
201
202 int
203 main (int argcx, char *argvx[])
204 {
205   static char *const argv[] = {
206     "test-gnunet-daemon-hostlist",
207     "-c", "test_gnunet_daemon_hostlist_data.conf",
208     NULL
209   };
210   static struct GNUNET_GETOPT_CommandLineOption options[] = {
211     GNUNET_GETOPT_OPTION_END
212   };
213
214   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
215   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
216   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
217   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
218                     "WARNING",
219                     NULL);
220   ok = 1;
221   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
222                       "test-gnunet-daemon-hostlist", "nohelp", options, &run,
223                       &ok);
224   if (0 == ok)
225   {
226     FPRINTF (stderr, "%s",  ".");
227     /* now do it again */
228     ok = 1;
229     GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
230                         "test-gnunet-daemon-hostlist", "nohelp", options, &run,
231                         &ok);
232     FPRINTF (stderr, "%s",  ".\n");
233   }
234   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
235   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
236   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
237   return ok;
238 }
239
240 /* end of test_gnunet_daemon_hostlist_reconnect.c */