exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-fakebank-run.c (6853B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2016-2022 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 "taler/platform.h"
     27 #include "taler/taler_fakebank_lib.h"
     28 #include "taler/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  * Amount to credit an account with on /register.
     57  */
     58 static struct TALER_Amount signup_bonus;
     59 
     60 /**
     61  * Stop the process.
     62  *
     63  * @param cls NULL
     64  */
     65 static void
     66 do_shutdown (void *cls)
     67 {
     68   (void) cls;
     69   TALER_FAKEBANK_stop (fb);
     70   fb = NULL;
     71   if (NULL != keepalive)
     72   {
     73     GNUNET_SCHEDULER_cancel (keepalive);
     74     keepalive = NULL;
     75   }
     76 }
     77 
     78 
     79 /**
     80  * Task that should never be run.
     81  *
     82  * @param cls NULL
     83  */
     84 static void
     85 keepalive_task (void *cls)
     86 {
     87   (void) cls;
     88   GNUNET_assert (0);
     89 }
     90 
     91 
     92 /**
     93  * Main function that will be run.
     94  *
     95  * @param cls closure
     96  * @param args remaining command-line arguments
     97  * @param cfgfile name of the configuration file used
     98  *        (for saving, can be NULL!)
     99  * @param cfg configuration
    100  */
    101 static void
    102 run (void *cls,
    103      char *const *args,
    104      const char *cfgfile,
    105      const struct GNUNET_CONFIGURATION_Handle *cfg)
    106 {
    107   unsigned long long port = 8082;
    108   unsigned long long ram = 1024 * 128; /* 128 k entries */
    109   char *currency_string;
    110   char *hostname;
    111   char *exchange_url;
    112 
    113   (void) cls;
    114   (void) args;
    115   (void) cfgfile;
    116   if (GNUNET_OK !=
    117       TALER_config_get_currency (cfg,
    118                                  "exchange",
    119                                  &currency_string))
    120   {
    121     ret = EXIT_NOTCONFIGURED;
    122     return;
    123   }
    124   if (GNUNET_OK !=
    125       GNUNET_CONFIGURATION_get_value_number (cfg,
    126                                              "bank",
    127                                              "HTTP_PORT",
    128                                              &port))
    129   {
    130     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    131                 "Listening on default port %llu\n",
    132                 port);
    133   }
    134   if (GNUNET_OK !=
    135       GNUNET_CONFIGURATION_get_value_string (cfg,
    136                                              "bank",
    137                                              "SUGGESTED_EXCHANGE",
    138                                              &exchange_url))
    139   {
    140     /* no suggested exchange */
    141     exchange_url = NULL;
    142   }
    143   if (GNUNET_OK !=
    144       GNUNET_CONFIGURATION_get_value_string (cfg,
    145                                              "bank",
    146                                              "HOSTNAME",
    147                                              &hostname))
    148   {
    149     hostname = GNUNET_strdup ("localhost");
    150   }
    151   if (GNUNET_OK !=
    152       GNUNET_CONFIGURATION_get_value_number (cfg,
    153                                              "bank",
    154                                              "RAM_LIMIT",
    155                                              &ram))
    156   {
    157     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    158                 "Maximum transaction history in RAM set to default of %llu\n",
    159                 ram);
    160   }
    161   {
    162     enum TALER_MHD_GlobalOptions go;
    163 
    164     go = TALER_MHD_GO_NONE;
    165     if (0 != connection_close)
    166       go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE;
    167     TALER_MHD_setup (go);
    168   }
    169   if (GNUNET_OK !=
    170       TALER_amount_is_valid (&signup_bonus))
    171   {
    172     GNUNET_assert (GNUNET_OK ==
    173                    TALER_amount_set_zero (currency_string,
    174                                           &signup_bonus));
    175   }
    176   if (0 != strcmp (currency_string,
    177                    signup_bonus.currency))
    178   {
    179     fprintf (stderr,
    180              "Signup bonus and main currency do not match\n");
    181     ret = EXIT_INVALIDARGUMENT;
    182     return;
    183   }
    184   fb = TALER_FAKEBANK_start3 (hostname,
    185                               (uint16_t) port,
    186                               exchange_url,
    187                               currency_string,
    188                               ram,
    189                               num_threads,
    190                               &signup_bonus);
    191   GNUNET_free (hostname);
    192   GNUNET_free (exchange_url);
    193   GNUNET_free (currency_string);
    194   if (NULL == fb)
    195   {
    196     GNUNET_break (0);
    197     ret = EXIT_FAILURE;
    198     return;
    199   }
    200   keepalive = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
    201                                             &keepalive_task,
    202                                             NULL);
    203   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
    204                                  NULL);
    205   ret = EXIT_SUCCESS;
    206 }
    207 
    208 
    209 /**
    210  * The main function.
    211  *
    212  * @param argc number of arguments from the command line
    213  * @param argv command line arguments
    214  * @return 0 ok, 1 on error
    215  */
    216 int
    217 main (int argc,
    218       char *const *argv)
    219 {
    220   const struct GNUNET_GETOPT_CommandLineOption options[] = {
    221     GNUNET_GETOPT_option_flag ('C',
    222                                "connection-close",
    223                                "force HTTP connections to be closed after each request",
    224                                &connection_close),
    225     GNUNET_GETOPT_option_uint ('n',
    226                                "num-threads",
    227                                "NUM_THREADS",
    228                                "size of the thread pool",
    229                                &num_threads),
    230     TALER_getopt_get_amount ('s',
    231                              "signup-bonus",
    232                              "AMOUNT",
    233                              "amount to credit newly registered account (created out of thin air)",
    234                              &signup_bonus),
    235     GNUNET_GETOPT_OPTION_END
    236   };
    237   enum GNUNET_GenericReturnValue iret;
    238 
    239   iret = GNUNET_PROGRAM_run (TALER_FAKEBANK_project_data (),
    240                              argc, argv,
    241                              "taler-fakebank-run",
    242                              "Runs the fakebank",
    243                              options,
    244                              &run,
    245                              NULL);
    246   if (GNUNET_SYSERR == iret)
    247     return EXIT_INVALIDARGUMENT;
    248   if (GNUNET_NO == iret)
    249     return EXIT_SUCCESS;
    250   return ret;
    251 }