Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2016, 2017 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it
6 : 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 : TALER is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not,
17 : see <http://www.gnu.org/licenses/>
18 : */
19 :
20 : /**
21 : * @file bank-lib/taler-fakebank-run.c
22 : * @brief Launch the fakebank, for testing the fakebank itself.
23 : * @author Marcello Stanisci
24 : * @author Christian Grothoff
25 : */
26 : #include "platform.h"
27 : #include "taler_fakebank_lib.h"
28 : #include "taler_mhd_lib.h"
29 :
30 : /**
31 : * Number of threads to use (-n)
32 : */
33 : static unsigned int num_threads;
34 :
35 : /**
36 : * Force connection close after each request (-C)
37 : */
38 : static int connection_close;
39 :
40 : /**
41 : * Global return value.
42 : */
43 : static int ret;
44 :
45 : /**
46 : * Handle for the service.
47 : */
48 : static struct TALER_FAKEBANK_Handle *fb;
49 :
50 : /**
51 : * Keepalive task in multi-threaded mode.
52 : */
53 : static struct GNUNET_SCHEDULER_Task *keepalive;
54 :
55 :
56 : /**
57 : * Stop the process.
58 : *
59 : * @param cls NULL
60 : */
61 : static void
62 1 : do_shutdown (void *cls)
63 : {
64 : (void) cls;
65 1 : TALER_FAKEBANK_stop (fb);
66 1 : fb = NULL;
67 1 : if (NULL != keepalive)
68 : {
69 1 : GNUNET_SCHEDULER_cancel (keepalive);
70 1 : keepalive = NULL;
71 : }
72 1 : }
73 :
74 :
75 : /**
76 : * Task that should never be run.
77 : *
78 : * @param cls NULL
79 : */
80 : static void
81 0 : keepalive_task (void *cls)
82 : {
83 : (void) cls;
84 0 : GNUNET_assert (0);
85 : }
86 :
87 :
88 : /**
89 : * Main function that will be run.
90 : *
91 : * @param cls closure
92 : * @param args remaining command-line arguments
93 : * @param cfgfile name of the configuration file used
94 : * (for saving, can be NULL!)
95 : * @param cfg configuration
96 : */
97 : static void
98 1 : run (void *cls,
99 : char *const *args,
100 : const char *cfgfile,
101 : const struct GNUNET_CONFIGURATION_Handle *cfg)
102 : {
103 1 : unsigned long long port = 8082;
104 1 : unsigned long long ram = 1024 * 128; /* 128 k entries */
105 : char *currency_string;
106 : char *hostname;
107 : char *exchange_url;
108 :
109 : (void) cls;
110 : (void) args;
111 : (void) cfgfile;
112 1 : if (GNUNET_OK !=
113 1 : TALER_config_get_currency (cfg,
114 : ¤cy_string))
115 : {
116 0 : ret = EXIT_NOTCONFIGURED;
117 0 : return;
118 : }
119 1 : if (GNUNET_OK !=
120 1 : GNUNET_CONFIGURATION_get_value_number (cfg,
121 : "bank",
122 : "HTTP_PORT",
123 : &port))
124 : {
125 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
126 : "Listening on default port %llu\n",
127 : port);
128 : }
129 1 : if (GNUNET_OK !=
130 1 : GNUNET_CONFIGURATION_get_value_string (cfg,
131 : "bank",
132 : "SUGGESTED_EXCHANGE",
133 : &exchange_url))
134 : {
135 : /* no suggested exchange */
136 1 : exchange_url = NULL;
137 : }
138 1 : if (GNUNET_OK !=
139 1 : GNUNET_CONFIGURATION_get_value_string (cfg,
140 : "bank",
141 : "HOSTNAME",
142 : &hostname))
143 : {
144 1 : hostname = GNUNET_strdup ("localhost");
145 : }
146 1 : if (GNUNET_OK !=
147 1 : GNUNET_CONFIGURATION_get_value_number (cfg,
148 : "bank",
149 : "RAM_LIMIT",
150 : &ram))
151 : {
152 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
153 : "Maximum transaction history in RAM set to default of %llu\n",
154 : ram);
155 : }
156 : {
157 : enum TALER_MHD_GlobalOptions go;
158 :
159 1 : go = TALER_MHD_GO_NONE;
160 1 : if (0 != connection_close)
161 0 : go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE;
162 1 : TALER_MHD_setup (go);
163 : }
164 2 : fb = TALER_FAKEBANK_start3 (hostname,
165 1 : (uint16_t) port,
166 : exchange_url,
167 : currency_string,
168 : ram,
169 : num_threads);
170 1 : GNUNET_free (hostname);
171 1 : GNUNET_free (exchange_url);
172 1 : GNUNET_free (currency_string);
173 1 : if (NULL == fb)
174 : {
175 0 : GNUNET_break (0);
176 0 : ret = EXIT_FAILURE;
177 0 : return;
178 : }
179 1 : keepalive = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
180 : &keepalive_task,
181 : NULL);
182 1 : GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
183 : NULL);
184 1 : ret = EXIT_SUCCESS;
185 : }
186 :
187 :
188 : /**
189 : * The main function.
190 : *
191 : * @param argc number of arguments from the command line
192 : * @param argv command line arguments
193 : * @return 0 ok, 1 on error
194 : */
195 : int
196 1 : main (int argc,
197 : char *const *argv)
198 : {
199 1 : const struct GNUNET_GETOPT_CommandLineOption options[] = {
200 1 : GNUNET_GETOPT_option_flag ('C',
201 : "connection-close",
202 : "force HTTP connections to be closed after each request",
203 : &connection_close),
204 1 : GNUNET_GETOPT_option_uint ('n',
205 : "num-threads",
206 : "NUM_THREADS",
207 : "size of the thread pool",
208 : &num_threads),
209 : GNUNET_GETOPT_OPTION_END
210 : };
211 : enum GNUNET_GenericReturnValue iret;
212 :
213 1 : iret = GNUNET_PROGRAM_run (argc, argv,
214 : "taler-fakebank-run",
215 : "Runs the fakebank",
216 : options,
217 : &run,
218 : NULL);
219 1 : if (GNUNET_SYSERR == iret)
220 0 : return EXIT_INVALIDARGUMENT;
221 1 : if (GNUNET_NO == iret)
222 0 : return EXIT_SUCCESS;
223 1 : return ret;
224 : }
|