diff --git a/router/connection.go b/router/connection.go index fd118b59..4d72a286 100644 --- a/router/connection.go +++ b/router/connection.go @@ -13,10 +13,10 @@ import ( type Connection interface { Local() *Peer Remote() *Peer - BreakTie(Connection) ConnectionTieBreak RemoteTCPAddr() string - Established() bool Outbound() bool + Established() bool + BreakTie(Connection) ConnectionTieBreak Shutdown(error) Log(args ...interface{}) } @@ -33,8 +33,8 @@ type RemoteConnection struct { local *Peer remote *Peer remoteTCPAddr string - established bool outbound bool + established bool } type LocalConnection struct { @@ -68,41 +68,23 @@ type ConnectionInteraction struct { payload interface{} } -func NewRemoteConnection(from, to *Peer, tcpAddr string, established bool, outbound bool) *RemoteConnection { +func NewRemoteConnection(from, to *Peer, tcpAddr string, outbound bool, established bool) *RemoteConnection { return &RemoteConnection{ local: from, remote: to, remoteTCPAddr: tcpAddr, + outbound: outbound, established: established, - outbound: outbound} + } } -func (conn *RemoteConnection) Local() *Peer { - return conn.local -} - -func (conn *RemoteConnection) Remote() *Peer { - return conn.remote -} - -func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak { - return TieBreakTied -} - -func (conn *RemoteConnection) RemoteTCPAddr() string { - return conn.remoteTCPAddr -} - -func (conn *RemoteConnection) Established() bool { - return conn.established -} - -func (conn *RemoteConnection) Outbound() bool { - return conn.outbound -} - -func (conn *RemoteConnection) Shutdown(error) { -} +func (conn *RemoteConnection) Local() *Peer { return conn.local } +func (conn *RemoteConnection) Remote() *Peer { return conn.remote } +func (conn *RemoteConnection) RemoteTCPAddr() string { return conn.remoteTCPAddr } +func (conn *RemoteConnection) Outbound() bool { return conn.outbound } +func (conn *RemoteConnection) Established() bool { return conn.established } +func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak { return TieBreakTied } +func (conn *RemoteConnection) Shutdown(error) {} func (conn *RemoteConnection) Log(args ...interface{}) { log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s]:", conn.remote.Name)), args...)...) diff --git a/router/local_peer.go b/router/local_peer.go index 120719b4..6f2b1979 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -111,7 +111,7 @@ func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) err if err != nil { return err } - connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), false, true) + connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), true, false) connLocal := NewLocalConnection(connRemote, tcpConn, udpAddr, peer.Router) connLocal.Start(acceptNewPeer) return nil diff --git a/router/peers.go b/router/peers.go index 2e1a1f6f..514de2fd 100644 --- a/router/peers.go +++ b/router/peers.go @@ -267,8 +267,8 @@ func (peer *Peer) encode(enc *gob.Encoder) { checkFatal(connsEnc.Encode(conn.Remote().NameByte)) checkFatal(connsEnc.Encode(conn.RemoteTCPAddr())) // DANGER holding rlock on peer, going to take rlock on conn - checkFatal(connsEnc.Encode(conn.Established())) checkFatal(connsEnc.Encode(conn.Outbound())) + checkFatal(connsEnc.Encode(conn.Established())) } checkFatal(enc.Encode(connsBuf.Bytes())) } @@ -305,24 +305,24 @@ func connsIterator(input []byte, fun func([]byte, string, bool, bool)) error { if err := dec.Decode(&foundAt); err != nil { return err } - var established bool - if err := dec.Decode(&established); err != nil { - return err - } var outbound bool if err := dec.Decode(&outbound); err != nil { return err } - fun(nameByte, foundAt, established, outbound) + var established bool + if err := dec.Decode(&established); err != nil { + return err + } + fun(nameByte, foundAt, outbound, established) } } func readConnsMap(peer *Peer, buf []byte, table map[PeerName]*Peer) map[PeerName]Connection { conns := make(map[PeerName]Connection) - if err := connsIterator(buf, func(nameByte []byte, remoteTCPAddr string, established bool, outbound bool) { + if err := connsIterator(buf, func(nameByte []byte, remoteTCPAddr string, outbound bool, established bool) { name := PeerNameFromBin(nameByte) remotePeer := table[name] - conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr, established, outbound) + conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr, outbound, established) conns[name] = conn }); err != io.EOF { // this should never happen since we've already successfully