From 559a24156677e0f4899b71c6f04f2c42657b5a58 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 2 Feb 2015 02:48:12 +0000 Subject: [PATCH] implement a simple wantlist command to allow the user to view their wantlist --- core/commands/root.go | 1 + core/commands/wantlist.go | 34 ++++++++++++++++++++++++++++++++++ exchange/bitswap/bitswap.go | 8 ++++++++ exchange/interface.go | 2 ++ exchange/offline/offline.go | 5 +++++ 5 files changed, 50 insertions(+) create mode 100644 core/commands/wantlist.go diff --git a/core/commands/root.go b/core/commands/root.go index 0ab07fecb..8040a2434 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -98,6 +98,7 @@ var rootSubcommands = map[string]*cmds.Command{ "swarm": SwarmCmd, "update": UpdateCmd, "version": VersionCmd, + "wantlist": WantlistCmd, } func init() { diff --git a/core/commands/wantlist.go b/core/commands/wantlist.go new file mode 100644 index 000000000..35c7f0a71 --- /dev/null +++ b/core/commands/wantlist.go @@ -0,0 +1,34 @@ +package commands + +import cmds "github.com/jbenet/go-ipfs/commands" + +var WantlistCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "A set of commands to work with the bitswap wantlist", + ShortDescription: ``, + }, + Subcommands: map[string]*cmds.Command{ + "show": showWantlistCmd, + }, +} + +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 + } + + res.SetOutput(&KeyList{nd.Exchange.GetWantlist()}) + }, + Marshalers: cmds.MarshalerMap{ + cmds.Text: KeyListTextMarshaler, + }, +} diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index 5508f66e3..1101ffd9b 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -402,3 +402,11 @@ func (bs *bitswap) send(ctx context.Context, p peer.ID, m bsmsg.BitSwapMessage) func (bs *bitswap) Close() error { return bs.process.Close() } + +func (bs *bitswap) GetWantlist() []u.Key { + var out []u.Key + for _, e := range bs.wantlist.Entries() { + out = append(out, e.Key) + } + return out +} diff --git a/exchange/interface.go b/exchange/interface.go index c07d2a471..8a4189ee8 100644 --- a/exchange/interface.go +++ b/exchange/interface.go @@ -21,5 +21,7 @@ type Interface interface { // available on the network? HasBlock(context.Context, *blocks.Block) error + GetWantlist() []u.Key + io.Closer } diff --git a/exchange/offline/offline.go b/exchange/offline/offline.go index fe8fa723c..e3e50bbf4 100644 --- a/exchange/offline/offline.go +++ b/exchange/offline/offline.go @@ -66,3 +66,8 @@ func (e *offlineExchange) GetBlocks(ctx context.Context, ks []u.Key) (<-chan *bl }() return out, nil } + +// implement Exchange +func (e *offlineExchange) GetWantlist() []u.Key { + return nil +}