Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
9fc0f86a
Commit
9fc0f86a
authored
Jan 10, 2015
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup from PR
parent
50c8561b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
55 deletions
+53
-55
core/commands/ping.go
core/commands/ping.go
+53
-55
No files found.
core/commands/ping.go
View file @
9fc0f86a
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"time"
"time"
cmds
"github.com/jbenet/go-ipfs/commands"
cmds
"github.com/jbenet/go-ipfs/commands"
core
"github.com/jbenet/go-ipfs/core"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
u
"github.com/jbenet/go-ipfs/util"
u
"github.com/jbenet/go-ipfs/util"
...
@@ -25,18 +26,18 @@ var PingCmd = &cmds.Command{
...
@@ -25,18 +26,18 @@ var PingCmd = &cmds.Command{
Helptext
:
cmds
.
HelpText
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"send echo request packets to IPFS hosts"
,
Tagline
:
"send echo request packets to IPFS hosts"
,
Synopsis
:
`
Synopsis
:
`
ipfs ping <peer.ID> -
Send pings to a peer using the routing system to discover its address
Send pings to a peer using the routing system to discover its address
`
,
`
,
ShortDescription
:
`
ShortDescription
:
`
ipfs ping is a tool to find a node (in the routing system),
ipfs ping is a tool to find a node (in the routing system),
send pings, wait for pongs, and print out round-trip latency information.
send pings, wait for pongs, and print out round-trip latency information.
`
,
`
,
},
},
Arguments
:
[]
cmds
.
Argument
{
Arguments
:
[]
cmds
.
Argument
{
cmds
.
StringArg
(
"peer ID"
,
true
,
true
,
"ID of peer to be pinged"
),
cmds
.
StringArg
(
"peer ID"
,
true
,
true
,
"ID of peer to be pinged"
),
},
},
Options
:
[]
cmds
.
Option
{
Options
:
[]
cmds
.
Option
{
cmds
.
IntOption
(
"count"
,
"n"
),
cmds
.
IntOption
(
"count"
,
"n"
,
"number of ping messages to send"
),
},
},
Marshalers
:
cmds
.
MarshalerMap
{
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
...
@@ -79,12 +80,11 @@ send pings, wait for pongs, and print out round-trip latency information.
...
@@ -79,12 +80,11 @@ send pings, wait for pongs, and print out round-trip latency information.
return
nil
,
errNotOnline
return
nil
,
errNotOnline
}
}
if
len
(
req
.
Arguments
())
==
0
{
peerID
,
err
:=
peer
.
IDB58Decode
(
req
.
Arguments
()[
0
])
return
nil
,
cmds
.
ClientError
(
"no peer specified!"
)
if
err
!=
nil
{
return
nil
,
err
}
}
outChan
:=
make
(
chan
interface
{},
5
)
// Set up number of pings
// Set up number of pings
numPings
:=
10
numPings
:=
10
val
,
found
,
err
:=
req
.
Option
(
"count"
)
.
Int
()
val
,
found
,
err
:=
req
.
Option
(
"count"
)
.
Int
()
...
@@ -95,54 +95,52 @@ send pings, wait for pongs, and print out round-trip latency information.
...
@@ -95,54 +95,52 @@ send pings, wait for pongs, and print out round-trip latency information.
numPings
=
val
numPings
=
val
}
}
// One argument of input required, must be base58 encoded peerID
outChan
:=
make
(
chan
interface
{})
peerID
,
err
:=
peer
.
IDB58Decode
(
req
.
Arguments
()[
0
])
if
err
!=
nil
{
return
nil
,
err
}
go
func
()
{
defer
close
(
outChan
)
// Make sure we can find the node in question
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Looking up peer %s"
,
peerID
.
Pretty
()),
}
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
kPingTimeout
)
p
,
err
:=
n
.
Routing
.
FindPeer
(
ctx
,
peerID
)
n
.
Peerstore
.
AddPeerInfo
(
p
)
if
err
!=
nil
{
outChan
<-
&
PingResult
{
Text
:
"Peer lookup error!"
}
outChan
<-
&
PingResult
{
Text
:
err
.
Error
()}
return
}
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Peer found, starting pings."
),
}
var
total
time
.
Duration
go
pingPeer
(
n
,
peerID
,
numPings
,
outChan
)
for
i
:=
0
;
i
<
numPings
;
i
++
{
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
kPingTimeout
)
took
,
err
:=
n
.
Routing
.
Ping
(
ctx
,
p
.
ID
)
if
err
!=
nil
{
log
.
Errorf
(
"Ping error: %s"
,
err
)
outChan
<-
&
PingResult
{}
break
}
outChan
<-
&
PingResult
{
Success
:
true
,
Time
:
took
,
}
total
+=
took
time
.
Sleep
(
time
.
Second
)
}
averagems
:=
total
.
Seconds
()
*
1000
/
float64
(
numPings
)
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Average latency: %.2fms"
,
averagems
),
}
}()
return
outChan
,
nil
return
outChan
,
nil
},
},
Type
:
PingResult
{},
Type
:
PingResult
{},
}
}
func
pingPeer
(
n
*
core
.
IpfsNode
,
pid
peer
.
ID
,
numPings
int
,
outChan
chan
interface
{})
{
defer
close
(
outChan
)
// Make sure we can find the node in question
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Looking up peer %s"
,
pid
.
Pretty
()),
}
// TODO: get master context passed in
ctx
,
_
:=
context
.
WithTimeout
(
context
.
TODO
(),
kPingTimeout
)
p
,
err
:=
n
.
Routing
.
FindPeer
(
ctx
,
pid
)
if
err
!=
nil
{
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Peer lookup error: %s"
,
err
)}
return
}
n
.
Peerstore
.
AddPeerInfo
(
p
)
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Peer found, starting pings."
)}
var
total
time
.
Duration
for
i
:=
0
;
i
<
numPings
;
i
++
{
ctx
,
_
=
context
.
WithTimeout
(
context
.
TODO
(),
kPingTimeout
)
took
,
err
:=
n
.
Routing
.
Ping
(
ctx
,
p
.
ID
)
if
err
!=
nil
{
log
.
Errorf
(
"Ping error: %s"
,
err
)
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Ping error: %s"
,
err
)}
break
}
outChan
<-
&
PingResult
{
Success
:
true
,
Time
:
took
,
}
total
+=
took
time
.
Sleep
(
time
.
Second
)
}
averagems
:=
total
.
Seconds
()
*
1000
/
float64
(
numPings
)
outChan
<-
&
PingResult
{
Text
:
fmt
.
Sprintf
(
"Average latency: %.2fms"
,
averagems
),
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment