refactor(bitswap:msg) move to package
Brian Tiger Chow committed
Sep 12, 2014 at 20:40 UTC
b8bd2bc4553bfb7f210ef57bdc1b09c879394731
5 files changed
+13
-12
bitswap/bitswap.go
+5
-4
@@ -6,6 +6,7 @@ import (
6
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
8
9
+ bsmsg "github.com/jbenet/go-ipfs/bitswap/message"
10
notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
11
blocks "github.com/jbenet/go-ipfs/blocks"
12
swarm "github.com/jbenet/go-ipfs/net/swarm"
@@ -119,7 +120,7 @@ func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*bloc
120
ctx, _ := context.WithTimeout(context.Background(), timeout)
121
blockChannel := bs.notifications.Subscribe(ctx, k)
122
122
- message := newMessage()
123
+ message := bsmsg.New()
124
message.AppendWanted(k)
125
bs.meschan.Outgoing <- message.ToSwarm(p)
126
@@ -148,7 +149,7 @@ func (bs *BitSwap) HaveBlock(blk *blocks.Block) error {
149
}
150
151
func (bs *BitSwap) SendBlock(p *peer.Peer, b *blocks.Block) {
151
- message := newMessage()
152
+ message := bsmsg.New()
153
message.AppendBlock(b)
154
bs.meschan.Outgoing <- message.ToSwarm(p)
155
}
@@ -157,7 +158,7 @@ func (bs *BitSwap) handleMessages() {
158
for {
159
select {
160
case mes := <-bs.meschan.Incoming:
160
- bsmsg, err := FromSwarm(*mes)
161
+ bsmsg, err := bsmsg.FromSwarm(*mes)
162
if err != nil {
163
u.PErr("%v\n", err)
164
continue
@@ -250,7 +251,7 @@ func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger {
251
}
252
253
func (bs *BitSwap) SendWantList(wl KeySet) error {
253
- message := newMessage()
254
+ message := bsmsg.New()
255
for k, _ := range wl {
256
message.AppendWanted(k)
257
}
bitswap/message/message.go
renamed
+2
-2
@@ -1,4 +1,4 @@
1
-package bitswap
1
+package message
2
3
import (
4
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
@@ -33,7 +33,7 @@ func newMessageFromProto(pb PBMessage) *message {
33
return &message{pb: pb}
34
}
35
36
-func newMessage() *message {
36
+func New() *message {
37
return new(message)
38
}
39
bitswap/message/message.pb.go
renamed
+1
-1
@@ -11,7 +11,7 @@ It is generated from these files:
11
It has these top-level messages:
12
PBMessage
13
*/
14
-package bitswap
14
+package message
15
16
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
17
import math "math"
bitswap/message/message.proto
renamed
+1
-1
@@ -1,4 +1,4 @@
1
-package bitswap;
1
+package message;
2
3
message PBMessage {
4
repeated string wantlist = 1;
bitswap/message/message_test.go
renamed
+4
-4
@@ -1,4 +1,4 @@
1
-package bitswap
1
+package message
2
3
import (
4
"bytes"
@@ -10,7 +10,7 @@ import (
10
11
func TestAppendWanted(t *testing.T) {
12
const str = "foo"
13
- m := newMessage()
13
+ m := New()
14
m.AppendWanted(u.Key(str))
15
16
if !contains(m.ToProto().GetWantlist(), str) {
@@ -37,7 +37,7 @@ func TestAppendBlock(t *testing.T) {
37
strs = append(strs, "Celeritas")
38
strs = append(strs, "Incendia")
39
40
- m := newMessage()
40
+ m := New()
41
for _, str := range strs {
42
block, err := blocks.NewBlock([]byte(str))
43
if err != nil {
@@ -57,7 +57,7 @@ func TestAppendBlock(t *testing.T) {
57
58
func TestCopyProtoByValue(t *testing.T) {
59
const str = "foo"
60
- m := newMessage()
60
+ m := New()
61
protoBeforeAppend := m.ToProto()
62
m.AppendWanted(u.Key(str))
63
if contains(protoBeforeAppend.GetWantlist(), str) {