Exemplo n.º 1
0
func gameSrvCronHandler(response http.ResponseWriter, request *http.Request) {
	context := appengine.NewContext(request)
	if !user.IsAdmin(context) {
		http.Error(response, ERR_FORBIDDEN_CRON_URL, http.StatusForbidden)
		return
	}

	query := datastore.NewQuery(model.DB_KIND_GAME_SERVER).
		Filter("Active =", true)
	for queryIterator := query.Run(context); ; {
		var server model.GameServer
		key, err := queryIterator.Next(&server)
		if err == datastore.Done {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		err = pingGameServer(context, server.Address)
		if err != nil {
			log.Println(err)
			server.Active = false
			server.LastActivationChange = time.Now()
			_, err := datastore.Put(context, key, &server)
			if err != nil {
				log.Fatal(err)
			}
		}
	}
}
Exemplo n.º 2
0
func gameSrvActivationHandler(response http.ResponseWriter, request *http.Request) {
	if !(utils.CheckValue(request, KEY_SERVER_NAME, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_PROTOCOL_VERSION, utils.Rule{Number: true, Min: 1, Max: 1000}) &&
		utils.CheckValue(request, KEY_ACTIVE, utils.Rule{Values: []string{"true", "false"}})) {
		http.Error(response, utils.GetLastValidationError().Error(), http.StatusBadRequest)
		return
	}

	protocolVersion, _ := strconv.Atoi(request.FormValue(KEY_PROTOCOL_VERSION))
	if protocolVersion < base.CURRENT_PROTOCOL_VERSION {
		http.Error(response, ERR_WRONG_PROTOCOL, http.StatusNotAcceptable)
		return
	}

	context := appengine.NewContext(request)

	err := pingGameServer(context, request.RemoteAddr+DEFAULT_GAME_SERVER_PORT)
	if err != nil {
		http.Error(response, err.Error(), http.StatusInternalServerError)
		return
	}

	gameServer := new(model.GameServer)
	key, err := datastore.DecodeKey(request.FormValue(KEY_SERVER_ID))
	if err == nil {
		err = datastore.Get(context, key, gameServer)
		if err != nil {
			log.Println("Can't find game server with id: making new one")
		}
	}
	gameServer.Name = request.FormValue(KEY_SERVER_NAME)
	gameServer.Address = request.RemoteAddr + DEFAULT_GAME_SERVER_PORT
	gameServer.Active = request.FormValue(KEY_ACTIVE) == "true"
	gameServer.ProtocolVersion = protocolVersion
	gameServer.LastActivationChange = time.Now()
	if err != nil {
		gameServer.RegisteredAt = time.Now()
		key = datastore.NewIncompleteKey(context, model.DB_KIND_GAME_SERVER, nil)
	}

	key, err = datastore.Put(context, key, gameServer)
	if err != nil {
		log.Fatal(err)
	}

	utils.WriteJsonResponseObject(response, GameServerActivationResult{Id: key.Encode()})
}