Commit c0ab7e46 authored by Brian Tiger Chow's avatar Brian Tiger Chow

refac(bitswap) let adapter be created with nil delegate

yay deleting code.
parent 978a60f7
...@@ -31,16 +31,15 @@ type Routing interface { ...@@ -31,16 +31,15 @@ type Routing interface {
// NewSession initializes a bitswap session. // NewSession initializes a bitswap session.
func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, directory Routing) exchange.Interface { func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, directory Routing) exchange.Interface {
// FIXME(brian): instantiate a concrete Strategist adapter := bsnet.NewNetworkAdapter(s, nil)
receiver := bsnet.Forwarder{}
bs := &bitswap{ bs := &bitswap{
blockstore: blockstore.NewBlockstore(d), blockstore: blockstore.NewBlockstore(d),
notifications: notifications.New(), notifications: notifications.New(),
strategy: strategy.New(), strategy: strategy.New(),
routing: directory, routing: directory,
sender: bsnet.NewNetworkAdapter(s, &receiver), sender: adapter,
} }
receiver.Delegate(bs) adapter.SetDelegate(bs)
return bs return bs
} }
......
package network
import (
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"
peer "github.com/jbenet/go-ipfs/peer"
)
// Forwarder receives messages and forwards them to the delegate.
//
// Forwarder breaks the circular dependency between the BitSwap Session and the
// Network Service.
type Forwarder struct {
delegate Receiver
}
func (r *Forwarder) ReceiveMessage(
ctx context.Context, sender *peer.Peer, incoming bsmsg.BitSwapMessage) (
*peer.Peer, bsmsg.BitSwapMessage, error) {
if r.delegate == nil {
return nil, nil, nil
}
return r.delegate.ReceiveMessage(ctx, sender, incoming)
}
func (r *Forwarder) Delegate(delegate Receiver) {
r.delegate = delegate
}
package network
import (
"testing"
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"
peer "github.com/jbenet/go-ipfs/peer"
)
func TestDoesntPanicIfDelegateNotPresent(t *testing.T) {
fwdr := Forwarder{}
fwdr.ReceiveMessage(context.Background(), &peer.Peer{}, bsmsg.New())
}
func TestForwardsMessageToDelegate(t *testing.T) {
fwdr := Forwarder{delegate: &EchoDelegate{}}
fwdr.ReceiveMessage(context.Background(), &peer.Peer{}, bsmsg.New())
}
type EchoDelegate struct{}
func (d *EchoDelegate) ReceiveMessage(ctx context.Context, p *peer.Peer,
incoming bsmsg.BitSwapMessage) (*peer.Peer, bsmsg.BitSwapMessage, error) {
return p, incoming, nil
}
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