Ejemplo n.º 1
0
// connect establishes gossip sessions with the given addresses,
// handing them off to peerManager once established.
func connect(addrs []string, peerManager gossip.PeerManager) {
	for _, addr := range addrs {
		conn, err := net.Dial("tcp", addr)
		if err != nil {
			log.Fatalf("Error dialing initial peer %s: %s", addr, err)
		}
		peer := NewPeer(conn)
		if err := peerManager.AddPeer(peer); err != nil {
			log.Fatalf("Error adding initial peer %s: %s", peer, err)
		}
	}
}
Ejemplo n.º 2
0
// listen listens on the given address,
// establishing gossip sessions with incoming connections,
// then handing them off to peerManager
func listen(addr string, peerManager gossip.PeerManager) {
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		panic(err)
	}
	defer listener.Close()
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Printf("Error accepting connection: %s", err)
		}
		peer := NewPeer(conn)
		if err := peerManager.AddPeer(peer); err != nil {
			log.Printf("Error adding new peer %s: %s", peer, err)
		}
	}
}