github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/commands/bitswap.go (about) 1 package commands 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 8 cmds "github.com/ipfs/go-ipfs/commands" 9 bitswap "github.com/ipfs/go-ipfs/exchange/bitswap" 10 peer "github.com/ipfs/go-ipfs/p2p/peer" 11 u "github.com/ipfs/go-ipfs/util" 12 ) 13 14 var BitswapCmd = &cmds.Command{ 15 Helptext: cmds.HelpText{ 16 Tagline: "A set of commands to manipulate the bitswap agent", 17 ShortDescription: ``, 18 }, 19 Subcommands: map[string]*cmds.Command{ 20 "wantlist": showWantlistCmd, 21 "stat": bitswapStatCmd, 22 }, 23 } 24 25 var showWantlistCmd = &cmds.Command{ 26 Helptext: cmds.HelpText{ 27 Tagline: "Show blocks currently on the wantlist", 28 ShortDescription: ` 29 Print out all blocks currently on the bitswap wantlist for the local peer`, 30 }, 31 Options: []cmds.Option{ 32 cmds.StringOption("peer", "p", "specify which peer to show wantlist for (default self)"), 33 }, 34 Type: KeyList{}, 35 Run: func(req cmds.Request, res cmds.Response) { 36 nd, err := req.InvocContext().GetNode() 37 if err != nil { 38 res.SetError(err, cmds.ErrNormal) 39 return 40 } 41 42 if !nd.OnlineMode() { 43 res.SetError(errNotOnline, cmds.ErrClient) 44 return 45 } 46 47 bs, ok := nd.Exchange.(*bitswap.Bitswap) 48 if !ok { 49 res.SetError(u.ErrCast(), cmds.ErrNormal) 50 return 51 } 52 53 pstr, found, err := req.Option("peer").String() 54 if err != nil { 55 res.SetError(err, cmds.ErrNormal) 56 return 57 } 58 if found { 59 pid, err := peer.IDB58Decode(pstr) 60 if err != nil { 61 res.SetError(err, cmds.ErrNormal) 62 return 63 } 64 res.SetOutput(&KeyList{bs.WantlistForPeer(pid)}) 65 } else { 66 res.SetOutput(&KeyList{bs.GetWantlist()}) 67 } 68 }, 69 Marshalers: cmds.MarshalerMap{ 70 cmds.Text: KeyListTextMarshaler, 71 }, 72 } 73 74 var bitswapStatCmd = &cmds.Command{ 75 Helptext: cmds.HelpText{ 76 Tagline: "show some diagnostic information on the bitswap agent", 77 ShortDescription: ``, 78 }, 79 Type: bitswap.Stat{}, 80 Run: func(req cmds.Request, res cmds.Response) { 81 nd, err := req.InvocContext().GetNode() 82 if err != nil { 83 res.SetError(err, cmds.ErrNormal) 84 return 85 } 86 87 if !nd.OnlineMode() { 88 res.SetError(errNotOnline, cmds.ErrClient) 89 return 90 } 91 92 bs, ok := nd.Exchange.(*bitswap.Bitswap) 93 if !ok { 94 res.SetError(u.ErrCast(), cmds.ErrNormal) 95 return 96 } 97 98 st, err := bs.Stat() 99 if err != nil { 100 res.SetError(err, cmds.ErrNormal) 101 return 102 } 103 104 res.SetOutput(st) 105 }, 106 Marshalers: cmds.MarshalerMap{ 107 cmds.Text: func(res cmds.Response) (io.Reader, error) { 108 out, ok := res.Output().(*bitswap.Stat) 109 if !ok { 110 return nil, u.ErrCast() 111 } 112 buf := new(bytes.Buffer) 113 fmt.Fprintln(buf, "bitswap status") 114 fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize) 115 fmt.Fprintf(buf, "\tblocks received: %d\n", out.BlocksReceived) 116 fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived) 117 fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist)) 118 for _, k := range out.Wantlist { 119 fmt.Fprintf(buf, "\t\t%s\n", k.B58String()) 120 } 121 fmt.Fprintf(buf, "\tpartners [%d]\n", len(out.Peers)) 122 for _, p := range out.Peers { 123 fmt.Fprintf(buf, "\t\t%s\n", p) 124 } 125 return buf, nil 126 }, 127 }, 128 }