Exemplo n.º 1
0
func main() {
	// Print a nice separator at the end of execution so that 'make fancyrun' looks good
	defer fmt.Println("-------------------")

	// Initialize globals
	processing = false
	complete = make(chan bool)

	// Begin listening on the port passed on the command line
	listener := easynet.HostWithAddress(os.Args[1])
	defer listener.Close()

	// Set up a TCP connection to the master
	connectionToMaster := easynet.Accept(listener)
	defer connectionToMaster.Close()

	// Read configuration from the connection to master
	config = new(ttypes.CoordConfig)
	easynet.ReceiveJson(connectionToMaster, config)

	respondingToRequestsFor = 0
	infoQueue = make([][]ttypes.BotInfo, 1, config.NumTurns)

	// Confirm configuration
	connectionToMaster.Write([]uint8("connected"))

	// Set up bots and connect to adjacent coordinators
	setupAll(listener)
	// Remember to terminate all child processes upon termination of this process
	defer killChildren()

	// Close all connections when program terminates
	for _, conn := range adjsServe {
		defer conn.Close()
	}

	// Confirm setup
	connectionToMaster.Write([]uint8("setup complete"))

	fmt.Printf("%d sees data at start as: \n%v\n    grid: %v\n", config.Identifier, botInfosForNeighbor(0), config.Terrain)

	// Transition to both listening states
	go listenForMaster(connectionToMaster)
	go listenForPeer()

	// Wait for those loops to exit
	<-complete
	<-complete

	fmt.Printf("%d sees data at end as: \n%v\n    grid: %v\n", config.Identifier, botInfosForNeighbor(0), config.Terrain)

	// Confirm termination
	finalTally := new(ttypes.Finish)
	for _, s := range botStates {
		if !s.Dead() {
			finalTally.NumBots += 1
		}
	}
	easynet.SendJson(connectionToMaster, finalTally)
}
Exemplo n.º 2
0
func main() {
	fmt.Printf("testbot launched on %s\n", os.Args[0])
	listener := easynet.HostWithAddress(os.Args[0])
	defer listener.Close()
	conn := easynet.Accept(listener)
	defer conn.Close()

	conn.Write([]uint8("bot setup complete " + os.Args[0]))

	listenForMoveRequests(conn)
}