mirror of
https://github.com/magefree/mage.git
synced 2025-12-22 03:22:00 -08:00
[load] one place server ping
This commit is contained in:
parent
e62f06cc54
commit
a746ca680b
8 changed files with 61 additions and 8 deletions
|
|
@ -85,6 +85,9 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.prefs.Preferences;
|
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 Map<UUID, TournamentPanel> tournaments = new HashMap<UUID, TournamentPanel>();
|
||||||
private static MageUI ui = new MageUI();
|
private static MageUI ui = new MageUI();
|
||||||
|
|
||||||
|
private static ScheduledExecutorService pingTaskExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the session
|
* @return the session
|
||||||
*/
|
*/
|
||||||
|
|
@ -193,6 +198,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
|
||||||
desktopPane.add(errorDialog, JLayeredPane.POPUP_LAYER);
|
desktopPane.add(errorDialog, JLayeredPane.POPUP_LAYER);
|
||||||
ui.addComponent(MageComponents.DESKTOP_PANE, desktopPane);
|
ui.addComponent(MageComponents.DESKTOP_PANE, desktopPane);
|
||||||
|
|
||||||
|
pingTaskExecutor.scheduleAtFixedRate(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
session.ping();
|
||||||
|
}
|
||||||
|
}, 60, 60, TimeUnit.SECONDS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
tablesPane = new TablesPane();
|
tablesPane = new TablesPane();
|
||||||
desktopPane.add(tablesPane, javax.swing.JLayeredPane.DEFAULT_LAYER);
|
desktopPane.add(tablesPane, javax.swing.JLayeredPane.DEFAULT_LAYER);
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ public interface MageServer {
|
||||||
|
|
||||||
public boolean setUserData(String userName, String sessionId, UserDataView userDataView) throws MageException;
|
public boolean setUserData(String userName, String sessionId, UserDataView userDataView) throws MageException;
|
||||||
public ServerState getServerState() throws MageException;
|
public ServerState getServerState() throws MageException;
|
||||||
|
|
||||||
|
public boolean ping(String sessionId) throws MageException;
|
||||||
|
|
||||||
//table methods
|
//table methods
|
||||||
public TableView createTable(String sessionId, UUID roomId, MatchOptions matchOptions) throws MageException;
|
public TableView createTable(String sessionId, UUID roomId, MatchOptions matchOptions) throws MageException;
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ public interface Session {
|
||||||
|
|
||||||
boolean isConnected();
|
boolean isConnected();
|
||||||
|
|
||||||
|
boolean ping();
|
||||||
|
|
||||||
String[] getPlayerTypes();
|
String[] getPlayerTypes();
|
||||||
|
|
||||||
List<GameTypeView> getGameTypes();
|
List<GameTypeView> getGameTypes();
|
||||||
|
|
|
||||||
|
|
@ -1090,6 +1090,21 @@ public class SessionImpl implements Session {
|
||||||
public void setEmbeddedMageServerAction(Action embeddedMageServerAction) {
|
public void setEmbeddedMageServerAction(Action embeddedMageServerAction) {
|
||||||
this.embeddedMageServerAction = 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 {
|
class MageAuthenticator extends Authenticator {
|
||||||
|
|
|
||||||
|
|
@ -248,6 +248,11 @@ public class MageServerImpl implements MageServer {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean ping(String sessionId) {
|
||||||
|
return SessionManager.getInstance().extendUserSession(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deregisterClient(final String sessionId) throws MageException {
|
public void deregisterClient(final String sessionId) throws MageException {
|
||||||
|
|
|
||||||
|
|
@ -28,19 +28,17 @@
|
||||||
|
|
||||||
package mage.server;
|
package mage.server;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import mage.MageException;
|
import mage.MageException;
|
||||||
import mage.players.net.UserData;
|
|
||||||
import mage.players.net.UserGroup;
|
|
||||||
import mage.server.services.LogKeys;
|
import mage.server.services.LogKeys;
|
||||||
import mage.server.services.LogService;
|
|
||||||
import mage.server.services.impl.LogServiceImpl;
|
import mage.server.services.impl.LogServiceImpl;
|
||||||
import mage.view.UserDataView;
|
import mage.view.UserDataView;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.jboss.remoting.callback.InvokerCallbackHandler;
|
import org.jboss.remoting.callback.InvokerCallbackHandler;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author BetaSteward_at_googlemail.com
|
* @author BetaSteward_at_googlemail.com
|
||||||
|
|
@ -148,4 +146,11 @@ public class SessionManager {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean extendUserSession(String sessionId) {
|
||||||
|
if (sessions.containsKey(sessionId)) {
|
||||||
|
return UserManager.getInstance().extendUserSession(sessions.get(sessionId).getUserId());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -177,6 +177,10 @@ public class User {
|
||||||
lastActivity = new Date();
|
lastActivity = new Date();
|
||||||
GameManager.getInstance().sendPlayerInteger(gameId, userId, data);
|
GameManager.getInstance().sendPlayerInteger(gameId, userId, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateLastActivity() {
|
||||||
|
lastActivity = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isExpired(Date expired) {
|
public boolean isExpired(Date expired) {
|
||||||
return /*userState == UserState.Disconnected && */ lastActivity.before(expired);
|
return /*userState == UserState.Disconnected && */ lastActivity.before(expired);
|
||||||
|
|
|
||||||
|
|
@ -124,13 +124,21 @@ public class UserManager {
|
||||||
users.remove(userId);
|
users.remove(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean extendUserSession(UUID userId) {
|
||||||
|
if (users.containsKey(userId)) {
|
||||||
|
users.get(userId).updateLastActivity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void checkExpired() {
|
private void checkExpired() {
|
||||||
Calendar expired = Calendar.getInstance();
|
Calendar expired = Calendar.getInstance();
|
||||||
expired.add(Calendar.MINUTE, -2) ;
|
expired.add(Calendar.MINUTE, -3) ;
|
||||||
for (User user: users.values()) {
|
for (User user: users.values()) {
|
||||||
if (user.isExpired(expired.getTime())) {
|
if (user.isExpired(expired.getTime())) {
|
||||||
logger.info("user session expired " + user.getId());
|
logger.info(user.getName() + " session expired " + user.getId());
|
||||||
user.kill();
|
user.kill();
|
||||||
users.remove(user.getId());
|
users.remove(user.getId());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue