Commit cab16ee4 authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub
Browse files

Merge pull request #99 from libp2p/feat/pass-smux

swarm: pass in stream muxer on construct
parents 04b3077f a56440a9
...@@ -3,12 +3,10 @@ package main ...@@ -3,12 +3,10 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"os" "os"
transport "github.com/ipfs/go-libp2p-transport" transport "github.com/ipfs/go-libp2p-transport"
ma "github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
smux "github.com/jbenet/go-stream-muxer"
"github.com/libp2p/go-libp2p/p2p/net/swarm" "github.com/libp2p/go-libp2p/p2p/net/swarm"
) )
...@@ -17,52 +15,27 @@ func fatal(i interface{}) { ...@@ -17,52 +15,27 @@ func fatal(i interface{}) {
os.Exit(1) 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() { func main() {
laddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/5555") laddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/5555")
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
swarm.PSTransport = new(NullMux) // create a new swarm with a dummy peer ID, no private key, and no stream muxer
s := swarm.NewBlankSwarm(context.Background(), "bob", nil, nil)
s := swarm.NewBlankSwarm(context.Background(), "bob", nil)
// Add a TCP transport to it
s.AddTransport(transport.NewTCPTransport()) s.AddTransport(transport.NewTCPTransport())
// Add an address to start listening on
err = s.AddListenAddr(laddr) err = s.AddListenAddr(laddr)
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
// Set a handler for incoming connections
s.SetConnHandler(func(c *swarm.Conn) { s.SetConnHandler(func(c *swarm.Conn) {
fmt.Println("CALLED OUR CONN HANDLER!") fmt.Println("Got a new connection!")
defer c.Close() defer c.Close()
buf := make([]byte, 1024) buf := make([]byte, 1024)
for { for {
...@@ -81,5 +54,6 @@ func main() { ...@@ -81,5 +54,6 @@ func main() {
} }
}) })
// Wait forever
<-make(chan bool) <-make(chan bool)
} }
...@@ -145,9 +145,9 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr, ...@@ -145,9 +145,9 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
return s, nil 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{ s := &Swarm{
swarm: ps.NewSwarm(PSTransport), swarm: ps.NewSwarm(pstpt),
local: id, local: id,
peers: pstore.NewPeerstore(), peers: pstore.NewPeerstore(),
ctx: ctx, ctx: ctx,
......
...@@ -129,9 +129,9 @@ ...@@ -129,9 +129,9 @@
"version": "0.0.0" "version": "0.0.0"
}, },
{ {
"hash": "QmduCCgTaLnxwwf9RFQy2PMUytrKcEH9msohtVxSBZUdgu", "hash": "QmRmFKJgjjQhrT1uDyhpS87kE5M9YbMT8RBWam5uk8o4uH",
"name": "go-peerstream", "name": "go-peerstream",
"version": "1.0.0" "version": "1.1.0"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
......
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