[load] one place server ping

This commit is contained in:
magenoxx 2012-05-04 22:49:04 +04:00
parent a17f8e6059
commit 1c0b807a02
8 changed files with 61 additions and 8 deletions

View file

@ -85,6 +85,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.prefs.Preferences;
/**
@ -116,6 +119,8 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
private static Map<UUID, TournamentPanel> tournaments = new HashMap<UUID, TournamentPanel>();
private static MageUI ui = new MageUI();
private static ScheduledExecutorService pingTaskExecutor = Executors.newSingleThreadScheduledExecutor();
/**
* @return the session
*/
@ -193,6 +198,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
desktopPane.add(errorDialog, JLayeredPane.POPUP_LAYER);
ui.addComponent(MageComponents.DESKTOP_PANE, desktopPane);
pingTaskExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
session.ping();
}
}, 60, 60, TimeUnit.SECONDS);
try {
tablesPane = new TablesPane();
desktopPane.add(tablesPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

View file

@ -51,6 +51,8 @@ public interface MageServer {
public boolean setUserData(String userName, String sessionId, UserDataView userDataView) throws MageException;
public ServerState getServerState() throws MageException;
public boolean ping(String sessionId) throws MageException;
//table methods
public TableView createTable(String sessionId, UUID roomId, MatchOptions matchOptions) throws MageException;

View file

@ -57,6 +57,8 @@ public interface Session {
boolean isConnected();
boolean ping();
String[] getPlayerTypes();
List<GameTypeView> getGameTypes();

View file

@ -1090,6 +1090,21 @@ public class SessionImpl implements Session {
public void setEmbeddedMageServerAction(Action embeddedMageServerAction) {
this.embeddedMageServerAction = embeddedMageServerAction;
}
@Override
public boolean ping() {
try {
if (isConnected()) {
server.ping(sessionId);
}
return true;
} catch (MageException ex) {
handleMageException(ex);
} catch (Throwable t) {
handleThrowable(t);
}
return false;
}
}
class MageAuthenticator extends Authenticator {

View file

@ -248,6 +248,11 @@ public class MageServerImpl implements MageServer {
}
return null;
}
@Override
public boolean ping(String sessionId) {
return SessionManager.getInstance().extendUserSession(sessionId);
}
@Override
public void deregisterClient(final String sessionId) throws MageException {

View file

@ -28,19 +28,17 @@
package mage.server;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import mage.MageException;
import mage.players.net.UserData;
import mage.players.net.UserGroup;
import mage.server.services.LogKeys;
import mage.server.services.LogService;
import mage.server.services.impl.LogServiceImpl;
import mage.view.UserDataView;
import org.apache.log4j.Logger;
import org.jboss.remoting.callback.InvokerCallbackHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author BetaSteward_at_googlemail.com
@ -148,4 +146,11 @@ public class SessionManager {
}
return null;
}
public boolean extendUserSession(String sessionId) {
if (sessions.containsKey(sessionId)) {
return UserManager.getInstance().extendUserSession(sessions.get(sessionId).getUserId());
}
return false;
}
}

View file

@ -177,6 +177,10 @@ public class User {
lastActivity = new Date();
GameManager.getInstance().sendPlayerInteger(gameId, userId, data);
}
public void updateLastActivity() {
lastActivity = new Date();
}
public boolean isExpired(Date expired) {
return /*userState == UserState.Disconnected && */ lastActivity.before(expired);

View file

@ -124,13 +124,21 @@ public class UserManager {
users.remove(userId);
}
}
public boolean extendUserSession(UUID userId) {
if (users.containsKey(userId)) {
users.get(userId).updateLastActivity();
return true;
}
return false;
}
private void checkExpired() {
Calendar expired = Calendar.getInstance();
expired.add(Calendar.MINUTE, -2) ;
expired.add(Calendar.MINUTE, -3) ;
for (User user: users.values()) {
if (user.isExpired(expired.getTime())) {
logger.info("user session expired " + user.getId());
logger.info(user.getName() + " session expired " + user.getId());
user.kill();
users.remove(user.getId());
}