Skip to content
Snippets Groups Projects
Commit e207e592 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tools/dhtcnode: count values in listen, add "ll"

parent 0302cee8
Branches
Tags
No related merge requests found
...@@ -36,12 +36,18 @@ struct op_context { ...@@ -36,12 +36,18 @@ struct op_context {
struct listen_context { struct listen_context {
dht_runner* runner; dht_runner* runner;
dht_op_token* token; dht_op_token* token;
size_t count;
}; };
bool dht_value_callback(const dht_value* value, bool expired, void* user_data) bool dht_value_callback(const dht_value* value, bool expired, void* user_data)
{ {
struct listen_context* ctx = (struct listen_context*) user_data;
if (expired)
ctx->count--;
else
ctx->count++;
dht_data_view data = dht_value_get_data(value); dht_data_view data = dht_value_get_data(value);
printf("Value callback %s: %.*s.\n", expired ? "expired" : "new", (int)data.size, data.data); printf("Listen: %s value: %.*s (total %zu).\n", expired ? "expired" : "new", (int)data.size, data.data, ctx->count);
return true; return true;
} }
...@@ -193,12 +199,11 @@ int main(int argc, char **argv) ...@@ -193,12 +199,11 @@ int main(int argc, char **argv)
char value[256]; char value[256];
while (true) { while (true) {
const char* line_read = readline("> "); const char* line_read = readline("> ");
if (line_read && *line_read)
add_history(line_read);
if (!line_read) if (!line_read)
break; break;
if (!strcmp(line_read, "\0")) if (!*line_read)
continue; continue;
add_history(line_read);
memset(cmd, 0, sizeof cmd); memset(cmd, 0, sizeof cmd);
memset(arg, 0, sizeof arg); memset(arg, 0, sizeof arg);
...@@ -218,6 +223,10 @@ int main(int argc, char **argv) ...@@ -218,6 +223,10 @@ int main(int argc, char **argv)
free(addrs); free(addrs);
} }
continue; continue;
} else if (!strcmp(cmd, "ll")) {
dht_infohash node_id = dht_runner_get_node_id(runner);
printf("DHT node %s running on port %u\n", dht_infohash_print(&node_id), dht_runner_get_bound_port(runner, AF_INET));
continue;
} }
dht_infohash key; dht_infohash key;
...@@ -231,6 +240,7 @@ int main(int argc, char **argv) ...@@ -231,6 +240,7 @@ int main(int argc, char **argv)
} else if (!strcmp(cmd, "l")) { } else if (!strcmp(cmd, "l")) {
struct listen_context* ctx = malloc(sizeof(struct listen_context)); struct listen_context* ctx = malloc(sizeof(struct listen_context));
ctx->runner = runner; ctx->runner = runner;
ctx->count = 0;
ctx->token = dht_runner_listen(runner, &key, dht_value_callback, listen_context_free, ctx); ctx->token = dht_runner_listen(runner, &key, dht_value_callback, listen_context_free, ctx);
} else if (!strcmp(cmd, "p")) { } else if (!strcmp(cmd, "p")) {
dht_value* val = dht_value_new_from_string(value); dht_value* val = dht_value_new_from_string(value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment