mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
refactor(bitswap:msg) move to package
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
|||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
|
||||||
|
|
||||||
|
bsmsg "github.com/jbenet/go-ipfs/bitswap/message"
|
||||||
notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
|
notifications "github.com/jbenet/go-ipfs/bitswap/notifications"
|
||||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||||
swarm "github.com/jbenet/go-ipfs/net/swarm"
|
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
|
|||||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||||
blockChannel := bs.notifications.Subscribe(ctx, k)
|
blockChannel := bs.notifications.Subscribe(ctx, k)
|
||||||
|
|
||||||
message := newMessage()
|
message := bsmsg.New()
|
||||||
message.AppendWanted(k)
|
message.AppendWanted(k)
|
||||||
bs.meschan.Outgoing <- message.ToSwarm(p)
|
bs.meschan.Outgoing <- message.ToSwarm(p)
|
||||||
|
|
||||||
@ -148,7 +149,7 @@ func (bs *BitSwap) HaveBlock(blk *blocks.Block) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BitSwap) SendBlock(p *peer.Peer, b *blocks.Block) {
|
func (bs *BitSwap) SendBlock(p *peer.Peer, b *blocks.Block) {
|
||||||
message := newMessage()
|
message := bsmsg.New()
|
||||||
message.AppendBlock(b)
|
message.AppendBlock(b)
|
||||||
bs.meschan.Outgoing <- message.ToSwarm(p)
|
bs.meschan.Outgoing <- message.ToSwarm(p)
|
||||||
}
|
}
|
||||||
@ -157,7 +158,7 @@ func (bs *BitSwap) handleMessages() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case mes := <-bs.meschan.Incoming:
|
case mes := <-bs.meschan.Incoming:
|
||||||
bsmsg, err := FromSwarm(*mes)
|
bsmsg, err := bsmsg.FromSwarm(*mes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
u.PErr("%v\n", err)
|
u.PErr("%v\n", err)
|
||||||
continue
|
continue
|
||||||
@ -250,7 +251,7 @@ func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BitSwap) SendWantList(wl KeySet) error {
|
func (bs *BitSwap) SendWantList(wl KeySet) error {
|
||||||
message := newMessage()
|
message := bsmsg.New()
|
||||||
for k, _ := range wl {
|
for k, _ := range wl {
|
||||||
message.AppendWanted(k)
|
message.AppendWanted(k)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package bitswap
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
||||||
@ -33,7 +33,7 @@ func newMessageFromProto(pb PBMessage) *message {
|
|||||||
return &message{pb: pb}
|
return &message{pb: pb}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMessage() *message {
|
func New() *message {
|
||||||
return new(message)
|
return new(message)
|
||||||
}
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ It is generated from these files:
|
|||||||
It has these top-level messages:
|
It has these top-level messages:
|
||||||
PBMessage
|
PBMessage
|
||||||
*/
|
*/
|
||||||
package bitswap
|
package message
|
||||||
|
|
||||||
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
||||||
import math "math"
|
import math "math"
|
@ -1,4 +1,4 @@
|
|||||||
package bitswap;
|
package message;
|
||||||
|
|
||||||
message PBMessage {
|
message PBMessage {
|
||||||
repeated string wantlist = 1;
|
repeated string wantlist = 1;
|
@ -1,4 +1,4 @@
|
|||||||
package bitswap
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func TestAppendWanted(t *testing.T) {
|
func TestAppendWanted(t *testing.T) {
|
||||||
const str = "foo"
|
const str = "foo"
|
||||||
m := newMessage()
|
m := New()
|
||||||
m.AppendWanted(u.Key(str))
|
m.AppendWanted(u.Key(str))
|
||||||
|
|
||||||
if !contains(m.ToProto().GetWantlist(), str) {
|
if !contains(m.ToProto().GetWantlist(), str) {
|
||||||
@ -37,7 +37,7 @@ func TestAppendBlock(t *testing.T) {
|
|||||||
strs = append(strs, "Celeritas")
|
strs = append(strs, "Celeritas")
|
||||||
strs = append(strs, "Incendia")
|
strs = append(strs, "Incendia")
|
||||||
|
|
||||||
m := newMessage()
|
m := New()
|
||||||
for _, str := range strs {
|
for _, str := range strs {
|
||||||
block, err := blocks.NewBlock([]byte(str))
|
block, err := blocks.NewBlock([]byte(str))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,7 +57,7 @@ func TestAppendBlock(t *testing.T) {
|
|||||||
|
|
||||||
func TestCopyProtoByValue(t *testing.T) {
|
func TestCopyProtoByValue(t *testing.T) {
|
||||||
const str = "foo"
|
const str = "foo"
|
||||||
m := newMessage()
|
m := New()
|
||||||
protoBeforeAppend := m.ToProto()
|
protoBeforeAppend := m.ToProto()
|
||||||
m.AppendWanted(u.Key(str))
|
m.AppendWanted(u.Key(str))
|
||||||
if contains(protoBeforeAppend.GetWantlist(), str) {
|
if contains(protoBeforeAppend.GetWantlist(), str) {
|
Reference in New Issue
Block a user