func (cs *centralServer) RegisterGameServer(args *centralrpc.RegisterGameServerArgs, reply *centralrpc.RegisterGameServerReply) error { cs.gameServersLock.Lock() var id uint32 if gs, exists := cs.hostPortToGameServer[args.HostPort]; !exists { // Check if we hit the limit if len(cs.gameServers) >= cs.numGameServers { LOGE.Println("Received request to register game server when ring is FULL") reply.Status = centralrpc.Full cs.gameServersLock.Unlock() return nil } // Get a new ID id = cs.nextGameServerID cs.nextGameServerID++ // Get host:port hostport := args.HostPort // Add new server object to map gs = &gameServer{paxosrpc.Node{id, hostport}, 0} cs.gameServers[id] = gs cs.hostPortToGameServer[hostport] = gs } else { id = gs.info.ID } // Check if all the game servers in the ring have registered. If they // haven't, then reply with not ready. Otherwise, reply with OK, send back // to the unique ID, and the list of all game servers. if len(cs.gameServers) < cs.numGameServers { reply.Status = centralrpc.NotReady } else { reply.Status = centralrpc.OK reply.GameServerID = id // Check if the game servers sliced has been cached. If it hasn't, make it. if cs.gameServersSlice == nil { cs.gameServersSlice = make([]paxosrpc.Node, len(cs.gameServers)) i := 0 for _, node := range cs.gameServers { cs.gameServersSlice[i] = node.info i++ } } reply.Servers = cs.gameServersSlice } LOGV.Printf("Received registration request from %d, reply was %d\n", id, reply.Status) cs.gameServersLock.Unlock() return nil }