From a56440a9c77627a1405e6bf0fee55daa853f5abb Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 4 Sep 2016 08:13:36 -0700 Subject: [PATCH] swarm: pass in stream muxer on construct --- examples/justtcp/main.go | 40 +++++++--------------------------------- p2p/net/swarm/swarm.go | 4 ++-- package.json | 4 ++-- 3 files changed, 11 insertions(+), 37 deletions(-) diff --git a/examples/justtcp/main.go b/examples/justtcp/main.go index 41609dc2..89e1d69b 100644 --- a/examples/justtcp/main.go +++ b/examples/justtcp/main.go @@ -3,12 +3,10 @@ package main import ( "context" "fmt" - "net" "os" transport "github.com/ipfs/go-libp2p-transport" ma "github.com/jbenet/go-multiaddr" - smux "github.com/jbenet/go-stream-muxer" "github.com/libp2p/go-libp2p/p2p/net/swarm" ) @@ -17,52 +15,27 @@ func fatal(i interface{}) { os.Exit(1) } -type NullMux struct{} - -type NullMuxConn struct { - net.Conn -} - -func (c *NullMuxConn) AcceptStream() (smux.Stream, error) { - panic("We don't do this") -} - -func (c *NullMuxConn) IsClosed() bool { - return false -} - -func (c *NullMuxConn) OpenStream() (smux.Stream, error) { - panic("if only you could see how disappointed i am in you right now") -} - -func (c *NullMuxConn) Serve(_ smux.StreamHandler) { -} - -func (nm NullMux) NewConn(c net.Conn, server bool) (smux.Conn, error) { - return &NullMuxConn{c}, nil -} - -var _ smux.Transport = (*NullMux)(nil) - func main() { laddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/5555") if err != nil { fatal(err) } - swarm.PSTransport = new(NullMux) - - s := swarm.NewBlankSwarm(context.Background(), "bob", nil) + // create a new swarm with a dummy peer ID, no private key, and no stream muxer + s := swarm.NewBlankSwarm(context.Background(), "bob", nil, nil) + // Add a TCP transport to it s.AddTransport(transport.NewTCPTransport()) + // Add an address to start listening on err = s.AddListenAddr(laddr) if err != nil { fatal(err) } + // Set a handler for incoming connections s.SetConnHandler(func(c *swarm.Conn) { - fmt.Println("CALLED OUR CONN HANDLER!") + fmt.Println("Got a new connection!") defer c.Close() buf := make([]byte, 1024) for { @@ -81,5 +54,6 @@ func main() { } }) + // Wait forever <-make(chan bool) } diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go index 05402f50..a3787ced 100644 --- a/p2p/net/swarm/swarm.go +++ b/p2p/net/swarm/swarm.go @@ -145,9 +145,9 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr, return s, nil } -func NewBlankSwarm(ctx context.Context, id peer.ID, privkey ci.PrivKey) *Swarm { +func NewBlankSwarm(ctx context.Context, id peer.ID, privkey ci.PrivKey, pstpt pst.Transport) *Swarm { s := &Swarm{ - swarm: ps.NewSwarm(PSTransport), + swarm: ps.NewSwarm(pstpt), local: id, peers: pstore.NewPeerstore(), ctx: ctx, diff --git a/package.json b/package.json index deaba52f..34064023 100644 --- a/package.json +++ b/package.json @@ -129,9 +129,9 @@ "version": "0.0.0" }, { - "hash": "QmduCCgTaLnxwwf9RFQy2PMUytrKcEH9msohtVxSBZUdgu", + "hash": "QmRmFKJgjjQhrT1uDyhpS87kE5M9YbMT8RBWam5uk8o4uH", "name": "go-peerstream", - "version": "1.0.0" + "version": "1.1.0" }, { "author": "whyrusleeping",