* Mage server - Some changes to disconnect reason handling.

This commit is contained in:
LevelX2 2014-06-09 13:16:35 +02:00
parent 382e9e3506
commit 6a3b0afc09
10 changed files with 71 additions and 55 deletions

View file

@ -64,7 +64,7 @@ public class ChatManager {
public void leaveChat(UUID chatId, UUID userId) { public void leaveChat(UUID chatId, UUID userId) {
if (chatSessions.containsKey(chatId)) { if (chatSessions.containsKey(chatId)) {
chatSessions.get(chatId).kill(userId, User.DisconnectReason.CleaningUp); chatSessions.get(chatId).kill(userId, DisconnectReason.CleaningUp);
} }
} }
@ -158,7 +158,7 @@ public class ChatManager {
} }
} }
public void removeUser(UUID userId, User.DisconnectReason reason) { public void removeUser(UUID userId, DisconnectReason reason) {
Logger.getLogger(ChatManager.class).debug("ChatManager: Remove user start"); Logger.getLogger(ChatManager.class).debug("ChatManager: Remove user start");
for (ChatSession chat: chatSessions.values()) { for (ChatSession chat: chatSessions.values()) {
chat.kill(userId, reason); chat.kill(userId, reason);

View file

@ -65,7 +65,7 @@ public class ChatSession {
} }
} }
synchronized public void kill(UUID userId, User.DisconnectReason reason) { synchronized public void kill(UUID userId, DisconnectReason reason) {
if (userId != null && clients.containsKey(userId)) { if (userId != null && clients.containsKey(userId)) {
String userName = clients.get(userId); String userName = clients.get(userId);
logger.debug(userName + " leaves chat: " + chatId); logger.debug(userName + " leaves chat: " + chatId);
@ -133,7 +133,7 @@ public class ChatSession {
user.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color, messageType, soundToPlay))); user.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color, messageType, soundToPlay)));
} }
else { else {
kill(userId, User.DisconnectReason.CleaningUp); kill(userId, DisconnectReason.CleaningUp);
} }
} }
} }

View file

@ -0,0 +1,10 @@
package mage.server;
/**
*
* @author LevelX2
*/
public enum DisconnectReason {
LostConnection, Disconnected, CleaningUp, ConnectingOtherInstance, AdminDisconnect, SessionExpired, Undefined;
}

View file

@ -187,11 +187,11 @@ public class Main {
} }
sessionInfo.append(" at ").append(session.getHost()).append(" sessionId: ").append(session.getId()); sessionInfo.append(" at ").append(session.getHost()).append(" sessionId: ").append(session.getId());
if (throwable instanceof ClientDisconnectedException) { if (throwable instanceof ClientDisconnectedException) {
SessionManager.getInstance().disconnect(client.getSessionId(), true); SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.Disconnected);
logger.debug("Client disconnected - " + sessionInfo); logger.debug("Client disconnected - " + sessionInfo);
} }
else { else {
SessionManager.getInstance().disconnect(client.getSessionId(), false); SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection);
logger.info("Connection to client lost - " + sessionInfo); logger.info("Connection to client lost - " + sessionInfo);
} }
} }
@ -259,7 +259,7 @@ public class Main {
public void removeListener(InvokerCallbackHandler callbackHandler) { public void removeListener(InvokerCallbackHandler callbackHandler) {
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler; ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
String sessionId = handler.getClientSessionId(); String sessionId = handler.getClientSessionId();
SessionManager.getInstance().disconnect(sessionId, true); SessionManager.getInstance().disconnect(sessionId, DisconnectReason.Disconnected);
} }
} }

View file

