diff --git a/Mage.Common/src/mage/interfaces/callback/CallbackClientDaemon.java b/Mage.Common/src/mage/interfaces/callback/CallbackClientDaemon.java index ed75f8cccc..25b2ca5662 100644 --- a/Mage.Common/src/mage/interfaces/callback/CallbackClientDaemon.java +++ b/Mage.Common/src/mage/interfaces/callback/CallbackClientDaemon.java @@ -32,6 +32,8 @@ import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import mage.remote.Connection; +import mage.remote.ServerUnavailable; +import mage.remote.Session; import mage.remote.method.Ack; import mage.remote.method.Callback; import org.apache.log4j.Logger; @@ -46,13 +48,13 @@ public class CallbackClientDaemon extends Thread { private ExecutorService callbackExecutor = Executors.newFixedThreadPool(1); private final CallbackClient client; - private final Connection connection; + private final Session session; private final UUID id; private boolean end = false; - public CallbackClientDaemon(UUID id, CallbackClient client, Connection connection) { + public CallbackClientDaemon(UUID id, CallbackClient client, Session session) { this.client = client; - this.connection = connection; + this.session = session; this.id = id; setDaemon(true); start(); @@ -63,10 +65,8 @@ public class CallbackClientDaemon extends Thread { try { while(!end) { try { - Callback callbackMethod = new Callback(connection, id); - final ClientCallback callback = callbackMethod.makeDirectCall(); - Ack ackMethod = new Ack(connection, id, callback.getMessageId()); - ackMethod.makeCall(); + final ClientCallback callback = session.callback(id); + session.ack(id, callback.getMessageId()); if (callbackExecutor.isShutdown()) logger.fatal("Attempt to submit callback to shutdown executor"); else @@ -87,9 +87,11 @@ public class CallbackClientDaemon extends Thread { logger.fatal("Callback failed ", ex); } } + } catch (ServerUnavailable ex) { + session.disconnect(true); } catch(Exception ex) { logger.fatal("CallbackClientDaemon error ", ex); - stopDaemon(); + session.disconnect(true); } } diff --git a/Mage.Common/src/mage/remote/Session.java b/Mage.Common/src/mage/remote/Session.java index ecd4d284e1..5199b38642 100644 --- a/Mage.Common/src/mage/remote/Session.java +++ b/Mage.Common/src/mage/remote/Session.java @@ -51,6 +51,7 @@ import mage.game.tournament.TournamentOptions; import mage.interfaces.Client; import mage.interfaces.ServerState; import mage.interfaces.callback.CallbackClientDaemon; +import mage.interfaces.callback.ClientCallback; import mage.utils.MageVersion; import mage.view.DraftPickView; import mage.view.GameTypeView; @@ -117,12 +118,12 @@ public class Session { Registry reg = LocateRegistry.getRegistry(connection.getHost(), connection.getPort()); this.userName = connection.getUsername(); sessionId = registerClient(userName, client.getId(), client.getVersion()); - callbackDaemon = new CallbackClientDaemon(sessionId, client, connection); serverState = getServerState(); + sessionState = SessionState.CONNECTED; + callbackDaemon = new CallbackClientDaemon(sessionId, client, this); future = sessionExecutor.scheduleWithFixedDelay(new ServerPinger(), 5, 5, TimeUnit.SECONDS); logger.info("Connected to RMI server at " + connection.getHost() + ":" + connection.getPort()); client.connected("Connected to " + connection.getHost() + ":" + connection.getPort() + " "); - sessionState = SessionState.CONNECTED; return true; } catch (Exception ex) { logger.fatal("", ex); @@ -143,7 +144,7 @@ public class Session { try { deregisterClient(); } catch (Exception ex) { - logger.fatal("Error disconnecting ...", ex); + logger.fatal("Error disconnecting ...", ex); } } ServerCache.removeServerFromCache(connection); @@ -239,6 +240,19 @@ public class Session { return false; } + public ClientCallback callback(UUID clientId) throws ServerUnavailable, MageException { + if (sessionState == SessionState.CONNECTED) { + Callback method = new Callback(connection, clientId); + return method.makeDirectCall(); + } + return null; + } + + public boolean ack(UUID clientId, int messageId) { + Ack method = new Ack(connection, clientId, messageId); + return handleCall(method); + } + public UUID getMainRoomId() { GetMainRoomId method = new GetMainRoomId(connection); if (handleCall(method))