|
|
@ -2,6 +2,7 @@ package p2p |
|
|
|
|
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
golog "log" |
|
|
|
"net" |
|
|
|
"time" |
|
|
|
|
|
|
@ -13,6 +14,8 @@ import ( |
|
|
|
"github.com/tendermint/tendermint/p2p/conn" |
|
|
|
) |
|
|
|
|
|
|
|
const testCh = 0x01 |
|
|
|
|
|
|
|
func AddPeerToSwitch(sw *Switch, peer Peer) { |
|
|
|
sw.peers.Add(peer) |
|
|
|
} |
|
|
@ -84,8 +87,9 @@ func Connect2Switches(switches []*Switch, i, j int) { |
|
|
|
switchJ := switches[j] |
|
|
|
|
|
|
|
p1 := &remotePeer{ |
|
|
|
Config: switchJ.peerConfig, |
|
|
|
PrivKey: switchJ.nodeKey.PrivKey, |
|
|
|
Config: switchJ.peerConfig, |
|
|
|
PrivKey: switchJ.nodeKey.PrivKey, |
|
|
|
channels: switchJ.NodeInfo().Channels, |
|
|
|
} |
|
|
|
p1.Start() |
|
|
|
|
|
|
@ -99,8 +103,9 @@ func Connect2Switches(switches []*Switch, i, j int) { |
|
|
|
} |
|
|
|
|
|
|
|
p2 := &remotePeer{ |
|
|
|
Config: switchI.peerConfig, |
|
|
|
PrivKey: switchI.nodeKey.PrivKey, |
|
|
|
Config: switchI.peerConfig, |
|
|
|
PrivKey: switchI.nodeKey.PrivKey, |
|
|
|
channels: switchI.NodeInfo().Channels, |
|
|
|
} |
|
|
|
p2.Start() |
|
|
|
|
|
|
@ -183,3 +188,77 @@ func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch f |
|
|
|
sw.SetNodeKey(nodeKey) |
|
|
|
return sw |
|
|
|
} |
|
|
|
|
|
|
|
type remotePeer struct { |
|
|
|
PrivKey crypto.PrivKey |
|
|
|
Config *PeerConfig |
|
|
|
addr *NetAddress |
|
|
|
quit chan struct{} |
|
|
|
channels cmn.HexBytes |
|
|
|
} |
|
|
|
|
|
|
|
func (rp *remotePeer) Addr() *NetAddress { |
|
|
|
return rp.addr |
|
|
|
} |
|
|
|
|
|
|
|
func (rp *remotePeer) ID() ID { |
|
|
|
return PubKeyToID(rp.PrivKey.PubKey()) |
|
|
|
} |
|
|
|
|
|
|
|
func (rp *remotePeer) Start() { |
|
|
|
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
|
|
|
|
if e != nil { |
|
|
|
golog.Fatalf("net.Listen tcp :0: %+v", e) |
|
|
|
} |
|
|
|
rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr()) |
|
|
|
rp.quit = make(chan struct{}) |
|
|
|
if rp.channels == nil { |
|
|
|
rp.channels = []byte{testCh} |
|
|
|
} |
|
|
|
go rp.accept(l) |
|
|
|
} |
|
|
|
|
|
|
|
func (rp *remotePeer) Stop() { |
|
|
|
close(rp.quit) |
|
|
|
} |
|
|
|
|
|
|
|
func (rp *remotePeer) accept(l net.Listener) { |
|
|
|
conns := []net.Conn{} |
|
|
|
|
|
|
|
for { |
|
|
|
conn, err := l.Accept() |
|
|
|
if err != nil { |
|
|
|
golog.Fatalf("Failed to accept conn: %+v", err) |
|
|
|
} |
|
|
|
|
|
|
|
pc, err := newInboundPeerConn(conn, rp.Config, rp.PrivKey) |
|
|
|
if err != nil { |
|
|
|
golog.Fatalf("Failed to create a peer: %+v", err) |
|
|
|
} |
|
|
|
|
|
|
|
_, err = pc.HandshakeTimeout(NodeInfo{ |
|
|
|
ID: rp.Addr().ID, |
|
|
|
Moniker: "remote_peer", |
|
|
|
Network: "localhost", |
|
|
|
Version: "123.123.123", |
|
|
|
ListenAddr: l.Addr().String(), |
|
|
|
Channels: rp.channels, |
|
|
|
}, 1*time.Second) |
|
|
|
if err != nil { |
|
|
|
golog.Fatalf("Failed to perform handshake: %+v", err) |
|
|
|
} |
|
|
|
|
|
|
|
conns = append(conns, conn) |
|
|
|
|
|
|
|
select { |
|
|
|
case <-rp.quit: |
|
|
|
for _, conn := range conns { |
|
|
|
if err := conn.Close(); err != nil { |
|
|
|
golog.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
default: |
|
|
|
} |
|
|
|
} |
|
|
|
} |