Merge remote-tracking branch 'origin/master'
[oweals/gnunet.git] / src / auction / gnunet-auction-create.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19    */
20
21 /**
22  * @file auction/gnunet-auction-create.c
23  * @brief tool to create a new auction
24  * @author Markus Teich
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_json_lib.h"
29 /* #include "gnunet_auction_service.h" */
30
31 #define FIRST_PRICE 0
32 #define OUTCOME_PRIVATE 0
33 #define OUTCOME_PUBLIC 1
34
35 static int ret; /** Final status code. */
36 static char *fndesc; /** filename of the item description */
37 static char *fnprices; /** filename of the price map */
38 static struct GNUNET_TIME_Relative dround; /** max round duration */
39 static struct GNUNET_TIME_Relative dstart; /** time until auction starts */
40 static unsigned int m = FIRST_PRICE; /** auction parameter m */
41 static int outcome = OUTCOME_PRIVATE; /** outcome */
42 static int interactive; /** keep running in foreground */
43
44
45 /**
46  * Main function that will be run by the scheduler.
47  *
48  * @param cls closure
49  * @param args remaining command-line arguments
50  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
51  * @param cfg configuration
52  */
53 static void
54 run (void *cls,
55          char *const *args,
56          const char *cfgfile,
57          const struct GNUNET_CONFIGURATION_Handle *cfg)
58 {
59         /* cmdline parsing */
60         if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dstart.rel_value_us)
61         {
62                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
63                             "required argument --regtime missing or invalid (zero)\n");
64                 goto fail;
65         }
66         if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dround.rel_value_us)
67         {
68                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
69                             "required argument --roundtime missing or invalid (zero)\n");
70                 goto fail;
71         }
72         if (!fndesc)
73         {
74                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
75                             "required argument --description missing\n");
76                 goto fail;
77         }
78         if (!fnprices)
79         {
80                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
81                             "required argument --pricemap missing\n");
82                 goto fail;
83         }
84
85 fail:
86         ret = 1;
87         return;
88 }
89
90
91 /**
92  * The main function.
93  *
94  * @param argc number of arguments from the command line
95  * @param argv command line arguments
96  * @return 0 ok, 1 on error
97  */
98 int
99 main (int argc, char *const *argv)
100 {
101         static const struct GNUNET_GETOPT_CommandLineOption options[] = {
102                 {'d', "description", "FILE",
103                         gettext_noop ("description of the item to be sold"),
104                         1, &GNUNET_GETOPT_set_filename, &fndesc},
105                 {'p', "pricemap", "FILE",
106                         gettext_noop ("mapping of possible prices"),
107                         1, &GNUNET_GETOPT_set_filename, &fnprices},
108                 {'r', "roundtime", "DURATION",
109                         gettext_noop ("max duration per round"),
110                         1, &GNUNET_GETOPT_set_relative_time, &dround},
111                 {'s', "regtime", "DURATION",
112                         gettext_noop ("duration until auction starts"),
113                         1, &GNUNET_GETOPT_set_relative_time, &dstart},
114                 {'m', "m", "NUMBER",
115                         gettext_noop ("number of items to sell\n"
116                                       "0 for first price auction\n"
117                                       ">0 for vickrey/M+1st price auction"),
118                         1, &GNUNET_GETOPT_set_uint, &m},
119                 {'u', "public", NULL,
120                         gettext_noop ("public auction outcome"),
121                         0, &GNUNET_GETOPT_set_one, &outcome},
122                 {'i', "interactive", NULL,
123                         gettext_noop ("keep running in foreground until auction completes"),
124                         0, &GNUNET_GETOPT_set_one, &interactive},
125                 GNUNET_GETOPT_OPTION_END
126         };
127         if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
128                 return 2;
129
130         ret = (GNUNET_OK ==
131                    GNUNET_PROGRAM_run (argc, argv,
132                                                            "gnunet-auction-create",
133                                                            gettext_noop ("create a new auction and "
134                                                                          "start listening for bidders"),
135                                                            options,
136                                                            &run,
137                                                            NULL)) ? ret : 1;
138         GNUNET_free ((void*) argv);
139         return ret;
140 }