示例#1
0
文件: peer.go 项目: kanjar/whisper
// 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)
	}
}
示例#2
0
文件: part4.go 项目: kanjar/whisper
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
		}
	}
}