Skip to content

Commit

Permalink
refactor(bitswap:message) add wrapper for proto
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Tiger Chow committed Sep 11, 2014
1 parent c35a8d0 commit 367217b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 12 deletions.
21 changes: 9 additions & 12 deletions bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,12 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) ([]byte, error) {
u.DOut("[%s] getBlock '%s' from [%s]\n", bs.peer.ID.Pretty(), k.Pretty(), p.ID.Pretty())

pmes := new(PBMessage)
pmes.Wantlist = []string{string(k)}
message := newMessage()
message.AppendWanted(k)

after := time.After(timeout)
resp := bs.listener.Listen(string(k), 1, timeout)
smes := swarm.NewMessage(p, pmes)
bs.meschan.Outgoing <- smes
bs.meschan.Outgoing <- message.ToSwarm(p)

select {
case resp_mes := <-resp:
Expand Down Expand Up @@ -149,11 +148,9 @@ func (bs *BitSwap) HaveBlock(blk *blocks.Block) error {
}

func (bs *BitSwap) SendBlock(p *peer.Peer, b *blocks.Block) {
pmes := new(PBMessage)
pmes.Blocks = [][]byte{b.Data}

swarm_mes := swarm.NewMessage(p, pmes)
bs.meschan.Outgoing <- swarm_mes
message := newMessage()
message.AppendBlock(b)
bs.meschan.Outgoing <- message.ToSwarm(p)
}

func (bs *BitSwap) handleMessages() {
Expand Down Expand Up @@ -257,14 +254,14 @@ func (bs *BitSwap) GetLedger(p *peer.Peer) *Ledger {
}

func (bs *BitSwap) SendWantList(wl KeySet) error {
pmes := new(PBMessage)
message := newMessage()
for k, _ := range wl {
pmes.Wantlist = append(pmes.Wantlist, string(k))
message.AppendWanted(k)
}

// Lets just ping everybody all at once
for _, ledger := range bs.partners {
bs.meschan.Outgoing <- swarm.NewMessage(ledger.Partner, pmes)
bs.meschan.Outgoing <- message.ToSwarm(ledger.Partner)
}

return nil
Expand Down
38 changes: 38 additions & 0 deletions bitswap/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package bitswap

import (
blocks "github.com/jbenet/go-ipfs/blocks"
peer "github.com/jbenet/go-ipfs/peer"
swarm "github.com/jbenet/go-ipfs/swarm"
u "github.com/jbenet/go-ipfs/util"
)

// message wraps a proto message for convenience
type message struct {
pb PBMessage
}

func newMessageFromProto(pb PBMessage) *message {
return &message{pb: pb}
}

func newMessage() *message {
return new(message)
}

func (m *message) AppendWanted(k u.Key) {
m.pb.Wantlist = append(m.pb.Wantlist, string(k))
}

func (m *message) AppendBlock(b *blocks.Block) {
m.pb.Blocks = append(m.pb.Blocks, b.Data)
}

func (m *message) ToProto() *PBMessage {
cp := m.pb
return &cp
}

func (m *message) ToSwarm(p *peer.Peer) *swarm.Message {
return swarm.NewMessage(p, m.ToProto())
}
75 changes: 75 additions & 0 deletions bitswap/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package bitswap

import (
"bytes"
"testing"

"github.com/jbenet/go-ipfs/blocks"
u "github.com/jbenet/go-ipfs/util"
)

func TestAppendWanted(t *testing.T) {
const str = "foo"
m := newMessage()
m.AppendWanted(u.Key(str))

if !contains(m.ToProto().GetWantlist(), str) {
t.Fail()
}
}

func TestNewMessageFromProto(t *testing.T) {
const str = "a_key"
protoMessage := new(PBMessage)
protoMessage.Wantlist = []string{string(str)}
if !contains(protoMessage.Wantlist, str) {
t.Fail()
}
m := newMessageFromProto(*protoMessage)
if !contains(m.ToProto().GetWantlist(), str) {
t.Fail()
}
}

func TestAppendBlock(t *testing.T) {

strs := make([]string, 2)
strs = append(strs, "Celeritas")
strs = append(strs, "Incendia")

m := newMessage()
for _, str := range strs {
block, err := blocks.NewBlock([]byte(str))
if err != nil {
t.Fail()
}
m.AppendBlock(block)
}

// assert strings are in proto message
for _, blockbytes := range m.ToProto().GetBlocks() {
s := bytes.NewBuffer(blockbytes).String()
if !contains(strs, s) {
t.Fail()
}
}
}

func TestCopyProtoByValue(t *testing.T) {
const str = "foo"
m := newMessage()
protoBeforeAppend := m.ToProto()
m.AppendWanted(u.Key(str))
if contains(protoBeforeAppend.GetWantlist(), str) {
t.Fail()
}
}

func contains(s []string, x string) bool {
for _, a := range s {
if a == x {
return true
}
}
return false
}

0 comments on commit 367217b

Please sign in to comment.