@cryptotaxi247 / kubo / commits / 061331875

decomp chan creation for listener + bugfix

test failed to compile, as NewSwarm now takes a parm.

Juan Batiz-Benet committed Jul 29, 2014 at 18:01 UTC 061331875c442d0e7e8a7951855c6ad919d4f504
2 files changed +18 -11
swarm/conn.go
+17 -10
@@ -44,29 +44,36 @@ func Dial(network string, peer *peer.Peer) (*Conn, error) {
44 return nil, err
45 }
46
47 - out := msgio.NewChan(10)
48 - inc := msgio.NewChan(10)
49 -
47 conn := &Conn{
48 Peer: peer,
49 Addr: addr,
50 Conn: nconn,
51 + }
52
55 - Outgoing: out,
56 - Incoming: inc,
57 - Closed: make(chan bool, 1),
53 + newConnChans(conn)
54 + return conn, nil
55 +}
56 +
57 +// Construct new channels for given Conn.
58 +func newConnChans(c *Conn) error {
59 + if c.Outgoing != nil || c.Incoming != nil {
60 + return fmt.Errorf("Conn already initialized")
61 }
62
60 - go out.WriteTo(nconn)
61 - go inc.ReadFrom(nconn, 1<<12)
63 + c.Outgoing = msgio.NewChan(10)
64 + c.Incoming = msgio.NewChan(10)
65 + c.Closed = make(chan bool, 1)
66
63 - return conn, nil
67 + go c.Outgoing.WriteTo(c.Conn)
68 + go c.Incoming.ReadFrom(c.Conn, 1<<12)
69 +
70 + return nil
71 }
72
73 // Close closes the connection, and associated channels.
74 func (s *Conn) Close() error {
75 if s.Conn == nil {
69 - return fmt.Errorf("Already closed.") // already closed
76 + return fmt.Errorf("Already closed") // already closed
77 }
78
79 // closing net connection
swarm/swarm_test.go
+1 -1
@@ -42,7 +42,7 @@ func pong(c net.Conn, peer *peer.Peer) {
42
43 func TestSwarm(t *testing.T) {
44
45 - swarm := NewSwarm()
45 + swarm := NewSwarm(nil)
46 peers := []*peer.Peer{}
47 listeners := []*net.Listener{}
48 peerNames := map[string]string{