Ejemplo n.º 1
0
// poll periodically fetches a peer list from the master and connects to any
// new peers.
func poll(self string) {
	for {
		addrs, err := util.ListPeers()
		if err != nil {
			log.Println(err)
			continue
		}
		for _, addr := range addrs {
			// Don't connect to self.
			if addr == self {
				continue
			}

			// Don't connect if we're already connected.
			Peers.RLock()
			_, ok := Peers.m[addr]
			Peers.RUnlock()
			if ok {
				continue
			}

			go connect(addr)
		}
		time.Sleep(refreshInterval)
	}
}
Ejemplo n.º 2
0
func main() {
	go listen()

	peers, err := util.ListPeers()
	if err != nil {
		log.Fatal(err)
	}
	for _, peer := range peers {
		go connect(peer)
	}

	r := bufio.NewReader(os.Stdin)
	for {
		s, err := r.ReadString('\n')
		if err != nil {
			log.Fatal(err)
		}
		m := Message{Body: s}
		for _, peerc := range Peers {
			peerc <- m
		}
	}
}