From 5770dd6014ce753e2a36e928f000a9a4416dd5f0 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Thu, 14 Apr 2016 23:43:08 +0200 Subject: [PATCH] Added some lost and reconnect statistics to footer data. --- Mage.Common/src/mage/utils/CompressUtil.java | 14 ++++--- .../src/main/java/mage/server/User.java | 37 +++++++++------- .../mage/server/util/ServerMessagesUtil.java | 42 +++++++++++++++---- 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Mage.Common/src/mage/utils/CompressUtil.java b/Mage.Common/src/mage/utils/CompressUtil.java index dcc6dc4bf4..0ec83936cb 100644 --- a/Mage.Common/src/mage/utils/CompressUtil.java +++ b/Mage.Common/src/mage/utils/CompressUtil.java @@ -11,8 +11,8 @@ import mage.remote.traffic.ZippedObjectImpl; public class CompressUtil { /** - * Defines should data be compressed or not. True by default. - * Read from system property: + * Defines should data be compressed or not. True by default. Read from + * system property: */ private static boolean compressData = true; @@ -28,10 +28,12 @@ public class CompressUtil { /** * Hidden constructor */ - private CompressUtil() {} + private CompressUtil() { + } /** - * Decompress data, but only if it was compressed previously return original object otherwise. + * Decompress data, but only if it was compressed previously return original + * object otherwise. * * @param data Data to decompress * @return Decompressed object @@ -40,7 +42,7 @@ public class CompressUtil { if (data == null || !(data instanceof ZippedObject)) { return data; } - return ((ZippedObject)data).unzip(); + return ((ZippedObject) data).unzip(); } /** @@ -51,7 +53,7 @@ public class CompressUtil { */ public static Object compress(Object data) { if (data != null && compressData) { - return new ZippedObjectImpl(data); + return new ZippedObjectImpl<>(data); } return null; } diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index f223cdfcec..17a07913c8 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -53,6 +53,7 @@ import mage.server.record.UserStatsRepository; import mage.server.tournament.TournamentController; import mage.server.tournament.TournamentManager; import mage.server.tournament.TournamentSession; +import mage.server.util.ServerMessagesUtil; import mage.server.util.SystemUtil; import mage.view.TableClientMessage; import org.apache.log4j.Logger; @@ -63,7 +64,7 @@ import org.apache.log4j.Logger; */ public class User { - private static final Logger LOGGER = Logger.getLogger(User.class); + private static final Logger logger = Logger.getLogger(User.class); public enum UserState { @@ -131,15 +132,15 @@ public class User { if (sessionId.isEmpty()) { userState = UserState.Disconnected; lostConnection(); - LOGGER.trace("USER - lost connection: " + userName + " id: " + userId); + logger.trace("USER - lost connection: " + userName + " id: " + userId); } else if (userState == UserState.Created) { userState = UserState.Connected; - LOGGER.trace("USER - created: " + userName + " id: " + userId); + logger.trace("USER - created: " + userName + " id: " + userId); } else { userState = UserState.Reconnected; reconnect(); - LOGGER.trace("USER - reconnected: " + userName + " id: " + userId); + logger.trace("USER - reconnected: " + userName + " id: " + userId); } } @@ -150,6 +151,7 @@ public class User { GameManager.getInstance().stopWatching(gameId, userId); iterator.remove(); } + ServerMessagesUtil.getInstance().incLostConnection(); } public boolean isConnected() { @@ -275,17 +277,18 @@ public class User { public boolean isExpired(Date expired) { if (lastActivity.before(expired)) { - LOGGER.trace(userName + " is expired!"); + logger.trace(userName + " is expired!"); userState = UserState.Expired; return true; } - LOGGER.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString()); + logger.trace("isExpired: User " + userName + " lastActivity: " + lastActivity + " expired: " + expired); return false; /*userState == UserState.Disconnected && */ } private void reconnect() { + logger.trace(userName + " started reconnect"); for (Entry entry : tables.entrySet()) { ccJoinedTable(entry.getValue().getRoomId(), entry.getValue().getId(), entry.getValue().isTournament()); } @@ -316,6 +319,8 @@ public class User { TableController controller = TableManager.getInstance().getController(entry.getKey()); ccSideboard(entry.getValue(), entry.getKey(), controller.getRemainingTime(), controller.getOptions().isLimited()); } + ServerMessagesUtil.getInstance().incReconnects(); + logger.trace(userName + " ended reconnect"); } public void addGame(UUID playerId, GameSessionPlayer gameSession) { @@ -363,35 +368,35 @@ public class User { } public void remove(DisconnectReason reason) { - LOGGER.trace("REMOVE " + getName() + " Draft sessions " + draftSessions.size()); + logger.trace("REMOVE " + getName() + " Draft sessions " + draftSessions.size()); for (DraftSession draftSession : draftSessions.values()) { draftSession.setKilled(); } draftSessions.clear(); - LOGGER.trace("REMOVE " + getName() + " Tournament sessions " + userTournaments.size()); + logger.trace("REMOVE " + getName() + " Tournament sessions " + userTournaments.size()); for (UUID tournamentId : userTournaments.values()) { TournamentManager.getInstance().quit(tournamentId, getId()); } userTournaments.clear(); - LOGGER.trace("REMOVE " + getName() + " Tables " + tables.size()); + logger.trace("REMOVE " + getName() + " Tables " + tables.size()); for (Entry entry : tables.entrySet()) { - LOGGER.debug("-- leave tableId: " + entry.getValue().getId()); + logger.debug("-- leave tableId: " + entry.getValue().getId()); TableManager.getInstance().leaveTable(userId, entry.getValue().getId()); } tables.clear(); - LOGGER.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size()); + logger.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size()); for (GameSessionPlayer gameSessionPlayer : gameSessions.values()) { - LOGGER.debug("-- kill game session of gameId: " + gameSessionPlayer.getGameId()); + logger.debug("-- kill game session of gameId: " + gameSessionPlayer.getGameId()); GameManager.getInstance().quitMatch(gameSessionPlayer.getGameId(), userId); gameSessionPlayer.quitGame(); } gameSessions.clear(); - LOGGER.trace("REMOVE " + getName() + " watched Games " + watchedGames.size()); + logger.trace("REMOVE " + getName() + " watched Games " + watchedGames.size()); for (UUID gameId : watchedGames) { GameManager.getInstance().stopWatching(gameId, userId); } watchedGames.clear(); - LOGGER.trace("REMOVE " + getName() + " Chats "); + logger.trace("REMOVE " + getName() + " Chats "); ChatManager.getInstance().removeUser(userId, reason); } @@ -450,11 +455,11 @@ public class User { } } else { // can happen if tournamet has just ended - LOGGER.debug(getName() + " tournament player missing - tableId:" + table.getId(), null); + logger.debug(getName() + " tournament player missing - tableId:" + table.getId(), null); tablesToDelete.add(tableEntry.getKey()); } } else { - LOGGER.error(getName() + " tournament key missing - tableId: " + table.getId(), null); + logger.error(getName() + " tournament key missing - tableId: " + table.getId(), null); } } else { switch (table.getState()) { diff --git a/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java b/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java index 15161135f6..7ba4ca5cb4 100644 --- a/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java +++ b/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java @@ -24,11 +24,9 @@ * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. -*/ + */ package mage.server.util; -import org.apache.log4j.Logger; - import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @@ -41,10 +39,11 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.log4j.Logger; /** - * Handles server messages (Messages of the Day). - * Reloads messages every 5 minutes. + * Handles server messages (Messages of the Day). Reloads messages every 5 + * minutes. * * @author nantuko */ @@ -66,6 +65,8 @@ public class ServerMessagesUtil { private static long startDate; private static final AtomicInteger gamesStarted = new AtomicInteger(0); private static final AtomicInteger tournamentsStarted = new AtomicInteger(0); + private static final AtomicInteger lostConnection = new AtomicInteger(0); + private static final AtomicInteger reconnects = new AtomicInteger(0); static { pathToExternalMessages = System.getProperty("messagesPath"); @@ -102,6 +103,7 @@ public class ServerMessagesUtil { newMessages.addAll(motdMessages); } newMessages.add(getServerStatistics()); + newMessages.add(getServerStatistics2()); lock.writeLock().lock(); try { @@ -166,7 +168,7 @@ public class ServerMessagesUtil { private String getServerStatistics() { long current = System.currentTimeMillis(); - long hours = ((current - startDate)/(1000*60*60)); + long hours = ((current - startDate) / (1000 * 60 * 60)); StringBuilder statistics = new StringBuilder("Server uptime: "); statistics.append(hours); statistics.append(" hour(s), games played: "); @@ -176,12 +178,25 @@ public class ServerMessagesUtil { return statistics.toString(); } + private String getServerStatistics2() { + long current = System.currentTimeMillis(); + long minutes = ((current - startDate) / (1000 * 60)); + if (minutes == 0) { + minutes = 1; + } + StringBuilder statistics = new StringBuilder("Disconnects: "); + statistics.append(lostConnection.get()); + statistics.append(" avg/hour ").append(lostConnection.get() * 60 / minutes); + statistics.append(" Reconnects: ").append(reconnects.get()); + statistics.append(" avg/hour ").append(reconnects.get() * 60 / minutes); + return statistics.toString(); + } + // private Timer timer = new Timer(1000 * 60, new ActionListener() { // public void actionPerformed(ActionEvent e) { // reloadMessages(); // } // }); - public void setStartDate(long milliseconds) { this.startDate = milliseconds; } @@ -200,5 +215,18 @@ public class ServerMessagesUtil { } while (!tournamentsStarted.compareAndSet(value, value + 1)); } + public void incReconnects() { + int value; + do { + value = reconnects.get(); + } while (!reconnects.compareAndSet(value, value + 1)); + } + + public void incLostConnection() { + int value; + do { + value = lostConnection.get(); + } while (!lostConnection.compareAndSet(value, value + 1)); + } }