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:
@ -4,7 +4,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
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"
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
|
||||||
|
|
||||||
notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
|
notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
|
||||||
@ -158,14 +157,14 @@ func (bs *BitSwap) handleMessages() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case mes := <-bs.meschan.Incoming:
|
case mes := <-bs.meschan.Incoming:
|
||||||
pmes := new(PBMessage)
|
bsmsg, err := FromSwarm(*mes)
|
||||||
err := proto.Unmarshal(mes.Data, pmes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
u.PErr("%v\n", err)
|
u.PErr("%v\n", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if pmes.Blocks != nil {
|
|
||||||
for _, blkData := range pmes.Blocks {
|
if bsmsg.Blocks() != nil {
|
||||||
|
for _, blkData := range bsmsg.Blocks() {
|
||||||
blk, err := blocks.NewBlock(blkData)
|
blk, err := blocks.NewBlock(blkData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
u.PErr("%v\n", err)
|
u.PErr("%v\n", err)
|
||||||
@ -175,8 +174,8 @@ func (bs *BitSwap) handleMessages() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if pmes.Wantlist != nil {
|
if bsmsg.Wantlist() != nil {
|
||||||
for _, want := range pmes.Wantlist {
|
for _, want := range bsmsg.Wantlist() {
|
||||||
go bs.peerWantsBlock(mes.Peer, want)
|
go bs.peerWantsBlock(mes.Peer, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BitSwapMessage interface {
|
type BitSwapMessage interface {
|
||||||
|
Wantlist() []string
|
||||||
|
Blocks() [][]byte
|
||||||
AppendWanted(k u.Key)
|
AppendWanted(k u.Key)
|
||||||
AppendBlock(b *blocks.Block)
|
AppendBlock(b *blocks.Block)
|
||||||
Exportable
|
Exportable
|
||||||
@ -35,6 +37,16 @@ func newMessage() *message {
|
|||||||
return new(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) {
|
func (m *message) AppendWanted(k u.Key) {
|
||||||
m.pb.Wantlist = append(m.pb.Wantlist, string(k))
|
m.pb.Wantlist = append(m.pb.Wantlist, string(k))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user