From 96cfb51686364b73dcc5051ca32072b42d9499f0 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Sun, 29 Apr 2012 19:03:08 +0400 Subject: [PATCH] Extracted Session interface, moved implementation to SessionImpl --- .../src/main/java/mage/client/MageFrame.java | 7 +- .../mage/client/dialog/NewTableDialog.java | 18 +- .../mage/client/dialog/PickPileDialog.java | 5 - .../java/mage/client/draft/DraftPanel.java | 14 +- .../java/mage/client/game/FeedbackPanel.java | 1 - .../plugins/adapters/MageActionCallback.java | 1 - .../mage/client/game/MultiConnectTest.java | 3 +- Mage.Common/src/mage/remote/Session.java | 1138 ++--------------- Mage.Common/src/mage/remote/SessionImpl.java | 1109 ++++++++++++++++ .../mage/server/console/ConsoleFrame.java | 3 +- .../mage/server/console/ConsolePanel.java | 1 + 11 files changed, 1265 insertions(+), 1035 deletions(-) create mode 100644 Mage.Common/src/mage/remote/SessionImpl.java diff --git a/Mage.Client/src/main/java/mage/client/MageFrame.java b/Mage.Client/src/main/java/mage/client/MageFrame.java index aae4014e5b..cfadab795a 100644 --- a/Mage.Client/src/main/java/mage/client/MageFrame.java +++ b/Mage.Client/src/main/java/mage/client/MageFrame.java @@ -34,7 +34,6 @@ package mage.client; -import mage.Constants; import mage.cards.Card; import mage.cards.decks.Deck; import mage.client.cards.CardsStorage; @@ -60,7 +59,6 @@ import mage.client.util.EDTExceptionHandler; import mage.client.util.SettingsManager; import mage.client.util.gui.ArrowBuilder; import mage.components.ImagePanel; -import mage.game.match.MatchOptions; import mage.interfaces.Action; import mage.interfaces.MageClient; import mage.interfaces.callback.CallbackClient; @@ -68,10 +66,9 @@ import mage.interfaces.callback.ClientCallback; import mage.remote.Connection; import mage.remote.Connection.ProxyType; import mage.remote.Session; +import mage.remote.SessionImpl; import mage.server.Main; -import mage.sets.Sets; import mage.utils.MageVersion; -import mage.view.TableView; import org.apache.log4j.Logger; import org.mage.card.arcane.ManaSymbols; @@ -181,7 +178,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { SettingsManager.getInstance().setScreenWidthAndHeight(1024, 768); this.setExtendedState(JFrame.MAXIMIZED_BOTH); - session = new Session(this); + session = new SessionImpl(this); session.setEmbeddedMageServerAction(new Action() { @Override public void execute() { diff --git a/Mage.Client/src/main/java/mage/client/dialog/NewTableDialog.java b/Mage.Client/src/main/java/mage/client/dialog/NewTableDialog.java index 719adae741..ec06e6fe84 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/NewTableDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/NewTableDialog.java @@ -27,29 +27,27 @@ */ package mage.client.dialog; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; -import javax.swing.DefaultComboBoxModel; -import javax.swing.JOptionPane; -import javax.swing.SpinnerNumberModel; import mage.Constants.MultiplayerAttackOption; import mage.Constants.RangeOfInfluence; +import mage.cards.decks.importer.DeckImporterUtil; import mage.client.MageFrame; import mage.client.components.MageComponents; -import mage.cards.decks.importer.DeckImporterUtil; import mage.client.table.TablePlayerPanel; import mage.client.util.Event; import mage.client.util.Listener; import mage.game.match.MatchOptions; import mage.remote.Session; -import mage.sets.Sets; import mage.view.GameTypeView; import mage.view.TableView; import org.apache.log4j.Logger; +import javax.swing.*; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + /** * * @author BetaSteward_at_googlemail.com diff --git a/Mage.Client/src/main/java/mage/client/dialog/PickPileDialog.java b/Mage.Client/src/main/java/mage/client/dialog/PickPileDialog.java index 20665af981..864780703b 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/PickPileDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/PickPileDialog.java @@ -29,7 +29,6 @@ package mage.client.dialog; import java.awt.BorderLayout; -import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.UUID; @@ -37,15 +36,11 @@ import java.util.UUID; import javax.swing.JButton; import javax.swing.JLayeredPane; import javax.swing.JPanel; -import javax.swing.SwingUtilities; import mage.cards.CardDimensions; import mage.client.MageFrame; import mage.client.cards.BigCard; import mage.client.cards.CardArea; -import mage.client.util.SettingsManager; -import mage.client.util.gui.GuiDisplayUtil; -import mage.remote.Session; import mage.view.CardsView; /** diff --git a/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java b/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java index 8d5596cdcd..d90c07e1a6 100644 --- a/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java +++ b/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java @@ -34,22 +34,22 @@ package mage.client.draft; -import java.awt.Component; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.UUID; -import javax.swing.Timer; import mage.client.MageFrame; -import mage.client.constants.Constants.SortBy; import mage.client.plugins.impl.Plugins; import mage.client.util.CardsViewUtil; -import mage.remote.Session; import mage.client.util.Event; import mage.client.util.Listener; +import mage.remote.Session; import mage.view.CardsView; import mage.view.DraftPickView; import mage.view.DraftView; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.UUID; + /** * * @author BetaSteward_at_googlemail.com diff --git a/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java b/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java index a55d1c0cc2..5f60d96058 100644 --- a/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java @@ -36,7 +36,6 @@ package mage.client.game; import java.awt.*; import java.awt.event.ActionEvent; -import java.awt.event.MouseEvent; import java.io.Serializable; import java.util.Map; import java.util.UUID; diff --git a/Mage.Client/src/main/java/mage/client/plugins/adapters/MageActionCallback.java b/Mage.Client/src/main/java/mage/client/plugins/adapters/MageActionCallback.java index 630fa8c8e4..5a12962bcd 100644 --- a/Mage.Client/src/main/java/mage/client/plugins/adapters/MageActionCallback.java +++ b/Mage.Client/src/main/java/mage/client/plugins/adapters/MageActionCallback.java @@ -5,7 +5,6 @@ import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.util.List; import java.util.UUID; -import java.util.prefs.Preferences; import javax.swing.*; diff --git a/Mage.Client/src/test/java/mage/client/game/MultiConnectTest.java b/Mage.Client/src/test/java/mage/client/game/MultiConnectTest.java index 5922313e03..90bc6476ec 100644 --- a/Mage.Client/src/test/java/mage/client/game/MultiConnectTest.java +++ b/Mage.Client/src/test/java/mage/client/game/MultiConnectTest.java @@ -8,6 +8,7 @@ import mage.interfaces.MageClient; import mage.interfaces.callback.ClientCallback; import mage.remote.Connection; import mage.remote.Session; +import mage.remote.SessionImpl; import mage.utils.MageVersion; import org.apache.log4j.Logger; import org.junit.Ignore; @@ -46,7 +47,7 @@ public class MultiConnectTest { } public void connect() { - session = new Session(this); + session = new SessionImpl(this); Connection connection = new Connection(); connection.setUsername(username); connection.setHost("localhost"); diff --git a/Mage.Common/src/mage/remote/Session.java b/Mage.Common/src/mage/remote/Session.java index 9eb5d69192..a708e075dd 100644 --- a/Mage.Common/src/mage/remote/Session.java +++ b/Mage.Common/src/mage/remote/Session.java @@ -28,1018 +28,148 @@ package mage.remote; -import mage.MageException; import mage.cards.decks.DeckCardLists; -import mage.constants.Constants.SessionState; -import mage.game.GameException; import mage.game.match.MatchOptions; import mage.game.tournament.TournamentOptions; import mage.interfaces.Action; -import mage.interfaces.MageClient; -import mage.interfaces.MageServer; -import mage.interfaces.ServerState; -import mage.interfaces.callback.ClientCallback; -import mage.utils.CompressUtil; import mage.view.*; -import org.apache.log4j.Logger; -import org.jboss.remoting.*; -import org.jboss.remoting.callback.Callback; -import org.jboss.remoting.callback.HandleCallbackException; -import org.jboss.remoting.callback.InvokerCallbackHandler; -import org.jboss.remoting.transport.bisocket.Bisocket; -import org.jboss.remoting.transport.socket.SocketWrapper; -import org.jboss.remoting.transporter.TransporterClient; -import java.net.*; -import java.util.*; +import java.util.Collection; +import java.util.List; +import java.util.UUID; /** + * Extracted interface for SessionImpl class. * - * @author BetaSteward_at_googlemail.com + * @author noxx */ -public class Session { - - private final static Logger logger = Logger.getLogger(Session.class); - - private String sessionId; - private MageServer server; - private MageClient client; - private Client callbackClient; - private ServerState serverState; - private SessionState sessionState = SessionState.DISCONNECTED; - private Connection connection; - - private Action embeddedMageServerAction; - - private static boolean debugMode = false; - private static boolean standalone = true; - - private boolean canceled = false; - - static { - debugMode = System.getProperty("debug.mage") != null; - if (System.getProperty("skip.standalone") != null) { - standalone = false; - } - } - - public Session(MageClient client) { - this.client = client; - } - - public synchronized boolean connect(Connection connection) { - if (isConnected()) { - disconnect(true); - } - this.connection = connection; - this.canceled = false; - return connect(); - } - - public boolean stopConnecting() { - canceled = true; - return true; - } - - public boolean connect() { - - if (standalone && connection.getHost().equals("localhost")) { - runEmbeddedMageServer(); - } - - sessionState = SessionState.CONNECTING; - try { - System.setProperty("http.nonProxyHosts", "code.google.com"); - System.setProperty("socksNonProxyHosts", "code.google.com"); - - // clear previous values - System.clearProperty("socksProxyHost"); - System.clearProperty("socksProxyPort"); - System.clearProperty("http.proxyHost"); - System.clearProperty("http.proxyPort"); - - switch (connection.getProxyType()) { - case SOCKS: - System.setProperty("socksProxyHost", connection.getProxyHost()); - System.setProperty("socksProxyPort", Integer.toString(connection.getProxyPort())); - break; - case HTTP: - System.setProperty("http.proxyHost", connection.getProxyHost()); - System.setProperty("http.proxyPort", Integer.toString(connection.getProxyPort())); - Authenticator.setDefault(new MageAuthenticator(connection.getProxyUsername(), connection.getProxyPassword())); - break; - } - InvokerLocator clientLocator = new InvokerLocator(connection.getURI()); - Map metadata = new HashMap(); - metadata.put(SocketWrapper.WRITE_TIMEOUT, "2000"); - metadata.put("generalizeSocketException", "true"); - server = (MageServer) TransporterClient.createTransporterClient(clientLocator.getLocatorURI(), MageServer.class, metadata); - - Map clientMetadata = new HashMap(); - clientMetadata.put(SocketWrapper.WRITE_TIMEOUT, "2000"); - clientMetadata.put("generalizeSocketException", "true"); - clientMetadata.put(Client.ENABLE_LEASE, "true"); - clientMetadata.put(Remoting.USE_CLIENT_CONNECTION_IDENTITY, "true"); - callbackClient = new Client(clientLocator, "callback", clientMetadata); - - Map listenerMetadata = new HashMap(); - if (debugMode) { - // prevent client from disconnecting while debugging - listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_PERIOD, "1000000"); - listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_TIMEOUT, "900000"); - } else { - listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_PERIOD, "10000"); - listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_TIMEOUT, "9000"); - } - callbackClient.connect(new ClientConnectionListener(), listenerMetadata); - - Map callbackMetadata = new HashMap(); - callbackMetadata.put(Bisocket.IS_CALLBACK_SERVER, "true"); - CallbackHandler callbackHandler = new CallbackHandler(); - callbackClient.addListener(callbackHandler, callbackMetadata); - callbackClient.invoke(""); - - this.sessionId = callbackClient.getSessionId(); - boolean registerResult = false; - if (connection.getPassword() == null) { - UserDataView userDataView = new UserDataView(connection.getAvatarId()); - // for backward compatibility. don't remove twice call - first one does nothing but for version checking - registerResult = server.registerClient(connection.getUsername(), sessionId, client.getVersion()); - server.setUserData(connection.getUsername(), sessionId, userDataView); - } else { - registerResult = server.registerAdmin(connection.getPassword(), sessionId, client.getVersion()); - } - if (registerResult) { - sessionState = SessionState.CONNECTED; - serverState = server.getServerState(); - logger.info("Connected to MAGE server at " + connection.getHost() + ":" + connection.getPort()); - client.connected("Connected to " + connection.getHost() + ":" + connection.getPort() + " "); - return true; - } - disconnect(false); - client.showMessage("Unable to connect to server."); - } catch (MalformedURLException ex) { - logger.fatal("", ex); - client.showMessage("Unable to connect to server. " + ex.getMessage()); - } catch (MageVersionException ex) { - if (!canceled) { - client.showMessage("Unable to connect to server. " + ex.getMessage()); - } - // TODO: download client that matches server version - } catch (CannotConnectException ex) { - if (!canceled) { - handleCannotConnectException(ex); - } - } catch (Throwable t) { - logger.fatal("Unable to connect to server - ", t); - if (!canceled) { - disconnect(false); - client.showMessage("Unable to connect to server. " + t.getMessage()); - } - } - return false; - } - - private void runEmbeddedMageServer() { - if (embeddedMageServerAction != null) { - try { - embeddedMageServerAction.execute(); - } catch (MageException e) { - logger.error(e); - } - } - } - - private void handleCannotConnectException(CannotConnectException ex) { - logger.warn("Cannot connect", ex); - Throwable t = ex.getCause(); - String message = ""; - while (t != null) { - if (t instanceof ConnectException) { - message = "Server is likely offline."; - break; - } - if (t instanceof SocketException) { - message = "Check your internet connection."; - break; - } - if (t instanceof SocketTimeoutException) { - message = "Server is not responding."; - break; - } - t = t.getCause(); - } - client.showMessage("Unable to connect to server. " + message); - } - - public synchronized void disconnect(boolean showMessage) { - if (isConnected()) - sessionState = SessionState.DISCONNECTING; - if (connection == null) - return; - try { - callbackClient.disconnect(); - TransporterClient.destroyTransporterClient(server); - } catch (Throwable ex) { - logger.fatal("Error disconnecting ...", ex); - } - if (sessionState == SessionState.DISCONNECTING || sessionState == SessionState.CONNECTING) { - sessionState = SessionState.DISCONNECTED; - logger.info("Disconnected ... "); - } - client.disconnected(); - if (showMessage) - client.showError("Network error. You have been disconnected"); - } - - public synchronized boolean sendFeedback(String title, String type, String message, String email) { - if (isConnected()) { - try { - server.sendFeedbackMessage(sessionId, connection.getUsername(), title, type, message, email); - return true; - } catch (MageException e) { - logger.error(e); - } - } - return false; - } - - class CallbackHandler implements InvokerCallbackHandler { - @Override - public void handleCallback(Callback callback) throws HandleCallbackException { - logger.info("callback handler"); - client.processCallback((ClientCallback)callback.getCallbackObject()); - } - } - - class ClientConnectionListener implements ConnectionListener { - @Override - public void handleConnectionException(Throwable throwable, Client client) { - logger.info("connection to server lost - " + throwable.getMessage()); - disconnect(true); - } - } - - public boolean isConnected() { - if (callbackClient == null) - return false; - return callbackClient.isConnected(); - } - - public String[] getPlayerTypes() { - return serverState.getPlayerTypes(); - } - - public List getGameTypes() { - return serverState.getGameTypes(); - } - - public String[] getDeckTypes() { - return serverState.getDeckTypes(); - } - - public List getTournamentTypes() { - return serverState.getTournamentTypes(); - } - - public boolean isTestMode() { - if (serverState != null) - return serverState.isTestMode(); - return false; - } - - public UUID getMainRoomId() { - try { - if (isConnected()) - return server.getMainRoomId(); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public UUID getRoomChatId(UUID roomId) { - try { - if (isConnected()) - return server.getRoomChatId(roomId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public UUID getTableChatId(UUID tableId) { - try { - if (isConnected()) - return server.getTableChatId(tableId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public UUID getGameChatId(UUID gameId) { - try { - if (isConnected()) - return server.getGameChatId(gameId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public TableView getTable(UUID roomId, UUID tableId) { - try { - if (isConnected()) - return server.getTable(roomId, tableId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public boolean watchTable(UUID roomId, UUID tableId) { - try { - if (isConnected()) { - server.watchTable(sessionId, roomId, tableId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean joinTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill, DeckCardLists deckList) { - try { - if (isConnected()) - return server.joinTable(sessionId, roomId, tableId, playerName, playerType, skill, deckList); - } catch (GameException ex) { - handleGameException(ex); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean joinTournamentTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill) { - try { - if (isConnected()) - return server.joinTournamentTable(sessionId, roomId, tableId, playerName, playerType, skill); - } catch (GameException ex) { - handleGameException(ex); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public Collection getTables(UUID roomId) throws MageRemoteException { - try { - if (isConnected()) - return server.getTables(roomId); - } catch (MageException ex) { - handleMageException(ex); - throw new MageRemoteException(); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public Collection getFinishedMatches(UUID roomId) throws MageRemoteException { - try { - if (isConnected()) - return server.getFinishedMatches(roomId); - } catch (MageException ex) { - handleMageException(ex); - throw new MageRemoteException(); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public Collection getConnectedPlayers(UUID roomId) throws MageRemoteException { - try { - if (isConnected()) - return server.getConnectedPlayers(roomId); - } catch (MageException ex) { - handleMageException(ex); - throw new MageRemoteException(); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public TournamentView getTournament(UUID tournamentId) throws MageRemoteException { - try { - if (isConnected()) - return server.getTournament(tournamentId); - } catch (MageException ex) { - handleMageException(ex); - throw new MageRemoteException(); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public UUID getTournamentChatId(UUID tournamentId) { - try { - if (isConnected()) - return server.getTournamentChatId(tournamentId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public boolean sendPlayerUUID(UUID gameId, UUID data) { - try { - if (isConnected()) { - server.sendPlayerUUID(gameId, sessionId, data); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean sendPlayerBoolean(UUID gameId, boolean data) { - try { - if (isConnected()) { - server.sendPlayerBoolean(gameId, sessionId, data); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean sendPlayerInteger(UUID gameId, int data) { - try { - if (isConnected()) { - server.sendPlayerInteger(gameId, sessionId, data); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean sendPlayerString(UUID gameId, String data) { - try { - if (isConnected()) { - server.sendPlayerString(gameId, sessionId, data); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public DraftPickView sendCardPick(UUID draftId, UUID cardId) { - try { - if (isConnected()) - return server.sendCardPick(draftId, sessionId, cardId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public boolean joinChat(UUID chatId) { - try { - if (isConnected()) { - server.joinChat(chatId, sessionId, connection.getUsername()); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean leaveChat(UUID chatId) { -// lock.readLock().lock(); - try { - if (isConnected()) { - server.leaveChat(chatId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); -// } finally { -// lock.readLock().unlock(); - } - return false; - } - - public boolean sendChatMessage(UUID chatId, String message) { -// lock.readLock().lock(); - try { - if (isConnected()) { - server.sendChatMessage(chatId, connection.getUsername(), message); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); -// } finally { -// lock.readLock().unlock(); - } - return false; - } - - public boolean sendBroadcastMessage(String message) { - try { - if (isConnected()) { - server.sendBroadcastMessage(sessionId, message); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean joinGame(UUID gameId) { - try { - if (isConnected()) { - server.joinGame(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean joinDraft(UUID draftId) { - try { - if (isConnected()) { - server.joinDraft(draftId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean joinTournament(UUID tournamentId) { - try { - if (isConnected()) { - server.joinTournament(tournamentId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean watchGame(UUID gameId) { - try { - if (isConnected()) { - server.watchGame(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean replayGame(UUID gameId) { - try { - if (isConnected()) { - server.replayGame(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public TableView createTable(UUID roomId, MatchOptions matchOptions) { - try { - if (isConnected()) - return server.createTable(sessionId, roomId, matchOptions); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public TableView createTournamentTable(UUID roomId, TournamentOptions tournamentOptions) { - try { - if (isConnected()) - return server.createTournamentTable(sessionId, roomId, tournamentOptions); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public boolean isTableOwner(UUID roomId, UUID tableId) { - try { - if (isConnected()) - return server.isTableOwner(sessionId, roomId, tableId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean removeTable(UUID roomId, UUID tableId) { - try { - if (isConnected()) { - server.removeTable(sessionId, roomId, tableId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean removeTable(UUID tableId) { - try { - if (isConnected()) { - server.removeTable(sessionId, tableId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean swapSeats(UUID roomId, UUID tableId, int seatNum1, int seatNum2) { - try { - if (isConnected()) { - server.swapSeats(sessionId, roomId, tableId, seatNum1, seatNum2); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean leaveTable(UUID roomId, UUID tableId) { - try { - if (isConnected()) { - server.leaveTable(sessionId, roomId, tableId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean startGame(UUID roomId, UUID tableId) { - try { - if (isConnected()) { - server.startMatch(sessionId, roomId, tableId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean startTournament(UUID roomId, UUID tableId) { - try { - if (isConnected()) { - server.startTournament(sessionId, roomId, tableId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean startChallenge(UUID roomId, UUID tableId, UUID challengeId) { - try { - if (isConnected()) { - server.startChallenge(sessionId, roomId, tableId, challengeId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean submitDeck(UUID tableId, DeckCardLists deck) { - try { - if (isConnected()) - return server.submitDeck(sessionId, tableId, deck); - } catch (GameException ex) { - handleGameException(ex); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean updateDeck(UUID tableId, DeckCardLists deck) { - try { - if (isConnected()) { - server.updateDeck(sessionId, tableId, deck); - return true; - } - } catch (GameException ex) { - handleGameException(ex); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean concedeGame(UUID gameId) { - try { - if (isConnected()) { - server.concedeGame(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean stopWatching(UUID gameId) { - try { - if (isConnected()) { - server.stopWatching(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean startReplay(UUID gameId) { - try { - if (isConnected()) { - server.startReplay(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean stopReplay(UUID gameId) { - try { - if (isConnected()) { - server.stopReplay(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean nextPlay(UUID gameId) { - try { - if (isConnected()) { - server.nextPlay(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean previousPlay(UUID gameId) { - try { - if (isConnected()) { - server.previousPlay(gameId, sessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean skipForward(UUID gameId, int moves) { - try { - if (isConnected()) { - server.skipForward(gameId, sessionId, moves); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public boolean cheat(UUID gameId, UUID playerId, DeckCardLists deckList) { - try { - if (isConnected()) { - server.cheat(gameId, sessionId, playerId, deckList); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public List getUsers() { - try { - if (isConnected()) - return server.getUsers(sessionId); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public List getServerMessages() { - try { - if (isConnected()) - return (List) CompressUtil.decompress(server.getServerMessagesCompressed(sessionId)); - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return null; - } - - public boolean disconnectUser(String userSessionId) { - try { - if (isConnected()) { - server.disconnectUser(sessionId, userSessionId); - return true; - } - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - private void handleThrowable(Throwable t) { - logger.fatal("Communication error", t); - sessionState = SessionState.SERVER_UNAVAILABLE; - disconnect(true); - } - - private void handleMageException(MageException ex) { - logger.fatal("Server error", ex); - } - - private void handleGameException(GameException ex) { - logger.warn(ex.getMessage()); - client.showError(ex.getMessage()); - } - - - public String getUserName() { - return connection.getUsername(); - } - - public boolean updateAvatar(int avatarId) { - try { - if (isConnected()) { - UserDataView userDataView = new UserDataView(avatarId); - server.setUserData(connection.getUsername(), sessionId, userDataView); - } - return true; - } catch (MageException ex) { - handleMageException(ex); - } catch (Throwable t) { - handleThrowable(t); - } - return false; - } - - public void setEmbeddedMageServerAction(Action embeddedMageServerAction) { - this.embeddedMageServerAction = embeddedMageServerAction; - } +public interface Session { + + boolean connect(Connection connection); + + boolean stopConnecting(); + + boolean connect(); + + void disconnect(boolean showMessage); + + boolean sendFeedback(String title, String type, String message, String email); + + boolean isConnected(); + + String[] getPlayerTypes(); + + List getGameTypes(); + + String[] getDeckTypes(); + + List getTournamentTypes(); + + boolean isTestMode(); + + UUID getMainRoomId(); + + UUID getRoomChatId(UUID roomId); + + UUID getTableChatId(UUID tableId); + + UUID getGameChatId(UUID gameId); + + TableView getTable(UUID roomId, UUID tableId); + + boolean watchTable(UUID roomId, UUID tableId); + + boolean joinTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill, DeckCardLists deckList); + + boolean joinTournamentTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill); + + Collection getTables(UUID roomId) throws MageRemoteException; + + Collection getFinishedMatches(UUID roomId) throws MageRemoteException; + + Collection getConnectedPlayers(UUID roomId) throws MageRemoteException; + + TournamentView getTournament(UUID tournamentId) throws MageRemoteException; + + UUID getTournamentChatId(UUID tournamentId); + + boolean sendPlayerUUID(UUID gameId, UUID data); + + boolean sendPlayerBoolean(UUID gameId, boolean data); + + boolean sendPlayerInteger(UUID gameId, int data); + + boolean sendPlayerString(UUID gameId, String data); + + DraftPickView sendCardPick(UUID draftId, UUID cardId); + + boolean joinChat(UUID chatId); + + boolean leaveChat(UUID chatId); + + boolean sendChatMessage(UUID chatId, String message); + + boolean sendBroadcastMessage(String message); + + boolean joinGame(UUID gameId); + + boolean joinDraft(UUID draftId); + + boolean joinTournament(UUID tournamentId); + + boolean watchGame(UUID gameId); + + boolean replayGame(UUID gameId); + + TableView createTable(UUID roomId, MatchOptions matchOptions); + + TableView createTournamentTable(UUID roomId, TournamentOptions tournamentOptions); + + boolean isTableOwner(UUID roomId, UUID tableId); + + boolean removeTable(UUID roomId, UUID tableId); + + boolean removeTable(UUID tableId); + + boolean swapSeats(UUID roomId, UUID tableId, int seatNum1, int seatNum2); + + boolean leaveTable(UUID roomId, UUID tableId); + + boolean startGame(UUID roomId, UUID tableId); + + boolean startTournament(UUID roomId, UUID tableId); + + boolean startChallenge(UUID roomId, UUID tableId, UUID challengeId); + + boolean submitDeck(UUID tableId, DeckCardLists deck); + + boolean updateDeck(UUID tableId, DeckCardLists deck); + + boolean concedeGame(UUID gameId); + + boolean stopWatching(UUID gameId); + + boolean startReplay(UUID gameId); + + boolean stopReplay(UUID gameId); + + boolean nextPlay(UUID gameId); + + boolean previousPlay(UUID gameId); + + boolean skipForward(UUID gameId, int moves); + + boolean cheat(UUID gameId, UUID playerId, DeckCardLists deckList); + + List getUsers(); + + List getServerMessages(); + + boolean disconnectUser(String userSessionId); + + String getUserName(); + + boolean updateAvatar(int avatarId); + + void setEmbeddedMageServerAction(Action embeddedMageServerAction); } - -class MageAuthenticator extends Authenticator { - - private String username; - private String password; - - public MageAuthenticator(String username, String password) { - this.username = username; - this.password = password; - } - - @Override - public PasswordAuthentication getPasswordAuthentication () { - return new PasswordAuthentication (username, password.toCharArray()); - } -} \ No newline at end of file diff --git a/Mage.Common/src/mage/remote/SessionImpl.java b/Mage.Common/src/mage/remote/SessionImpl.java new file mode 100644 index 0000000000..7e807865f6 --- /dev/null +++ b/Mage.Common/src/mage/remote/SessionImpl.java @@ -0,0 +1,1109 @@ +/* +* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are +* permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this list of +* conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, this list +* of conditions and the following disclaimer in the documentation and/or other materials +* provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those of the +* authors and should not be interpreted as representing official policies, either expressed +* or implied, of BetaSteward_at_googlemail.com. +*/ + +package mage.remote; + +import mage.MageException; +import mage.cards.decks.DeckCardLists; +import mage.constants.Constants.SessionState; +import mage.game.GameException; +import mage.game.match.MatchOptions; +import mage.game.tournament.TournamentOptions; +import mage.interfaces.Action; +import mage.interfaces.MageClient; +import mage.interfaces.MageServer; +import mage.interfaces.ServerState; +import mage.interfaces.callback.ClientCallback; +import mage.utils.CompressUtil; +import mage.view.*; +import org.apache.log4j.Logger; +import org.jboss.remoting.*; +import org.jboss.remoting.callback.Callback; +import org.jboss.remoting.callback.HandleCallbackException; +import org.jboss.remoting.callback.InvokerCallbackHandler; +import org.jboss.remoting.transport.bisocket.Bisocket; +import org.jboss.remoting.transport.socket.SocketWrapper; +import org.jboss.remoting.transporter.TransporterClient; + +import java.net.*; +import java.util.*; + +/** + * + * @author BetaSteward_at_googlemail.com + */ +public class SessionImpl implements Session { + + private final static Logger logger = Logger.getLogger(SessionImpl.class); + + private String sessionId; + private MageServer server; + private MageClient client; + private Client callbackClient; + private ServerState serverState; + private SessionState sessionState = SessionState.DISCONNECTED; + private Connection connection; + + private Action embeddedMageServerAction; + + private static boolean debugMode = false; + private static boolean standalone = true; + + private boolean canceled = false; + + static { + debugMode = System.getProperty("debug.mage") != null; + if (System.getProperty("skip.standalone") != null) { + standalone = false; + } + } + + public SessionImpl(MageClient client) { + this.client = client; + } + + @Override + public synchronized boolean connect(Connection connection) { + if (isConnected()) { + disconnect(true); + } + this.connection = connection; + this.canceled = false; + return connect(); + } + + @Override + public boolean stopConnecting() { + canceled = true; + return true; + } + + @Override + public boolean connect() { + + if (standalone && connection.getHost().equals("localhost")) { + runEmbeddedMageServer(); + } + + sessionState = SessionState.CONNECTING; + try { + System.setProperty("http.nonProxyHosts", "code.google.com"); + System.setProperty("socksNonProxyHosts", "code.google.com"); + + // clear previous values + System.clearProperty("socksProxyHost"); + System.clearProperty("socksProxyPort"); + System.clearProperty("http.proxyHost"); + System.clearProperty("http.proxyPort"); + + switch (connection.getProxyType()) { + case SOCKS: + System.setProperty("socksProxyHost", connection.getProxyHost()); + System.setProperty("socksProxyPort", Integer.toString(connection.getProxyPort())); + break; + case HTTP: + System.setProperty("http.proxyHost", connection.getProxyHost()); + System.setProperty("http.proxyPort", Integer.toString(connection.getProxyPort())); + Authenticator.setDefault(new MageAuthenticator(connection.getProxyUsername(), connection.getProxyPassword())); + break; + } + InvokerLocator clientLocator = new InvokerLocator(connection.getURI()); + Map metadata = new HashMap(); + metadata.put(SocketWrapper.WRITE_TIMEOUT, "2000"); + metadata.put("generalizeSocketException", "true"); + server = (MageServer) TransporterClient.createTransporterClient(clientLocator.getLocatorURI(), MageServer.class, metadata); + + Map clientMetadata = new HashMap(); + clientMetadata.put(SocketWrapper.WRITE_TIMEOUT, "2000"); + clientMetadata.put("generalizeSocketException", "true"); + clientMetadata.put(Client.ENABLE_LEASE, "true"); + clientMetadata.put(Remoting.USE_CLIENT_CONNECTION_IDENTITY, "true"); + callbackClient = new Client(clientLocator, "callback", clientMetadata); + + Map listenerMetadata = new HashMap(); + if (debugMode) { + // prevent client from disconnecting while debugging + listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_PERIOD, "1000000"); + listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_TIMEOUT, "900000"); + } else { + listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_PERIOD, "10000"); + listenerMetadata.put(ConnectionValidator.VALIDATOR_PING_TIMEOUT, "9000"); + } + callbackClient.connect(new ClientConnectionListener(), listenerMetadata); + + Map callbackMetadata = new HashMap(); + callbackMetadata.put(Bisocket.IS_CALLBACK_SERVER, "true"); + CallbackHandler callbackHandler = new CallbackHandler(); + callbackClient.addListener(callbackHandler, callbackMetadata); + callbackClient.invoke(""); + + this.sessionId = callbackClient.getSessionId(); + boolean registerResult = false; + if (connection.getPassword() == null) { + UserDataView userDataView = new UserDataView(connection.getAvatarId()); + // for backward compatibility. don't remove twice call - first one does nothing but for version checking + registerResult = server.registerClient(connection.getUsername(), sessionId, client.getVersion()); + server.setUserData(connection.getUsername(), sessionId, userDataView); + } else { + registerResult = server.registerAdmin(connection.getPassword(), sessionId, client.getVersion()); + } + if (registerResult) { + sessionState = SessionState.CONNECTED; + serverState = server.getServerState(); + logger.info("Connected to MAGE server at " + connection.getHost() + ":" + connection.getPort()); + client.connected("Connected to " + connection.getHost() + ":" + connection.getPort() + " "); + return true; + } + disconnect(false); + client.showMessage("Unable to connect to server."); + } catch (MalformedURLException ex) { + logger.fatal("", ex); + client.showMessage("Unable to connect to server. " + ex.getMessage()); + } catch (MageVersionException ex) { + if (!canceled) { + client.showMessage("Unable to connect to server. " + ex.getMessage()); + } + // TODO: download client that matches server version + } catch (CannotConnectException ex) { + if (!canceled) { + handleCannotConnectException(ex); + } + } catch (Throwable t) { + logger.fatal("Unable to connect to server - ", t); + if (!canceled) { + disconnect(false); + client.showMessage("Unable to connect to server. " + t.getMessage()); + } + } + return false; + } + + private void runEmbeddedMageServer() { + if (embeddedMageServerAction != null) { + try { + embeddedMageServerAction.execute(); + } catch (MageException e) { + logger.error(e); + } + } + } + + private void handleCannotConnectException(CannotConnectException ex) { + logger.warn("Cannot connect", ex); + Throwable t = ex.getCause(); + String message = ""; + while (t != null) { + if (t instanceof ConnectException) { + message = "Server is likely offline."; + break; + } + if (t instanceof SocketException) { + message = "Check your internet connection."; + break; + } + if (t instanceof SocketTimeoutException) { + message = "Server is not responding."; + break; + } + t = t.getCause(); + } + client.showMessage("Unable to connect to server. " + message); + } + + @Override + public synchronized void disconnect(boolean showMessage) { + if (isConnected()) + sessionState = SessionState.DISCONNECTING; + if (connection == null) + return; + try { + callbackClient.disconnect(); + TransporterClient.destroyTransporterClient(server); + } catch (Throwable ex) { + logger.fatal("Error disconnecting ...", ex); + } + if (sessionState == SessionState.DISCONNECTING || sessionState == SessionState.CONNECTING) { + sessionState = SessionState.DISCONNECTED; + logger.info("Disconnected ... "); + } + client.disconnected(); + if (showMessage) + client.showError("Network error. You have been disconnected"); + } + + @Override + public synchronized boolean sendFeedback(String title, String type, String message, String email) { + if (isConnected()) { + try { + server.sendFeedbackMessage(sessionId, connection.getUsername(), title, type, message, email); + return true; + } catch (MageException e) { + logger.error(e); + } + } + return false; + } + + class CallbackHandler implements InvokerCallbackHandler { + @Override + public void handleCallback(Callback callback) throws HandleCallbackException { + logger.info("callback handler"); + client.processCallback((ClientCallback)callback.getCallbackObject()); + } + } + + class ClientConnectionListener implements ConnectionListener { + @Override + public void handleConnectionException(Throwable throwable, Client client) { + logger.info("connection to server lost - " + throwable.getMessage()); + disconnect(true); + } + } + + @Override + public boolean isConnected() { + if (callbackClient == null) + return false; + return callbackClient.isConnected(); + } + + @Override + public String[] getPlayerTypes() { + return serverState.getPlayerTypes(); + } + + @Override + public List getGameTypes() { + return serverState.getGameTypes(); + } + + @Override + public String[] getDeckTypes() { + return serverState.getDeckTypes(); + } + + @Override + public List getTournamentTypes() { + return serverState.getTournamentTypes(); + } + + @Override + public boolean isTestMode() { + if (serverState != null) + return serverState.isTestMode(); + return false; + } + + @Override + public UUID getMainRoomId() { + try { + if (isConnected()) + return server.getMainRoomId(); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public UUID getRoomChatId(UUID roomId) { + try { + if (isConnected()) + return server.getRoomChatId(roomId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public UUID getTableChatId(UUID tableId) { + try { + if (isConnected()) + return server.getTableChatId(tableId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public UUID getGameChatId(UUID gameId) { + try { + if (isConnected()) + return server.getGameChatId(gameId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public TableView getTable(UUID roomId, UUID tableId) { + try { + if (isConnected()) + return server.getTable(roomId, tableId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public boolean watchTable(UUID roomId, UUID tableId) { + try { + if (isConnected()) { + server.watchTable(sessionId, roomId, tableId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean joinTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill, DeckCardLists deckList) { + try { + if (isConnected()) + return server.joinTable(sessionId, roomId, tableId, playerName, playerType, skill, deckList); + } catch (GameException ex) { + handleGameException(ex); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean joinTournamentTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill) { + try { + if (isConnected()) + return server.joinTournamentTable(sessionId, roomId, tableId, playerName, playerType, skill); + } catch (GameException ex) { + handleGameException(ex); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public Collection getTables(UUID roomId) throws MageRemoteException { + try { + if (isConnected()) + return server.getTables(roomId); + } catch (MageException ex) { + handleMageException(ex); + throw new MageRemoteException(); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public Collection getFinishedMatches(UUID roomId) throws MageRemoteException { + try { + if (isConnected()) + return server.getFinishedMatches(roomId); + } catch (MageException ex) { + handleMageException(ex); + throw new MageRemoteException(); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public Collection getConnectedPlayers(UUID roomId) throws MageRemoteException { + try { + if (isConnected()) + return server.getConnectedPlayers(roomId); + } catch (MageException ex) { + handleMageException(ex); + throw new MageRemoteException(); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public TournamentView getTournament(UUID tournamentId) throws MageRemoteException { + try { + if (isConnected()) + return server.getTournament(tournamentId); + } catch (MageException ex) { + handleMageException(ex); + throw new MageRemoteException(); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public UUID getTournamentChatId(UUID tournamentId) { + try { + if (isConnected()) + return server.getTournamentChatId(tournamentId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public boolean sendPlayerUUID(UUID gameId, UUID data) { + try { + if (isConnected()) { + server.sendPlayerUUID(gameId, sessionId, data); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean sendPlayerBoolean(UUID gameId, boolean data) { + try { + if (isConnected()) { + server.sendPlayerBoolean(gameId, sessionId, data); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean sendPlayerInteger(UUID gameId, int data) { + try { + if (isConnected()) { + server.sendPlayerInteger(gameId, sessionId, data); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean sendPlayerString(UUID gameId, String data) { + try { + if (isConnected()) { + server.sendPlayerString(gameId, sessionId, data); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public DraftPickView sendCardPick(UUID draftId, UUID cardId) { + try { + if (isConnected()) + return server.sendCardPick(draftId, sessionId, cardId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public boolean joinChat(UUID chatId) { + try { + if (isConnected()) { + server.joinChat(chatId, sessionId, connection.getUsername()); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean leaveChat(UUID chatId) { +// lock.readLock().lock(); + try { + if (isConnected()) { + server.leaveChat(chatId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); +// } finally { +// lock.readLock().unlock(); + } + return false; + } + + @Override + public boolean sendChatMessage(UUID chatId, String message) { +// lock.readLock().lock(); + try { + if (isConnected()) { + server.sendChatMessage(chatId, connection.getUsername(), message); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); +// } finally { +// lock.readLock().unlock(); + } + return false; + } + + @Override + public boolean sendBroadcastMessage(String message) { + try { + if (isConnected()) { + server.sendBroadcastMessage(sessionId, message); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean joinGame(UUID gameId) { + try { + if (isConnected()) { + server.joinGame(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean joinDraft(UUID draftId) { + try { + if (isConnected()) { + server.joinDraft(draftId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean joinTournament(UUID tournamentId) { + try { + if (isConnected()) { + server.joinTournament(tournamentId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean watchGame(UUID gameId) { + try { + if (isConnected()) { + server.watchGame(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean replayGame(UUID gameId) { + try { + if (isConnected()) { + server.replayGame(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public TableView createTable(UUID roomId, MatchOptions matchOptions) { + try { + if (isConnected()) + return server.createTable(sessionId, roomId, matchOptions); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public TableView createTournamentTable(UUID roomId, TournamentOptions tournamentOptions) { + try { + if (isConnected()) + return server.createTournamentTable(sessionId, roomId, tournamentOptions); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public boolean isTableOwner(UUID roomId, UUID tableId) { + try { + if (isConnected()) + return server.isTableOwner(sessionId, roomId, tableId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean removeTable(UUID roomId, UUID tableId) { + try { + if (isConnected()) { + server.removeTable(sessionId, roomId, tableId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean removeTable(UUID tableId) { + try { + if (isConnected()) { + server.removeTable(sessionId, tableId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean swapSeats(UUID roomId, UUID tableId, int seatNum1, int seatNum2) { + try { + if (isConnected()) { + server.swapSeats(sessionId, roomId, tableId, seatNum1, seatNum2); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean leaveTable(UUID roomId, UUID tableId) { + try { + if (isConnected()) { + server.leaveTable(sessionId, roomId, tableId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean startGame(UUID roomId, UUID tableId) { + try { + if (isConnected()) { + server.startMatch(sessionId, roomId, tableId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean startTournament(UUID roomId, UUID tableId) { + try { + if (isConnected()) { + server.startTournament(sessionId, roomId, tableId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean startChallenge(UUID roomId, UUID tableId, UUID challengeId) { + try { + if (isConnected()) { + server.startChallenge(sessionId, roomId, tableId, challengeId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean submitDeck(UUID tableId, DeckCardLists deck) { + try { + if (isConnected()) + return server.submitDeck(sessionId, tableId, deck); + } catch (GameException ex) { + handleGameException(ex); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean updateDeck(UUID tableId, DeckCardLists deck) { + try { + if (isConnected()) { + server.updateDeck(sessionId, tableId, deck); + return true; + } + } catch (GameException ex) { + handleGameException(ex); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean concedeGame(UUID gameId) { + try { + if (isConnected()) { + server.concedeGame(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean stopWatching(UUID gameId) { + try { + if (isConnected()) { + server.stopWatching(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean startReplay(UUID gameId) { + try { + if (isConnected()) { + server.startReplay(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean stopReplay(UUID gameId) { + try { + if (isConnected()) { + server.stopReplay(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean nextPlay(UUID gameId) { + try { + if (isConnected()) { + server.nextPlay(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean previousPlay(UUID gameId) { + try { + if (isConnected()) { + server.previousPlay(gameId, sessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean skipForward(UUID gameId, int moves) { + try { + if (isConnected()) { + server.skipForward(gameId, sessionId, moves); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public boolean cheat(UUID gameId, UUID playerId, DeckCardLists deckList) { + try { + if (isConnected()) { + server.cheat(gameId, sessionId, playerId, deckList); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public List getUsers() { + try { + if (isConnected()) + return server.getUsers(sessionId); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public List getServerMessages() { + try { + if (isConnected()) + return (List) CompressUtil.decompress(server.getServerMessagesCompressed(sessionId)); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } + + @Override + public boolean disconnectUser(String userSessionId) { + try { + if (isConnected()) { + server.disconnectUser(sessionId, userSessionId); + return true; + } + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + private void handleThrowable(Throwable t) { + logger.fatal("Communication error", t); + sessionState = SessionState.SERVER_UNAVAILABLE; + disconnect(true); + } + + private void handleMageException(MageException ex) { + logger.fatal("Server error", ex); + } + + private void handleGameException(GameException ex) { + logger.warn(ex.getMessage()); + client.showError(ex.getMessage()); + } + + + @Override + public String getUserName() { + return connection.getUsername(); + } + + @Override + public boolean updateAvatar(int avatarId) { + try { + if (isConnected()) { + UserDataView userDataView = new UserDataView(avatarId); + server.setUserData(connection.getUsername(), sessionId, userDataView); + } + return true; + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return false; + } + + @Override + public void setEmbeddedMageServerAction(Action embeddedMageServerAction) { + this.embeddedMageServerAction = embeddedMageServerAction; + } +} + +class MageAuthenticator extends Authenticator { + + private String username; + private String password; + + public MageAuthenticator(String username, String password) { + this.username = username; + this.password = password; + } + + @Override + public PasswordAuthentication getPasswordAuthentication () { + return new PasswordAuthentication (username, password.toCharArray()); + } +} \ No newline at end of file diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java b/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java index 7f5341b677..316738daef 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java @@ -38,6 +38,7 @@ import mage.interfaces.MageClient; import mage.interfaces.callback.ClientCallback; import mage.remote.Connection; import mage.remote.Session; +import mage.remote.SessionImpl; import mage.utils.MageVersion; import org.apache.log4j.Logger; @@ -79,7 +80,7 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient { initComponents(); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); - session = new Session(this); + session = new SessionImpl(this); connectDialog = new ConnectDialog(); } catch (Exception ex) { logger.fatal("", ex); diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java index cc4aa69c8d..c66f2bc94b 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java @@ -40,6 +40,7 @@ import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import javax.swing.SwingWorker; import javax.swing.table.AbstractTableModel; + import mage.remote.Session; import mage.view.TableView; import mage.view.UserView;