Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bitswap:message) add wrapper for proto #41

Merged
merged 1 commit into from
Sep 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

blocks "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
}