ping.go 2.8 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4
package commands

import (
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"errors"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
6 7 8 9 10 11 12 13 14 15 16
	"fmt"
	"io"
	"time"

	cmds "github.com/jbenet/go-ipfs/commands"
	peer "github.com/jbenet/go-ipfs/p2p/peer"
	u "github.com/jbenet/go-ipfs/util"

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

Jeromy's avatar
Jeromy committed
17 18
const kPingTimeout = 10 * time.Second

Brian Tiger Chow's avatar
Brian Tiger Chow committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
type PingResult struct {
	Success bool
	Time    time.Duration
}

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{
Jeromy's avatar
Jeromy committed
36
		cmds.StringArg("count", false, true, "Number of pings to perform"),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			outChan, ok := res.Output().(chan interface{})
			if !ok {
				return nil, u.ErrCast()
			}

			marshal := func(v interface{}) (io.Reader, error) {
				obj, ok := v.(*PingResult)
				if !ok {
					return nil, u.ErrCast()
				}

				buf := new(bytes.Buffer)
				if obj.Success {
					fmt.Fprintf(buf, "Pong took %.2fms\n", obj.Time.Seconds()*1000)
				} else {
					fmt.Fprintf(buf, "Pong failed\n")
				}
				return buf, nil
			}

			return &cmds.ChannelMarshaler{
				Channel:   outChan,
				Marshaler: marshal,
			}, nil
		},
	},
	Run: func(req cmds.Request) (interface{}, error) {
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}

Jeromy's avatar
Jeromy committed
72
		// Must be online!
Brian Tiger Chow's avatar
Brian Tiger Chow committed
73 74 75 76
		if !n.OnlineMode() {
			return nil, errNotOnline
		}

Jeromy's avatar
Jeromy committed
77 78 79 80 81 82 83
		if len(req.Arguments()) == 0 {
			return nil, errors.New("no peer specified!")
		}

		// Set up number of pings
		numPings := 10
		val, found, err := req.Option("count").Int()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85 86
		if err != nil {
			return nil, err
		}
Jeromy's avatar
Jeromy committed
87 88 89 90 91 92 93 94 95 96 97
		if found {
			numPings = val
		}

		// One argument of input required, must be base58 encoded peerID
		peerID, err := peer.IDB58Decode(req.Arguments()[0])
		if err != nil {
			return nil, err
		}

		// Make sure we can find the node in question
Brian Tiger Chow's avatar
Brian Tiger Chow committed
98 99 100 101 102 103 104 105 106 107
		ctx, _ := context.WithTimeout(context.Background(), kPingTimeout)
		p, err := n.Routing.FindPeer(ctx, peerID)
		if err != nil {
			return nil, err
		}

		outChan := make(chan interface{})

		go func() {
			defer close(outChan)
Jeromy's avatar
Jeromy committed
108
			for i := 0; i < numPings; i++ {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
109 110 111 112
				ctx, _ = context.WithTimeout(context.Background(), kPingTimeout)
				before := time.Now()
				err := n.Routing.Ping(ctx, p.ID)
				if err != nil {
Jeromy's avatar
Jeromy committed
113
					log.Errorf("Ping error: %s", err)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
					outChan <- &PingResult{}
					break
				}
				took := time.Now().Sub(before)
				outChan <- &PingResult{
					Success: true,
					Time:    took,
				}
				time.Sleep(time.Second)
			}
		}()

		return outChan, nil
	},
	Type: PingResult{},
}