Fix message order in ConnectionControl

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@533 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-21 03:09:21 +02:00
parent 900554a09b
commit fc3c5a3e28
3 changed files with 72 additions and 63 deletions

View file

@ -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;
}
}
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,7 +547,6 @@ 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);
@ -561,7 +587,6 @@ public class ConnectionControl implements IConnectionControl {
emitLater(connectionFailedEvent, error);
}
}
}
private LoginError doConnect() {
try {
@ -630,7 +655,6 @@ 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);
@ -639,7 +663,6 @@ public class ConnectionControl implements IConnectionControl {
connection.sendPacket(message);
}
}
}
abstract protected void addData(DefaultPacketExtension extension);

View file

@ -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);
}

View file

@ -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());