forked from External/mage
Backed out changeset: b5d9cea65b73
This commit is contained in:
parent
ab599dd335
commit
92d42c16b5
7 changed files with 20 additions and 105 deletions
|
|
@ -37,9 +37,6 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
@ -72,7 +69,6 @@ import mage.view.TournamentView;
|
||||||
public class Session {
|
public class Session {
|
||||||
|
|
||||||
private final static Logger logger = Logging.getLogger(Session.class.getName());
|
private final static Logger logger = Logging.getLogger(Session.class.getName());
|
||||||
private static ScheduledExecutorService sessionExecutor = Executors.newScheduledThreadPool(1);
|
|
||||||
|
|
||||||
private UUID sessionId;
|
private UUID sessionId;
|
||||||
private Server server;
|
private Server server;
|
||||||
|
|
@ -116,7 +112,6 @@ public class Session {
|
||||||
sessionId = server.registerClient(userName, client.getId(), frame.getVersion());
|
sessionId = server.registerClient(userName, client.getId(), frame.getVersion());
|
||||||
callbackDaemon = new CallbackClientDaemon(sessionId, client, server);
|
callbackDaemon = new CallbackClientDaemon(sessionId, client, server);
|
||||||
serverState = server.getServerState();
|
serverState = server.getServerState();
|
||||||
sessionExecutor.scheduleWithFixedDelay(new ServerPinger(), 15, 5, TimeUnit.SECONDS);
|
|
||||||
logger.info("Connected to RMI server at " + serverName + ":" + port);
|
logger.info("Connected to RMI server at " + serverName + ":" + port);
|
||||||
frame.setStatusText("Connected to " + serverName + ":" + port + " ");
|
frame.setStatusText("Connected to " + serverName + ":" + port + " ");
|
||||||
frame.enableButtons();
|
frame.enableButtons();
|
||||||
|
|
@ -139,6 +134,7 @@ public class Session {
|
||||||
|
|
||||||
if (isConnected()) {
|
if (isConnected()) {
|
||||||
try {
|
try {
|
||||||
|
frame.hideTables();
|
||||||
for (UUID chatId: chats.keySet()) {
|
for (UUID chatId: chats.keySet()) {
|
||||||
server.leaveChat(chatId, sessionId);
|
server.leaveChat(chatId, sessionId);
|
||||||
}
|
}
|
||||||
|
|
@ -149,25 +145,18 @@ public class Session {
|
||||||
try {
|
try {
|
||||||
//TODO: stop daemon
|
//TODO: stop daemon
|
||||||
server.deregisterClient(sessionId);
|
server.deregisterClient(sessionId);
|
||||||
|
server = null;
|
||||||
|
logger.info("Disconnected ... ");
|
||||||
} catch (RemoteException ex) {
|
} catch (RemoteException ex) {
|
||||||
logger.log(Level.SEVERE, "Error disconnecting ...", ex);
|
logger.log(Level.SEVERE, "Error disconnecting ...", ex);
|
||||||
} catch (MageException ex) {
|
} catch (MageException ex) {
|
||||||
logger.log(Level.SEVERE, "Error disconnecting ...", ex);
|
logger.log(Level.SEVERE, "Error disconnecting ...", ex);
|
||||||
}
|
}
|
||||||
removeServer();
|
frame.setStatusText("Not connected ");
|
||||||
|
frame.disableButtons();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeServer() {
|
|
||||||
sessionExecutor.shutdownNow();
|
|
||||||
server = null;
|
|
||||||
frame.hideTables();
|
|
||||||
frame.setStatusText("Not connected");
|
|
||||||
frame.disableButtons();
|
|
||||||
logger.info("Disconnected ... ");
|
|
||||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Disconnected.", "Disconnected", JOptionPane.INFORMATION_MESSAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ack(String message) {
|
public void ack(String message) {
|
||||||
try {
|
try {
|
||||||
server.ack(message, sessionId);
|
server.ack(message, sessionId);
|
||||||
|
|
@ -178,17 +167,6 @@ public class Session {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean ping() {
|
|
||||||
try {
|
|
||||||
return server.ping(sessionId);
|
|
||||||
} catch (RemoteException ex) {
|
|
||||||
handleRemoteException(ex);
|
|
||||||
} catch (MageException ex) {
|
|
||||||
handleMageException(ex);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return server != null;
|
return server != null;
|
||||||
}
|
}
|
||||||
|
|
@ -728,7 +706,14 @@ public class Session {
|
||||||
|
|
||||||
private void handleRemoteException(RemoteException ex) {
|
private void handleRemoteException(RemoteException ex) {
|
||||||
logger.log(Level.SEVERE, "Communication error", ex);
|
logger.log(Level.SEVERE, "Communication error", ex);
|
||||||
removeServer();
|
if (ex instanceof java.rmi.ConnectException) {
|
||||||
|
server = null;
|
||||||
|
frame.setStatusText("Not connected");
|
||||||
|
frame.disableButtons();
|
||||||
|
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Communication error - disconnecting.", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Communication error.", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleMageException(MageException ex) {
|
private void handleMageException(MageException ex) {
|
||||||
|
|
@ -756,24 +741,4 @@ public class Session {
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ServerPinger implements Runnable {
|
}
|
||||||
|
|
||||||
private int missed = 0;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (!ping()) {
|
|
||||||
missed++;
|
|
||||||
if (missed > 10) {
|
|
||||||
logger.info("Connection to server timed out");
|
|
||||||
removeServer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
missed = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -190,11 +190,10 @@ public class TablesPanel extends javax.swing.JPanel {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void hideTables() {
|
public void hideTables() {
|
||||||
if (tableWaitingDialog != null && tableWaitingDialog.isVisible()) {
|
if (tableWaitingDialog.isVisible()) {
|
||||||
tableWaitingDialog.closeDialog();
|
tableWaitingDialog.closeDialog();
|
||||||
}
|
}
|
||||||
if (updateTask != null)
|
updateTask.cancel(true);
|
||||||
updateTask.cancel(true);
|
|
||||||
this.chatPanel.disconnect();
|
this.chatPanel.disconnect();
|
||||||
|
|
||||||
Component c = this.getParent();
|
Component c = this.getParent();
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,6 @@ public interface Server extends Remote, CallbackServer {
|
||||||
public void ack(String message, UUID sessionId) throws RemoteException, MageException;
|
public void ack(String message, UUID sessionId) throws RemoteException, MageException;
|
||||||
|
|
||||||
public ServerState getServerState() throws RemoteException, MageException;
|
public ServerState getServerState() throws RemoteException, MageException;
|
||||||
public boolean ping(UUID sessionId) throws RemoteException, MageException;
|
|
||||||
|
|
||||||
//table methods
|
//table methods
|
||||||
public TableView createTable(UUID sessionId, UUID roomId, MatchOptions matchOptions) throws RemoteException, MageException;
|
public TableView createTable(UUID sessionId, UUID roomId, MatchOptions matchOptions) throws RemoteException, MageException;
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ public class ChatManager {
|
||||||
chatSessions.get(chatId).broadcast(userName, message, color);
|
chatSessions.get(chatId).broadcast(userName, message, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeSession(UUID sessionId) {
|
void removeSession(UUID sessionId) {
|
||||||
for (ChatSession chat: chatSessions.values()) {
|
for (ChatSession chat: chatSessions.values()) {
|
||||||
chat.kill(sessionId);
|
chat.kill(sessionId);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,6 @@ import mage.game.tournament.TournamentOptions;
|
||||||
import mage.interfaces.Server;
|
import mage.interfaces.Server;
|
||||||
import mage.interfaces.ServerState;
|
import mage.interfaces.ServerState;
|
||||||
import mage.interfaces.callback.ClientCallback;
|
import mage.interfaces.callback.ClientCallback;
|
||||||
import mage.server.Session;
|
|
||||||
import mage.server.game.DeckValidatorFactory;
|
import mage.server.game.DeckValidatorFactory;
|
||||||
import mage.server.draft.DraftManager;
|
import mage.server.draft.DraftManager;
|
||||||
import mage.server.game.GameFactory;
|
import mage.server.game.GameFactory;
|
||||||
|
|
@ -106,16 +105,6 @@ public class ServerImpl extends RemoteServer implements Server {
|
||||||
SessionManager.getInstance().getSession(sessionId).ack(message);
|
SessionManager.getInstance().getSession(sessionId).ack(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean ping(UUID sessionId) {
|
|
||||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
|
||||||
if (session != null) {
|
|
||||||
session.ping();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UUID registerClient(String userName, UUID clientId, MageVersion version) throws MageException, RemoteException {
|
public UUID registerClient(String userName, UUID clientId, MageVersion version) throws MageException, RemoteException {
|
||||||
|
|
||||||
|
|
@ -252,8 +241,8 @@ public class ServerImpl extends RemoteServer implements Server {
|
||||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
session.kill();
|
session.kill();
|
||||||
logger.info("Client deregistered ...");
|
|
||||||
}
|
}
|
||||||
|
logger.info("Client deregistered ...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@
|
||||||
|
|
||||||
package mage.server;
|
package mage.server;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import mage.cards.decks.Deck;
|
import mage.cards.decks.Deck;
|
||||||
|
|
@ -51,7 +50,6 @@ public class Session {
|
||||||
private String username;
|
private String username;
|
||||||
private int messageId = 0;
|
private int messageId = 0;
|
||||||
private String ackMessage;
|
private String ackMessage;
|
||||||
private long lastPing;
|
|
||||||
private final CallbackServerSession callback = new CallbackServerSession();
|
private final CallbackServerSession callback = new CallbackServerSession();
|
||||||
|
|
||||||
public Session(String userName, UUID clientId) {
|
public Session(String userName, UUID clientId) {
|
||||||
|
|
@ -139,12 +137,4 @@ public class Session {
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ping() {
|
|
||||||
this.lastPing = System.currentTimeMillis();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean stillAlive() {
|
|
||||||
return (System.currentTimeMillis() - lastPing) < 60000;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,6 @@ package mage.server;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import mage.interfaces.MageException;
|
import mage.interfaces.MageException;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
|
@ -44,20 +41,15 @@ public class SessionManager {
|
||||||
|
|
||||||
private final static Logger logger = Logger.getLogger(SessionManager.class);
|
private final static Logger logger = Logger.getLogger(SessionManager.class);
|
||||||
private final static SessionManager INSTANCE = new SessionManager();
|
private final static SessionManager INSTANCE = new SessionManager();
|
||||||
private static ScheduledExecutorService sessionExecutor;
|
|
||||||
|
|
||||||
public static SessionManager getInstance() {
|
public static SessionManager getInstance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected SessionManager() {
|
|
||||||
sessionExecutor = Executors.newScheduledThreadPool(1);
|
|
||||||
sessionExecutor.scheduleWithFixedDelay(new SessionChecker(), 30, 10, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ConcurrentHashMap<UUID, Session> sessions = new ConcurrentHashMap<UUID, Session>();
|
private ConcurrentHashMap<UUID, Session> sessions = new ConcurrentHashMap<UUID, Session>();
|
||||||
|
|
||||||
public Session getSession(UUID sessionId) {
|
public Session getSession(UUID sessionId) {
|
||||||
|
if (sessions == null || sessionId == null) return null;
|
||||||
return sessions.get(sessionId);
|
return sessions.get(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,24 +74,5 @@ public class SessionManager {
|
||||||
public void removeSession(UUID sessionId) {
|
public void removeSession(UUID sessionId) {
|
||||||
sessions.remove(sessionId);
|
sessions.remove(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkSessions() {
|
|
||||||
for (Session session: sessions.values()) {
|
|
||||||
if (!session.stillAlive()) {
|
|
||||||
logger.info("Client for user " + session.getUsername() + " timed out - releasing resources");
|
|
||||||
session.kill();
|
|
||||||
sessions.remove(session.getId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SessionChecker implements Runnable {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
checkSessions();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue