paragraph for gnunet devs that don't know how to use the web
[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 it
6    under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License,
8    or (at your 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    Affero General Public License for more details.
14   
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17    */
18
19 /**
20  * @file auction/gnunet-auction-create.c
21  * @brief tool to create a new auction
22  * @author Markus Teich
23  */
24 #include "platform.h"
25
26 #include <float.h>
27
28 #include "gnunet_util_lib.h"
29 #include "gnunet_json_lib.h"
30 /* #include "gnunet_auction_service.h" */
31
32 #define FIRST_PRICE 0
33 #define OUTCOME_PRIVATE 0
34 #define OUTCOME_PUBLIC 1
35
36 static int ret; /** Final status code. */
37 static char *fndesc; /** filename of the item description */
38 static char *fnprices; /** filename of the price map */
39 static struct GNUNET_TIME_Relative dround; /** max round duration */
40 static struct GNUNET_TIME_Relative dstart; /** time until auction starts */
41 static unsigned int m = FIRST_PRICE; /** auction parameter m */
42 static int outcome = OUTCOME_PRIVATE; /** outcome */
43 static int interactive; /** keep running in foreground */
44
45
46 /**
47  * Main function that will be run by the scheduler.
48  *
49  * @param cls closure
50  * @param args remaining command-line arguments
51  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
52  * @param cfg configuration
53  */
54 static void
55 run (void *cls,
56          char *const *args,
57          const char *cfgfile,
58          const struct GNUNET_CONFIGURATION_Handle *cfg)
59 {
60         unsigned int i;
61         double cur, prev = DBL_MAX;
62         json_t *pmap;
63         json_t *parray;
64         json_t *pnode;
65         json_error_t jerr;
66
67         /* cmdline parsing */
68         if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dstart.rel_value_us)
69         {
70                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
71                             "required argument --regtime missing or invalid (zero)\n");
72                 goto fail;
73         }
74         if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dround.rel_value_us)
75         {
76                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
77                             "required argument --roundtime missing or invalid (zero)\n");
78                 goto fail;
79         }
80         if (!fndesc)
81         {
82                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83                             "required argument --description missing\n");
84                 goto fail;
85         }
86         if (!fnprices)
87         {
88                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89                             "required argument --pricemap missing\n");
90                 goto fail;
91         }
92
93         /* parse and check pricemap validity */
94         if (!(pmap = json_load_file (fnprices, JSON_DECODE_INT_AS_REAL, &jerr)))
95         {
96                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97                             "parsing pricemap json at %d:%d: %s\n",
98                             jerr.line, jerr.column, jerr.text);
99                 goto fail;
100         }
101         if (-1 == json_unpack_ex (pmap, &jerr, JSON_VALIDATE_ONLY,
102                                   "{s:s, s:[]}", "currency", "prices"))
103         {
104                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105                             "validating pricemap: %s\n", jerr.text);
106                 goto fail;
107         }
108         if (!(parray = json_object_get (pmap, "prices")) || !json_is_array (parray))
109         {
110                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                             "could not get `prices` array node from pricemap\n");
112                 goto fail;
113         }
114         if (0 == json_array_size (parray))
115         {
116                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "empty pricemap array\n");
117                 goto fail;
118         }
119         json_array_foreach (parray, i, pnode)
120         {
121                 if (-1 == json_unpack_ex (pnode, &jerr, 0, "F", &cur))
122                 {
123                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124                                     "validating pricearray index %d: %s\n", i, jerr.text);
125                         goto fail;
126                 }
127                 if (prev <= cur)
128                 {
129                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130                                     "validating pricearray index %d: "
131                                     "prices must be strictly monotonically decreasing\n",
132                                     i);
133                         goto fail;
134                 }
135                 prev = cur;
136         }
137
138         return;
139
140 fail:
141         ret = 1;
142         return;
143 }
144
145
146 /**
147  * The main function.
148  *
149  * @param argc number of arguments from the command line
150  * @param argv command line arguments
151  * @return 0 ok, 1 on error
152  */
153 int
154 main (int argc, char *const *argv)
155 {
156         struct GNUNET_GETOPT_CommandLineOption options[] = {
157
158                 GNUNET_GETOPT_option_filename ('d',
159                                                "description",
160                                                "FILE",
161                                                gettext_noop ("description of the item to be sold"),
162                                                &fndesc),
163
164                 GNUNET_GETOPT_option_filename ('p',
165                                                "pricemap",
166                                                "FILE",
167                                                gettext_noop ("mapping of possible prices"),
168                                                &fnprices),
169
170                 GNUNET_GETOPT_option_relative_time ('r',
171                                                         "roundtime",
172                                                         "DURATION",
173                                                         gettext_noop ("max duration per round"),
174                                                         &dround),
175
176                 GNUNET_GETOPT_option_relative_time ('s',
177                                                         "regtime",
178                                                         "DURATION",
179                                                         gettext_noop ("duration until auction starts"),
180                                                         &dstart),
181                 GNUNET_GETOPT_option_uint ('m',
182                                                "m",
183                                                "NUMBER",
184                                                gettext_noop ("number of items to sell\n"
185                                                              "0 for first price auction\n"
186                                                              ">0 for vickrey/M+1st price auction"),
187                                                &m), 
188
189                 GNUNET_GETOPT_option_flag ('u',
190                                               "public",
191                                               gettext_noop ("public auction outcome"),
192                                               &outcome),
193
194                 GNUNET_GETOPT_option_flag ('i',
195                                               "interactive",
196                                               gettext_noop ("keep running in foreground until auction completes"),
197                                               &interactive),
198
199                 GNUNET_GETOPT_OPTION_END
200         };
201         if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
202                 return 2;
203
204         ret = (GNUNET_OK ==
205                    GNUNET_PROGRAM_run (argc, argv,
206                                                            "gnunet-auction-create",
207                                                            gettext_noop ("create a new auction and "
208                                                                          "start listening for bidders"),
209                                                            options,
210                                                            &run,
211                                                            NULL)) ? ret : 1;
212         GNUNET_free ((void*) argv);
213         return ret;
214 }