more client fixes

This commit is contained in:
BetaSteward 2011-06-02 23:09:21 -04:00
parent 3ef090b6f5
commit d6e239586b
5 changed files with 54 additions and 42 deletions

View file

@ -733,7 +733,8 @@ public class MageFrame extends javax.swing.JFrame implements Client {
private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
if (session.getState() == SessionState.CONNECTED) { 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) { if (JOptionPane.showConfirmDialog(this, "Are you sure you want to disconnect?", "Confirm disconnect", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
session.disconnect(true); session.disconnect(false);
showMessage("You have disconnected");
} }
} else { } else {
connectDialog.showDialog(); connectDialog.showDialog();
@ -747,7 +748,7 @@ public class MageFrame extends javax.swing.JFrame implements Client {
}//GEN-LAST:event_btnAboutActionPerformed }//GEN-LAST:event_btnAboutActionPerformed
public void exitApp() { public void exitApp() {
session.disconnect(true); session.disconnect(false);
Plugins.getInstance().shutdown(); Plugins.getInstance().shutdown();
dispose(); dispose();
System.exit(0); System.exit(0);
@ -915,12 +916,12 @@ public class MageFrame extends javax.swing.JFrame implements Client {
@Override @Override
public void showMessage(String message) { public void showMessage(String message) {
JOptionPane.showMessageDialog(desktopPane, message); JOptionPane.showMessageDialog(this, message);
} }
@Override @Override
public void showError(String message) { public void showError(String message) {
JOptionPane.showMessageDialog(desktopPane, message, "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
} }
@Override @Override

View file

@ -80,7 +80,8 @@ public class ChatPanel extends javax.swing.JPanel {
} }
public void disconnect() { public void disconnect() {
session.leaveChat(chatId); if (session != null)
session.leaveChat(chatId);
} }
public void receiveMessage(String message, MessageColor color) { public void receiveMessage(String message, MessageColor color) {

View file

@ -44,7 +44,7 @@ public class CallbackClientDaemon extends Thread {
private final static Logger logger = Logger.getLogger(CallbackClientDaemon.class); private final static Logger logger = Logger.getLogger(CallbackClientDaemon.class);
private static ExecutorService callbackExecutor = Executors.newCachedThreadPool(); private ExecutorService callbackExecutor = Executors.newFixedThreadPool(1);
private final CallbackClient client; private final CallbackClient client;
private final Connection connection; private final Connection connection;
private final UUID id; private final UUID id;
@ -67,19 +67,22 @@ public class CallbackClientDaemon extends Thread {
final ClientCallback callback = callbackMethod.makeDirectCall(); final ClientCallback callback = callbackMethod.makeDirectCall();
Ack ackMethod = new Ack(connection, id, callback.getMessageId()); Ack ackMethod = new Ack(connection, id, callback.getMessageId());
ackMethod.makeCall(); ackMethod.makeCall();
callbackExecutor.submit( if (callbackExecutor.isShutdown())
new Runnable() { logger.fatal("Attempt to submit callback to shutdown executor");
@Override else
public void run() { callbackExecutor.submit(
try { new Runnable() {
client.processCallback(callback); @Override
} public void run() {
catch (Exception ex) { try {
logger.fatal("CallbackClientDaemon error ", ex); client.processCallback(callback);
}
catch (Exception ex) {
logger.fatal("CallbackClientDaemon error ", ex);
}
} }
} }
} );
);
} catch (CallbackException ex) { } catch (CallbackException ex) {
logger.fatal("Callback failed ", ex); logger.fatal("Callback failed ", ex);
} }

View file

@ -63,6 +63,7 @@ public abstract class RemoteMethodCall<T> extends AbstractRemoteMethodCall<T> {
@Override @Override
public T makeCall() { public T makeCall() {
returnVal = null; returnVal = null;
logger.debug("Calling: " + name);
try { try {
returnVal = super.makeCall(); returnVal = super.makeCall();
} }
@ -81,6 +82,7 @@ public abstract class RemoteMethodCall<T> extends AbstractRemoteMethodCall<T> {
public T makeDirectCall() throws ServerUnavailable, MageException { public T makeDirectCall() throws ServerUnavailable, MageException {
T returnValue = null; T returnValue = null;
logger.debug("Calling direct: " + name);
try { try {
returnValue = super.makeCall(); returnValue = super.makeCall();
} }

View file

@ -85,9 +85,7 @@ public class Session {
} }
public synchronized boolean connect(Connection connection) { public synchronized boolean connect(Connection connection) {
if (this.connection != null && sessionState == SessionState.DISCONNECTED) { cleanupSession();
disconnect(true);
}
this.connection = connection; this.connection = connection;
return connect(); return connect();
} }
@ -128,33 +126,30 @@ public class Session {
return true; return true;
} catch (Exception ex) { } catch (Exception ex) {
logger.fatal("", ex); logger.fatal("", ex);
if (sessionState == SessionState.CONNECTING) {
disconnect(false);
client.showMessage("Unable to connect to server. " + ex.getMessage());
}
sessionState = SessionState.SERVER_UNAVAILABLE; sessionState = SessionState.SERVER_UNAVAILABLE;
disconnect(false);
client.showMessage("Unable to connect to server. " + ex.getMessage());
} }
return false; return false;
} }
public synchronized void disconnect(boolean voluntary) { public synchronized void disconnect(boolean showMessage) {
sessionState = SessionState.DISCONNECTING; if (sessionState == SessionState.CONNECTED)
sessionState = SessionState.DISCONNECTING;
cleanupSession();
if (connection == null) if (connection == null)
return; return;
if (future != null && !future.isDone()) if (sessionState == SessionState.CONNECTED) {
future.cancel(true); try {
try { deregisterClient();
if (callbackDaemon != null) } catch (Exception ex) {
callbackDaemon.stopDaemon(); logger.fatal("Error disconnecting ...", ex);
deregisterClient(); }
} catch (MageException ex) {
logger.fatal("Error disconnecting ...", ex);
} }
ServerCache.removeServerFromCache(connection); ServerCache.removeServerFromCache(connection);
client.disconnected(); client.disconnected();
logger.info("Disconnected ... "); logger.info("Disconnected ... ");
if (!voluntary) { if (sessionState == SessionState.SERVER_UNAVAILABLE && showMessage) {
sessionState = SessionState.SERVER_UNAVAILABLE;
client.showError("Server error. You have been disconnected"); client.showError("Server error. You have been disconnected");
} }
else { else {
@ -162,6 +157,14 @@ public class Session {
} }
} }
private void cleanupSession() {
q.clear();
if (future != null && !future.isDone())
future.cancel(true);
if (callbackDaemon != null)
callbackDaemon.stopDaemon();
}
private boolean handleCall(RemoteMethodCall method) { private boolean handleCall(RemoteMethodCall method) {
try { try {
if (sessionState == method.getAllowedState()) { if (sessionState == method.getAllowedState()) {
@ -188,15 +191,16 @@ public class Session {
} }
private UUID registerClient(String userName, UUID clientId, MageVersion version) throws MageException, ServerUnavailable { private UUID registerClient(String userName, UUID clientId, MageVersion version) throws MageException, ServerUnavailable {
RegisterClient method = new RegisterClient(connection, userName, clientId, version); if (sessionState == SessionState.CONNECTING) {
if (handleCall(method)) RegisterClient method = new RegisterClient(connection, userName, clientId, version);
return method.getReturnVal(); return method.makeDirectCall();
}
return null; return null;
} }
private void deregisterClient() throws MageException { private void deregisterClient() throws MageException, ServerUnavailable {
DeregisterClient method = new DeregisterClient(connection, sessionId); DeregisterClient method = new DeregisterClient(connection, sessionId);
handleCall(method); method.makeDirectCall();
} }
private ServerState getServerState() { private ServerState getServerState() {
@ -480,8 +484,9 @@ public class Session {
} }
private void handleServerUnavailable(ServerUnavailable ex) { private void handleServerUnavailable(ServerUnavailable ex) {
sessionState = SessionState.SERVER_UNAVAILABLE;
logger.fatal("server unavailable - ", ex); logger.fatal("server unavailable - ", ex);
disconnect(false); disconnect(true);
} }
private void handleGameException(GameException ex) { private void handleGameException(GameException ex) {