2 This file is part of GNUnet
3 (C) 2012 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.
22 * @file testbed/gnunet-testbed-helper.c
23 * @brief Helper binary that is started from a remote controller to start
24 * gnunet-service-testbed. This binary also receives configuration
25 * from the remove controller which is put in a temporary location
26 * with ports and paths fixed so that gnunet-service-testbed runs
27 * without any hurdels. This binary also kills the testbed service
28 * should the connection from the remote controller is dropped
29 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
34 #include "gnunet_util_lib.h"
35 #include "gnunet_testing_lib-new.h"
37 #include "testbed_helper.h"
41 * Generic debug logging shortcut
43 #define LOG_DEBUG(...) \
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48 * Handle to the testing system
50 static struct GNUNET_TESTING_System *test_system;
53 * Our message stream tokenizer
55 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
58 * Disk handle from stdin
60 static struct GNUNET_DISK_FileHandle *stdin_fd;
63 * Message receive buffer
68 * The size of the above buffer
70 static size_t buf_size;
73 * Task identifier for the read task
75 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
78 * Task identifier for the shutdown task
80 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
83 * Task to shutting down nicely
86 * @return tc the task context
89 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
92 GNUNET_SCHEDULER_cancel (read_task_id);
93 (void) GNUNET_DISK_file_close (stdin_fd);
94 GNUNET_free_non_null (buf);
95 GNUNET_SERVER_mst_destroy (tokenizer);
96 if (NULL != test_system)
97 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
102 * Functions with this signature are called whenever a
103 * complete message is received by the tokenizer.
105 * Do not call GNUNET_SERVER_mst_destroy in callback
108 * @param client identification of the client
109 * @param message the actual message
111 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
114 tokenizer_cb (void *cls, void *client,
115 const struct GNUNET_MessageHeader *message)
123 * Task to read from stdin
126 * @return tc the task context
129 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 static int ignore_reading = GNUNET_NO;
135 read_task_id = GNUNET_SCHEDULER_NO_TASK;
136 if (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)
138 sread = GNUNET_DISK_file_read (stdin_fd, buf, buf_size);
139 if (GNUNET_SYSERR == sread)
141 GNUNET_break (0); /* FIXME: stdin closed - kill child */
142 GNUNET_SCHEDULER_cancel (shutdown_task_id);
143 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
146 LOG_DEBUG ("Read %u bytes\n", sread);
147 read_task_id = /* No timeout while reading */
148 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
149 stdin_fd, &read_task, NULL);
150 if (GNUNET_YES == ignore_reading)
152 ret = GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
153 GNUNET_NO, GNUNET_NO);
154 GNUNET_assert (GNUNET_SYSERR != ret);
155 if (GNUNET_NO == ret)
157 LOG_DEBUG ("We only listen for 1 message -- ignoring others\n");
158 ignore_reading = GNUNET_YES;
164 * Main function that will be run.
167 * @param args remaining command-line arguments
168 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
169 * @param cfg configuration
172 run (void *cls, char *const *args, const char *cfgfile,
173 const struct GNUNET_CONFIGURATION_Handle * cfg)
175 LOG_DEBUG ("Starting testbed helper...\n");
176 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
177 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
178 buf_size = sizeof (struct GNUNET_TESTBED_HelperInit) + 8 * 1024;
179 buf = GNUNET_malloc (buf_size);
180 read_task_id = /* No timeout while reading */
181 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
182 stdin_fd, &read_task, NULL);
184 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
185 &shutdown_task, NULL);
192 * @param argc the number of command line arguments
193 * @param argv command line arg array
194 * @return return code
196 int main (int argc, char **argv)
198 struct GNUNET_GETOPT_CommandLineOption options[] = {
199 GNUNET_GETOPT_OPTION_END
203 GNUNET_PROGRAM_run ( argc, argv, "gnunet-testbed-helper",
204 "Helper for starting gnunet-service-testbed",
205 options, &run, NULL))