From c5bc99b8de9e2051d6da778bcab6f0ece3601660 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 5 Dec 2014 20:14:28 +0100 Subject: [PATCH] * Changes to session lock handling. --- .../src/main/java/mage/server/Main.java | 5 ++-- .../src/main/java/mage/server/Session.java | 26 +++++++------------ .../main/java/mage/server/TableManager.java | 4 +-- .../main/java/mage/server/UserManager.java | 3 +-- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index 29697af457..0a9099bdeb 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -213,10 +213,9 @@ public class Main { // So it should be possible to reconnect to server and continue games if DisconnectReason is set to LostConnection //SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.Disconnected); SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection); - logger.info("CLIENT DISCONNECTED - " + sessionInfo, throwable); + logger.info("CLIENT DISCONNECTED - " + sessionInfo); logger.debug("Stack Trace", throwable); - } - else { + } else { SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection); logger.info("LOST CONNECTION - " + sessionInfo); if (logger.isDebugEnabled()) { diff --git a/Mage.Server/src/main/java/mage/server/Session.java b/Mage.Server/src/main/java/mage/server/Session.java index a805028954..a2d8019e0e 100644 --- a/Mage.Server/src/main/java/mage/server/Session.java +++ b/Mage.Server/src/main/java/mage/server/Session.java @@ -83,6 +83,10 @@ public class Session { return returnMessage; } + public boolean isLocked() { + return lock.isLocked(); + } + public String registerUserHandling(String userName) throws MageException { this.isAdmin = false; if (userName.equals("Admin")) { @@ -216,6 +220,7 @@ public class Session { try { if(lock.tryLock(500, TimeUnit.MILLISECONDS)) { lockSet = true; + logger.debug("SESSION LOCK SET sessionId: " + sessionId); User user = UserManager.getInstance().getUser(userId); if (user == null || !user.isConnected()) { return; //user was already disconnected by other thread @@ -236,6 +241,7 @@ public class Session { finally { if (lockSet) { lock.unlock(); + logger.debug("SESSION LOCK UNLOCK sessionId: " + sessionId); } } @@ -246,6 +252,7 @@ public class Session { try { if(lock.tryLock(500, TimeUnit.MILLISECONDS)) { lockSet = true; + logger.debug("SESSION LOCK SET sessionId: " + sessionId); UserManager.getInstance().removeUser(userId, reason); } else { logger.error("SESSION LOCK - kill: userId " + userId); @@ -256,22 +263,17 @@ public class Session { finally { if (lockSet) { lock.unlock(); + logger.debug("SESSION LOCK UNLOCK sessionId: " + sessionId); + } } } public void fireCallback(final ClientCallback call) { - boolean lockSet = false; try { - if (lock.tryLock(500, TimeUnit.MILLISECONDS)) { - lockSet = true; call.setMessageId(messageId++); callbackHandler.handleCallbackOneway(new Callback(call)); - } else { - logger.error("SESSION LOCK callback: userId " + userId); - logger.error(" - method: " + call.getMethod()); - } } catch (HandleCallbackException ex) { User user = UserManager.getInstance().getUser(userId); logger.warn("SESSION CALLBACK EXCEPTION - " + (user != null ? user.getName():"") + " userId " + userId); @@ -279,16 +281,6 @@ public class Session { logger.warn(" - cause: " + getBasicCause(ex).toString()); logger.trace("Stack trace:", ex); userLostConnection(); - } catch (InterruptedException ex) { - logger.error("SESSION LOCK callback -userId: " + userId); - logger.error(" - method: " + call.getMethod()); - logger.error(" - cause: " + getBasicCause(ex).toString()); - logger.trace("Stack trace:", ex); - } - finally { - if (lockSet) { - lock.unlock(); - } } } diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index fbdbf543bb..5176cd214f 100644 --- a/Mage.Server/src/main/java/mage/server/TableManager.java +++ b/Mage.Server/src/main/java/mage/server/TableManager.java @@ -362,9 +362,9 @@ public class TableManager { public void debugServerState() { logger.debug("--- Server state ----------------------------------------------"); Collection users = UserManager.getInstance().getUsers(); - logger.debug("--------User: " + users.size() + " [userId | since | name -----------------------"); + logger.debug("--------User: " + users.size() + " [userId | since | lock | name -----------------------"); for (User user :users) { - logger.debug(user.getId() + " | " + formatter.format(user.getConnectionTime()) + " | " + user.getName() +" (" +user.getUserState().toString() + " - " + user.getPingInfo() + ")"); + logger.debug(user.getId() + " | " + formatter.format(user.getConnectionTime()) + " | " + (SessionManager.getInstance().getSession(user.getSessionId()).isLocked()?"L":"-") + " | " + user.getName() +" (" +user.getUserState().toString() + " - " + user.getPingInfo() + ")"); } ArrayList chatSessions = ChatManager.getInstance().getChatSessions(); logger.debug("------- ChatSessions: " + chatSessions.size() + " ----------------------------------"); diff --git a/Mage.Server/src/main/java/mage/server/UserManager.java b/Mage.Server/src/main/java/mage/server/UserManager.java index 710dd1a357..f1d2e1e32b 100644 --- a/Mage.Server/src/main/java/mage/server/UserManager.java +++ b/Mage.Server/src/main/java/mage/server/UserManager.java @@ -142,7 +142,7 @@ public class UserManager { @Override public void run() { try { - logger.debug("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId); + logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId); user.remove(reason); logger.debug("USER REMOVE END - " + user.getName()); } catch (Exception ex) { @@ -180,7 +180,6 @@ public class UserManager { usersToCheck.addAll(users.values()); for (User user : usersToCheck) { if (!user.getUserState().equals(UserState.Expired) && user.isExpired(calendar.getTime())) { - logger.info(user.getName() + ": session expired userId: "+ user.getId() + " Host: " + user.getHost()); removeUser(user.getId(), DisconnectReason.SessionExpired); } }