Skip to content

Commit 6206616

Browse files
committed
Added some comments and timeouts info
1 parent 0173e95 commit 6206616

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

Mage.Server/src/main/java/mage/server/UserManager.java

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
21
package mage.server;
32

4-
import java.util.*;
5-
import java.util.concurrent.*;
6-
import java.util.concurrent.locks.Lock;
7-
import java.util.concurrent.locks.ReadWriteLock;
8-
import java.util.concurrent.locks.ReentrantReadWriteLock;
93
import mage.server.User.UserState;
104
import mage.server.record.UserStats;
115
import mage.server.record.UserStatsRepository;
126
import mage.server.util.ThreadExecutor;
137
import mage.view.UserView;
148
import org.apache.log4j.Logger;
159

10+
import java.util.*;
11+
import java.util.concurrent.*;
12+
import java.util.concurrent.locks.Lock;
13+
import java.util.concurrent.locks.ReadWriteLock;
14+
import java.util.concurrent.locks.ReentrantReadWriteLock;
15+
1616
/**
1717
* manages users - if a user is disconnected and 10 minutes have passed with no
1818
* activity the user is removed
@@ -22,6 +22,9 @@
2222
public enum UserManager {
2323
instance;
2424

25+
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)
26+
private static final int SERVER_TIMEOUTS_USER_REMOVE_FROM_SERVER_AFTER_SECS = 8 * 60; // removes from users list
27+
2528
private static final Logger logger = Logger.getLogger(UserManager.class);
2629

2730
protected final ScheduledExecutorService expireExecutor = Executors.newSingleThreadScheduledExecutor();
@@ -127,17 +130,17 @@ public void removeUserFromAllTablesAndChat(final UUID userId, final DisconnectRe
127130
if (userId != null) {
128131
getUser(userId).ifPresent(user
129132
-> USER_EXECUTOR.execute(
130-
() -> {
131-
try {
132-
logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId + " [" + user.getGameInfo() + ']');
133-
user.removeUserFromAllTables(reason);
134-
ChatManager.instance.removeUser(user.getId(), reason);
135-
logger.debug("USER REMOVE END - " + user.getName());
136-
} catch (Exception ex) {
137-
handleException(ex);
138-
}
139-
}
140-
));
133+
() -> {
134+
try {
135+
logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId + " [" + user.getGameInfo() + ']');
136+
user.removeUserFromAllTables(reason);
137+
ChatManager.instance.removeUser(user.getId(), reason);
138+
logger.debug("USER REMOVE END - " + user.getName());
139+
} catch (Exception ex) {
140+
handleException(ex);
141+
}
142+
}
143+
));
141144

142145
}
143146
}
@@ -155,16 +158,15 @@ public boolean extendUserSession(UUID userId, String pingInfo) {
155158

156159
/**
157160
* Is the connection lost for more than 3 minutes, the user will be set to
158-
* offline status. The user will be removed in validity check after 15
161+
* offline status. The user will be removed in validity check after 8
159162
* minutes of no activities
160-
*
161163
*/
162164
private void checkExpired() {
163165
try {
164166
Calendar calendarExp = Calendar.getInstance();
165-
calendarExp.add(Calendar.MINUTE, -3);
167+
calendarExp.add(Calendar.SECOND, -1 * SERVER_TIMEOUTS_USER_DISCONNECT_FROM_SERVER_AFTER_SECS);
166168
Calendar calendarRemove = Calendar.getInstance();
167-
calendarRemove.add(Calendar.MINUTE, -8);
169+
calendarRemove.add(Calendar.SECOND, -1 * SERVER_TIMEOUTS_USER_REMOVE_FROM_SERVER_AFTER_SECS);
168170
List<User> toRemove = new ArrayList<>();
169171
logger.debug("Start Check Expired");
170172
ArrayList<User> userList = new ArrayList<>();
@@ -179,18 +181,18 @@ private void checkExpired() {
179181
try {
180182
if (user.getUserState() == UserState.Offline) {
181183
if (user.isExpired(calendarRemove.getTime())) {
184+
// removes from users list
182185
toRemove.add(user);
183186
}
184187
} else {
185188
if (user.isExpired(calendarExp.getTime())) {
189+
// set disconnected status and removes from all activities (tourney/tables/games/drafts/chats)
186190
if (user.getUserState() == UserState.Connected) {
187191
user.lostConnection();
188192
disconnect(user.getId(), DisconnectReason.BecameInactive);
189193
}
190194
removeUserFromAllTablesAndChat(user.getId(), DisconnectReason.SessionExpired);
191195
user.setUserState(UserState.Offline);
192-
// Remove the user from all tournaments
193-
194196
}
195197
}
196198
} catch (Exception ex) {
@@ -215,7 +217,6 @@ private void checkExpired() {
215217

216218
/**
217219
* This method recreated the user list that will be send to all clients
218-
*
219220
*/
220221
private void updateUserInfoList() {
221222
try {

Mage.Server/src/main/java/mage/server/game/GameController.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
*/
5050
public class GameController implements GameCallback {
5151

52+
private static final int GAME_TIMEOUTS_CHECK_JOINING_STATUS_EVERY_SECS = 15; // checks and inform players about joining status
53+
private static final int GAME_TIMEOUTS_CANCEL_PLAYER_GAME_JOINING_AFTER_INACTIVE_SECS = 4 * 60; // leave player from game if it don't join and inactive on server
54+
5255
private static final ExecutorService gameExecutor = ThreadExecutor.instance.getGameExecutor();
5356
private static final Logger logger = Logger.getLogger(GameController.class);
5457

@@ -226,7 +229,7 @@ private void init() {
226229
} catch (Exception ex) {
227230
logger.fatal("Send info about player not joined yet:", ex);
228231
}
229-
}, 15, 15, TimeUnit.SECONDS);
232+
}, GAME_TIMEOUTS_CHECK_JOINING_STATUS_EVERY_SECS, GAME_TIMEOUTS_CHECK_JOINING_STATUS_EVERY_SECS, TimeUnit.SECONDS);
230233
checkStart();
231234
}
232235

@@ -320,6 +323,7 @@ private synchronized void startGame() {
320323
}
321324

322325
private void sendInfoAboutPlayersNotJoinedYet() {
326+
// runs every 15 secs untill all players join
323327
for (Player player : game.getPlayers().values()) {
324328
if (!player.hasLeft() && player.isHuman()) {
325329
Optional<User> requestedUser = getUserByPlayerId(player.getId());
@@ -333,12 +337,12 @@ private void sendInfoAboutPlayersNotJoinedYet() {
333337
logger.debug("Player " + player.getName() + " (disconnected) has joined gameId: " + game.getId());
334338
}
335339
ChatManager.instance.broadcast(chatId, player.getName(), user.getPingInfo() + " is pending to join the game", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
336-
if (user.getSecondsDisconnected() > 240) {
340+
if (user.getSecondsDisconnected() > GAME_TIMEOUTS_CANCEL_PLAYER_GAME_JOINING_AFTER_INACTIVE_SECS) {
341+
// TODO: 2019.04.22 - if user playing another game on server but not joining (that's the reason?), then that's check will never trigger
337342
// Cancel player join possibility lately after 4 minutes
338343
logger.debug("Player " + player.getName() + " - canceled game (after 240 seconds) gameId: " + game.getId());
339344
player.leave();
340345
}
341-
342346
}
343347
} else if (!player.hasLeft()) {
344348
logger.debug("Player " + player.getName() + " canceled game (no user) gameId: " + game.getId());

0 commit comments

Comments
 (0)