refactor SwarmConn -> Conn
Juan Batiz-Benet committed
Dec 16, 2014 at 02:56 UTC
b5b4390ca4c65b35a5575bf68714956626282fc6
4 files changed
+27
-27
net/swarm2/swarm.go
+3
-3
@@ -50,7 +50,7 @@ func (s *Swarm) teardown() error {
50
return s.swarm.Close()
51
}
52
53
-// Close stops the Swarm. See
53
+// Close stops the Swarm.
54
func (s *Swarm) Close() error {
55
return s.cg.Close()
56
}
@@ -80,12 +80,12 @@ func (s *Swarm) StreamsWithPeer(p peer.Peer) []*Stream {
80
}
81
82
// ConnectionsToPeer returns all the live connections to p
83
-func (s *Swarm) ConnectionsToPeer(p peer.Peer) []*SwarmConn {
83
+func (s *Swarm) ConnectionsToPeer(p peer.Peer) []*Conn {
84
return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns()))
85
}
86
87
// Connections returns a slice of all connections.
88
-func (s *Swarm) Connections() []*SwarmConn {
88
+func (s *Swarm) Connections() []*Conn {
89
return wrapConns(s.swarm.Conns())
90
}
91
net/swarm2/swarm_conn.go
+19
-19
@@ -11,23 +11,23 @@ import (
11
ps "github.com/jbenet/go-peerstream"
12
)
13
14
-// a SwarmConn is a simple wrapper around a ps.Conn that also exposes
14
+// a Conn is a simple wrapper around a ps.Conn that also exposes
15
// some of the methods from the underlying conn.Conn.
16
// There's **five** "layers" to each connection:
17
// - 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc)
18
// - 1. the manet.Conn - provides multiaddr friendly Conn
19
// - 2. the conn.Conn - provides Peer friendly Conn (inc Secure channel)
20
// - 3. the peerstream.Conn - provides peerstream / spdysptream happiness
21
-// - 4. the SwarmConn - abstracts everyting out, exposing only key parts of underlying layers
21
+// - 4. the Conn - abstracts everyting out, exposing only key parts of underlying layers
22
// (I know, this is kinda crazy. it's more historical than a good design. though the
23
// layers do build up pieces of functionality. and they're all just io.RW :) )
24
-type SwarmConn ps.Conn
24
+type Conn ps.Conn
25
26
-func (c *SwarmConn) StreamConn() *ps.Conn {
26
+func (c *Conn) StreamConn() *ps.Conn {
27
return (*ps.Conn)(c)
28
}
29
30
-func (c *SwarmConn) RawConn() conn.Conn {
30
+func (c *Conn) RawConn() conn.Conn {
31
// righly panic if these things aren't true. it is an expected
32
// invariant that these Conns are all of the typewe expect:
33
// ps.Conn wrapping a conn.Conn
@@ -36,48 +36,48 @@ func (c *SwarmConn) RawConn() conn.Conn {
36
}
37
38
// LocalMultiaddr is the Multiaddr on this side
39
-func (c *SwarmConn) LocalMultiaddr() ma.Multiaddr {
39
+func (c *Conn) LocalMultiaddr() ma.Multiaddr {
40
return c.RawConn().LocalMultiaddr()
41
}
42
43
// LocalPeer is the Peer on our side of the connection
44
-func (c *SwarmConn) LocalPeer() peer.Peer {
44
+func (c *Conn) LocalPeer() peer.Peer {
45
return c.RawConn().LocalPeer()
46
}
47
48
// RemoteMultiaddr is the Multiaddr on the remote side
49
-func (c *SwarmConn) RemoteMultiaddr() ma.Multiaddr {
49
+func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
50
return c.RawConn().RemoteMultiaddr()
51
}
52
53
// RemotePeer is the Peer on the remote side
54
-func (c *SwarmConn) RemotePeer() peer.Peer {
54
+func (c *Conn) RemotePeer() peer.Peer {
55
return c.RawConn().RemotePeer()
56
}
57
58
// NewStream returns a new Stream from this connection
59
-func (c *SwarmConn) NewStream() (*Stream, error) {
59
+func (c *Conn) NewStream() (*Stream, error) {
60
s, err := c.StreamConn().NewStream()
61
return wrapStream(s), err
62
}
63
64
-func (c *SwarmConn) Close() error {
64
+func (c *Conn) Close() error {
65
return c.StreamConn().Close()
66
}
67
68
-func wrapConn(psc *ps.Conn) (*SwarmConn, error) {
68
+func wrapConn(psc *ps.Conn) (*Conn, error) {
69
// grab the underlying connection.
70
if _, ok := psc.NetConn().(conn.Conn); !ok {
71
// this should never happen. if we see it ocurring it means that we added
72
// a Listener to the ps.Swarm that is NOT one of our net/conn.Listener.
73
return nil, fmt.Errorf("swarm connHandler: invalid conn (not a conn.Conn): %s", psc)
74
}
75
- return (*SwarmConn)(psc), nil
75
+ return (*Conn)(psc), nil
76
}
77
78
-// wrapConns returns a *SwarmConn for all these ps.Conns
79
-func wrapConns(conns1 []*ps.Conn) []*SwarmConn {
80
- conns2 := make([]*SwarmConn, len(conns1))
78
+// wrapConns returns a *Conn for all these ps.Conns
79
+func wrapConns(conns1 []*ps.Conn) []*Conn {
80
+ conns2 := make([]*Conn, len(conns1))
81
for i, c1 := range conns1 {
82
if c2, err := wrapConn(c1); err == nil {
83
conns2[i] = c2
@@ -88,9 +88,9 @@ func wrapConns(conns1 []*ps.Conn) []*SwarmConn {
88
89
// newConnSetup does the swarm's "setup" for a connection. returns the underlying
90
// conn.Conn this method is used by both swarm.Dial and ps.Swarm connHandler
91
-func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*SwarmConn, error) {
91
+func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*Conn, error) {
92
93
- // wrap with a SwarmConn
93
+ // wrap with a Conn
94
sc, err := wrapConn(psConn)
95
if err != nil {
96
return nil, err
@@ -114,7 +114,7 @@ func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*SwarmConn,
114
return sc, nil
115
}
116
117
-// func runHandshake3(ctx context.Context, s *Swarm, c *SwarmConn) error {
117
+// func runHandshake3(ctx context.Context, s *Swarm, c *Conn) error {
118
// log.Event(ctx, "newConnection", c.LocalPeer(), c.RemotePeer())
119
120
// stream, err := c.NewStream()
net/swarm2/swarm_dial.go
+2
-2
@@ -17,7 +17,7 @@ import (
17
// the connection will happen over. Swarm can use whichever it choses.
18
// This allows us to use various transport protocols, do NAT traversal/relay,
19
// etc. to achive connection.
20
-func (s *Swarm) Dial(p peer.Peer) (*SwarmConn, error) {
20
+func (s *Swarm) Dial(p peer.Peer) (*Conn, error) {
21
ctx := context.TODO()
22
23
if p.ID().Equal(s.local.ID()) {
@@ -77,7 +77,7 @@ func (s *Swarm) Dial(p peer.Peer) (*SwarmConn, error) {
77
78
// dialConnSetup is the setup logic for a connection from the dial side. it
79
// needs to add the Conn to the StreamSwarm, then run newConnSetup
80
-func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*SwarmConn, error) {
80
+func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {
81
82
psC, err := s.swarm.AddConn(connC)
83
if err != nil {
net/swarm2/swarm_stream.go
+3
-3
@@ -5,7 +5,7 @@ import (
5
)
6
7
// a Stream is a wrapper around a ps.Stream that exposes a way to get
8
-// our SwarmConn and Swarm (instead of just the ps.Conn and ps.Swarm)
8
+// our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
9
type Stream ps.Stream
10
11
// StreamHandler is called when new streams are opened from remote peers.
@@ -18,8 +18,8 @@ func (s *Stream) Stream() *ps.Stream {
18
}
19
20
// Conn returns the Conn associated with this Stream
21
-func (s *Stream) Conn() *SwarmConn {
22
- return (*SwarmConn)(s.Stream().Conn())
21
+func (s *Stream) Conn() *Conn {
22
+ return (*Conn)(s.Stream().Conn())
23
}
24
25
// Write writes bytes to a stream, calling write data for each call.