Some minor changes to server connect handling and logging.

This commit is contained in:
LevelX2 2017-04-17 18:28:35 +02:00
parent 804fb12b1d
commit 8ad957447e
3 changed files with 32 additions and 30 deletions

View file

@ -27,6 +27,10 @@
*/ */
package mage.server; 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.MageException;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo; import mage.cards.repository.CardInfo;
@ -65,11 +69,6 @@ import mage.view.ChatMessage.MessageColor;
import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.log4j.Logger; 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 * @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 { public boolean connectUser(String userName, String password, String sessionId, MageVersion version, String userIdStr) throws MageException {
try { try {
if (version.compareTo(Main.getVersion()) != 0) { 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()); throw new MageVersionException(version, Main.getVersion());
} }
return SessionManager.instance.connectUser(sessionId, userName, password, userIdStr); return SessionManager.instance.connectUser(sessionId, userName, password, userIdStr);
@ -1118,9 +1117,9 @@ public class MageServerImpl implements MageServer {
@Override @Override
public void toggleActivation(final String sessionId, final String userName) throws MageException { public void toggleActivation(final String sessionId, final String userName) throws MageException {
execute("toggleActivation", sessionId, () -> execute("toggleActivation", sessionId, ()
UserManager.instance.getUserByName(userName).ifPresent(user -> -> UserManager.instance.getUserByName(userName).ifPresent(user
{ -> {
user.setActive(!user.isActive()); user.setActive(!user.isActive());
if (!user.isActive() && user.isConnected()) { if (!user.isActive() && user.isConnected()) {
SessionManager.instance.disconnectUser(sessionId, user.getSessionId()); SessionManager.instance.disconnectUser(sessionId, user.getSessionId());
@ -1158,11 +1157,9 @@ public class MageServerImpl implements MageServer {
@Override @Override
public void sendFeedbackMessage(final String sessionId, final String username, final String title, final String type, final String message, final String email) throws MageException { 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) { if (title != null && message != null) {
execute("sendFeedbackMessage", sessionId, () -> execute("sendFeedbackMessage", sessionId, ()
SessionManager.instance.getSession(sessionId).ifPresent( -> SessionManager.instance.getSession(sessionId).ifPresent(
session -> FeedbackServiceImpl.instance.feedback(username, title, type, message, email, session.getHost()) 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<Object> { private static class MyActionWithNullNegativeResult extends ActionWithNullNegativeResult<Object> {
@Override @Override
public Object execute() throws MageException { public Object execute() throws MageException {
return CompressUtil.compress(ServerMessagesUtil.instance.getMessages()); return CompressUtil.compress(ServerMessagesUtil.instance.getMessages());
@ -1260,6 +1258,7 @@ public class MageServerImpl implements MageServer {
} }
private static class ListActionWithNullNegativeResult extends ActionWithNullNegativeResult<List<UserView>> { private static class ListActionWithNullNegativeResult extends ActionWithNullNegativeResult<List<UserView>> {
@Override @Override
public List<UserView> execute() throws MageException { public List<UserView> execute() throws MageException {
List<UserView> users = new ArrayList<>(); List<UserView> users = new ArrayList<>();
@ -1282,6 +1281,7 @@ public class MageServerImpl implements MageServer {
} }
private static class GameViewActionWithNullNegativeResult extends ActionWithNullNegativeResult<GameView> { private static class GameViewActionWithNullNegativeResult extends ActionWithNullNegativeResult<GameView> {
private final String sessionId; private final String sessionId;
private final UUID gameId; private final UUID gameId;
private final UUID playerId; private final UUID playerId;
@ -1306,6 +1306,7 @@ public class MageServerImpl implements MageServer {
} }
private static class MyActionWithBooleanResult extends ActionWithBooleanResult { private static class MyActionWithBooleanResult extends ActionWithBooleanResult {
private final String sessionId; private final String sessionId;
private final UUID tableId; private final UUID tableId;
@ -1328,6 +1329,7 @@ public class MageServerImpl implements MageServer {
} }
private static class DraftPickViewActionWithNullNegativeResult extends ActionWithNullNegativeResult<DraftPickView> { private static class DraftPickViewActionWithNullNegativeResult extends ActionWithNullNegativeResult<DraftPickView> {
private final String sessionId; private final String sessionId;
private final UUID draftId; private final UUID draftId;
private final UUID cardPick; private final UUID cardPick;
@ -1353,6 +1355,7 @@ public class MageServerImpl implements MageServer {
} }
private static class MyActionWithTableViewResult extends ActionWithTableViewResult { private static class MyActionWithTableViewResult extends ActionWithTableViewResult {
private final String sessionId; private final String sessionId;
private final MatchOptions options; private final MatchOptions options;
private final UUID roomId; private final UUID roomId;

View file

@ -265,7 +265,7 @@ public final class Main {
String sessionId = client.getSessionId(); String sessionId = client.getSessionId();
Optional<Session> session = SessionManager.instance.getSession(sessionId); Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) { if (!session.isPresent()) {
logger.error("Session not found : " + sessionId); logger.trace("Session not found : " + sessionId);
} else { } else {
UUID userId = session.get().getUserId(); UUID userId = session.get().getUserId();
StringBuilder sessionInfo = new StringBuilder(); StringBuilder sessionInfo = new StringBuilder();

View file

@ -31,14 +31,12 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import mage.MageException; import mage.MageException;
import mage.players.net.UserData; import mage.players.net.UserData;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jboss.remoting.callback.InvokerCallbackHandler; import org.jboss.remoting.callback.InvokerCallbackHandler;
import javax.annotation.Nonnull;
/** /**
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
@ -46,13 +44,12 @@ public enum SessionManager {
instance; instance;
private static final Logger logger = Logger.getLogger(SessionManager.class); private static final Logger logger = Logger.getLogger(SessionManager.class);
private final ConcurrentHashMap<String, Session> sessions = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, Session> sessions = new ConcurrentHashMap<>();
public Optional<Session> getSession(@Nonnull String sessionId) { public Optional<Session> getSession(@Nonnull String sessionId) {
Session session = sessions.get(sessionId); Session session = sessions.get(sessionId);
if (session == null) { if (session == null) {
logger.error("Session with sessionId " + sessionId + " is not found"); logger.trace("Session with sessionId " + sessionId + " is not found");
return Optional.empty(); return Optional.empty();
} }
if (session.getUserId() != null && UserManager.instance.getUser(session.getUserId()) == null) { if (session.getUserId() != null && UserManager.instance.getUser(session.getUserId()) == null) {
@ -136,8 +133,10 @@ public enum SessionManager {
logger.debug("DISCONNECT " + reason.toString() + " - sessionId: " + sessionId); logger.debug("DISCONNECT " + reason.toString() + " - sessionId: " + sessionId);
sessions.remove(sessionId); sessions.remove(sessionId);
switch (reason) { switch (reason) {
case Disconnected: // regular session end case Disconnected: // regular session end or wrong client version
if (session.getUserId() != null) { // if wrong client version no userId is set
session.kill(reason); session.kill(reason);
}
break; break;
case SessionExpired: // session ends after no reconnect happens in the defined time span case SessionExpired: // session ends after no reconnect happens in the defined time span
session.kill(reason); session.kill(reason);