aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-dht-get.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-14 07:57:44 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-14 07:57:44 +0000
commit381f66253e4c1deb4fc1c54120f5a92cdfca14e9 (patch)
treeb56b9202d076357ecc32b4909359e876d233d55c /src/dht/gnunet-dht-get.c
parent6df5efa2a39f4204dbc6341edf42986f138f0343 (diff)
downloadgnunet-381f66253e4c1deb4fc1c54120f5a92cdfca14e9.tar.gz
gnunet-381f66253e4c1deb4fc1c54120f5a92cdfca14e9.zip
code cleanup
Diffstat (limited to 'src/dht/gnunet-dht-get.c')
-rw-r--r--src/dht/gnunet-dht-get.c72
1 files changed, 24 insertions, 48 deletions
diff --git a/src/dht/gnunet-dht-get.c b/src/dht/gnunet-dht-get.c
index 959f0b847..2dff4767f 100644
--- a/src/dht/gnunet-dht-get.c
+++ b/src/dht/gnunet-dht-get.c
@@ -42,14 +42,9 @@ static unsigned int replication = 5;
42static char *query_key; 42static char *query_key;
43 43
44/** 44/**
45 * User supplied timeout value (in seconds) 45 * User supplied timeout value
46 */ 46 */
47static unsigned long long timeout_request = 5; 47static struct GNUNET_TIME_Relative timeout_request = { 60000 };
48
49/**
50 * When this request should really die
51 */
52struct GNUNET_TIME_Absolute absolute_timeout;
53 48
54/** 49/**
55 * Be verbose 50 * Be verbose
@@ -82,26 +77,25 @@ static unsigned int result_count;
82static int ret; 77static int ret;
83 78
84 79
85static void 80/**
86shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 81 * Task run to clean up on timeout.
87{ 82 *
88 if (dht_handle != NULL) 83 * @param cls unused
89 { 84 * @param tc unused
90 GNUNET_DHT_disconnect (dht_handle); 85 */
91 dht_handle = NULL;
92 }
93}
94
95
96static void 86static void
97cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 87cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98{ 88{
99 if (get_handle != NULL) 89 if (NULL != get_handle)
100 { 90 {
101 GNUNET_DHT_get_stop (get_handle); 91 GNUNET_DHT_get_stop (get_handle);
102 get_handle = NULL; 92 get_handle = NULL;
103 } 93 }
104 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); 94 if (NULL != dht_handle)
95 {
96 GNUNET_DHT_disconnect (dht_handle);
97 dht_handle = NULL;
98 }
105} 99}
106 100
107 101
@@ -129,7 +123,9 @@ get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
129 unsigned int put_path_length, enum GNUNET_BLOCK_Type type, 123 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
130 size_t size, const void *data) 124 size_t size, const void *data)
131{ 125{
132 FPRINTF (stdout, "Result %d, type %d:\n%.*s\n", result_count, type, 126 FPRINTF (stdout,
127 _("Result %d, type %d:\n%.*s\n"),
128 result_count, type,
133 (unsigned int) size, (char *) data); 129 (unsigned int) size, (char *) data);
134 result_count++; 130 result_count++;
135} 131}
@@ -147,44 +143,26 @@ static void
147run (void *cls, char *const *args, const char *cfgfile, 143run (void *cls, char *const *args, const char *cfgfile,
148 const struct GNUNET_CONFIGURATION_Handle *c) 144 const struct GNUNET_CONFIGURATION_Handle *c)
149{ 145{
150 struct GNUNET_TIME_Relative timeout;
151 struct GNUNET_HashCode key; 146 struct GNUNET_HashCode key;
152 147
153 cfg = c; 148 cfg = c;
154 149 if (NULL == query_key)
155 if (query_key == NULL)
156 { 150 {
157 if (verbose) 151 FPRINTF (stderr, "%s", _("Must provide key for DHT GET!\n"));
158 FPRINTF (stderr, "%s", "Must provide key for DHT GET!\n");
159 ret = 1; 152 ret = 1;
160 return; 153 return;
161 } 154 }
162 155 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
163 dht_handle = GNUNET_DHT_connect (cfg, 1);
164
165 if (dht_handle == NULL)
166 { 156 {
167 if (verbose) 157 FPRINTF (stderr, "%s", _("Failed to connect to DHT service!\n"));
168 FPRINTF (stderr, "%s", "Couldn't connect to DHT service!\n");
169 ret = 1; 158 ret = 1;
170 return; 159 return;
171 } 160 }
172 else if (verbose)
173 FPRINTF (stderr, "%s", "Connected to DHT service!\n");
174
175 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */ 161 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
176 query_type = GNUNET_BLOCK_TYPE_TEST; 162 query_type = GNUNET_BLOCK_TYPE_TEST;
177
178 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key); 163 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
179 164 GNUNET_SCHEDULER_add_delayed (timeout_request,
180 timeout = 165 &cleanup_task, NULL);
181 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
182 absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout);
183
184 if (verbose)
185 FPRINTF (stderr, "Issuing GET request for %s!\n", query_key);
186 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
187 (absolute_timeout), &cleanup_task, NULL);
188 get_handle = 166 get_handle =
189 GNUNET_DHT_get_start (dht_handle, query_type, &key, replication, 167 GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
190 GNUNET_DHT_RO_NONE, NULL, 0, &get_result_iterator, 168 GNUNET_DHT_RO_NONE, NULL, 0, &get_result_iterator,
@@ -208,7 +186,7 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
208 1, &GNUNET_GETOPT_set_uint, &query_type}, 186 1, &GNUNET_GETOPT_set_uint, &query_type},
209 {'T', "timeout", "TIMEOUT", 187 {'T', "timeout", "TIMEOUT",
210 gettext_noop ("how long to execute this query before giving up?"), 188 gettext_noop ("how long to execute this query before giving up?"),
211 1, &GNUNET_GETOPT_set_ulong, &timeout_request}, 189 1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
212 {'V', "verbose", NULL, 190 {'V', "verbose", NULL,
213 gettext_noop ("be verbose (print progress information)"), 191 gettext_noop ("be verbose (print progress information)"),
214 0, &GNUNET_GETOPT_set_one, &verbose}, 192 0, &GNUNET_GETOPT_set_one, &verbose},
@@ -226,10 +204,8 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
226int 204int
227main (int argc, char *const *argv) 205main (int argc, char *const *argv)
228{ 206{
229
230 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 207 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
231 return 2; 208 return 2;
232
233 return (GNUNET_OK == 209 return (GNUNET_OK ==
234 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get", 210 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
235 gettext_noop 211 gettext_noop