Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands/ping: use new cmds lib #5675

Merged
merged 1 commit into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 27 additions & 48 deletions core/commands/ping.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package commands

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"time"

cmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"

u "gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util"
cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
ma "gx/ipfs/QmT4U94DnD8FRfqr21obWY32HLM5VExccPKMjQHofeYqr9/go-multiaddr"
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

const kPingTimeout = 10 * time.Second
Expand Down Expand Up @@ -49,72 +48,52 @@ trip latency information.
Options: []cmdkit.Option{
cmdkit.IntOption(pingCountOptionName, "n", "Number of ping messages to send.").WithDefault(10),
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

obj, ok := v.(*PingResult)
if !ok {
return nil, u.ErrCast()
}

buf := new(bytes.Buffer)
if len(obj.Text) > 0 {
buf = bytes.NewBufferString(obj.Text + "\n")
} else if obj.Success {
fmt.Fprintf(buf, "Pong received: time=%.2f ms\n", obj.Time.Seconds()*1000)
} else {
fmt.Fprintf(buf, "Pong failed\n")
}
return buf, nil
},
},
Run: func(req cmds.Request, res cmds.Response) {
ctx := req.Context()
n, err := req.InvocContext().GetNode()
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

// Must be online!
if !n.OnlineMode() {
res.SetError(ErrNotOnline, cmdkit.ErrClient)
return
return ErrNotOnline
}

addr, peerID, err := ParsePeerParam(req.Arguments()[0])
addr, peerID, err := ParsePeerParam(req.Arguments[0])
if err != nil {
res.SetError(fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments()[0], err), cmdkit.ErrNormal)
return
return fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments[0], err)
}

if peerID == n.Identity {
res.SetError(ErrPingSelf, cmdkit.ErrNormal)
return
return ErrPingSelf
}

if addr != nil {
n.Peerstore.AddAddr(peerID, addr, pstore.TempAddrTTL) // temporary
}

numPings, _, err := req.Option(pingCountOptionName).Int()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

numPings, _ := req.Options[pingCountOptionName].(int)
if numPings <= 0 {
res.SetError(fmt.Errorf("error: ping count must be greater than 0, was %d", numPings), cmdkit.ErrNormal)
return fmt.Errorf("error: ping count must be greater than 0, was %d", numPings)
}

outChan := pingPeer(ctx, n, peerID, numPings)
res.SetOutput(outChan)
outChan := pingPeer(req.Context, n, peerID, numPings)

return res.Emit(outChan)
},
Type: PingResult{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PingResult) error {
if len(out.Text) > 0 {
fmt.Fprintln(w, out.Text)
} else if out.Success {
fmt.Fprintf(w, "Pong received: time=%.2f ms\n", out.Time.Seconds()*1000)
} else {
fmt.Fprintf(w, "Pong failed\n")
}
return nil
}),
},
}

func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int) <-chan interface{} {
Expand Down
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{
"name": name.NameCmd,
"object": ocmd.ObjectCmd,
"pin": lgc.NewCommand(PinCmd),
"ping": lgc.NewCommand(PingCmd),
"ping": PingCmd,
"p2p": lgc.NewCommand(P2PCmd),
"refs": lgc.NewCommand(RefsCmd),
"resolve": ResolveCmd,
Expand Down