More actions got logging. Some refactoring to use ActionWithResult.

This commit is contained in:
magenoxx 2012-01-26 00:40:00 +04:00
parent 039d9663f0
commit a60c61cb00
5 changed files with 82 additions and 22 deletions

View file

@ -34,16 +34,20 @@ import mage.game.GameException;
import mage.game.match.MatchOptions;
import mage.game.tournament.TournamentOptions;
import mage.interfaces.Action;
import mage.interfaces.ActionWithResult;
import mage.interfaces.MageServer;
import mage.interfaces.ServerState;
import mage.interfaces.callback.ClientCallback;
import mage.remote.MageVersionException;
import mage.server.draft.DraftManager;
import mage.server.game.*;
import mage.server.services.LogKeys;
import mage.server.services.impl.LogServiceImpl;
import mage.server.tournament.TournamentFactory;
import mage.server.tournament.TournamentManager;
import mage.server.util.ServerMessagesUtil;
import mage.server.util.ThreadExecutor;
import mage.utils.ActionWithBooleanResult;
import mage.utils.CompressUtil;
import mage.utils.MageVersion;
import mage.view.*;
@ -122,8 +126,11 @@ public class MageServerImpl implements MageServer {
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(userId, options);
logger.info("Table " + table.getTableId() + " created");
return table;
}
LogServiceImpl.instance.log(LogKeys.KEY_TABLE_CREATED, sessionId, userId.toString(), table.getTableId().toString());
return table;
} else {
LogServiceImpl.instance.log(LogKeys.KEY_NOT_VALID_SESSION, "createTable", sessionId, roomId.toString());
}
}
catch (Exception ex) {
handleException(ex);
@ -138,8 +145,11 @@ public class MageServerImpl implements MageServer {
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTournamentTable(userId, options);
logger.info("Tournament table " + table.getTableId() + " created");
return table;
}
LogServiceImpl.instance.log(LogKeys.KEY_TOURNAMENT_TABLE_CREATED, sessionId, userId.toString(), table.getTableId().toString());
return table;
} else {
LogServiceImpl.instance.log(LogKeys.KEY_NOT_VALID_SESSION, "createTournamentTable", sessionId, roomId.toString());
}
}
catch (Exception ex) {
handleException(ex);
@ -352,7 +362,7 @@ public class MageServerImpl implements MageServer {
execute(sessionId, new Action() {
public void execute() {
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ChatManager.getInstance().joinChat(chatId, userId);
ChatManager.getInstance().joinChat(chatId, userId);
}
});
}
@ -390,17 +400,15 @@ public class MageServerImpl implements MageServer {
}
@Override
public boolean isTableOwner(String sessionId, UUID roomId, UUID tableId) throws MageException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return TableManager.getInstance().isTableOwner(tableId, userId);
}
}
catch (Exception ex) {
handleException(ex);
}
return false;
public boolean isTableOwner(final String sessionId, UUID roomId, final UUID tableId) throws MageException {
return
executeWithResult(sessionId, new ActionWithBooleanResult() {
@Override
public Boolean execute() {
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return TableManager.getInstance().isTableOwner(tableId, userId);
}
});
}
@Override
@ -777,4 +785,17 @@ public class MageServerImpl implements MageServer {
}
}
}
protected <T> T executeWithResult(final String sessionId, final ActionWithResult<T> action) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
return action.execute();
}
} catch (Exception ex) {
handleException(ex);
}
}
return action.negativeResult();
}
}

View file

@ -34,6 +34,9 @@ import java.util.concurrent.ConcurrentHashMap;
import mage.MageException;
import mage.players.net.UserData;
import mage.players.net.UserGroup;
import mage.server.services.LogKeys;
import mage.server.services.LogService;
import mage.server.services.impl.LogServiceImpl;
import mage.view.UserDataView;
import org.apache.log4j.Logger;
import org.jboss.remoting.callback.InvokerCallbackHandler;
@ -67,6 +70,7 @@ public class SessionManager {
Session session = sessions.get(sessionId);
if (session != null) {
session.registerUser(userName);
LogServiceImpl.instance.log(LogKeys.KEY_USER_CONNECTED, userName, session.getHost(), sessionId);
logger.info("User " + userName + " connected from " + session.getHost() + " sessionId: " + sessionId);
return true;
}
@ -77,6 +81,7 @@ public class SessionManager {
Session session = sessions.get(sessionId);
if (session != null) {
session.registerAdmin();
LogServiceImpl.instance.log(LogKeys.KEY_ADMIN_CONNECTED, "Admin", session.getHost(), sessionId);
logger.info("Admin connected from " + session.getHost());
return true;
}
@ -96,12 +101,14 @@ public class SessionManager {
Session session = sessions.get(sessionId);
sessions.remove(sessionId);
if (session != null) {
if (voluntary)
if (voluntary) {
session.kill();
else
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_KILLED, sessionId);
} else {
session.disconnect();
}
else {
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_DISCONNECTED, sessionId);
}
} else {
logger.info("could not find session with id " + sessionId);
}
}
@ -117,6 +124,7 @@ public class SessionManager {
public void disconnectUser(String sessionId, String userSessionId) {
if (isAdmin(sessionId)) {
disconnect(userSessionId, true);
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_DISCONNECTED_BY_ADMIN, sessionId, userSessionId);
}
}

View file

@ -51,6 +51,7 @@ import mage.players.Player;
import mage.server.challenge.ChallengeManager;
import mage.server.draft.DraftManager;
import mage.server.game.*;
import mage.server.services.LogKeys;
import mage.server.services.LogService;
import mage.server.services.impl.LogServiceImpl;
import mage.server.tournament.TournamentFactory;
@ -358,7 +359,7 @@ public class TableController {
ServerMessagesUtil.getInstance().incGamesStarted();
// log about game started
LogServiceImpl.instance.log(LogService.KEY_GAME_STARTED, String.valueOf(userPlayerMap.size()), creator, opponent);
LogServiceImpl.instance.log(LogKeys.KEY_GAME_STARTED, String.valueOf(userPlayerMap.size()), creator, opponent);
}
catch (Exception ex) {
logger.fatal("Error starting game", ex);

View file

@ -0,0 +1,25 @@
package mage.server.services;
/**
* @author noxx
*/
public interface LogKeys {
public static final String KEY_GAME_STARTED = "gameStarted";
public static final String KEY_USER_CONNECTED = "userConnected";
public static final String KEY_ADMIN_CONNECTED = "adminConnected";
public static final String KEY_SESSION_KILLED = "sessionKilled";
public static final String KEY_SESSION_DISCONNECTED = "sessionDisconnected";
public static final String KEY_SESSION_DISCONNECTED_BY_ADMIN = "sessionDisconnectedByAdmin";
public static final String KEY_NOT_VALID_SESSION = "sessionNotValid";
public static final String KEY_TABLE_CREATED = "tableCreated";
public static final String KEY_TOURNAMENT_TABLE_CREATED = "tournamentTableCreated";
}

View file

@ -6,7 +6,12 @@ package mage.server.services;
* @author noxx
*/
public interface LogService {
public static final String KEY_GAME_STARTED = "gameStarted";
/**
* Logs any information
*
* @param key Log key. Should be the same for the same types of logs.
* @param args Any parameters in string representation.
*/
void log(String key, String... args);
}