-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #719 from jbenet/feat/wantlistcmd
wantlist command
- Loading branch information
Showing
5 changed files
with
156 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
cmds "github.com/jbenet/go-ipfs/commands" | ||
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap" | ||
u "github.com/jbenet/go-ipfs/util" | ||
"io" | ||
) | ||
|
||
var BitswapCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "A set of commands to manipulate the bitswap agent", | ||
ShortDescription: ``, | ||
}, | ||
Subcommands: map[string]*cmds.Command{ | ||
"wantlist": showWantlistCmd, | ||
"stat": bitswapStatCmd, | ||
}, | ||
} | ||
|
||
var showWantlistCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Show blocks currently on the wantlist", | ||
ShortDescription: ` | ||
Print out all blocks currently on the bitswap wantlist for the local peer`, | ||
}, | ||
Type: KeyList{}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
nd, err := req.Context().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
bs, ok := nd.Exchange.(*bitswap.Bitswap) | ||
if !ok { | ||
res.SetError(u.ErrCast(), cmds.ErrNormal) | ||
return | ||
} | ||
|
||
res.SetOutput(&KeyList{bs.GetWantlist()}) | ||
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: KeyListTextMarshaler, | ||
}, | ||
} | ||
|
||
var bitswapStatCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "show some diagnostic information on the bitswap agent", | ||
ShortDescription: ``, | ||
}, | ||
Type: bitswap.Stat{}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
nd, err := req.Context().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
bs, ok := nd.Exchange.(*bitswap.Bitswap) | ||
if !ok { | ||
res.SetError(u.ErrCast(), cmds.ErrNormal) | ||
return | ||
} | ||
|
||
st, err := bs.Stat() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
res.SetOutput(st) | ||
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
out, ok := res.Output().(*bitswap.Stat) | ||
if !ok { | ||
return nil, u.ErrCast() | ||
} | ||
buf := new(bytes.Buffer) | ||
fmt.Fprintln(buf, "bitswap status") | ||
fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize) | ||
fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist)) | ||
for _, k := range out.Wantlist { | ||
fmt.Fprintf(buf, "\t\t%s\n", k.B58String()) | ||
} | ||
fmt.Fprintf(buf, "\tpartners [%d]\n", len(out.Peers)) | ||
for _, p := range out.Peers { | ||
fmt.Fprintf(buf, "\t\t%s\n", p) | ||
} | ||
return buf, nil | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package bitswap | ||
|
||
import ( | ||
u "github.com/jbenet/go-ipfs/util" | ||
"sort" | ||
) | ||
|
||
type Stat struct { | ||
ProvideBufLen int | ||
Wantlist []u.Key | ||
Peers []string | ||
} | ||
|
||
func (bs *Bitswap) Stat() (*Stat, error) { | ||
st := new(Stat) | ||
st.ProvideBufLen = len(bs.newBlocks) | ||
st.Wantlist = bs.GetWantlist() | ||
|
||
for _, p := range bs.engine.Peers() { | ||
st.Peers = append(st.Peers, p.Pretty()) | ||
} | ||
sort.Strings(st.Peers) | ||
|
||
return st, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters