From 51639b354a76ea386e2ac7ce10a47bbae774b2f3 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Wed, 7 Jun 2017 23:20:00 -0300 Subject: [PATCH] Added ping stats output. --- core/ping.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/core/ping.c b/core/ping.c index b02ce96..0a423ab 100644 --- a/core/ping.c +++ b/core/ping.c @@ -27,6 +27,8 @@ int ipfs_ping (int argc, char **argv) char* id = NULL; struct FSRepo* fs_repo = NULL; char* repo_path = NULL; + struct timeval time1, time2; + int time_us; // sanity check local_node.peerstore = NULL; @@ -58,6 +60,7 @@ int ipfs_ping (int argc, char **argv) if (strstr(argv[2], "Qm") == &argv[2][0]) { // resolve the peer id + fprintf (stderr, "Looking up peer %s\n", argv[2]); peer_to_ping = ipfs_resolver_find_peer(argv[2], &local_node); } else { // perhaps they passed an IP and port @@ -78,17 +81,31 @@ int ipfs_ping (int argc, char **argv) if (peer_to_ping == NULL) goto exit; - if (!local_node.routing->Ping(local_node.routing, peer_to_ping)) { - id = malloc(peer_to_ping->id_size + 1); + id = malloc(peer_to_ping->id_size + 1); + if (id) { memcpy(id, peer_to_ping->id, peer_to_ping->id_size); id[peer_to_ping->id_size] = 0; - fprintf(stderr, "Unable to ping %s\n", id); - free(id); - goto exit; + fprintf (stderr, "PING %s.\n", id); + } + + for (;;) { + gettimeofday(&time1, NULL); + if (!local_node.routing->Ping(local_node.routing, peer_to_ping)) { + fprintf(stderr, "Unable to ping %s\n", id); + goto exit; + } + gettimeofday(&time2, NULL); + + time_us = (time2.tv_sec - time1.tv_sec) * 1000000; + time_us += (time2.tv_usec - time1.tv_usec); + fprintf (stderr, "Pong received: time=%d.%03d ms\n", time_us / 1000, time_us % 1000); + usleep(1000000 - time_us); } retVal = 1; exit: + if (id != NULL) + free(id); if (fs_repo != NULL) ipfs_repo_fsrepo_free(fs_repo); if (local_node.peerstore != NULL)