Commit bd392b81 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

clean up and add inet.Network to bitswap

new Service interface
parent 866f2538
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network" bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications" notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy" strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
inet "github.com/jbenet/go-ipfs/net"
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"
) )
...@@ -19,14 +20,17 @@ var log = u.Logger("bitswap") ...@@ -19,14 +20,17 @@ var log = u.Logger("bitswap")
// NetMessageSession initializes a BitSwap session that communicates over the // NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service // provided NetMessage service
func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore, nice bool) exchange.Interface { func NetMessageSession(parent context.Context, p *peer.Peer,
net inet.Network, srv inet.Service, directory bsnet.Routing,
d ds.Datastore, nice bool) exchange.Interface {
networkAdapter := bsnet.NetMessageAdapter(s, nil) networkAdapter := bsnet.NetMessageAdapter(srv, nil)
bs := &bitswap{ bs := &bitswap{
blockstore: blockstore.NewBlockstore(d), blockstore: blockstore.NewBlockstore(d),
notifications: notifications.New(), notifications: notifications.New(),
strategy: strategy.New(nice), strategy: strategy.New(nice),
routing: directory, routing: directory,
network: net,
sender: networkAdapter, sender: networkAdapter,
wantlist: u.NewKeySet(), wantlist: u.NewKeySet(),
} }
...@@ -38,6 +42,9 @@ func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageS ...@@ -38,6 +42,9 @@ func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageS
// bitswap instances implement the bitswap protocol. // bitswap instances implement the bitswap protocol.
type bitswap struct { type bitswap struct {
// network maintains connections to the outside world.
network inet.Network
// sender delivers messages on behalf of the session // sender delivers messages on behalf of the session
sender bsnet.Adapter sender bsnet.Adapter
...@@ -79,7 +86,7 @@ func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error) ...@@ -79,7 +86,7 @@ func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error)
} }
message.AppendWanted(k) message.AppendWanted(k)
for iiiii := range peersToQuery { for iiiii := range peersToQuery {
// log.Debug("bitswap got peersToQuery: %s", iiiii) log.Debug("bitswap got peersToQuery: %s", iiiii)
go func(p *peer.Peer) { go func(p *peer.Peer) {
response, err := bs.sender.SendRequest(ctx, p, message) response, err := bs.sender.SendRequest(ctx, p, message)
if err != nil { if err != nil {
......
...@@ -2,10 +2,8 @@ package network ...@@ -2,10 +2,8 @@ package network
import ( 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"
netservice "github.com/jbenet/go-ipfs/net/service"
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message" bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
netmsg "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"
) )
...@@ -38,13 +36,6 @@ type Receiver interface { ...@@ -38,13 +36,6 @@ type Receiver interface {
ReceiveError(error) ReceiveError(error)
} }
// TODO(brian): move this to go-ipfs/net package
type NetMessageService interface {
SendRequest(ctx context.Context, m netmsg.NetMessage) (netmsg.NetMessage, error)
SendMessage(ctx context.Context, m netmsg.NetMessage) error
SetHandler(netservice.Handler)
}
// TODO rename -> Router? // TODO rename -> Router?
type Routing interface { type Routing interface {
// FindProvidersAsync returns a channel of providers for the given key // FindProvidersAsync returns a channel of providers for the given key
......
...@@ -4,12 +4,13 @@ import ( ...@@ -4,12 +4,13 @@ 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"
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message" bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
inet "github.com/jbenet/go-ipfs/net"
netmsg "github.com/jbenet/go-ipfs/net/message" netmsg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
) )
// NetMessageAdapter wraps a NetMessage network service // NetMessageAdapter wraps a NetMessage network service
func NetMessageAdapter(s NetMessageService, r Receiver) Adapter { func NetMessageAdapter(s inet.Service, r Receiver) Adapter {
adapter := impl{ adapter := impl{
nms: s, nms: s,
receiver: r, receiver: r,
...@@ -20,7 +21,7 @@ func NetMessageAdapter(s NetMessageService, r Receiver) Adapter { ...@@ -20,7 +21,7 @@ func NetMessageAdapter(s NetMessageService, r Receiver) Adapter {
// implements an Adapter that integrates with a NetMessage network service // implements an Adapter that integrates with a NetMessage network service
type impl struct { type impl struct {
nms NetMessageService nms inet.Service
// inbound messages from the network are forwarded to the receiver // inbound messages from the network are forwarded to the receiver
receiver Receiver receiver Receiver
......
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