From 8ad957447e571325c1d940e3265f55386526a8a5 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Mon, 17 Apr 2017 18:28:35 +0200 Subject: [PATCH] Some minor changes to server connect handling and logging. --- .../main/java/mage/server/MageServerImpl.java | 43 ++++++++++--------- .../src/main/java/mage/server/Main.java | 2 +- .../main/java/mage/server/SessionManager.java | 17 ++++---- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index ef727c93a4..0d75682167 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -27,6 +27,10 @@ */ package mage.server; +import java.security.SecureRandom; +import java.util.*; +import java.util.concurrent.ExecutorService; +import javax.management.timer.Timer; import mage.MageException; import mage.cards.decks.DeckCardLists; import mage.cards.repository.CardInfo; @@ -65,11 +69,6 @@ import mage.view.ChatMessage.MessageColor; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.log4j.Logger; -import javax.management.timer.Timer; -import java.security.SecureRandom; -import java.util.*; -import java.util.concurrent.ExecutorService; - /** * @author BetaSteward_at_googlemail.com, noxx */ @@ -163,7 +162,7 @@ public class MageServerImpl implements MageServer { public boolean connectUser(String userName, String password, String sessionId, MageVersion version, String userIdStr) throws MageException { try { if (version.compareTo(Main.getVersion()) != 0) { - logger.info("MageVersionException: userName=" + userName + ", version=" + version); + logger.info("MageVersionException: userName=" + userName + ", version=" + version + " sessionId=" + sessionId); throw new MageVersionException(version, Main.getVersion()); } return SessionManager.instance.connectUser(sessionId, userName, password, userIdStr); @@ -1118,14 +1117,14 @@ public class MageServerImpl implements MageServer { @Override public void toggleActivation(final String sessionId, final String userName) throws MageException { - execute("toggleActivation", sessionId, () -> - UserManager.instance.getUserByName(userName).ifPresent(user -> - { - user.setActive(!user.isActive()); - if (!user.isActive() && user.isConnected()) { - SessionManager.instance.disconnectUser(sessionId, user.getSessionId()); - } - })); + execute("toggleActivation", sessionId, () + -> UserManager.instance.getUserByName(userName).ifPresent(user + -> { + user.setActive(!user.isActive()); + if (!user.isActive() && user.isConnected()) { + SessionManager.instance.disconnectUser(sessionId, user.getSessionId()); + } + })); } @Override @@ -1158,12 +1157,10 @@ public class MageServerImpl implements MageServer { @Override public void sendFeedbackMessage(final String sessionId, final String username, final String title, final String type, final String message, final String email) throws MageException { if (title != null && message != null) { - execute("sendFeedbackMessage", sessionId, () -> - SessionManager.instance.getSession(sessionId).ifPresent( - session -> FeedbackServiceImpl.instance.feedback(username, title, type, message, email, session.getHost()) - - - )); + execute("sendFeedbackMessage", sessionId, () + -> SessionManager.instance.getSession(sessionId).ifPresent( + session -> FeedbackServiceImpl.instance.feedback(username, title, type, message, email, session.getHost()) + )); } } @@ -1253,6 +1250,7 @@ public class MageServerImpl implements MageServer { } private static class MyActionWithNullNegativeResult extends ActionWithNullNegativeResult { + @Override public Object execute() throws MageException { return CompressUtil.compress(ServerMessagesUtil.instance.getMessages()); @@ -1260,6 +1258,7 @@ public class MageServerImpl implements MageServer { } private static class ListActionWithNullNegativeResult extends ActionWithNullNegativeResult> { + @Override public List execute() throws MageException { List users = new ArrayList<>(); @@ -1282,6 +1281,7 @@ public class MageServerImpl implements MageServer { } private static class GameViewActionWithNullNegativeResult extends ActionWithNullNegativeResult { + private final String sessionId; private final UUID gameId; private final UUID playerId; @@ -1306,6 +1306,7 @@ public class MageServerImpl implements MageServer { } private static class MyActionWithBooleanResult extends ActionWithBooleanResult { + private final String sessionId; private final UUID tableId; @@ -1328,6 +1329,7 @@ public class MageServerImpl implements MageServer { } private static class DraftPickViewActionWithNullNegativeResult extends ActionWithNullNegativeResult { + private final String sessionId; private final UUID draftId; private final UUID cardPick; @@ -1353,6 +1355,7 @@ public class MageServerImpl implements MageServer { } private static class MyActionWithTableViewResult extends ActionWithTableViewResult { + private final String sessionId; private final MatchOptions options; private final UUID roomId; diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index 713517fc59..6bdf365894 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -265,7 +265,7 @@ public final class Main { String sessionId = client.getSessionId(); Optional session = SessionManager.instance.getSession(sessionId); if (!session.isPresent()) { - logger.error("Session not found : " + sessionId); + logger.trace("Session not found : " + sessionId); } else { UUID userId = session.get().getUserId(); StringBuilder sessionInfo = new StringBuilder(); diff --git a/Mage.Server/src/main/java/mage/server/SessionManager.java b/Mage.Server/src/main/java/mage/server/SessionManager.java index b1df7969f3..5ddc333875 100644 --- a/Mage.Server/src/main/java/mage/server/SessionManager.java +++ b/Mage.Server/src/main/java/mage/server/SessionManager.java @@ -31,14 +31,12 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; - +import javax.annotation.Nonnull; import mage.MageException; import mage.players.net.UserData; import org.apache.log4j.Logger; import org.jboss.remoting.callback.InvokerCallbackHandler; -import javax.annotation.Nonnull; - /** * @author BetaSteward_at_googlemail.com */ @@ -46,13 +44,12 @@ public enum SessionManager { instance; private static final Logger logger = Logger.getLogger(SessionManager.class); - private final ConcurrentHashMap sessions = new ConcurrentHashMap<>(); public Optional getSession(@Nonnull String sessionId) { Session session = sessions.get(sessionId); - if(session == null){ - logger.error("Session with sessionId " + sessionId + " is not found"); + if (session == null) { + logger.trace("Session with sessionId " + sessionId + " is not found"); return Optional.empty(); } if (session.getUserId() != null && UserManager.instance.getUser(session.getUserId()) == null) { @@ -136,8 +133,10 @@ public enum SessionManager { logger.debug("DISCONNECT " + reason.toString() + " - sessionId: " + sessionId); sessions.remove(sessionId); switch (reason) { - case Disconnected: // regular session end - session.kill(reason); + case Disconnected: // regular session end or wrong client version + if (session.getUserId() != null) { // if wrong client version no userId is set + session.kill(reason); + } break; case SessionExpired: // session ends after no reconnect happens in the defined time span session.kill(reason); @@ -173,7 +172,7 @@ public enum SessionManager { public void disconnectUser(String sessionId, String userSessionId) { if (isAdmin(sessionId)) { getUserFromSession(sessionId).ifPresent(admin -> { - Optional u = getUserFromSession(userSessionId); + Optional u = getUserFromSession(userSessionId); if (u.isPresent()) { User user = u.get(); user.showUserMessage("Admin operation", "Your session was disconnected by Admin.");