1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 09:59:13 +08:00

refactor(bitswap:msg) add, use getters

This commit is contained in:
Brian Tiger Chow
2014-09-12 19:25:13 -07:00
parent 21639564e7
commit 282acb8f26
2 changed files with 18 additions and 7 deletions

View File

@ -4,7 +4,6 @@ import (
"time"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
@ -158,14 +157,14 @@ func (bs *BitSwap) handleMessages() {
for {
select {
case mes := <-bs.meschan.Incoming:
pmes := new(PBMessage)
err := proto.Unmarshal(mes.Data, pmes)
bsmsg, err := FromSwarm(*mes)
if err != nil {
u.PErr("%v\n", err)
continue
}
if pmes.Blocks != nil {
for _, blkData := range pmes.Blocks {
if bsmsg.Blocks() != nil {
for _, blkData := range bsmsg.Blocks() {
blk, err := blocks.NewBlock(blkData)
if err != nil {
u.PErr("%v\n", err)
@ -175,8 +174,8 @@ func (bs *BitSwap) handleMessages() {
}
}
if pmes.Wantlist != nil {
for _, want := range pmes.Wantlist {
if bsmsg.Wantlist() != nil {
for _, want := range bsmsg.Wantlist() {
go bs.peerWantsBlock(mes.Peer, want)
}
}

View File

@ -11,6 +11,8 @@ import (
)
type BitSwapMessage interface {
Wantlist() []string
Blocks() [][]byte
AppendWanted(k u.Key)
AppendBlock(b *blocks.Block)
Exportable
@ -35,6 +37,16 @@ func newMessage() *message {
return new(message)
}
// TODO(brian): convert these into keys
func (m *message) Wantlist() []string {
return m.pb.Wantlist
}
// TODO(brian): convert these into blocks
func (m *message) Blocks() [][]byte {
return m.pb.Blocks
}
func (m *message) AppendWanted(k u.Key) {
m.pb.Wantlist = append(m.pb.Wantlist, string(k))
}