@ -106,7 +106,7 @@ public class Session {
//throw new MageException("This machine is already connected"); //throw new MageException("This machine is already connected");
//disconnect previous one //disconnect previous one
logger.info("Disconnecting another user instance: " + userName); logger.info("Disconnecting another user instance: " + userName);
UserManager.getInstance().disconnect(user.getId(), User.DisconnectReason.ConnectingOtherInstance); UserManager.getInstance().disconnect(user.getId(), DisconnectReason.ConnectingOtherInstance);
} }
} else { } else {
return new StringBuilder("User name ").append(userName).append(" already in use (or your IP address changed)").toString(); return new StringBuilder("User name ").append(userName).append(" already in use (or your IP address changed)").toString();
@ -205,12 +205,12 @@ public class Session {
sb.append(" sessionId: ").append(sessionId); sb.append(" sessionId: ").append(sessionId);
logger.info(sb); logger.info(sb);
} }
UserManager.getInstance().disconnect(userId, User.DisconnectReason.LostConnection); UserManager.getInstance().disconnect(userId, DisconnectReason.LostConnection);
} }
public void kill() { public void kill(DisconnectReason reason) {
logger.debug("session removed for user " + userId); logger.debug("session removed for user " + userId + " - reason: " + reason.toString());
UserManager.getInstance().removeUser(userId, User.DisconnectReason.Disconnected); UserManager.getInstance().removeUser(userId, reason);
} }
synchronized void fireCallback(final ClientCallback call) { synchronized void fireCallback(final ClientCallback call) {

View file

@ -27,6 +27,9 @@
*/ */
package mage.server; package mage.server;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import mage.MageException; import mage.MageException;
import mage.server.services.LogKeys; import mage.server.services.LogKeys;
import mage.server.services.impl.LogServiceImpl; import mage.server.services.impl.LogServiceImpl;
@ -34,10 +37,6 @@ import mage.view.UserDataView;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jboss.remoting.callback.InvokerCallbackHandler; import org.jboss.remoting.callback.InvokerCallbackHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
@ -56,7 +55,13 @@ public class SessionManager {
if (sessions == null || sessionId == null) { if (sessions == null || sessionId == null) {
return null; return null;
} }
return sessions.get(sessionId); Session session = sessions.get(sessionId);
if (session != null && session.getUserId() != null && UserManager.getInstance().getUser(session.getUserId()) == null) {
logger.error("User for session " + sessionId + " with userId " + session.getUserId() + " is missing. Session removed. Cause for this still unclear.");
disconnect(sessionId, DisconnectReason.LostConnection);
return null;
}
return session;
} }
public void createSession(String sessionId, InvokerCallbackHandler callbackHandler) { public void createSession(String sessionId, InvokerCallbackHandler callbackHandler) {
@ -102,21 +107,31 @@ public class SessionManager {
return false; return false;
} }
public synchronized void disconnect(String sessionId, boolean voluntary) { public synchronized void disconnect(String sessionId, DisconnectReason reason) {
Session session = sessions.get(sessionId); Session session = sessions.get(sessionId);
sessions.remove(sessionId);
if (session != null) { if (session != null) {
if (voluntary) { sessions.remove(sessionId);
// disconnected switch (reason) {
session.kill(); case Disconnected:
session.kill(reason);
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_KILLED, sessionId); LogServiceImpl.instance.log(LogKeys.KEY_SESSION_KILLED, sessionId);
} else { break;
// lost connection case SessionExpired:
session.kill(reason);
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_EXPIRED, sessionId);
break;
case AdminDisconnect:
session.kill(reason);
break;
case LostConnection:
session.userLostConnection(); session.userLostConnection();
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_DISCONNECTED, sessionId); LogServiceImpl.instance.log(LogKeys.KEY_SESSION_DISCONNECTED, sessionId);
break;
default:
logger.error("endSession: unexpected reason " + reason.toString() + " - sessionId: "+ sessionId);
} }
} else { } else {
logger.info("disconnect: could not find session with id " + sessionId); logger.error("endSession: could not find session with id " + sessionId);
} }
} }
@ -130,14 +145,14 @@ public class SessionManager {
public void disconnectUser(String sessionId, String userSessionId) { public void disconnectUser(String sessionId, String userSessionId) {
if (isAdmin(sessionId)) { if (isAdmin(sessionId)) {
disconnect(userSessionId, true); disconnect(userSessionId, DisconnectReason.AdminDisconnect);
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_DISCONNECTED_BY_ADMIN, sessionId, userSessionId); LogServiceImpl.instance.log(LogKeys.KEY_SESSION_DISCONNECTED_BY_ADMIN, sessionId, userSessionId);
} }
} }
public void endUserSession(String sessionId, String userSessionId) { public void endUserSession(String sessionId, String userSessionId) {
if (isAdmin(sessionId)) { if (isAdmin(sessionId)) {
disconnect(userSessionId, false); disconnect(userSessionId, DisconnectReason.AdminDisconnect);
LogServiceImpl.instance.log(LogKeys.KEY_SESSION_END_BY_ADMIN, sessionId, userSessionId); LogServiceImpl.instance.log(LogKeys.KEY_SESSION_END_BY_ADMIN, sessionId, userSessionId);
} }
} }

View file

