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

View file

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

View file

@ -40,6 +40,7 @@ public interface MageClient extends CallbackClient {
MageVersion getVersion();
void connected(String message);
void disconnected();
void reconnect();
void showMessage(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.transporter.TransporterClient;
import javax.swing.*;
import java.net.*;
import java.util.*;
@ -1286,6 +1287,9 @@ public class SessionImpl implements Session {
if (isConnected()) {
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());
if (JOptionPane.showConfirmDialog(null, "The connection to server was lost. Reconnect?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
client.reconnect();
}
}
}
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
public void showMessage(final String message) {
if (SwingUtilities.isEventDispatchThread()) {