GNUnet  0.10.x
gnunet-auction-create.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 
28 #include <float.h>
29 
30 #include "gnunet_util_lib.h"
31 #include <jansson.h>
32 /* #include "gnunet_auction_service.h" */
33 
34 #define FIRST_PRICE 0
35 #define OUTCOME_PRIVATE 0
36 #define OUTCOME_PUBLIC 1
37 
38 static int ret;
39 static char *fndesc;
40 static char *fnprices;
43 static unsigned int m = FIRST_PRICE;
44 static int outcome = OUTCOME_PRIVATE;
45 static int interactive;
56 static void
57 run(void *cls,
58  char *const *args,
59  const char *cfgfile,
60  const struct GNUNET_CONFIGURATION_Handle *cfg)
61 {
62  unsigned int i;
63  double cur, prev = DBL_MAX;
64  json_t *pmap;
65  json_t *parray;
66  json_t *pnode;
67  json_error_t jerr;
68 
69  /* cmdline parsing */
70  if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dstart.rel_value_us)
71  {
73  "required argument --regtime missing or invalid (zero)\n");
74  goto fail;
75  }
76  if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dround.rel_value_us)
77  {
79  "required argument --roundtime missing or invalid (zero)\n");
80  goto fail;
81  }
82  if (!fndesc)
83  {
85  "required argument --description missing\n");
86  goto fail;
87  }
88  if (!fnprices)
89  {
91  "required argument --pricemap missing\n");
92  goto fail;
93  }
94 
95  /* parse and check pricemap validity */
96  if (!(pmap = json_load_file(fnprices, JSON_DECODE_INT_AS_REAL, &jerr)))
97  {
99  "parsing pricemap json at %d:%d: %s\n",
100  jerr.line, jerr.column, jerr.text);
101  goto fail;
102  }
103  if (-1 == json_unpack_ex(pmap, &jerr, JSON_VALIDATE_ONLY,
104  "{s:s, s:[]}", "currency", "prices"))
105  {
107  "validating pricemap: %s\n", jerr.text);
108  goto fail;
109  }
110  if (!(parray = json_object_get(pmap, "prices")) || !json_is_array(parray))
111  {
113  "could not get `prices` array node from pricemap\n");
114  goto fail;
115  }
116  if (0 == json_array_size(parray))
117  {
118  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "empty pricemap array\n");
119  goto fail;
120  }
121  json_array_foreach(parray, i, pnode)
122  {
123  if (-1 == json_unpack_ex(pnode, &jerr, 0, "F", &cur))
124  {
126  "validating pricearray index %d: %s\n", i, jerr.text);
127  goto fail;
128  }
129  if (prev <= cur)
130  {
132  "validating pricearray index %d: "
133  "prices must be strictly monotonically decreasing\n",
134  i);
135  goto fail;
136  }
137  prev = cur;
138  }
139 
140  return;
141 
142 fail:
143  ret = 1;
144  return;
145 }
146 
147 
155 int
156 main(int argc, char *const *argv)
157 {
158  struct GNUNET_GETOPT_CommandLineOption options[] = {
160  "description",
161  "FILE",
162  gettext_noop("description of the item to be sold"),
163  &fndesc),
164 
166  "pricemap",
167  "FILE",
168  gettext_noop("mapping of possible prices"),
169  &fnprices),
170 
172  "roundtime",
173  "DURATION",
174  gettext_noop("max duration per round"),
175  &dround),
176 
178  "regtime",
179  "DURATION",
180  gettext_noop("duration until auction starts"),
181  &dstart),
183  "m",
184  "NUMBER",
185  gettext_noop("number of items to sell\n"
186  "0 for first price auction\n"
187  ">0 for vickrey/M+1st price auction"),
188  &m),
189 
191  "public",
192  gettext_noop("public auction outcome"),
193  &outcome),
194 
196  "interactive",
197  gettext_noop("keep running in foreground until auction completes"),
198  &interactive),
199 
201  };
202 
203  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
204  return 2;
205 
206  ret = (GNUNET_OK ==
207  GNUNET_PROGRAM_run(argc, argv,
208  "gnunet-auction-create",
209  gettext_noop("create a new auction and "
210  "start listening for bidders"),
211  options,
212  &run,
213  NULL)) ? ret : 1;
214  GNUNET_free((void*)argv);
215  return ret;
216 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time)...
uint64_t rel_value_us
The actual value.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
static int outcome
auction parameter m
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Definition of a command line option.
#define FIRST_PRICE
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static struct GNUNET_TIME_Relative dround
filename of the price map
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_filename(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a filename (automatically path expanded).
static struct GNUNET_TIME_Relative dstart
max round duration
static char * fndesc
Final status code.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
keep running in foreground
static int ret
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static unsigned int m
time until auction starts
#define OUTCOME_PRIVATE
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
configuration data
Definition: configuration.c:83
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
#define GNUNET_log(kind,...)
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
static int interactive
outcome
int main(int argc, char *const *argv)
The main function.
static char * fnprices
filename of the item description
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
#define gettext_noop(String)
Definition: gettext.h:69