@ -28,12 +28,15 @@
package mage.server; package mage.server;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import mage.MageException; import mage.MageException;
import mage.cards.decks.Deck; import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
@ -43,7 +46,6 @@ import mage.game.Table;
import mage.game.draft.Draft; import mage.game.draft.Draft;
import mage.game.match.Match; import mage.game.match.Match;
import mage.game.match.MatchOptions; import mage.game.match.MatchOptions;
import mage.game.match.MatchPlayer;
import mage.game.tournament.Tournament; import mage.game.tournament.Tournament;
import mage.game.tournament.TournamentOptions; import mage.game.tournament.TournamentOptions;
import mage.players.Player; import mage.players.Player;
@ -51,7 +53,6 @@ import mage.server.game.GameManager;
import mage.server.game.GamesRoomManager; import mage.server.game.GamesRoomManager;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
@ -255,12 +256,6 @@ public class TableManager {
} }
} }
// public void startChallenge(UUID userId, UUID roomId, UUID tableId, UUID challengeId) {
// if (controllers.containsKey(tableId)) {
// controllers.get(tableId).startChallenge(userId, challengeId);
// }
// }
public void startTournament(UUID userId, UUID roomId, UUID tableId) { public void startTournament(UUID userId, UUID roomId, UUID tableId) {
if (controllers.containsKey(tableId)) { if (controllers.containsKey(tableId)) {
controllers.get(tableId).startTournament(userId); controllers.get(tableId).startTournament(userId);

View file

@ -62,10 +62,6 @@ public class User {
Created, Connected, Disconnected, Reconnected; Created, Connected, Disconnected, Reconnected;
} }
public enum DisconnectReason {
LostConnection, Disconnected, CleaningUp, ConnectingOtherInstance;
}
private final UUID userId; private final UUID userId;
private final String userName; private final String userName;
private final String host; private final String host;

View file

@ -38,7 +38,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import mage.server.util.ThreadExecutor; import mage.server.util.ThreadExecutor;
import mage.view.ChatMessage.MessageColor;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
@ -52,15 +51,17 @@ public class UserManager {
protected static ScheduledExecutorService expireExecutor = Executors.newSingleThreadScheduledExecutor(); protected static ScheduledExecutorService expireExecutor = Executors.newSingleThreadScheduledExecutor();
private static final UserManager INSTANCE = new UserManager();
private static final Logger logger = Logger.getLogger(UserManager.class); private static final Logger logger = Logger.getLogger(UserManager.class);
private final ConcurrentHashMap<UUID, User> users = new ConcurrentHashMap<>();
private static final ExecutorService callExecutor = ThreadExecutor.getInstance().getCallExecutor();
private static final UserManager INSTANCE = new UserManager();
public static UserManager getInstance() { public static UserManager getInstance() {
return INSTANCE; return INSTANCE;
} }
private static final ExecutorService callExecutor = ThreadExecutor.getInstance().getCallExecutor();
private UserManager() { private UserManager() {
expireExecutor.scheduleAtFixedRate(new Runnable() { expireExecutor.scheduleAtFixedRate(new Runnable() {
@Override @Override
@ -70,8 +71,6 @@ public class UserManager {
}, 60, 60, TimeUnit.SECONDS); }, 60, 60, TimeUnit.SECONDS);
} }
private final ConcurrentHashMap<UUID, User> users = new ConcurrentHashMap<>();
public User createUser(String userName, String host) { public User createUser(String userName, String host) {
if (findUser(userName) != null) { if (findUser(userName) != null) {
return null; //user already exists return null; //user already exists
@ -106,7 +105,7 @@ public class UserManager {
return false; return false;
} }
public void disconnect(UUID userId, User.DisconnectReason reason) { public void disconnect(UUID userId, DisconnectReason reason) {
if (userId != null) { if (userId != null) {
if (users.containsKey(userId)) { if (users.containsKey(userId)) {
User user = users.get(userId); User user = users.get(userId);
@ -123,7 +122,7 @@ public class UserManager {
return false; return false;
} }
public void removeUser(UUID userId, User.DisconnectReason reason) { public void removeUser(UUID userId, DisconnectReason reason) {
User user = users.get(userId); User user = users.get(userId);
if (user != null) { if (user != null) {
logger.debug(new StringBuilder("Remove user: ").append(user.getName()) logger.debug(new StringBuilder("Remove user: ").append(user.getName())
@ -166,8 +165,7 @@ public class UserManager {
if (user.isExpired(expired.getTime())) { if (user.isExpired(expired.getTime())) {
logger.info(new StringBuilder(user.getName()).append(": session expired userId: ").append(user.getId()) logger.info(new StringBuilder(user.getName()).append(": session expired userId: ").append(user.getId())
.append(" Host: ").append(user.getHost())); .append(" Host: ").append(user.getHost()));
user.kill(User.DisconnectReason.LostConnection); SessionManager.getInstance().getSession(user.getSessionId()).kill(DisconnectReason.SessionExpired);
users.remove(user.getId());
} }
} }
logger.debug("checkExpired - end"); logger.debug("checkExpired - end");

View file

@ -13,6 +13,8 @@ public interface LogKeys {
String KEY_SESSION_KILLED = "sessionKilled"; String KEY_SESSION_KILLED = "sessionKilled";
String KEY_SESSION_EXPIRED = "sessionExpired";
String KEY_SESSION_DISCONNECTED = "sessionDisconnected"; String KEY_SESSION_DISCONNECTED = "sessionDisconnected";
String KEY_SESSION_DISCONNECTED_BY_ADMIN = "sessionDisconnectedByAdmin"; String KEY_SESSION_DISCONNECTED_BY_ADMIN = "sessionDisconnectedByAdmin";