Skip to content

Commit

Permalink
ping WIP
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
  • Loading branch information
Brian Tiger Chow committed Dec 5, 2014
1 parent b1cfb3f commit f777332
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions core/commands/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package commands

import (
"time"

cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)

var PingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "send echo request packets to IPFS hosts",
Synopsis: `
ipfs ping <peer.ID> - Send pings to a peer using the routing system to discover its address
`,
ShortDescription: `
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.
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("peer-id", true, true, "ID of peer to ping"),
},
Run: func(req cmds.Request) (interface{}, error) {
n, err := req.Context().GetNode()
if err != nil {
return nil, err
}

if !n.OnlineMode() {
return nil, errNotOnline
}

peerID := peer.DecodePrettyID("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
const kPingTimeout = 10 * time.Second
ctx, _ := context.WithTimeout(context.Background(), kPingTimeout)
p, err := n.Routing.FindPeer(ctx, peerID)
if err != nil {
return nil, err
}
if err := n.Network.DialPeer(ctx, p); err != nil {
return nil, err
}
msg := message.New(p, nil)
n.Network.SendMessage(msg)
return nil, nil
},
}
1 change: 1 addition & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var rootSubcommands = map[string]*cmds.Command{
"name": nameCmd,
"object": objectCmd,
"pin": pinCmd,
"ping": PingCmd,
"refs": refsCmd,
"swarm": swarmCmd,
"update": UpdateCmd,
Expand Down

0 comments on commit f777332

Please sign in to comment.