* Changes to session lock handling.

This commit is contained in:
LevelX2 2014-12-05 20:14:28 +01:00
parent cee5d0d487
commit c5bc99b8de
4 changed files with 14 additions and 24 deletions

View file

@ -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 // 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.Disconnected);
SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection); SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection);
logger.info("CLIENT DISCONNECTED - " + sessionInfo, throwable); logger.info("CLIENT DISCONNECTED - " + sessionInfo);
logger.debug("Stack Trace", throwable); logger.debug("Stack Trace", throwable);
} } else {
else {
SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection); SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection);
logger.info("LOST CONNECTION - " + sessionInfo); logger.info("LOST CONNECTION - " + sessionInfo);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {

View file

@ -83,6 +83,10 @@ public class Session {
return returnMessage; return returnMessage;
} }
public boolean isLocked() {
return lock.isLocked();
}
public String registerUserHandling(String userName) throws MageException { public String registerUserHandling(String userName) throws MageException {
this.isAdmin = false; this.isAdmin = false;
if (userName.equals("Admin")) { if (userName.equals("Admin")) {
@ -216,6 +220,7 @@ public class Session {
try { try {
if(lock.tryLock(500, TimeUnit.MILLISECONDS)) { if(lock.tryLock(500, TimeUnit.MILLISECONDS)) {
lockSet = true; lockSet = true;
logger.debug("SESSION LOCK SET sessionId: " + sessionId);
User user = UserManager.getInstance().getUser(userId); User user = UserManager.getInstance().getUser(userId);
if (user == null || !user.isConnected()) { if (user == null || !user.isConnected()) {
return; //user was already disconnected by other thread return; //user was already disconnected by other thread
@ -236,6 +241,7 @@ public class Session {
finally { finally {
if (lockSet) { if (lockSet) {
lock.unlock(); lock.unlock();
logger.debug("SESSION LOCK UNLOCK sessionId: " + sessionId);
} }
} }
@ -246,6 +252,7 @@ public class Session {
try { try {
if(lock.tryLock(500, TimeUnit.MILLISECONDS)) { if(lock.tryLock(500, TimeUnit.MILLISECONDS)) {
lockSet = true; lockSet = true;
logger.debug("SESSION LOCK SET sessionId: " + sessionId);
UserManager.getInstance().removeUser(userId, reason); UserManager.getInstance().removeUser(userId, reason);
} else { } else {
logger.error("SESSION LOCK - kill: userId " + userId); logger.error("SESSION LOCK - kill: userId " + userId);
@ -256,22 +263,17 @@ public class Session {
finally { finally {
if (lockSet) { if (lockSet) {
lock.unlock(); lock.unlock();
logger.debug("SESSION LOCK UNLOCK sessionId: " + sessionId);
} }
} }
} }
public void fireCallback(final ClientCallback call) { public void fireCallback(final ClientCallback call) {
boolean lockSet = false;
try { try {
if (lock.tryLock(500, TimeUnit.MILLISECONDS)) {
lockSet = true;
call.setMessageId(messageId++); call.setMessageId(messageId++);
callbackHandler.handleCallbackOneway(new Callback(call)); callbackHandler.handleCallbackOneway(new Callback(call));
} else {
logger.error("SESSION LOCK callback: userId " + userId);
logger.error(" - method: " + call.getMethod());
}
} catch (HandleCallbackException ex) { } catch (HandleCallbackException ex) {
User user = UserManager.getInstance().getUser(userId); User user = UserManager.getInstance().getUser(userId);
logger.warn("SESSION CALLBACK EXCEPTION - " + (user != null ? user.getName():"") + " userId " + 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.warn(" - cause: " + getBasicCause(ex).toString());
logger.trace("Stack trace:", ex); logger.trace("Stack trace:", ex);
userLostConnection(); 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();
}
} }
} }

View file

@ -362,9 +362,9 @@ public class TableManager {
public void debugServerState() { public void debugServerState() {
logger.debug("--- Server state ----------------------------------------------"); logger.debug("--- Server state ----------------------------------------------");
Collection<User> users = UserManager.getInstance().getUsers(); Collection<User> 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) { 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<ChatSession> chatSessions = ChatManager.getInstance().getChatSessions(); ArrayList<ChatSession> chatSessions = ChatManager.getInstance().getChatSessions();
logger.debug("------- ChatSessions: " + chatSessions.size() + " ----------------------------------"); logger.debug("------- ChatSessions: " + chatSessions.size() + " ----------------------------------");

View file

@ -142,7 +142,7 @@ public class UserManager {
@Override @Override
public void run() { public void run() {
try { try {
logger.debug("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId); logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId);
user.remove(reason); user.remove(reason);
logger.debug("USER REMOVE END - " + user.getName()); logger.debug("USER REMOVE END - " + user.getName());
} catch (Exception ex) { } catch (Exception ex) {
@ -180,7 +180,6 @@ public class UserManager {
usersToCheck.addAll(users.values()); usersToCheck.addAll(users.values());
for (User user : usersToCheck) { for (User user : usersToCheck) {
if (!user.getUserState().equals(UserState.Expired) && user.isExpired(calendar.getTime())) { 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); removeUser(user.getId(), DisconnectReason.SessionExpired);
} }
} }