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:
parent
900554a09b
commit
fc3c5a3e28
3 changed files with 72 additions and 63 deletions
|
@ -3,6 +3,8 @@ package jrummikub.control.network;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ import org.jivesoftware.smackx.muc.DiscussionHistory;
|
||||||
import org.jivesoftware.smackx.muc.MultiUserChat;
|
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 {
|
public class ConnectionControl implements IConnectionControl {
|
||||||
private static class TurnEndData implements Serializable {
|
private static class TurnEndData implements Serializable {
|
||||||
|
@ -94,6 +96,8 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
|
|
||||||
private GameData currentGame;
|
private GameData currentGame;
|
||||||
|
|
||||||
|
private BlockingQueue<Runnable> actionQueue = new LinkedBlockingQueue<Runnable>();
|
||||||
|
|
||||||
private volatile GameData offeredGame;
|
private volatile GameData offeredGame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,6 +117,24 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect() {
|
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());
|
run(new ConnectRunner());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,12 +145,11 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
run(new Runnable() {
|
run(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
synchronized (ConnectionControl.this) {
|
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
connection = null;
|
connection = null;
|
||||||
}
|
}
|
||||||
}
|
ConnectionControl.this.run(null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -240,7 +261,13 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void run(Runnable runner) {
|
private void run(Runnable runner) {
|
||||||
new Thread(runner).start();
|
while (true) {
|
||||||
|
try {
|
||||||
|
actionQueue.put(runner);
|
||||||
|
return;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -520,7 +547,6 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
private class ConnectRunner implements Runnable {
|
private class ConnectRunner implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
synchronized (ConnectionControl.this) {
|
|
||||||
ConnectionConfiguration config = new ConnectionConfiguration(
|
ConnectionConfiguration config = new ConnectionConfiguration(
|
||||||
loginData.getServerName());
|
loginData.getServerName());
|
||||||
config.setSendPresence(false);
|
config.setSendPresence(false);
|
||||||
|
@ -561,7 +587,6 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
emitLater(connectionFailedEvent, error);
|
emitLater(connectionFailedEvent, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private LoginError doConnect() {
|
private LoginError doConnect() {
|
||||||
try {
|
try {
|
||||||
|
@ -630,7 +655,6 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
private abstract class SendRunner implements Runnable {
|
private abstract class SendRunner implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
synchronized (ConnectionControl.this) {
|
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
DefaultPacketExtension extension = createJRummikubExtension();
|
DefaultPacketExtension extension = createJRummikubExtension();
|
||||||
addData(extension);
|
addData(extension);
|
||||||
|
@ -639,7 +663,6 @@ public class ConnectionControl implements IConnectionControl {
|
||||||
connection.sendPacket(message);
|
connection.sendPacket(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
abstract protected void addData(DefaultPacketExtension extension);
|
abstract protected void addData(DefaultPacketExtension extension);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package jrummikub.control.network;
|
package jrummikub.control.network;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import jrummikub.control.RoundControl;
|
import jrummikub.control.RoundControl;
|
||||||
import jrummikub.control.turn.ITurnControl;
|
import jrummikub.control.turn.ITurnControl;
|
||||||
import jrummikub.model.IRoundState;
|
import jrummikub.model.IRoundState;
|
||||||
|
@ -34,14 +32,12 @@ public class NetworkRoundControl extends RoundControl {
|
||||||
connections.add(connectionControl.getTurnStartEvent().add(new IListener() {
|
connections.add(connectionControl.getTurnStartEvent().add(new IListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
System.err.println(new Date() + ": Received startTurn");
|
|
||||||
startTurn();
|
startTurn();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
connections.add(connectionControl.getNextPlayerEvent().add(new IListener() {
|
connections.add(connectionControl.getNextPlayerEvent().add(new IListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
System.err.println(new Date() + ": Received nextPlayer");
|
|
||||||
NetworkRoundControl.super.nextPlayer();
|
NetworkRoundControl.super.nextPlayer();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -68,9 +64,6 @@ public class NetworkRoundControl extends RoundControl {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println("Creating a " + (currentlyActive ? "normal" : "network")
|
|
||||||
+ " turn control for a " + type);
|
|
||||||
|
|
||||||
if (!currentlyActive) {
|
if (!currentlyActive) {
|
||||||
return new NetworkTurnControl(connectionControl);
|
return new NetworkTurnControl(connectionControl);
|
||||||
}
|
}
|
||||||
|
@ -81,7 +74,6 @@ public class NetworkRoundControl extends RoundControl {
|
||||||
@Override
|
@Override
|
||||||
protected void prepareTurn() {
|
protected void prepareTurn() {
|
||||||
if (currentlyActive) {
|
if (currentlyActive) {
|
||||||
System.err.println(new Date() + ": Sending startTurn");
|
|
||||||
connectionControl.startTurn();
|
connectionControl.startTurn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +92,6 @@ public class NetworkRoundControl extends RoundControl {
|
||||||
@Override
|
@Override
|
||||||
protected void nextPlayer() {
|
protected void nextPlayer() {
|
||||||
if (currentlyActive) {
|
if (currentlyActive) {
|
||||||
System.err.println(new Date() + ": Sending nextPlayer");
|
|
||||||
connectionControl.nextPlayer();
|
connectionControl.nextPlayer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,7 +99,6 @@ public class NetworkRoundControl extends RoundControl {
|
||||||
@Override
|
@Override
|
||||||
protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) {
|
protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) {
|
||||||
if (currentlyActive) {
|
if (currentlyActive) {
|
||||||
System.err.println(new Date() + ": Sending endTurn");
|
|
||||||
connectionControl.endTurn(roundState, invalidTurnInfo);
|
connectionControl.endTurn(roundState, invalidTurnInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package jrummikub.control.network;
|
package jrummikub.control.network;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import jrummikub.control.RoundControl.InvalidTurnInfo;
|
import jrummikub.control.RoundControl.InvalidTurnInfo;
|
||||||
import jrummikub.control.turn.AbstractTurnControl;
|
import jrummikub.control.turn.AbstractTurnControl;
|
||||||
import jrummikub.model.IRoundState;
|
import jrummikub.model.IRoundState;
|
||||||
|
@ -36,8 +34,6 @@ public class NetworkTurnControl extends AbstractTurnControl {
|
||||||
new IListener2<IRoundState, InvalidTurnInfo>() {
|
new IListener2<IRoundState, InvalidTurnInfo>() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(IRoundState state, InvalidTurnInfo invalidTurnInfo) {
|
public void handle(IRoundState state, InvalidTurnInfo invalidTurnInfo) {
|
||||||
System.err.println(new Date() + ": Received endTurn");
|
|
||||||
|
|
||||||
NetworkControl.fixGameSettings(state.getGameSettings(),
|
NetworkControl.fixGameSettings(state.getGameSettings(),
|
||||||
connectionControl.getNickname());
|
connectionControl.getNickname());
|
||||||
|
|
||||||
|
|
Reference in a new issue