1
0
Fork 0
mirror of https://github.com/correl/mage.git synced 2025-04-12 09:11:05 -09:00

More changes to user and session handling. Fixes somes issues with client reconnect and connecting other instance.

This commit is contained in:
LevelX2 2014-08-29 01:46:53 +02:00
parent 180bae323d
commit df04bf829d
5 changed files with 47 additions and 17 deletions

View file

@ -62,10 +62,11 @@ public class ChatManager {
}
public void joinChat(UUID chatId, UUID userId) {
if (chatSessions.containsKey(chatId)) {
chatSessions.get(chatId).join(userId);
ChatSession chatSession = chatSessions.get(chatId);
if (chatSession != null) {
chatSession.join(userId);
} else {
logger.trace("User could not join chatId: " + chatId +" userId: " + userId);
logger.trace("Chat to join not found - chatId: " + chatId +" userId: " + userId);
}
}
@ -77,12 +78,19 @@ public class ChatManager {
}
public void destroyChatSession(UUID chatId) {
if (chatId != null && chatSessions.containsKey(chatId)) {
chatSessions.remove(chatId);
logger.debug("Chat removed - chatId: " + chatId);
} else {
logger.trace("Chat to destroy does not exist - chatId: " + chatId);
}
if (chatId != null) {
ChatSession chatSession = chatSessions.get(chatId);
if (chatSession != null) {
synchronized (chatSession) {
if (chatSessions.containsKey(chatId)) {
chatSessions.remove(chatId);
logger.debug("Chat removed - chatId: " + chatId);
} else {
logger.trace("Chat to destroy does not exist - chatId: " + chatId);
}
}
}
}
}
public void broadcast(UUID chatId, String userName, String message, MessageColor color) {

View file

@ -213,7 +213,11 @@ public class Main {
SessionManager.getInstance().disconnect(client.getSessionId(), DisconnectReason.LostConnection);
logger.info("CONNECTION LOST - " + sessionInfo, throwable);
if (logger.isDebugEnabled()) {
throwable.printStackTrace();
if (throwable == null) {
logger.debug("Lease expired");
} else {
throwable.printStackTrace();
}
}
}
}

View file

@ -38,6 +38,7 @@ import mage.MageException;
import mage.interfaces.callback.ClientCallback;
import mage.players.net.UserData;
import mage.players.net.UserGroup;
import mage.server.game.GamesRoomManager;
import mage.server.util.ConfigSettings;
import mage.view.UserDataView;
import org.apache.log4j.Logger;
@ -94,20 +95,19 @@ public class Session {
return new StringBuilder("User name '").append(userName).append("' includes not allowed characters: use a-z, A-Z and 0-9").toString();
}
User user = UserManager.getInstance().createUser(userName, host);
boolean reconnect = false;
if (user == null) { // user already exists
user = UserManager.getInstance().findUser(userName);
if (user.getHost().equals(host)) {
user.updateLastActivity(); // minimizes possible expiration
this.userId = user.getId();
if (user.getSessionId().isEmpty()) {
// TODO Send Chat message to tables (user is not registered yet)
ChatManager.getInstance().sendReconnectMessage(user.getId());
logger.info("Reconnecting session for " + userName);
reconnect = true;
} else {
//throw new MageException("This machine is already connected");
//disconnect previous one
//disconnect previous session
logger.info("Disconnecting another user instance: " + userName);
UserManager.getInstance().disconnect(user.getId(), DisconnectReason.ConnectingOtherInstance);
SessionManager.getInstance().disconnect(user.getSessionId(), DisconnectReason.ConnectingOtherInstance);
}
} else {
return new StringBuilder("User name ").append(userName).append(" already in use (or your IP address changed)").toString();
@ -117,6 +117,13 @@ public class Session {
return new StringBuilder("Error connecting ").append(userName).toString();
}
this.userId = user.getId();
if (reconnect) { // must be connected to receive the message
UUID chatId = GamesRoomManager.getInstance().getRoom(GamesRoomManager.getInstance().getMainRoomId()).getChatId();
if (chatId != null) {
ChatManager.getInstance().joinChat(chatId, userId);
}
ChatManager.getInstance().sendReconnectMessage(userId);
}
return null;
}
@ -202,6 +209,11 @@ public class Session {
if (user == null || !user.isConnected()) {
return; //user was already disconnected by other thread
}
if (!user.getSessionId().equals(sessionId)) {
// user already reconnected with another instance
logger.info("OLD SESSION IGNORED - " + user.getName());
return;
}
logger.info("LOST CONNECTION - " + user.getName());
UserManager.getInstance().disconnect(userId, DisconnectReason.LostConnection);
}

View file

@ -240,6 +240,9 @@ public class User {
public void updateLastActivity() {
lastActivity = new Date();
if (userState == UserState.Disconnected) { // this can happen if user reconnects very fast after disconnect
userState = UserState.Reconnected;
}
}
public boolean isExpired(Date expired) {

View file

@ -192,8 +192,11 @@ public class UserManager {
}
public void handleException(Exception ex) {
if (ex != null && !ex.getMessage().equals("No message")) {
logger.fatal("", ex);
if (ex != null) {
logger.fatal("User manager exception");
ex.printStackTrace();
}else {
logger.fatal("User manager exception - null");
}
}
}