Commit c5333a20 authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

refactor(bitswap/network) rename -> BitSwapNetwork

remove 'adapter' concept
instead, describe the component as the bitswap network

it's still an adapter, but it's just not necessary to describe it as
such
parent 6a64affb
...@@ -37,7 +37,7 @@ func NetMessageSession(ctx context.Context, p peer.Peer, ...@@ -37,7 +37,7 @@ func NetMessageSession(ctx context.Context, p peer.Peer,
} }
}() }()
network := bsnet.NetMessageAdapter(srv, net, nil) network := bsnet.NewFromIpfsNetwork(srv, net)
bs := &bitswap{ bs := &bitswap{
blockstore: blockstore.NewBlockstore(d), blockstore: blockstore.NewBlockstore(d),
......
...@@ -4,31 +4,32 @@ import ( ...@@ -4,31 +4,32 @@ import (
"errors" "errors"
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"
"github.com/jbenet/go-ipfs/util"
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" 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"
util "github.com/jbenet/go-ipfs/util"
) )
var log = util.Logger("net_message_adapter") var log = util.Logger("bitswap_network")
// NetMessageAdapter wraps a NetMessage network service // NewFromIpfsNetwork returns a BitSwapNetwork supported by underlying IPFS
func NetMessageAdapter(s inet.Service, n inet.Network, r Receiver) BitSwapNetwork { // Network & Service
adapter := impl{ func NewFromIpfsNetwork(s inet.Service, n inet.Network) BitSwapNetwork {
nms: s, bitswapNetwork := impl{
net: n, service: s,
receiver: r, net: n,
} }
s.SetHandler(&adapter) s.SetHandler(&bitswapNetwork)
return &adapter return &bitswapNetwork
} }
// implements an Adapter that integrates with a NetMessage network service // impl transforms the ipfs network interface, which sends and receives
// NetMessage objects, into the bitswap network interface.
type impl struct { type impl struct {
nms inet.Service service inet.Service
net inet.Network net inet.Network
// inbound messages from the network are forwarded to the receiver // inbound messages from the network are forwarded to the receiver
receiver Receiver receiver Receiver
...@@ -36,30 +37,30 @@ type impl struct { ...@@ -36,30 +37,30 @@ type impl struct {
// HandleMessage marshals and unmarshals net messages, forwarding them to the // HandleMessage marshals and unmarshals net messages, forwarding them to the
// BitSwapMessage receiver // BitSwapMessage receiver
func (adapter *impl) HandleMessage( func (bsnet *impl) HandleMessage(
ctx context.Context, incoming netmsg.NetMessage) netmsg.NetMessage { ctx context.Context, incoming netmsg.NetMessage) netmsg.NetMessage {
if adapter.receiver == nil { if bsnet.receiver == nil {
return nil return nil
} }
received, err := bsmsg.FromNet(incoming) received, err := bsmsg.FromNet(incoming)
if err != nil { if err != nil {
go adapter.receiver.ReceiveError(err) go bsnet.receiver.ReceiveError(err)
return nil return nil
} }
p, bsmsg := adapter.receiver.ReceiveMessage(ctx, incoming.Peer(), received) p, bsmsg := bsnet.receiver.ReceiveMessage(ctx, incoming.Peer(), received)
// TODO(brian): put this in a helper function // TODO(brian): put this in a helper function
if bsmsg == nil || p == nil { if bsmsg == nil || p == nil {
adapter.receiver.ReceiveError(errors.New("ReceiveMessage returned nil peer or message")) bsnet.receiver.ReceiveError(errors.New("ReceiveMessage returned nil peer or message"))
return nil return nil
} }
outgoing, err := bsmsg.ToNet(p) outgoing, err := bsmsg.ToNet(p)
if err != nil { if err != nil {
go adapter.receiver.ReceiveError(err) go bsnet.receiver.ReceiveError(err)
return nil return nil
} }
...@@ -71,7 +72,7 @@ func (adapter *impl) DialPeer(ctx context.Context, p peer.Peer) error { ...@@ -71,7 +72,7 @@ func (adapter *impl) DialPeer(ctx context.Context, p peer.Peer) error {
return adapter.net.DialPeer(ctx, p) return adapter.net.DialPeer(ctx, p)
} }
func (adapter *impl) SendMessage( func (bsnet *impl) SendMessage(
ctx context.Context, ctx context.Context,
p peer.Peer, p peer.Peer,
outgoing bsmsg.BitSwapMessage) error { outgoing bsmsg.BitSwapMessage) error {
...@@ -80,10 +81,10 @@ func (adapter *impl) SendMessage( ...@@ -80,10 +81,10 @@ func (adapter *impl) SendMessage(
if err != nil { if err != nil {
return err return err
} }
return adapter.nms.SendMessage(ctx, nmsg) return bsnet.service.SendMessage(ctx, nmsg)
} }
func (adapter *impl) SendRequest( func (bsnet *impl) SendRequest(
ctx context.Context, ctx context.Context,
p peer.Peer, p peer.Peer,
outgoing bsmsg.BitSwapMessage) (bsmsg.BitSwapMessage, error) { outgoing bsmsg.BitSwapMessage) (bsmsg.BitSwapMessage, error) {
...@@ -92,13 +93,13 @@ func (adapter *impl) SendRequest( ...@@ -92,13 +93,13 @@ func (adapter *impl) SendRequest(
if err != nil { if err != nil {
return nil, err return nil, err
} }
incomingMsg, err := adapter.nms.SendRequest(ctx, outgoingMsg) incomingMsg, err := bsnet.service.SendRequest(ctx, outgoingMsg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return bsmsg.FromNet(incomingMsg) return bsmsg.FromNet(incomingMsg)
} }
func (adapter *impl) SetDelegate(r Receiver) { func (bsnet *impl) SetDelegate(r Receiver) {
adapter.receiver = r bsnet.receiver = r
} }
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