Merge pull request #99 from libp2p/feat/pass-smux
swarm: pass in stream muxer on construct
This commit is contained in:
commit
cab16ee439
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue