Added ping stats output.
This commit is contained in:
parent
a88636ae7e
commit
51639b354a
1 changed files with 22 additions and 5 deletions
21
core/ping.c
21
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);
|
||||
if (id) {
|
||||
memcpy(id, peer_to_ping->id, peer_to_ping->id_size);
|
||||
id[peer_to_ping->id_size] = 0;
|
||||
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);
|
||||
free(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)
|
||||
|
|
Loading…
Reference in a new issue