Object<User> DirectoryClient::login(const char* username, const char* password) {

	logger->debug("Login!");

	//get the path to the login method
	char loginPath[255];
	_snprintf(loginPath, 254, "%s%s", this->directoryContext, "/login/login");
	logger->debug(loginPath);

	openutils::WebForm* wf = (openutils::WebForm*)createWebRequest(this->directoryHostName, this->directoryPort, loginPath, "POST");
	wf->putVariable("username",username);
	wf->putVariable("password",password);

	wf->sendRequest("ROG");

	int response = wf->getResponseStatus();

	if (logger->isDebugEnabled()) {
		char responseCode[255];
		_snprintf(responseCode, 254, "HTTP: %d", response);
		logger->debug(responseCode);
	}

	if (response != 200) {
		delete wf;
		return NULL;
	}

	if (sessionId != NULL) {
		delete [] sessionId;
	}
	//save the session id for this user
	sessionId = strdup(wf->getResponseCookies());

	irr::io::IrrXMLReader* xmlReader = irr::io::createIrrXMLReader((irr::io::IFileReadCallBack*)new IrrCharArrayReaderSource((const char*)wf->getResponseData(), wf->getResponseLength()), false);
	if (xmlReader == NULL) {
		delete wf;
		throw new FatalException("Cannot open config file.");
	}

	Object<User> user = new User();
	user->loadFromReader(xmlReader);

	delete wf;

	return user;
}
Object<User> DirectoryClient::validateUser(long userId, long gameId) {
	logger->debug("validate!");

	//get the path to the login method
	char loginPath[255];
	_snprintf(loginPath, 254, "%s%s", this->directoryContext, "/gamer/validateUser");
	logger->debug(loginPath);

	char userIdStr[50];
	char gameIdStr[50];

	itoa(userId, userIdStr, 10);
	itoa(gameId, gameIdStr, 10);

	openutils::WebForm* wf = (openutils::WebForm*)createWebRequest(this->directoryHostName, this->directoryPort, loginPath, "POST");
	wf->putVariable("userId",userIdStr);
	wf->putVariable("gameId",gameIdStr);

	wf->sendRequest("ROG");

	int response = wf->getResponseStatus();

	if (logger->isDebugEnabled()) {
		char responseCode[255];
		_snprintf(responseCode, 254, "HTTP: %d", response);
		logger->debug(responseCode);
	}

	if (response != 200) {
		delete wf;
		return NULL;
	}

	irr::io::IrrXMLReader* xmlReader = irr::io::createIrrXMLReader((irr::io::IFileReadCallBack*)new IrrCharArrayReaderSource((const char*)wf->getResponseData(), wf->getResponseLength()), false);
	if (xmlReader == NULL) {
		delete wf;
		throw new FatalException("Cannot open config file.");
	}

	Object<User> user = new User();
	user->loadFromReader(xmlReader);

	delete wf;

	return user;
}
Object<ServerGame> DirectoryClient::createGame(const char* hostName, const int port, const char* hostGuid) {
	logger->debug("createGame!");

	//get the path to the login method
	char gameListPath[255];
	_snprintf(gameListPath, 254, "%s%s", this->directoryContext, "/gamer/startGame");
	logger->debug(gameListPath);

	char portBuf[25];
	_snprintf(portBuf, 24, "%d", port);

	openutils::WebForm* wf = (openutils::WebForm*)createWebRequest(this->directoryHostName, this->directoryPort, gameListPath, "POST");
	wf->putVariable("host", hostName);
	wf->putVariable("port", portBuf);
	wf->putVariable("guid", hostGuid);

	wf->sendRequest("ROG", this->sessionId);

	int response = wf->getResponseStatus();

	if (logger->isDebugEnabled()) {
		char responseCode[255];
		_snprintf(responseCode, 254, "HTTP: %d", response);
		logger->debug(responseCode);
	}

	if (response != 200) {
		delete wf;
		return NULL;
	}

	irr::io::IrrXMLReader* xmlReader = irr::io::createIrrXMLReader((irr::io::IFileReadCallBack*)new IrrCharArrayReaderSource((const char*)wf->getResponseData(), wf->getResponseLength()), false);
	if (xmlReader == NULL) {
		delete wf;
		throw new FatalException("Cannot open XML stream.");
	}

	Object<ServerGame> game = new ServerGame();
	game->loadFromReader(xmlReader);

	delete wf;

	return game;
}
Object<ServerGame> DirectoryClient::updateGame(Object<ServerGame> gameToUpdate) {
	logger->debug("updateGame!");

	//get the path to the login method
	char gameListPath[255];
	_snprintf(gameListPath, 254, "%s%s", this->directoryContext, "/gamer/update");
	logger->debug(gameListPath);

	char gameBuf[255];
	_snprintf(gameBuf, 254, "<game id=\"%d\"> <numberOfPlayers>%d</numberOfPlayers> </game>", gameToUpdate->getGameId(), gameToUpdate->getNumberOfPlayers());

	openutils::WebForm* wf = (openutils::WebForm*)createWebRequest(this->directoryHostName, this->directoryPort, gameListPath, "POST");
	wf->putVariable("game", gameBuf);

	wf->sendRequest("ROG", this->sessionId);

	int response = wf->getResponseStatus();

	if (logger->isDebugEnabled()) {
		char responseCode[255];
		_snprintf(responseCode, 254, "HTTP: %d", response);
		logger->debug(responseCode);
	}

	if (response != 200) {
		delete wf;
		throw new FatalException("Could not update server game");
	}

	irr::io::IrrXMLReader* xmlReader = irr::io::createIrrXMLReader((irr::io::IFileReadCallBack*)new IrrCharArrayReaderSource((const char*)wf->getResponseData(), wf->getResponseLength()), false);
	if (xmlReader == NULL) {
		delete wf;
		throw new FatalException("Cannot open XML stream.");
	}

	Object<ServerGame> game = new ServerGame();
	game->loadFromReader(xmlReader);

	delete wf;

	return game;
}
Object<GameList> DirectoryClient::getGameList() {

	logger->debug("GetGameList!");

	//get the path to the login method
	char gameListPath[255];
	_snprintf(gameListPath, 254, "%s%s", this->directoryContext, "/gamer/findGames");
	logger->debug(gameListPath);

	openutils::WebForm* wf = (openutils::WebForm*)createWebRequest(this->directoryHostName, this->directoryPort, gameListPath, "POST");
	wf->putVariable("page", "1");

	wf->sendRequest("ROG", this->sessionId);

	int response = wf->getResponseStatus();

	if (logger->isDebugEnabled()) {
		char responseCode[255];
		_snprintf(responseCode, 254, "HTTP: %d", response);
		logger->debug(responseCode);
	}

	if (response != 200) {
		delete wf;
		return NULL;
	}

	irr::io::IrrXMLReader* xmlReader = irr::io::createIrrXMLReader((irr::io::IFileReadCallBack*)new IrrCharArrayReaderSource((const char*)wf->getResponseData(), wf->getResponseLength()), false);
	if (xmlReader == NULL) {
		delete wf;
		throw new FatalException("Cannot open config file.");
	}

	Object<GameList> gameList = new GameList();
	gameList->loadFromReader(xmlReader);

	delete wf;

	return gameList;
}