Added some comments and timeouts info

This commit is contained in:
Oleg Agafonov 2019-04-22 15:58:32 +04:00
parent 0173e95736
commit 6206616af4
2 changed files with 32 additions and 27 deletions

View file

@ -1,11 +1,5 @@
package mage.server;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import mage.server.User.UserState;
import mage.server.record.UserStats;
import mage.server.record.UserStatsRepository;
@ -13,6 +7,12 @@ import mage.server.util.ThreadExecutor;
import mage.view.UserView;
import org.apache.log4j.Logger;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* manages users - if a user is disconnected and 10 minutes have passed with no
* activity the user is removed
@ -22,6 +22,9 @@ import org.apache.log4j.Logger;
public enum UserManager {
instance;
private static final int SERVER_TIMEOUTS_USER_DISCONNECT_FROM_SERVER_AFTER_SECS = 3 * 60; // removes from all games and chats too (can be seen in users list with disconnected status)
private static final int SERVER_TIMEOUTS_USER_REMOVE_FROM_SERVER_AFTER_SECS = 8 * 60; // removes from users list
private static final Logger logger = Logger.getLogger(UserManager.class);
protected final ScheduledExecutorService expireExecutor = Executors.newSingleThreadScheduledExecutor();
@ -127,17 +130,17 @@ public enum UserManager {
if (userId != null) {
getUser(userId).ifPresent(user
-> USER_EXECUTOR.execute(
() -> {
try {
logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId + " [" + user.getGameInfo() + ']');
user.removeUserFromAllTables(reason);
ChatManager.instance.removeUser(user.getId(), reason);
logger.debug("USER REMOVE END - " + user.getName());
} catch (Exception ex) {
handleException(ex);
}
}
));
() -> {
try {
logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId + " [" + user.getGameInfo() + ']');
user.removeUserFromAllTables(reason);
ChatManager.instance.removeUser(user.getId(), reason);
logger.debug("USER REMOVE END - " + user.getName());
} catch (Exception ex) {
handleException(ex);
}
}
));
}
}
@ -155,16 +158,15 @@ public enum UserManager {
/**
* Is the connection lost for more than 3 minutes, the user will be set to
* offline status. The user will be removed in validity check after 15
* offline status. The user will be removed in validity check after 8
* minutes of no activities
*
*/
private void checkExpired() {
try {
Calendar calendarExp = Calendar.getInstance();
calendarExp.add(Calendar.MINUTE, -3);
calendarExp.add(Calendar.SECOND, -1 * SERVER_TIMEOUTS_USER_DISCONNECT_FROM_SERVER_AFTER_SECS);
Calendar calendarRemove = Calendar.getInstance();
calendarRemove.add(Calendar.MINUTE, -8);
calendarRemove.add(Calendar.SECOND, -1 * SERVER_TIMEOUTS_USER_REMOVE_FROM_SERVER_AFTER_SECS);
List<User> toRemove = new ArrayList<>();
logger.debug("Start Check Expired");
ArrayList<User> userList = new ArrayList<>();
@ -179,18 +181,18 @@ public enum UserManager {
try {
if (user.getUserState() == UserState.Offline) {
if (user.isExpired(calendarRemove.getTime())) {
// removes from users list
toRemove.add(user);
}
} else {
if (user.isExpired(calendarExp.getTime())) {
// set disconnected status and removes from all activities (tourney/tables/games/drafts/chats)
if (user.getUserState() == UserState.Connected) {
user.lostConnection();
disconnect(user.getId(), DisconnectReason.BecameInactive);
}
removeUserFromAllTablesAndChat(user.getId(), DisconnectReason.SessionExpired);
user.setUserState(UserState.Offline);
// Remove the user from all tournaments
}
}
} catch (Exception ex) {
@ -215,7 +217,6 @@ public enum UserManager {
/**
* This method recreated the user list that will be send to all clients
*
*/
private void updateUserInfoList() {
try {