Added connection speed information per user (milliseconds the ping needs). Some minor changes to server console.

This commit is contained in:
LevelX2 2014-08-31 17:46:14 +02:00
parent 1285df5da3
commit b98c16f061
20 changed files with 166 additions and 54 deletions

View file

@ -74,7 +74,7 @@ public final class Constants {
public static final int PRIORITY_TIME_SEC = 1200;
public enum SessionState {
DISCONNECTED, CONNECTED, CONNECTING, DISCONNECTING, SERVER_UNAVAILABLE, SERVER_STARTING;
DISCONNECTED, CONNECTED, CONNECTING, DISCONNECTING, SERVER_STARTING;
}
public enum Option {

View file

@ -47,7 +47,6 @@ import mage.view.TournamentView;
import mage.view.UserDataView;
import mage.view.RoomUsersView;
import mage.view.UserView;
import mage.view.UsersView;
/**
*
@ -76,7 +75,7 @@ public interface MageServer {
Object getServerMessagesCompressed(String sessionId) throws MageException; // messages of the day
// ping - extends session
boolean ping(String sessionId) throws MageException;
boolean ping(String sessionId, String pingInfo) throws MageException;
//table methods
TableView createTable(String sessionId, UUID roomId, MatchOptions matchOptions) throws MageException;

View file

@ -57,6 +57,8 @@ import org.jboss.remoting.transporter.TransporterClient;
import java.net.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
*
@ -75,7 +77,9 @@ public class SessionImpl implements Session {
private ServerState serverState;
private SessionState sessionState = SessionState.DISCONNECTED;
private Connection connection;
private final static int PING_CYCLES = 10;
private final LinkedList<Long> pingTime = new LinkedList<>();
private String pingInfo = "";
private static boolean debugMode = false;
private boolean canceled = false;
@ -303,11 +307,11 @@ public class SessionImpl implements Session {
}
@Override
public synchronized void disconnect(boolean showMessage) {
public synchronized void disconnect(boolean errorCall) {
if (isConnected()) {
sessionState = SessionState.DISCONNECTING;
}
if (connection == null) {
if (connection == null || sessionState == SessionState.DISCONNECTED) {
return;
}
try {
@ -320,10 +324,11 @@ public class SessionImpl implements Session {
if (sessionState == SessionState.DISCONNECTING || sessionState == SessionState.CONNECTING) {
sessionState = SessionState.DISCONNECTED;
logger.info("Disconnected ... ");
}
client.disconnected();
if (showMessage) {
client.showError("Network error. You have been disconnected");
client.disconnected();
if (errorCall) {
client.showError("Network error. You have been disconnected");
}
pingTime.clear();
}
}
@ -1305,7 +1310,6 @@ public class SessionImpl implements Session {
private void handleThrowable(Throwable t) {
logger.fatal("Communication error", t);
sessionState = SessionState.SERVER_UNAVAILABLE;
disconnect(true);
}
@ -1350,13 +1354,27 @@ public class SessionImpl implements Session {
public boolean ping() {
try {
if (isConnected()) {
if (!server.ping(sessionId)) {
long startTime = System.nanoTime();
if (!server.ping(sessionId, pingInfo)) {
logger.error(new StringBuilder("Ping failed: ").append(this.getUserName()).append(" Session: ").append(sessionId).append(" to MAGE server at ").append(connection.getHost()).append(":").append(connection.getPort()).toString());
throw new MageException("Ping failed");
}
pingTime.add(System.nanoTime() - startTime);
long milliSeconds = TimeUnit.MILLISECONDS.convert(pingTime.getLast(), TimeUnit.NANOSECONDS);
String lastPing = milliSeconds > 0 ? milliSeconds+"ms" : "<1ms";
if (pingTime.size() > PING_CYCLES) {
pingTime.poll();
}
long sum = 0;
for (Long time :pingTime) {
sum += time;
}
milliSeconds = TimeUnit.MILLISECONDS.convert(sum / pingTime.size(), TimeUnit.NANOSECONDS);
pingInfo = lastPing + " (Av: " + (milliSeconds > 0 ? milliSeconds + "ms":"<1ms")+")";
}
return true;
} catch (MageException ex) {
handleMageException(ex);
handleMageException(ex);
} catch (Throwable t) {
handleThrowable(t);
}

View file

@ -31,6 +31,7 @@ import java.io.Serializable;
import java.util.Date;
/**
* Admin Console View
*
* @author BetaSteward_at_googlemail.com
*/
@ -40,12 +41,14 @@ public class UserView implements Serializable {
private final String host;
private final String sessionId;
private final Date timeConnected;
private final String gameInfo;
public UserView(String userName, String host, String sessionId, Date timeConnected) {
public UserView(String userName, String host, String sessionId, Date timeConnected, String gameInfo) {
this.userName = userName;
this.host = host;
this.sessionId = sessionId;
this.timeConnected = timeConnected;
this.gameInfo = gameInfo;
}
public String getUserName() {
@ -64,4 +67,8 @@ public class UserView implements Serializable {
return timeConnected;
}
public String getGameInfo() {
return gameInfo;
}
}

View file

@ -40,11 +40,13 @@ public class UsersView implements Serializable {
private final String userName;
private final String infoState;
private final String infoGames;
private final String infoPing;
public UsersView(String userName, String infoState, String infoGames) {
public UsersView(String userName, String infoState, String infoGames, String infoPing) {
this.userName = userName;
this.infoState = infoState;
this.infoGames = infoGames;
this.infoPing = infoPing;
}
public String getUserName() {
@ -59,4 +61,8 @@ public class UsersView implements Serializable {
return infoGames;
}
public String getInfoPing() {
return infoPing;
}
}