Commit b5b4390c authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

refactor SwarmConn -> Conn

parent 9c119705
...@@ -50,7 +50,7 @@ func (s *Swarm) teardown() error { ...@@ -50,7 +50,7 @@ func (s *Swarm) teardown() error {
return s.swarm.Close() return s.swarm.Close()
} }
// Close stops the Swarm. See // Close stops the Swarm.
func (s *Swarm) Close() error { func (s *Swarm) Close() error {
return s.cg.Close() return s.cg.Close()
} }
...@@ -80,12 +80,12 @@ func (s *Swarm) StreamsWithPeer(p peer.Peer) []*Stream { ...@@ -80,12 +80,12 @@ func (s *Swarm) StreamsWithPeer(p peer.Peer) []*Stream {
} }
// ConnectionsToPeer returns all the live connections to p // ConnectionsToPeer returns all the live connections to p
func (s *Swarm) ConnectionsToPeer(p peer.Peer) []*SwarmConn { func (s *Swarm) ConnectionsToPeer(p peer.Peer) []*Conn {
return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns())) return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns()))
} }
// Connections returns a slice of all connections. // Connections returns a slice of all connections.
func (s *Swarm) Connections() []*SwarmConn { func (s *Swarm) Connections() []*Conn {
return wrapConns(s.swarm.Conns()) return wrapConns(s.swarm.Conns())
} }
......
...@@ -11,23 +11,23 @@ import ( ...@@ -11,23 +11,23 @@ import (
ps "github.com/jbenet/go-peerstream" ps "github.com/jbenet/go-peerstream"
) )
// a SwarmConn is a simple wrapper around a ps.Conn that also exposes // a Conn is a simple wrapper around a ps.Conn that also exposes
// some of the methods from the underlying conn.Conn. // some of the methods from the underlying conn.Conn.
// There's **five** "layers" to each connection: // There's **five** "layers" to each connection:
// - 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc) // - 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc)
// - 1. the manet.Conn - provides multiaddr friendly Conn // - 1. the manet.Conn - provides multiaddr friendly Conn
// - 2. the conn.Conn - provides Peer friendly Conn (inc Secure channel) // - 2. the conn.Conn - provides Peer friendly Conn (inc Secure channel)
// - 3. the peerstream.Conn - provides peerstream / spdysptream happiness // - 3. the peerstream.Conn - provides peerstream / spdysptream happiness
// - 4. the SwarmConn - abstracts everyting out, exposing only key parts of underlying layers // - 4. the Conn - abstracts everyting out, exposing only key parts of underlying layers
// (I know, this is kinda crazy. it's more historical than a good design. though the // (I know, this is kinda crazy. it's more historical than a good design. though the
// layers do build up pieces of functionality. and they're all just io.RW :) ) // layers do build up pieces of functionality. and they're all just io.RW :) )
type SwarmConn ps.Conn type Conn ps.Conn
func (c *SwarmConn) StreamConn() *ps.Conn { func (c *Conn) StreamConn() *ps.Conn {
return (*ps.Conn)(c) return (*ps.Conn)(c)
} }
func (c *SwarmConn) RawConn() conn.Conn { func (c *Conn) RawConn() conn.Conn {
// righly panic if these things aren't true. it is an expected // righly panic if these things aren't true. it is an expected
// invariant that these Conns are all of the typewe expect: // invariant that these Conns are all of the typewe expect:
// ps.Conn wrapping a conn.Conn // ps.Conn wrapping a conn.Conn
...@@ -36,48 +36,48 @@ func (c *SwarmConn) RawConn() conn.Conn { ...@@ -36,48 +36,48 @@ func (c *SwarmConn) RawConn() conn.Conn {
} }
// LocalMultiaddr is the Multiaddr on this side // LocalMultiaddr is the Multiaddr on this side
func (c *SwarmConn) LocalMultiaddr() ma.Multiaddr { func (c *Conn) LocalMultiaddr() ma.Multiaddr {
return c.RawConn().LocalMultiaddr() return c.RawConn().LocalMultiaddr()
} }
// LocalPeer is the Peer on our side of the connection // LocalPeer is the Peer on our side of the connection
func (c *SwarmConn) LocalPeer() peer.Peer { func (c *Conn) LocalPeer() peer.Peer {
return c.RawConn().LocalPeer() return c.RawConn().LocalPeer()
} }
// RemoteMultiaddr is the Multiaddr on the remote side // RemoteMultiaddr is the Multiaddr on the remote side
func (c *SwarmConn) RemoteMultiaddr() ma.Multiaddr { func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
return c.RawConn().RemoteMultiaddr() return c.RawConn().RemoteMultiaddr()
} }
// RemotePeer is the Peer on the remote side // RemotePeer is the Peer on the remote side
func (c *SwarmConn) RemotePeer() peer.Peer { func (c *Conn) RemotePeer() peer.Peer {
return c.RawConn().RemotePeer() return c.RawConn().RemotePeer()
} }
// NewStream returns a new Stream from this connection // NewStream returns a new Stream from this connection
func (c *SwarmConn) NewStream() (*Stream, error) { func (c *Conn) NewStream() (*Stream, error) {
s, err := c.StreamConn().NewStream() s, err := c.StreamConn().NewStream()
return wrapStream(s), err return wrapStream(s), err
} }
func (c *SwarmConn) Close() error { func (c *Conn) Close() error {
return c.StreamConn().Close() return c.StreamConn().Close()
} }
func wrapConn(psc *ps.Conn) (*SwarmConn, error) { func wrapConn(psc *ps.Conn) (*Conn, error) {
// grab the underlying connection. // grab the underlying connection.
if _, ok := psc.NetConn().(conn.Conn); !ok { if _, ok := psc.NetConn().(conn.Conn); !ok {
// this should never happen. if we see it ocurring it means that we added // this should never happen. if we see it ocurring it means that we added
// a Listener to the ps.Swarm that is NOT one of our net/conn.Listener. // a Listener to the ps.Swarm that is NOT one of our net/conn.Listener.
return nil, fmt.Errorf("swarm connHandler: invalid conn (not a conn.Conn): %s", psc) return nil, fmt.Errorf("swarm connHandler: invalid conn (not a conn.Conn): %s", psc)
} }
return (*SwarmConn)(psc), nil return (*Conn)(psc), nil
} }
// wrapConns returns a *SwarmConn for all these ps.Conns // wrapConns returns a *Conn for all these ps.Conns
func wrapConns(conns1 []*ps.Conn) []*SwarmConn { func wrapConns(conns1 []*ps.Conn) []*Conn {
conns2 := make([]*SwarmConn, len(conns1)) conns2 := make([]*Conn, len(conns1))
for i, c1 := range conns1 { for i, c1 := range conns1 {
if c2, err := wrapConn(c1); err == nil { if c2, err := wrapConn(c1); err == nil {
conns2[i] = c2 conns2[i] = c2
...@@ -88,9 +88,9 @@ func wrapConns(conns1 []*ps.Conn) []*SwarmConn { ...@@ -88,9 +88,9 @@ func wrapConns(conns1 []*ps.Conn) []*SwarmConn {
// newConnSetup does the swarm's "setup" for a connection. returns the underlying // newConnSetup does the swarm's "setup" for a connection. returns the underlying
// conn.Conn this method is used by both swarm.Dial and ps.Swarm connHandler // conn.Conn this method is used by both swarm.Dial and ps.Swarm connHandler
func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*SwarmConn, error) { func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*Conn, error) {
// wrap with a SwarmConn // wrap with a Conn
sc, err := wrapConn(psConn) sc, err := wrapConn(psConn)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -114,7 +114,7 @@ func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*SwarmConn, ...@@ -114,7 +114,7 @@ func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*SwarmConn,
return sc, nil return sc, nil
} }
// func runHandshake3(ctx context.Context, s *Swarm, c *SwarmConn) error { // func runHandshake3(ctx context.Context, s *Swarm, c *Conn) error {
// log.Event(ctx, "newConnection", c.LocalPeer(), c.RemotePeer()) // log.Event(ctx, "newConnection", c.LocalPeer(), c.RemotePeer())
// stream, err := c.NewStream() // stream, err := c.NewStream()
......
...@@ -17,7 +17,7 @@ import ( ...@@ -17,7 +17,7 @@ import (
// the connection will happen over. Swarm can use whichever it choses. // the connection will happen over. Swarm can use whichever it choses.
// This allows us to use various transport protocols, do NAT traversal/relay, // This allows us to use various transport protocols, do NAT traversal/relay,
// etc. to achive connection. // etc. to achive connection.
func (s *Swarm) Dial(p peer.Peer) (*SwarmConn, error) { func (s *Swarm) Dial(p peer.Peer) (*Conn, error) {
ctx := context.TODO() ctx := context.TODO()
if p.ID().Equal(s.local.ID()) { if p.ID().Equal(s.local.ID()) {
...@@ -77,7 +77,7 @@ func (s *Swarm) Dial(p peer.Peer) (*SwarmConn, error) { ...@@ -77,7 +77,7 @@ func (s *Swarm) Dial(p peer.Peer) (*SwarmConn, error) {
// dialConnSetup is the setup logic for a connection from the dial side. it // dialConnSetup is the setup logic for a connection from the dial side. it
// needs to add the Conn to the StreamSwarm, then run newConnSetup // needs to add the Conn to the StreamSwarm, then run newConnSetup
func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*SwarmConn, error) { func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {
psC, err := s.swarm.AddConn(connC) psC, err := s.swarm.AddConn(connC)
if err != nil { if err != nil {
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
) )
// a Stream is a wrapper around a ps.Stream that exposes a way to get // a Stream is a wrapper around a ps.Stream that exposes a way to get
// our SwarmConn and Swarm (instead of just the ps.Conn and ps.Swarm) // our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
type Stream ps.Stream type Stream ps.Stream
// StreamHandler is called when new streams are opened from remote peers. // StreamHandler is called when new streams are opened from remote peers.
...@@ -18,8 +18,8 @@ func (s *Stream) Stream() *ps.Stream { ...@@ -18,8 +18,8 @@ func (s *Stream) Stream() *ps.Stream {
} }
// Conn returns the Conn associated with this Stream // Conn returns the Conn associated with this Stream
func (s *Stream) Conn() *SwarmConn { func (s *Stream) Conn() *Conn {
return (*SwarmConn)(s.Stream().Conn()) return (*Conn)(s.Stream().Conn())
} }
// Write writes bytes to a stream, calling write data for each call. // Write writes bytes to a stream, calling write data for each call.
......
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