@cryptotaxi247 / kubo / commits / 282acb8f2

refactor(bitswap:msg) add, use getters

Brian Tiger Chow committed Sep 12, 2014 at 19:25 UTC 282acb8f2669a2596006ab5d12f5a5226f9f24f4
2 files changed +18 -7
bitswap/bitswap.go
+6 -7
@@ -4,7 +4,6 @@ import (
4 "time"
5
6 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 - proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
7 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
8
9 notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
@@ -158,14 +157,14 @@ func (bs *BitSwap) handleMessages() {
157 for {
158 select {
159 case mes := <-bs.meschan.Incoming:
161 - pmes := new(PBMessage)
162 - err := proto.Unmarshal(mes.Data, pmes)
160 + bsmsg, err := FromSwarm(*mes)
161 if err != nil {
162 u.PErr("%v\n", err)
163 continue
164 }
167 - if pmes.Blocks != nil {
168 - for _, blkData := range pmes.Blocks {
165 +
166 + if bsmsg.Blocks() != nil {
167 + for _, blkData := range bsmsg.Blocks() {
168 blk, err := blocks.NewBlock(blkData)
169 if err != nil {
170 u.PErr("%v\n", err)
@@ -175,8 +174,8 @@ func (bs *BitSwap) handleMessages() {
174 }
175 }
176
178 - if pmes.Wantlist != nil {
179 - for _, want := range pmes.Wantlist {
177 + if bsmsg.Wantlist() != nil {
178 + for _, want := range bsmsg.Wantlist() {
179 go bs.peerWantsBlock(mes.Peer, want)
180 }
181 }
bitswap/message.go
+12
@@ -11,6 +11,8 @@ import (
11 )
12
13 type BitSwapMessage interface {
14 + Wantlist() []string
15 + Blocks() [][]byte
16 AppendWanted(k u.Key)
17 AppendBlock(b *blocks.Block)
18 Exportable
@@ -35,6 +37,16 @@ func newMessage() *message {
37 return new(message)
38 }
39
40 +// TODO(brian): convert these into keys
41 +func (m *message) Wantlist() []string {
42 + return m.pb.Wantlist
43 +}
44 +
45 +// TODO(brian): convert these into blocks
46 +func (m *message) Blocks() [][]byte {
47 + return m.pb.Blocks
48 +}
49 +
50 func (m *message) AppendWanted(k u.Key) {
51 m.pb.Wantlist = append(m.pb.Wantlist, string(k))
52 }