mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 04:52:07 -08:00
more client server improvements
This commit is contained in:
parent
3311f6a1f3
commit
b676e59001
5 changed files with 33 additions and 49 deletions
|
|
@ -75,6 +75,7 @@ import javax.swing.event.PopupMenuListener;
|
|||
import mage.client.deckeditor.DeckEditorPane;
|
||||
import mage.client.draft.DraftPane;
|
||||
import mage.client.game.GamePane;
|
||||
import mage.client.remote.Session.SessionState;
|
||||
import mage.client.table.TablesPane;
|
||||
import mage.client.tournament.TournamentPane;
|
||||
import mage.game.match.MatchOptions;
|
||||
|
|
@ -710,7 +711,7 @@ public class MageFrame extends javax.swing.JFrame {
|
|||
}//GEN-LAST:event_btnExitActionPerformed
|
||||
|
||||
private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
|
||||
if (session.isConnected()) {
|
||||
if (session.getState() == SessionState.CONNECTED) {
|
||||
if (JOptionPane.showConfirmDialog(this, "Are you sure you want to disconnect?", "Confirm disconnect", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
|
||||
session.disconnect(true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ import mage.client.remote.Session;
|
|||
import mage.view.ChatMessage.MessageColor;
|
||||
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import mage.client.remote.Session.SessionState;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -79,7 +80,7 @@ public class ChatPanel extends javax.swing.JPanel {
|
|||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (session != null && session.isConnected())
|
||||
if (session != null && session.getState() == SessionState.CONNECTED)
|
||||
session.leaveChat(chatId);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -77,11 +77,16 @@ public class Session {
|
|||
private final static Logger logger = Logger.getLogger(Session.class);
|
||||
private static ScheduledExecutorService sessionExecutor = Executors.newScheduledThreadPool(1);
|
||||
|
||||
public enum SessionState {
|
||||
DISCONNECTED, CONNECTED, CONNECTING, DISCONNECTING, SERVER_UNAVAILABLE;
|
||||
}
|
||||
|
||||
private UUID sessionId;
|
||||
private Client client;
|
||||
private String userName;
|
||||
private MageFrame frame;
|
||||
private ServerState serverState;
|
||||
private SessionState sessionState = SessionState.DISCONNECTED;
|
||||
private Map<UUID, ChatPanel> chats = new HashMap<UUID, ChatPanel>();
|
||||
private Map<UUID, GamePanel> games = new HashMap<UUID, GamePanel>();
|
||||
private Map<UUID, DraftPanel> drafts = new HashMap<UUID, DraftPanel>();
|
||||
|
|
@ -90,16 +95,13 @@ public class Session {
|
|||
private ScheduledFuture<?> future;
|
||||
private MageUI ui = new MageUI();
|
||||
private Connection connection;
|
||||
private boolean reconnecting = false;
|
||||
private boolean connecting = false;
|
||||
|
||||
public Session(MageFrame frame) {
|
||||
this.frame = frame;
|
||||
}
|
||||
|
||||
public synchronized boolean connect(Connection connection) {
|
||||
this.connecting = true;
|
||||
if (this.connection != null && isConnected()) {
|
||||
if (this.connection != null && sessionState == SessionState.DISCONNECTED) {
|
||||
disconnect(true);
|
||||
}
|
||||
this.connection = connection;
|
||||
|
|
@ -107,6 +109,7 @@ public class Session {
|
|||
}
|
||||
|
||||
public boolean connect() {
|
||||
sessionState = SessionState.CONNECTING;
|
||||
try {
|
||||
System.setSecurityManager(null);
|
||||
System.setProperty("http.nonProxyHosts", "code.google.com");
|
||||
|
|
@ -140,32 +143,27 @@ public class Session {
|
|||
logger.info("Connected to RMI server at " + connection.getHost() + ":" + connection.getPort());
|
||||
frame.setStatusText("Connected to " + connection.getHost() + ":" + connection.getPort() + " ");
|
||||
frame.enableButtons();
|
||||
reconnecting = false;
|
||||
connecting = false;
|
||||
sessionState = SessionState.CONNECTED;
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("", ex);
|
||||
if (!reconnecting) {
|
||||
if (sessionState == SessionState.CONNECTING) {
|
||||
disconnect(false);
|
||||
JOptionPane.showMessageDialog(frame, "Unable to connect to server. " + ex.getMessage());
|
||||
}
|
||||
sessionState = SessionState.SERVER_UNAVAILABLE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized void disconnect(boolean voluntary) {
|
||||
sessionState = SessionState.DISCONNECTING;
|
||||
if (connection == null)
|
||||
return;
|
||||
if (reconnecting)
|
||||
return;
|
||||
if (future != null && !future.isDone())
|
||||
future.cancel(true);
|
||||
frame.setStatusText("Not connected");
|
||||
frame.disableButtons();
|
||||
// if (!voluntary && !connecting) {
|
||||
// if (attemptReconnect())
|
||||
// return;
|
||||
// }
|
||||
try {
|
||||
for (UUID chatId: chats.keySet()) {
|
||||
leaveChat(chatId);
|
||||
|
|
@ -185,32 +183,22 @@ public class Session {
|
|||
frame.hideGames();
|
||||
frame.hideTables();
|
||||
logger.info("Disconnected ... ");
|
||||
if (!voluntary && !connecting)
|
||||
if (!voluntary)
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Server error. You have been disconnected", "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
// private boolean attemptReconnect() {
|
||||
// reconnecting = true;
|
||||
// ReconnectDialog rcd = new ReconnectDialog();
|
||||
// MageFrame.getDesktop().add(rcd, JLayeredPane.MODAL_LAYER);
|
||||
// rcd.showDialog(this);
|
||||
// reconnecting = false;
|
||||
// return rcd.getResult();
|
||||
// }
|
||||
|
||||
|
||||
public boolean ping() {
|
||||
Ping method = new Ping(connection, sessionId);
|
||||
try {
|
||||
return method.makeCall();
|
||||
} catch (ServerUnavailable ex) {
|
||||
logger.fatal("server unavailable - ", ex);
|
||||
handleServerUnavailable(ex);
|
||||
} catch (MageException ex) {
|
||||
logger.fatal("ping error", ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private UUID registerClient(String userName, UUID clientId, MageVersion version) throws MageException, ServerUnavailable {
|
||||
RegisterClient method = new RegisterClient(connection, userName, clientId, version);
|
||||
return method.makeCall();
|
||||
|
|
@ -230,15 +218,18 @@ public class Session {
|
|||
try {
|
||||
return method.makeCall();
|
||||
} catch (ServerUnavailable ex) {
|
||||
logger.fatal("server unavailable - ", ex);
|
||||
handleServerUnavailable(ex);
|
||||
} catch (MageException ex) {
|
||||
logger.fatal("GetServerState error", ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SessionState getState() {
|
||||
return sessionState;
|
||||
}
|
||||
public boolean isConnected() {
|
||||
return ping();
|
||||
return sessionState == SessionState.CONNECTED;
|
||||
}
|
||||
|
||||
public String[] getPlayerTypes() {
|
||||
|
|
@ -853,28 +844,12 @@ public class Session {
|
|||
return ui;
|
||||
}
|
||||
|
||||
// public Server getServerRef() {
|
||||
// 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");
|
||||
disconnect(false);
|
||||
}
|
||||
}
|
||||
else {
|
||||
missed = 0;
|
||||
}
|
||||
ping();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue