Issue#424: Client now detects connection loss and asks for reconnecting

This commit is contained in:
magenoxx 2014-04-28 22:02:29 +05:30
parent 9021449970
commit 23e1f9196c
5 changed files with 67 additions and 37 deletions

View file

@ -669,14 +669,21 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
} }
public boolean autoConnect() { public boolean autoConnect() {
boolean autoConnect = Boolean.parseBoolean(prefs.get("autoConnect", "false")); boolean autoConnectParamValue = Boolean.parseBoolean(prefs.get("autoConnect", "false"));
if (autoConnect) { boolean status = false;
if (autoConnectParamValue) {
status = performConnect();
}
return status;
}
private boolean performConnect() {
String userName = prefs.get("userName", ""); String userName = prefs.get("userName", "");
String server = prefs.get("serverAddress", ""); String server = prefs.get("serverAddress", "");
int port = Integer.parseInt(prefs.get("serverPort", "")); int port = Integer.parseInt(prefs.get("serverPort", ""));
String proxyServer = prefs.get("proxyAddress", ""); String proxyServer = prefs.get("proxyAddress", "");
int proxyPort = Integer.parseInt(prefs.get("proxyPort", "")); int proxyPort = Integer.parseInt(prefs.get("proxyPort", ""));
ProxyType proxyType = Connection.ProxyType.valueByText(prefs.get("proxyType", "None")); ProxyType proxyType = ProxyType.valueByText(prefs.get("proxyType", "None"));
String proxyUsername = prefs.get("proxyUsername", ""); String proxyUsername = prefs.get("proxyUsername", "");
String proxyPassword = prefs.get("proxyPassword", ""); String proxyPassword = prefs.get("proxyPassword", "");
int avatarId = PreferencesDialog.getSelectedAvatar(); int avatarId = PreferencesDialog.getSelectedAvatar();
@ -703,7 +710,6 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
} finally { } finally {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} }
}
return false; return false;
} }
@ -943,6 +949,15 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
System.exit(0); System.exit(0);
} }
public void reconnect() {
session.disconnect(false);
tablesPane.clearChat();
disableButtons();
if (performConnect()) {
enableButtons();
}
}
public void enableButtons() { public void enableButtons() {
btnConnect.setEnabled(true); btnConnect.setEnabled(true);
btnConnect.setText("Disconnect"); btnConnect.setText("Disconnect");

View file

@ -1,8 +1,5 @@
package mage.client.game; package mage.client.game;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import javax.swing.SwingUtilities;
import mage.client.components.MageUI; import mage.client.components.MageUI;
import mage.interfaces.MageClient; import mage.interfaces.MageClient;
import mage.interfaces.callback.ClientCallback; import mage.interfaces.callback.ClientCallback;
@ -13,6 +10,9 @@ import mage.utils.MageVersion;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.junit.Ignore; import org.junit.Ignore;
import javax.swing.*;
import java.util.concurrent.CountDownLatch;
/** /**
* Test for emulating the connection from multi mage clients. * Test for emulating the connection from multi mage clients.
* *
@ -74,6 +74,11 @@ public class MultiConnectTest {
logger.info("disconnected"); logger.info("disconnected");
} }
@Override
public void reconnect() {
logger.info("Not implemented");
}
@Override @Override
public void showMessage(String message) { public void showMessage(String message) {
logger.info("showMessage: " + message); logger.info("showMessage: " + message);

View file

@ -40,6 +40,7 @@ public interface MageClient extends CallbackClient {
MageVersion getVersion(); MageVersion getVersion();
void connected(String message); void connected(String message);
void disconnected(); void disconnected();
void reconnect();
void showMessage(String message); void showMessage(String message);
void showError(String message); void showError(String message);

View file

@ -54,6 +54,7 @@ import org.jboss.remoting.transport.bisocket.Bisocket;
import org.jboss.remoting.transport.socket.SocketWrapper; import org.jboss.remoting.transport.socket.SocketWrapper;
import org.jboss.remoting.transporter.TransporterClient; import org.jboss.remoting.transporter.TransporterClient;
import javax.swing.*;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
@ -1286,6 +1287,9 @@ public class SessionImpl implements Session {
if (isConnected()) { if (isConnected()) {
if (!server.ping(sessionId)) { if (!server.ping(sessionId)) {
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()); 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());
if (JOptionPane.showConfirmDialog(null, "The connection to server was lost. Reconnect?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
client.reconnect();
}
} }
} }
return true; return true;

View file

@ -257,6 +257,11 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient {
} }
} }
@Override
public void reconnect() {
logger.info("Not implemented.");
}
@Override @Override
public void showMessage(final String message) { public void showMessage(final String message) {
if (SwingUtilities.isEventDispatchThread()) { if (SwingUtilities.isEventDispatchThread()) {