Ejemplo 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)
}
Ejemplo 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)
}
Ejemplo n.º 3
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
}