convert fs publish to MQ
[oweals/gnunet.git] / src / dht / gnunet-dht-put.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  * @file dht/gnunet-dht-put.c
22  * @brief search for data in DHT
23  * @author Christian Grothoff
24  * @author Nathan Evans
25  */
26 #include "platform.h"
27 #include "gnunet_dht_service.h"
28
29 /**
30  * The type of the query
31  */
32 static unsigned int query_type;
33
34 /**
35  * The key used in the DHT
36  */
37 struct GNUNET_HashCode key;
38
39 /**
40  * The key for the query
41  */
42 static char *query_key;
43
44 /**
45  * User supplied expiration value
46  */
47 static unsigned long long expiration_seconds = 3600;
48
49 /**
50  * Desired replication level.
51  */
52 static unsigned int replication = 5;
53
54 /**
55  * Be verbose
56  */
57 static int verbose;
58
59 /**
60  * Use DHT demultixplex_everywhere
61  */
62 static int demultixplex_everywhere;
63
64 /**
65  * Handle to the DHT
66  */
67 static struct GNUNET_DHT_Handle *dht_handle;
68
69
70 /**
71  * Global handle of the configuration
72  */
73 static const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75 /**
76  * Global status value
77  */
78 static int ret;
79
80 /**
81  * The data to insert into the dht
82  */
83 static char *data;
84
85
86 static void
87 shutdown_task (void *cls)
88 {
89   if (NULL != dht_handle)
90   {
91     GNUNET_DHT_disconnect (dht_handle);
92     dht_handle = NULL;
93   }
94 }
95
96
97 /**
98  * Signature of the main function of a task.
99  *
100  * @param cls closure
101  * @param success #GNUNET_OK if the PUT was transmitted,
102  *                #GNUNET_NO on timeout,
103  *                #GNUNET_SYSERR on disconnect from service
104  *                after the PUT message was transmitted
105  *                (so we don't know if it was received or not)
106  */
107 static void
108 message_sent_cont (void *cls, int success)
109 {
110   if (verbose)
111   {
112     switch (success)
113     {
114     case GNUNET_OK:
115       FPRINTF (stderr, "%s `%s'!\n",  _("PUT request sent with key"), GNUNET_h2s_full(&key));
116       break;
117     case GNUNET_NO:
118       FPRINTF (stderr, "%s",  _("Timeout sending PUT request!\n"));
119       break;
120     case GNUNET_SYSERR:
121       FPRINTF (stderr, "%s",  _("PUT request not confirmed!\n"));
122       break;
123     default:
124       GNUNET_break (0);
125       break;
126     }
127   }
128   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
129 }
130
131
132 /**
133  * Main function that will be run by the scheduler.
134  *
135  * @param cls closure
136  * @param args remaining command-line arguments
137  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
138  * @param c configuration
139  */
140 static void
141 run (void *cls,
142      char *const *args,
143      const char *cfgfile,
144      const struct GNUNET_CONFIGURATION_Handle *c)
145 {
146   struct GNUNET_TIME_Absolute expiration;
147
148   cfg = c;
149   if ((NULL == query_key) || (NULL == data))
150   {
151     FPRINTF (stderr, "%s",  _("Must provide KEY and DATA for DHT put!\n"));
152     ret = 1;
153     return;
154   }
155
156   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
157   {
158     FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
159     ret = 1;
160     return;
161   }
162   if (GNUNET_BLOCK_TYPE_ANY == query_type)      /* Type of data not set */
163     query_type = GNUNET_BLOCK_TYPE_TEST;
164
165   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
166
167   expiration =
168       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
169                                         (GNUNET_TIME_UNIT_SECONDS,
170                                          expiration_seconds));
171   if (verbose)
172     FPRINTF (stderr, _("Issuing put request for `%s' with data `%s'!\n"),
173              query_key, data);
174   GNUNET_DHT_put (dht_handle,
175                   &key,
176                   replication,
177                   (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
178                   query_type,
179                   strlen (data),
180                   data,
181                   expiration,
182                   &message_sent_cont,
183                   NULL);
184 }
185
186
187 /**
188  * gnunet-dht-put command line options
189  */
190 static struct GNUNET_GETOPT_CommandLineOption options[] = {
191   {'d', "data", "DATA",
192    gettext_noop ("the data to insert under the key"),
193    1, &GNUNET_GETOPT_set_string, &data},
194   {'e', "expiration", "EXPIRATION",
195    gettext_noop ("how long to store this entry in the dht (in seconds)"),
196    1, &GNUNET_GETOPT_set_ulong, &expiration_seconds},
197   {'k', "key", "KEY",
198    gettext_noop ("the query key"),
199    1, &GNUNET_GETOPT_set_string, &query_key},
200   {'x', "demultiplex", NULL,
201    gettext_noop ("use DHT's demultiplex everywhere option"),
202    0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
203   {'r', "replication", "LEVEL",
204    gettext_noop ("how many replicas to create"),
205    1, &GNUNET_GETOPT_set_uint, &replication},
206   {'t', "type", "TYPE",
207    gettext_noop ("the type to insert data as"),
208    1, &GNUNET_GETOPT_set_uint, &query_type},
209   {'V', "verbose", NULL,
210    gettext_noop ("be verbose (print progress information)"),
211    0, &GNUNET_GETOPT_set_one, &verbose},
212   GNUNET_GETOPT_OPTION_END
213 };
214
215
216 /**
217  * Entry point for gnunet-dht-put
218  *
219  * @param argc number of arguments from the command line
220  * @param argv command line arguments
221  * @return 0 ok, 1 on error
222  */
223 int
224 main (int argc, char *const *argv)
225 {
226   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
227                                                  &argc, &argv))
228     return 2;
229   return (GNUNET_OK ==
230           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-put",
231                               gettext_noop
232                               ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."),
233                               options, &run, NULL)) ? ret : 1;
234 }
235
236 /* end of gnunet-dht-put.c */