Commit 49ab9018 authored by Jeromy's avatar Jeromy

swarm: refactor to make modular construction easier

parent aa0e1e6e
......@@ -10,6 +10,14 @@ import (
"sync"
"time"
metrics "github.com/libp2p/go-libp2p/p2p/metrics"
mconn "github.com/libp2p/go-libp2p/p2p/metrics/conn"
inet "github.com/libp2p/go-libp2p/p2p/net"
conn "github.com/libp2p/go-libp2p/p2p/net/conn"
filter "github.com/libp2p/go-libp2p/p2p/net/filter"
addrutil "github.com/libp2p/go-libp2p/p2p/net/swarm/addr"
ci "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
transport "github.com/ipfs/go-libp2p-transport"
......@@ -19,12 +27,6 @@ import (
pst "github.com/jbenet/go-stream-muxer"
"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
metrics "github.com/libp2p/go-libp2p/p2p/metrics"
mconn "github.com/libp2p/go-libp2p/p2p/metrics/conn"
inet "github.com/libp2p/go-libp2p/p2p/net"
conn "github.com/libp2p/go-libp2p/p2p/net/conn"
filter "github.com/libp2p/go-libp2p/p2p/net/filter"
addrutil "github.com/libp2p/go-libp2p/p2p/net/swarm/addr"
psmss "github.com/whyrusleeping/go-smux-multistream"
spdy "github.com/whyrusleeping/go-smux-spdystream"
yamux "github.com/whyrusleeping/go-smux-yamux"
......@@ -143,6 +145,31 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
return s, nil
}
func NewBlankSwarm(ctx context.Context, id peer.ID, privkey ci.PrivKey) *Swarm {
s := &Swarm{
swarm: ps.NewSwarm(PSTransport),
local: id,
peers: pstore.NewPeerstore(),
ctx: ctx,
dialT: DialTimeout,
notifs: make(map[inet.Notifiee]ps.Notifiee),
fdRateLimit: make(chan struct{}, concurrentFdDials),
Filters: filter.NewFilters(),
dialer: conn.NewDialer(id, privkey, nil),
}
// configure Swarm
s.limiter = newDialLimiter(s.dialAddr)
s.proc = goprocessctx.WithContextAndTeardown(ctx, s.teardown)
s.SetConnHandler(nil) // make sure to setup our own conn handler.
return s
}
func (s *Swarm) AddTransport(t transport.Transport) {
s.transports = append(s.transports, t)
}
func (s *Swarm) teardown() error {
return s.swarm.Close()
}
......
......@@ -13,37 +13,42 @@ import (
context "golang.org/x/net/context"
)
func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
tpt := s.transportForAddr(a)
if tpt == nil {
return fmt.Errorf("no transport for address: %s", a)
}
d, err := tpt.Dialer(a, transport.TimeoutOpt(DialTimeout), transport.ReusePorts)
if err != nil {
return err
}
s.dialer.AddDialer(d)
list, err := tpt.Listen(a)
if err != nil {
return err
}
err = s.addListener(list)
if err != nil {
return err
}
return nil
}
// Open listeners and reuse-dialers for the given addresses
func (s *Swarm) setupInterfaces(addrs []ma.Multiaddr) error {
errs := make([]error, len(addrs))
var succeeded int
for i, a := range addrs {
tpt := s.transportForAddr(a)
if tpt == nil {
errs[i] = fmt.Errorf("no transport for address: %s", a)
continue
}
d, err := tpt.Dialer(a, transport.TimeoutOpt(DialTimeout), transport.ReusePorts)
if err != nil {
errs[i] = err
continue
}
s.dialer.AddDialer(d)
list, err := tpt.Listen(a)
if err != nil {
if err := s.AddListenAddr(a); err != nil {
errs[i] = err
continue
} else {
succeeded++
}
err = s.addListener(list)
if err != nil {
errs[i] = err
continue
}
succeeded++
}
for i, e := range errs {
......@@ -51,6 +56,7 @@ func (s *Swarm) setupInterfaces(addrs []ma.Multiaddr) error {
log.Warning("listen on %s failed: %s", addrs[i], errs[i])
}
}
if succeeded == 0 && len(addrs) > 0 {
return fmt.Errorf("failed to listen on any addresses: %s", errs)
}
......@@ -83,7 +89,7 @@ func (s *Swarm) addListener(tptlist transport.Listener) error {
list.SetAddrFilters(s.Filters)
if cw, ok := list.(conn.ListenerConnWrapper); ok {
if cw, ok := list.(conn.ListenerConnWrapper); ok && s.bwc != nil {
cw.SetConnWrapper(func(c transport.Conn) transport.Conn {
return mconn.WrapConn(s.bwc, c)
})
......
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