2 This file is part of GNUnet
3 Copyright (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_transport_service.h"
32 * How long until we give up on transmitting the message?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
38 static struct GNUNET_SCHEDULER_Task * timeout_task;
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;
49 static struct PeerContext p1;
51 static struct PeerContext p2;
55 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
59 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
64 GNUNET_TRANSPORT_disconnect (p1.th);
69 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
74 GNUNET_TRANSPORT_disconnect (p2.th);
77 GNUNET_SCHEDULER_shutdown ();
84 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
88 "Timeout trying to connect peers, test failed.\n");
94 * Function called to notify transport users that another
95 * peer connected to us.
98 * @param peer the peer that connected
99 * @param latency current latency of the connection
100 * @param distance in overlay hops, as given by transport plugin
103 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
109 if (timeout_task != NULL)
111 GNUNET_SCHEDULER_cancel (timeout_task);
114 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
119 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
121 struct PeerContext *p = cls;
123 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Received HELLO, starting hostlist service.\n");
131 setup_peer (struct PeerContext *p, const char *cfgname)
135 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
136 p->cfg = GNUNET_CONFIGURATION_create ();
138 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
141 "gnunet-service-arm",
142 "-c", cfgname, NULL);
143 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
145 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, ¬ify_connect, NULL);
146 GNUNET_assert (p->th != NULL);
147 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
148 GNUNET_free (binary);
153 waitpid_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 struct PeerContext *p = cls;
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
158 if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
159 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
160 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
161 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
163 GNUNET_OS_process_get_pid (p->arm_proc));
164 GNUNET_OS_process_destroy (p->arm_proc);
166 GNUNET_CONFIGURATION_destroy (p->cfg);
171 stop_arm (struct PeerContext *p)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
174 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
179 * Try again to connect to transport service.
182 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 run (void *cls, char *const *args, const char *cfgfile,
191 const struct GNUNET_CONFIGURATION_Handle *cfg)
193 GNUNET_assert (ok == 1);
195 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
198 setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
199 setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
204 main (int argcx, char *argvx[])
206 static char *const argv[] = {
207 "test-gnunet-daemon-hostlist",
208 "-c", "test_gnunet_daemon_hostlist_data.conf",
211 static struct GNUNET_GETOPT_CommandLineOption options[] = {
212 GNUNET_GETOPT_OPTION_END
215 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
216 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
217 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
218 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
222 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
223 "test-gnunet-daemon-hostlist", "nohelp", options, &run,
227 FPRINTF (stderr, "%s", ".");
228 /* now do it again */
230 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
231 "test-gnunet-daemon-hostlist", "nohelp", options, &run,
233 FPRINTF (stderr, "%s", ".\n");
235 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
236 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
237 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
241 /* end of test_gnunet_daemon_hostlist_reconnect.c */