diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-21 03:09:21 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-21 03:09:21 +0200 |
commit | fc3c5a3e28e79dc75f93e1730145457974992ddd (patch) | |
tree | f2ff41c8fd65e80be436486134191ab83c11493b /src/jrummikub/control/network | |
parent | 900554a09bc3f47f9fed4cbe80b5db47de00bd01 (diff) | |
download | JRummikub-fc3c5a3e28e79dc75f93e1730145457974992ddd.tar JRummikub-fc3c5a3e28e79dc75f93e1730145457974992ddd.zip |
Fix message order in ConnectionControl
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@533 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control/network')
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 125 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkRoundControl.java | 10 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkTurnControl.java | 4 |
3 files changed, 74 insertions, 65 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index c63259d..2d8ff60 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -3,6 +3,8 @@ package jrummikub.control.network; import java.awt.Color; import java.io.Serializable; import java.util.UUID; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; import javax.swing.SwingUtilities; @@ -40,7 +42,7 @@ import org.jivesoftware.smackx.muc.DiscussionHistory; import org.jivesoftware.smackx.muc.MultiUserChat; /** - * Connection control managing network connections, essages and events + * Connection control managing network connections, messages and events */ public class ConnectionControl implements IConnectionControl { private static class TurnEndData implements Serializable { @@ -94,6 +96,8 @@ public class ConnectionControl implements IConnectionControl { private GameData currentGame; + private BlockingQueue<Runnable> actionQueue = new LinkedBlockingQueue<Runnable>(); + private volatile GameData offeredGame; /** @@ -113,6 +117,24 @@ public class ConnectionControl implements IConnectionControl { @Override public void connect() { + new Thread(new Runnable() { + @Override + public void run() { + while (true) { + Runnable runner; + try { + runner = actionQueue.take(); + if (runner == null) { + return; + } + + runner.run(); + } catch (InterruptedException e) { + } + } + } + }).start(); + run(new ConnectRunner()); } @@ -123,12 +145,11 @@ public class ConnectionControl implements IConnectionControl { run(new Runnable() { @Override public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.disconnect(); - connection = null; - } + if (connection != null) { + connection.disconnect(); + connection = null; } + ConnectionControl.this.run(null); } }); } @@ -240,7 +261,13 @@ public class ConnectionControl implements IConnectionControl { } private void run(Runnable runner) { - new Thread(runner).start(); + while (true) { + try { + actionQueue.put(runner); + return; + } catch (InterruptedException e) { + } + } } @Override @@ -520,46 +547,44 @@ public class ConnectionControl implements IConnectionControl { private class ConnectRunner implements Runnable { @Override public void run() { - synchronized (ConnectionControl.this) { - ConnectionConfiguration config = new ConnectionConfiguration( - loginData.getServerName()); - config.setSendPresence(false); - config.setRosterLoadedAtLogin(false); - config.setCompressionEnabled(true); - - connection = new XMPPConnection(config); - - connection.addPacketListener(new PacketListener() { - @Override - public void processPacket(final Packet packet) { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - ConnectionControl.this.processPacket(packet); - } - }); - - } - }, new AndFilter(new PacketTypeFilter(Message.class), - new PacketExtensionFilter(ELEMENT_NAME, NAMESPACE))); + ConnectionConfiguration config = new ConnectionConfiguration( + loginData.getServerName()); + config.setSendPresence(false); + config.setRosterLoadedAtLogin(false); + config.setCompressionEnabled(true); + + connection = new XMPPConnection(config); + + connection.addPacketListener(new PacketListener() { + @Override + public void processPacket(final Packet packet) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + ConnectionControl.this.processPacket(packet); + } + }); - LoginError error = doConnect(); - if (error == null) { - error = doLogin(); - } - if (error == null) { - error = doJoin(); } + }, new AndFilter(new PacketTypeFilter(Message.class), + new PacketExtensionFilter(ELEMENT_NAME, NAMESPACE))); - if (error == null) { - requestGames(); - emitLater(connectedEvent); - } else { - connection.disconnect(); - connection = null; + LoginError error = doConnect(); + if (error == null) { + error = doLogin(); + } + if (error == null) { + error = doJoin(); + } - emitLater(connectionFailedEvent, error); - } + if (error == null) { + requestGames(); + emitLater(connectedEvent); + } else { + connection.disconnect(); + connection = null; + + emitLater(connectionFailedEvent, error); } } @@ -630,14 +655,12 @@ public class ConnectionControl implements IConnectionControl { private abstract class SendRunner implements Runnable { @Override public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - DefaultPacketExtension extension = createJRummikubExtension(); - addData(extension); - Message message = createMessage(extension); - modifyMessage(message); - connection.sendPacket(message); - } + if (connection != null) { + DefaultPacketExtension extension = createJRummikubExtension(); + addData(extension); + Message message = createMessage(extension); + modifyMessage(message); + connection.sendPacket(message); } } diff --git a/src/jrummikub/control/network/NetworkRoundControl.java b/src/jrummikub/control/network/NetworkRoundControl.java index 609376e..afe6a6e 100644 --- a/src/jrummikub/control/network/NetworkRoundControl.java +++ b/src/jrummikub/control/network/NetworkRoundControl.java @@ -1,7 +1,5 @@ package jrummikub.control.network; -import java.util.Date; - import jrummikub.control.RoundControl; import jrummikub.control.turn.ITurnControl; import jrummikub.model.IRoundState; @@ -34,14 +32,12 @@ public class NetworkRoundControl extends RoundControl { connections.add(connectionControl.getTurnStartEvent().add(new IListener() { @Override public void handle() { - System.err.println(new Date() + ": Received startTurn"); startTurn(); } })); connections.add(connectionControl.getNextPlayerEvent().add(new IListener() { @Override public void handle() { - System.err.println(new Date() + ": Received nextPlayer"); NetworkRoundControl.super.nextPlayer(); } })); @@ -68,9 +64,6 @@ public class NetworkRoundControl extends RoundControl { break; } - System.err.println("Creating a " + (currentlyActive ? "normal" : "network") - + " turn control for a " + type); - if (!currentlyActive) { return new NetworkTurnControl(connectionControl); } @@ -81,7 +74,6 @@ public class NetworkRoundControl extends RoundControl { @Override protected void prepareTurn() { if (currentlyActive) { - System.err.println(new Date() + ": Sending startTurn"); connectionControl.startTurn(); } @@ -100,7 +92,6 @@ public class NetworkRoundControl extends RoundControl { @Override protected void nextPlayer() { if (currentlyActive) { - System.err.println(new Date() + ": Sending nextPlayer"); connectionControl.nextPlayer(); } } @@ -108,7 +99,6 @@ public class NetworkRoundControl extends RoundControl { @Override protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) { if (currentlyActive) { - System.err.println(new Date() + ": Sending endTurn"); connectionControl.endTurn(roundState, invalidTurnInfo); } diff --git a/src/jrummikub/control/network/NetworkTurnControl.java b/src/jrummikub/control/network/NetworkTurnControl.java index 5941a45..35002f7 100644 --- a/src/jrummikub/control/network/NetworkTurnControl.java +++ b/src/jrummikub/control/network/NetworkTurnControl.java @@ -1,7 +1,5 @@ package jrummikub.control.network; -import java.util.Date; - import jrummikub.control.RoundControl.InvalidTurnInfo; import jrummikub.control.turn.AbstractTurnControl; import jrummikub.model.IRoundState; @@ -36,8 +34,6 @@ public class NetworkTurnControl extends AbstractTurnControl { new IListener2<IRoundState, InvalidTurnInfo>() { @Override public void handle(IRoundState state, InvalidTurnInfo invalidTurnInfo) { - System.err.println(new Date() + ": Received endTurn"); - NetworkControl.fixGameSettings(state.getGameSettings(), connectionControl.getNickname()); |