aboutsummaryrefslogtreecommitdiff
path: root/src/dht
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-15 18:28:18 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-15 18:28:18 +0000
commitbaf16447ccaa8a1e18a00767d35cc05a9da99256 (patch)
treec93f2e35fa18a9491770f6253fc58784c16d3edc /src/dht
parentce1537de07f093ffcc3bccf3a444f7dfd9784181 (diff)
downloadgnunet-baf16447ccaa8a1e18a00767d35cc05a9da99256.tar.gz
gnunet-baf16447ccaa8a1e18a00767d35cc05a9da99256.zip
fixing #2544
Diffstat (limited to 'src/dht')
-rw-r--r--src/dht/gnunet-dht-monitor.c104
-rw-r--r--src/dht/gnunet-dht-put.c11
2 files changed, 43 insertions, 72 deletions
diff --git a/src/dht/gnunet-dht-monitor.c b/src/dht/gnunet-dht-monitor.c
index b16e91108..ecda1be14 100644
--- a/src/dht/gnunet-dht-monitor.c
+++ b/src/dht/gnunet-dht-monitor.c
@@ -73,25 +73,6 @@ static int ret;
73 73
74 74
75/** 75/**
76 * Function called on shutdown, disconnects from DHT if necessary.
77 *
78 * @param cls closure (unused)
79 * @param tc Task Context
80 */
81static void
82shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83{
84 if (verbose)
85 FPRINTF (stderr, "%s", "Shutting down!\n");
86 if (dht_handle != NULL)
87 {
88 GNUNET_DHT_disconnect (dht_handle);
89 dht_handle = NULL;
90 }
91}
92
93
94/**
95 * Stop monitoring request and start shutdown 76 * Stop monitoring request and start shutdown
96 * 77 *
97 * @param cls closure (unused) 78 * @param cls closure (unused)
@@ -102,12 +83,16 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102{ 83{
103 if (verbose) 84 if (verbose)
104 FPRINTF (stderr, "%s", "Cleaning up!\n"); 85 FPRINTF (stderr, "%s", "Cleaning up!\n");
105 if (monitor_handle != NULL) 86 if (NULL != monitor_handle)
106 { 87 {
107 GNUNET_DHT_monitor_stop (monitor_handle); 88 GNUNET_DHT_monitor_stop (monitor_handle);
108 monitor_handle = NULL; 89 monitor_handle = NULL;
109 } 90 }
110 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); 91 if (NULL != dht_handle)
92 {
93 GNUNET_DHT_disconnect (dht_handle);
94 dht_handle = NULL;
95 }
111} 96}
112 97
113 98
@@ -133,14 +118,14 @@ get_callback (void *cls,
133 const struct GNUNET_PeerIdentity *path, 118 const struct GNUNET_PeerIdentity *path,
134 const struct GNUNET_HashCode * key) 119 const struct GNUNET_HashCode * key)
135{ 120{
136 FPRINTF (stdout, "Result %d, operation: %s, type %d\n Key: %s\n ", 121 FPRINTF (stdout, "GET #%u: type %d, key `%s'\n",
137 result_count, 122 result_count,
138 "GET", 123 (int) type,
139 type,
140 GNUNET_h2s_full(key)); 124 GNUNET_h2s_full(key));
141 result_count++; 125 result_count++;
142} 126}
143 127
128
144/** 129/**
145 * Callback called on each GET reply going through the DHT. 130 * Callback called on each GET reply going through the DHT.
146 * 131 *
@@ -167,16 +152,17 @@ get_resp_callback (void *cls,
167 const void *data, 152 const void *data,
168 size_t size) 153 size_t size)
169{ 154{
170 FPRINTF (stdout, "Result %d, operation: %s, type %d:\n Key: %s\n %.*s\n", 155 FPRINTF (stdout,
156 "RESPONSE #%u: type %d, key `%s', data `%.*s'\n",
171 result_count, 157 result_count,
172 "GET_RESP", 158 (int) type,
173 type, 159 GNUNET_h2s_full (key),
174 GNUNET_h2s_full(key),
175 (unsigned int) size, 160 (unsigned int) size,
176 (char *) data); 161 (char *) data);
177 result_count++; 162 result_count++;
178} 163}
179 164
165
180/** 166/**
181 * Callback called on each PUT request going through the DHT. 167 * Callback called on each PUT request going through the DHT.
182 * 168 *
@@ -205,16 +191,17 @@ put_callback (void *cls,
205 const void *data, 191 const void *data,
206 size_t size) 192 size_t size)
207{ 193{
208 FPRINTF (stdout, "Result %d, operation: %s, type %d:\n Key: %s\n %.*s\n", 194 FPRINTF (stdout,
195 "PUT %u: type %d, key `%s', data `%.*s'\n",
209 result_count, 196 result_count,
210 "PUT", 197 (int) type,
211 type,
212 GNUNET_h2s_full(key), 198 GNUNET_h2s_full(key),
213 (unsigned int) size, 199 (unsigned int) size,
214 (char *) data); 200 (char *) data);
215 result_count++; 201 result_count++;
216} 202}
217 203
204
218/** 205/**
219 * Main function that will be run by the scheduler. 206 * Main function that will be run by the scheduler.
220 * 207 *
@@ -228,43 +215,36 @@ run (void *cls, char *const *args, const char *cfgfile,
228 const struct GNUNET_CONFIGURATION_Handle *c) 215 const struct GNUNET_CONFIGURATION_Handle *c)
229{ 216{
230 struct GNUNET_HashCode *key; 217 struct GNUNET_HashCode *key;
218 struct GNUNET_HashCode hc;
231 219
232 cfg = c; 220 cfg = c;
233 221
234 dht_handle = GNUNET_DHT_connect (cfg, 1); 222 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
235
236 if (dht_handle == NULL)
237 { 223 {
238 if (verbose) 224 FPRINTF (stderr, "%s",
239 FPRINTF (stderr, "%s", "Couldn't connect to DHT service!\n"); 225 _("Failed to connect to DHT service!\n"));
240 ret = 1; 226 ret = 1;
241 return; 227 return;
242 } 228 }
243 else if (verbose) 229 if (GNUNET_BLOCK_TYPE_ANY == block_type) /* Type of data not set */
244 FPRINTF (stderr, "%s", "Connected to DHT service!\n");
245
246 if (block_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
247 block_type = GNUNET_BLOCK_TYPE_TEST; 230 block_type = GNUNET_BLOCK_TYPE_TEST;
248 231 if (NULL != query_key)
249 if (query_key != NULL) { 232 {
250 key = GNUNET_malloc (sizeof(struct GNUNET_HashCode)); 233 key = &hc;
251 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key); 234 if (GNUNET_OK !=
252 } 235 GNUNET_CRYPTO_hash_from_string (query_key, key))
236 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
237 }
253 else 238 else
254 key = NULL; 239 {
255 240 key = NULL;
256 if (verbose) 241 }
257 {
258 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != timeout_request.rel_value)
259 FPRINTF (stderr, "Monitoring for %s\n",
260 GNUNET_TIME_relative_to_string (timeout_request));
261 else
262 FPRINTF (stderr, "%s", "Monitoring indefinitely (close with Ctrl+C)\n");
263 }
264
265 GNUNET_SCHEDULER_add_delayed (timeout_request, &cleanup_task, NULL); 242 GNUNET_SCHEDULER_add_delayed (timeout_request, &cleanup_task, NULL);
266 if (verbose) 243 if (verbose)
267 FPRINTF (stderr, "Issuing MONITOR request for %s!\n", query_key); 244 FPRINTF (stderr,
245 "Monitoring for %s\n",
246 GNUNET_TIME_relative_to_string (timeout_request));
247 GNUNET_SCHEDULER_add_delayed (timeout, &cleanup_task, NULL);
268 monitor_handle = GNUNET_DHT_monitor_start (dht_handle, 248 monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
269 block_type, 249 block_type,
270 key, 250 key,
@@ -272,15 +252,11 @@ run (void *cls, char *const *args, const char *cfgfile,
272 &get_resp_callback, 252 &get_resp_callback,
273 &put_callback, 253 &put_callback,
274 NULL); 254 NULL);
275 if (verbose)
276 FPRINTF (stderr, "%s", "MONITOR started!\n");
277 GNUNET_free_non_null (key);
278
279} 255}
280 256
281 257
282/** 258/**
283 * gnunet-dht-get command line options 259 * gnunet-dht-monitor command line options
284 */ 260 */
285static struct GNUNET_GETOPT_CommandLineOption options[] = { 261static struct GNUNET_GETOPT_CommandLineOption options[] = {
286 {'k', "key", "KEY", 262 {'k', "key", "KEY",
@@ -290,7 +266,7 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
290 gettext_noop ("the type of data to look for"), 266 gettext_noop ("the type of data to look for"),
291 1, &GNUNET_GETOPT_set_uint, &block_type}, 267 1, &GNUNET_GETOPT_set_uint, &block_type},
292 {'T', "timeout", "TIMEOUT", 268 {'T', "timeout", "TIMEOUT",
293 gettext_noop ("how long to execute? default 60s, use \"forever\" to monitor forever"), 269 gettext_noop ("how long should the monitor command run"),
294 1, &GNUNET_GETOPT_set_relative_time, &timeout_request}, 270 1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
295 {'V', "verbose", NULL, 271 {'V', "verbose", NULL,
296 gettext_noop ("be verbose (print progress information)"), 272 gettext_noop ("be verbose (print progress information)"),
@@ -313,7 +289,7 @@ main (int argc, char *const *argv)
313 return 2; 289 return 2;
314 290
315 return (GNUNET_OK == 291 return (GNUNET_OK ==
316 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get", 292 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-monitor",
317 gettext_noop 293 gettext_noop
318 ("Prints all packets that go through the DHT."), 294 ("Prints all packets that go through the DHT."),
319 options, &run, NULL)) ? ret : 1; 295 options, &run, NULL)) ? ret : 1;
diff --git a/src/dht/gnunet-dht-put.c b/src/dht/gnunet-dht-put.c
index a55986450..67884c423 100644
--- a/src/dht/gnunet-dht-put.c
+++ b/src/dht/gnunet-dht-put.c
@@ -148,24 +148,20 @@ run (void *cls, char *const *args, const char *cfgfile,
148 148
149 cfg = c; 149 cfg = c;
150 150
151 if ((query_key == NULL) || (data == NULL)) 151 if ((NULL == query_key) || (NULL == data))
152 { 152 {
153 FPRINTF (stderr, "%s", _("Must provide KEY and DATA for DHT put!\n")); 153 FPRINTF (stderr, "%s", _("Must provide KEY and DATA for DHT put!\n"));
154 ret = 1; 154 ret = 1;
155 return; 155 return;
156 } 156 }
157 157
158 dht_handle = GNUNET_DHT_connect (cfg, 1); 158 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
159 if (dht_handle == NULL)
160 { 159 {
161 FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT"); 160 FPRINTF (stderr, _("Could not connect to %s service!\n"), "DHT");
162 ret = 1; 161 ret = 1;
163 return; 162 return;
164 } 163 }
165 else if (verbose) 164 if (GNUNET_BLOCK_TYPE_ANY == query_type) /* Type of data not set */
166 FPRINTF (stderr, _("Connected to %s service!\n"), "DHT");
167
168 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
169 query_type = GNUNET_BLOCK_TYPE_TEST; 165 query_type = GNUNET_BLOCK_TYPE_TEST;
170 166
171 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key); 167 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
@@ -176,7 +172,6 @@ run (void *cls, char *const *args, const char *cfgfile,
176 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply 172 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
177 (GNUNET_TIME_UNIT_SECONDS, 173 (GNUNET_TIME_UNIT_SECONDS,
178 expiration_seconds)); 174 expiration_seconds));
179
180 if (verbose) 175 if (verbose)
181 FPRINTF (stderr, _("Issuing put request for `%s' with data `%s'!\n"), 176 FPRINTF (stderr, _("Issuing put request for `%s' with data `%s'!\n"),
182 query_key, data); 177 query_key, data);