-porting gnunet-testing command-line tool to new testing library
[oweals/gnunet.git] / src / testing / gnunet-testing-run-service.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 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 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 /**
23  * @file testing/gnunet-testing-run-service.c
24  * @brief tool to start a service for testing
25  * @author Florian Dold
26  *
27  * Start a peer, running only the service specified on the command line.
28  * Outputs the path to the temporary configuration file to stdout.
29  *
30  * The peer will run until this program is killed,
31  * or stdin is closed. When reading the character 'r' from stdin, the running service is
32  * restarted with the same configuration.
33  *
34  * This executable is intended to be used by gnunet-java, in order to reliably
35  * start and stop services for test cases.
36  */
37 #include "platform.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet_testing_lib-new.h"
40
41 #define LOG(kind,...)                                           \
42   GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__)
43
44
45 /**
46  * FIXME
47  */
48 static struct GNUNET_DISK_FileHandle *fh;
49
50 /**
51  * FIXME
52  */
53 static char *tmpfilename;
54
55 /**
56  * FIXME
57  */
58 static GNUNET_SCHEDULER_TaskIdentifier tid;
59
60 /**
61  * FIXME
62  */
63 static struct GNUNET_TESTING_Peer *my_peer;
64
65
66
67
68 /**
69  * Cleanup called by signal handlers and when stdin is closed.
70  * Removes the temporary file.
71  *
72  * @param cls unused
73  * @param tc scheduler context 
74  */
75 static void
76 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 {
78   if (NULL != tmpfilename)
79   {
80     if (0 != UNLINK (tmpfilename))
81       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", tmpfilename);
82   }
83   if (GNUNET_SCHEDULER_NO_TASK != tid)
84   {
85     GNUNET_SCHEDULER_cancel (tid);
86     tid = GNUNET_SCHEDULER_NO_TASK;
87   }
88   if (NULL != fh)
89   {
90     GNUNET_DISK_file_close (fh);
91     fh = NULL;
92   }
93 }
94
95
96 /**
97  * Called whenever we can read stdin non-blocking 
98  *
99  * @param cls unused
100  * @param tc scheduler context 
101  */
102 static void
103 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   int c;
106
107   tid = GNUNET_SCHEDULER_NO_TASK;
108   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
109     return;
110   GNUNET_assert (0 != (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason));
111   c = getchar ();
112   switch (c)
113   {
114   case EOF:
115   case 'q':
116     GNUNET_SCHEDULER_shutdown ();
117     return;
118   case 'r':
119     GNUNET_TESTING_peer_stop (my_peer); 
120     GNUNET_TESTING_peer_start (my_peer); 
121     printf ("restarted\n");
122     fflush (stdout);
123     break;
124   case '\n':
125   case '\r':
126     /* ignore whitespace */
127     break;
128   default:
129     fprintf (stderr, _("Unknown command, use 'q' to quit or 'r' to restart peer\n"));
130     break;
131   }
132   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh, &stdin_cb, NULL);    
133 }
134
135
136 /**
137  * Main function called by the testing library.
138  * Executed inside a running scheduler.
139  *
140  * @param cls unused
141  * @param cfg configuration of the peer that was started
142  * @param peer handle to the peer
143  */
144 static void
145 testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
146               const struct GNUNET_TESTING_Peer *peer)
147 {
148   my_peer = (struct GNUNET_TESTING_Peer *) peer;
149   if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-testing")))
150   {
151     GNUNET_break (0);
152     GNUNET_SCHEDULER_shutdown ();
153     return;
154   }
155   if (GNUNET_SYSERR == 
156       GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *) cfg, tmpfilename))
157   {
158     GNUNET_break (0);
159     return;
160   }
161   printf("%s\n", tmpfilename);
162   fflush(stdout);
163   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, NULL);
164   fh = GNUNET_DISK_get_handle_from_native (stdin);
165   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh, &stdin_cb, NULL);
166 }
167
168
169 /**
170  * The main function.
171  *
172  * @param argc number of arguments from the command line
173  * @param argv command line arguments
174  * @return 0 ok, 1 on error
175  */
176 int
177 main (int argc, char *const *argv)
178 {
179   static char *cfg_name;
180   static char *srv_name;
181   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
182     {'c', "config", "FILENAME",
183      gettext_noop ("name of the configuration file to use"), 1,
184      &GNUNET_GETOPT_set_string, &cfg_name},
185     {'s', "service", "SERVICE",
186      gettext_noop ("name of the service to run"), 1,
187      &GNUNET_GETOPT_set_string, &srv_name},
188     GNUNET_GETOPT_OPTION_HELP ("tool to start a service for testing"),
189     GNUNET_GETOPT_OPTION_END
190   };
191   int ret;
192
193   if (GNUNET_SYSERR ==
194       GNUNET_GETOPT_run("gnunet-testing-run-service", options, argc, argv))
195     return 1;
196   ret = GNUNET_TESTING_service_run_restartable ("gnunet_service_test", srv_name,
197                                                 cfg_name, &testing_main, NULL);
198   if (0 != ret)
199   {
200     printf ("error\n");
201   }
202   else 
203   {
204     printf ("bye\n");
205   }
206   return ret;
207 }
208