testbed helper
[oweals/gnunet.git] / src / testbed / gnunet-testbed-helper.c
1 /*
2       This file is part of GNUnet
3       (C) 2012 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 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., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
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> 
30  */
31
32
33 #include "platform.h"
34 #include "gnunet_util_lib.h"
35 #include "gnunet_testing_lib-new.h"
36
37 #include "testbed_helper.h"
38
39
40 /**
41  * Generic debug logging shortcut
42  */
43 #define LOG_DEBUG(...)                                  \
44   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
45
46
47 /**
48  * Handle to the testing system
49  */
50 static struct GNUNET_TESTING_System *test_system;
51
52 /**
53  * Our message stream tokenizer
54  */
55 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
56
57 /**
58  * Disk handle from stdin
59  */
60 static struct GNUNET_DISK_FileHandle *stdin_fd;
61
62 /**
63  * Message receive buffer
64  */
65 static void *buf;
66
67 /**
68  * The size of the above buffer
69  */
70 static size_t buf_size;
71
72 /**
73  * Task identifier for the read task
74  */
75 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
76
77 /**
78  * Task identifier for the shutdown task
79  */
80 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
81
82 /**
83  * Task to shutting down nicely
84  *
85  * @param cls NULL
86  * @return tc the task context
87  */
88 static void
89 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
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);
98 }
99
100
101 /**
102  * Functions with this signature are called whenever a
103  * complete message is received by the tokenizer.
104  *
105  * Do not call GNUNET_SERVER_mst_destroy in callback
106  *
107  * @param cls closure
108  * @param client identification of the client
109  * @param message the actual message
110  *
111  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
112  */
113 static int 
114 tokenizer_cb (void *cls, void *client,
115               const struct GNUNET_MessageHeader *message)
116 {
117   GNUNET_break (0);
118   return GNUNET_OK;
119 }
120
121
122 /**
123  * Task to read from stdin
124  *
125  * @param cls NULL
126  * @return tc the task context
127  */
128 static void
129 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 {
131   ssize_t sread;
132   static int ignore_reading = GNUNET_NO;
133   int ret;
134
135   read_task_id = GNUNET_SCHEDULER_NO_TASK;
136   if (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)
137     return;  
138   sread = GNUNET_DISK_file_read (stdin_fd, buf, buf_size);
139   if (GNUNET_SYSERR == sread)
140   {
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);
144     return;
145   }
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)
151     return;
152   ret = GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
153                                    GNUNET_YES, GNUNET_YES);
154   GNUNET_assert (GNUNET_SYSERR != ret);
155   if (GNUNET_NO == ret)
156   {
157     LOG_DEBUG ("We only listen for 1 message -- ignoring others\n");
158     ignore_reading = GNUNET_YES;
159   }
160 }
161
162
163 /**
164  * Main function that will be run.
165  *
166  * @param cls closure
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
170  */
171 static void 
172 run (void *cls, char *const *args, const char *cfgfile,
173      const struct GNUNET_CONFIGURATION_Handle * cfg)
174 {
175   tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
176   stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
177   buf_size = sizeof (struct GNUNET_TESTBED_HelperInit) + 8 * 1024;
178   buf = GNUNET_malloc (buf_size);
179   read_task_id =                /* No timeout while reading */
180     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
181                                     stdin_fd, &read_task, NULL);
182   shutdown_task_id = 
183     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
184                                   &shutdown_task, NULL);
185 }
186
187
188 /**
189  * Main function
190  *
191  * @param argc the number of command line arguments
192  * @param argv command line arg array
193  * @return return code
194  */
195 int main (int argc, char **argv)
196 {
197    struct GNUNET_GETOPT_CommandLineOption options[] = {
198     GNUNET_GETOPT_OPTION_END
199   };
200
201    if (GNUNET_OK != 
202        GNUNET_PROGRAM_run ( argc, argv, "gnunet-testbed-helper",
203                             "Helper for starting gnunet-service-testbed",
204                             options, &run, NULL))
205      return 1;
206   else return 0;
207 }