Example #1
0
// Asynchronously set up neighbors and bots
func setupAll(listener *net.TCPListener) {
	botsComplete := setupBots()
	adjsComplete := connectToAdjacents(listener)

	<-adjsComplete
	<-botsComplete

	for _, s := range botStates {
		fmt.Printf("%s\n", easynet.ReceiveFrom(s.Conn))
	}
}
Example #2
0
// Set up incoming and outgoing TCP connections to all adjacent coordinators
func connectToAdjacents(listener *net.TCPListener) chan bool {
	adjsServe = make(CoordMap, len(config.AdjacentCoords))
	adjsRequest = make(CoordMap, len(config.AdjacentCoords))
	listenServe = make(chan []uint8)

	serveFound := make(chan int)
	requestFound := make(chan int)
	allDone := make(chan bool)

	go func() {
		for _, adj := range config.AdjacentCoords {
			go func() {
				adjsRequest[adj.Identifier] = easynet.Dial(adj.Address)
				adjsRequest[adj.Identifier].Write([]uint8(strconv.Itoa(config.Identifier)))
				requestFound <- adj.Identifier
			}()
			go func() {
				newConn := easynet.Accept(listener)
				identifier, err := strconv.Atoi(string(easynet.ReceiveFrom(newConn)))
				easynet.DieIfError(err, "String conversion error")
				adjsServe[identifier] = newConn
				easynet.TieConnToChannel(newConn, listenServe)
				serveFound <- identifier
			}()
		}

		for i := 0; i < len(config.AdjacentCoords); i++ {
			<-requestFound
			<-serveFound
		}

		fmt.Printf("%d is connected to all neighbors\n", config.Identifier)

		allDone <- true
	}()

	return allDone
}