Commit 1aaa88fa authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat(bitswap:message) implement FromNet

parent 27386c5c
package message package message
import ( import (
"errors" proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
netmsg "github.com/jbenet/go-ipfs/net/message"
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
netmsg "github.com/jbenet/go-ipfs/net/message"
nm "github.com/jbenet/go-ipfs/net/message" nm "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
...@@ -68,7 +66,15 @@ func (m *message) AppendBlock(b blocks.Block) { ...@@ -68,7 +66,15 @@ func (m *message) AppendBlock(b blocks.Block) {
} }
func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) { func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
return nil, errors.New("TODO implement") pb := new(PBMessage)
if err := proto.Unmarshal(nmsg.Data(), pb); err != nil {
return nil, err
}
m, err := newMessageFromProto(*pb)
if err != nil {
return nil, err
}
return m, nil
} }
func (m *message) ToProto() *PBMessage { func (m *message) ToProto() *PBMessage {
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"testing" "testing"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil" testutil "github.com/jbenet/go-ipfs/util/testutil"
) )
...@@ -88,6 +89,79 @@ func TestCopyProtoByValue(t *testing.T) { ...@@ -88,6 +89,79 @@ func TestCopyProtoByValue(t *testing.T) {
} }
} }
func TestToNetMethodSetsPeer(t *testing.T) {
m := New()
p := &peer.Peer{ID: []byte("X")}
netmsg, err := m.ToNet(p)
if err != nil {
t.Fatal(err)
}
if !(netmsg.Peer().Key() == p.Key()) {
t.Fatal("Peer key is different")
}
}
func TestToNetFromNetPreservesWantList(t *testing.T) {
original := New()
original.AppendWanted(u.Key("M"))
original.AppendWanted(u.Key("B"))
original.AppendWanted(u.Key("D"))
original.AppendWanted(u.Key("T"))
original.AppendWanted(u.Key("F"))
netmsg, err := original.ToNet(&peer.Peer{ID: []byte("X")})
if err != nil {
t.Fatal(err)
}
copied, err := FromNet(netmsg)
if err != nil {
t.Fatal(err)
}
keys := make(map[u.Key]bool)
for _, k := range copied.Wantlist() {
keys[k] = true
}
for _, k := range original.Wantlist() {
if _, ok := keys[k]; !ok {
t.Fatalf("Key Missing: \"%v\"", k)
}
}
}
func TestToAndFromNetMessage(t *testing.T) {
original := New()
original.AppendBlock(testutil.NewBlockOrFail(t, "W"))
original.AppendBlock(testutil.NewBlockOrFail(t, "E"))
original.AppendBlock(testutil.NewBlockOrFail(t, "F"))
original.AppendBlock(testutil.NewBlockOrFail(t, "M"))
p := &peer.Peer{ID: []byte("X")}
netmsg, err := original.ToNet(p)
if err != nil {
t.Fatal(err)
}
m2, err := FromNet(netmsg)
if err != nil {
t.Fatal(err)
}
keys := make(map[u.Key]bool)
for _, b := range m2.Blocks() {
keys[b.Key()] = true
}
for _, b := range original.Blocks() {
if _, ok := keys[b.Key()]; !ok {
t.Fail()
}
}
}
func contains(s []string, x string) bool { func contains(s []string, x string) bool {
for _, a := range s { for _, a := range s {
if a == x { if a == x {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment