// Fork a bot and open a TCP connection to it func setupBot(conf ttypes.BotConf, portNumber int) *net.TCPConn { addrString := "127.0.0.1:" + strconv.Itoa(portNumber) fd := []*os.File{os.Stdin, os.Stdout, os.Stderr} _, err := os.StartProcess(conf.Path, []string{addrString}, nil, "", fd) easynet.DieIfError(err, "Error launching bot") return easynet.Dial(addrString) }
// 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 }