diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-19 19:46:06 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-19 19:46:06 +0200 |
commit | 45656861ab2e618938764f0c46f830184099a71d (patch) | |
tree | 8e5cd9ab28d47f242408d6dcfc29317902048605 /src/jrummikub/control/network | |
parent | 9b7aac51f58eb628978ed0e4d1a922a959917a3b (diff) | |
download | JRummikub-45656861ab2e618938764f0c46f830184099a71d.tar JRummikub-45656861ab2e618938764f0c46f830184099a71d.zip |
Added NetworkRoundControlTest and started NetworkRoundControl and NetworkTurnControl implementation
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@491 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control/network')
4 files changed, 190 insertions, 1 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 0a36ebd..acccbdc 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -6,6 +6,8 @@ import java.util.UUID; import javax.swing.SwingUtilities; import jrummikub.model.GameSettings; +import jrummikub.model.IRoundState; +import jrummikub.model.ITable; import jrummikub.util.Event; import jrummikub.util.Event1; import jrummikub.util.Event2; @@ -58,6 +60,10 @@ public class ConnectionControl implements IConnectionControl { private Event gameStartEvent = new Event(); + private Event1<ITable> tableUpdateEvent = new Event1<ITable>(); + private Event1<ITable> turnEndEvent = new Event1<ITable>(); + private Event1<IRoundState> turnStartEvent = new Event1<IRoundState>(); + private GameData currentGame; private volatile GameData offeredGame; @@ -145,6 +151,21 @@ public class ConnectionControl implements IConnectionControl { } @Override + public IEvent1<ITable> getTableUpdateEvent() { + return tableUpdateEvent; + } + + @Override + public IEvent1<ITable> getTurnEndEvent() { + return turnEndEvent; + } + + @Override + public IEvent1<IRoundState> getTurnStartEvent() { + return turnStartEvent; + } + + @Override public void offerGame(GameData data) { offeredGame = data; currentGame = data; @@ -235,7 +256,6 @@ public class ConnectionControl implements IConnectionControl { Base64.encodeObject(color, Base64.GZIP)); } }); - } @Override @@ -248,7 +268,45 @@ public class ConnectionControl implements IConnectionControl { extension.setValue("uuid", uuid.toString()); } }); + } + + @Override + public void updateTable(final ITable table) { + final UUID uuid = currentGame.getGameID(); + run(new SendRunner() { + @Override + protected void addData(DefaultPacketExtension extension) { + extension.setValue("messageType", "table_update"); + extension.setValue("uuid", uuid.toString()); + extension.setValue("table", Base64.encodeObject(table, Base64.GZIP)); + } + }); + } + + @Override + public void endTurn(final ITable table) { + final UUID uuid = currentGame.getGameID(); + run(new SendRunner() { + @Override + protected void addData(DefaultPacketExtension extension) { + extension.setValue("messageType", "turn_end"); + extension.setValue("uuid", uuid.toString()); + extension.setValue("table", Base64.encodeObject(table, Base64.GZIP)); + } + }); + } + @Override + public void startTurn(final IRoundState state) { + final UUID uuid = currentGame.getGameID(); + run(new SendRunner() { + @Override + protected void addData(DefaultPacketExtension extension) { + extension.setValue("messageType", "turn_start"); + extension.setValue("uuid", uuid.toString()); + extension.setValue("state", Base64.encodeObject(state, Base64.GZIP)); + } + }); } private void sendGameOffer() { @@ -348,6 +406,15 @@ public class ConnectionControl implements IConnectionControl { (Color) Base64.decodeToObject(extension.getValue("color"))); } else if (messageType.equals("game_start")) { gameStartEvent.emit(); + } else if (messageType.equals("table_update")) { + tableUpdateEvent.emit((ITable) Base64.decodeToObject(extension + .getValue("table"))); + } else if (messageType.equals("turn_end")) { + turnEndEvent.emit((ITable) Base64.decodeToObject(extension + .getValue("table"))); + } else if (messageType.equals("turn_start")) { + turnStartEvent.emit((IRoundState) Base64.decodeToObject(extension + .getValue("state"))); } else { System.err.println("Received unrecognized message of type '" + messageType + "'"); diff --git a/src/jrummikub/control/network/IConnectionControl.java b/src/jrummikub/control/network/IConnectionControl.java index feacf19..b329b4c 100644 --- a/src/jrummikub/control/network/IConnectionControl.java +++ b/src/jrummikub/control/network/IConnectionControl.java @@ -3,6 +3,8 @@ package jrummikub.control.network; import java.awt.Color; import java.util.UUID; +import jrummikub.model.IRoundState; +import jrummikub.model.ITable; import jrummikub.util.GameData; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; @@ -34,6 +36,12 @@ interface IConnectionControl { public IEvent getGameStartEvent(); + public IEvent1<ITable> getTableUpdateEvent(); + + public IEvent1<ITable> getTurnEndEvent(); + + public IEvent1<IRoundState> getTurnStartEvent(); + public void offerGame(GameData data); public void withdrawGame(); @@ -52,4 +60,10 @@ interface IConnectionControl { public void startGame(); + public void updateTable(ITable table); + + public void endTurn(ITable table); + + public void startTurn(IRoundState state); + }
\ No newline at end of file diff --git a/src/jrummikub/control/network/NetworkRoundControl.java b/src/jrummikub/control/network/NetworkRoundControl.java new file mode 100644 index 0000000..d7de9ea --- /dev/null +++ b/src/jrummikub/control/network/NetworkRoundControl.java @@ -0,0 +1,55 @@ +package jrummikub.control.network; + +import jrummikub.control.RoundControl; +import jrummikub.control.turn.ITurnControl; +import jrummikub.model.IRoundState; +import jrummikub.model.ITable; +import jrummikub.model.PlayerSettings; +import jrummikub.util.IListener1; +import jrummikub.view.IView; + +public class NetworkRoundControl extends RoundControl { + private IConnectionControl connectionControl; + private boolean currentlyActive; + + public NetworkRoundControl(IRoundState roundState, IView view, IConnectionControl connectionControl, boolean startActive) { + super(roundState, view); + + this.connectionControl = connectionControl; + currentlyActive = startActive; + } + + @Override + protected void addTurnControlListeners(ITurnControl turnControl) { + turnControl.getTableUpdateEvent().add(new IListener1<ITable>() { + @Override + public void handle(ITable table) { + connectionControl.updateTable(table); + } + }); + } + + @Override + protected ITurnControl createTurnControl(PlayerSettings playerSettings) { + switch (playerSettings.getType()) { + case HUMAN: + currentlyActive = true; + break; + case NETWORK: + currentlyActive = false; + break; + } + + if (!currentlyActive) { + return new NetworkTurnControl(connectionControl); + } + + return super.createTurnControl(playerSettings); + } + + @Override + protected void startTurn() { + connectionControl.startTurn(roundState); + super.startTurn(); + } +} diff --git a/src/jrummikub/control/network/NetworkTurnControl.java b/src/jrummikub/control/network/NetworkTurnControl.java new file mode 100644 index 0000000..d80c9b3 --- /dev/null +++ b/src/jrummikub/control/network/NetworkTurnControl.java @@ -0,0 +1,53 @@ +package jrummikub.control.network; + +import jrummikub.control.turn.AbstractTurnControl; +import jrummikub.model.IRoundState; +import jrummikub.model.ITable; +import jrummikub.util.Event1; +import jrummikub.util.IEvent1; +import jrummikub.util.IListener1; + +public class NetworkTurnControl extends AbstractTurnControl { + private IConnectionControl connectionControl; + private Event1<IRoundState> stateUpdateEvent = new Event1<IRoundState>(); + + + public NetworkTurnControl(IConnectionControl connectionControl) { + this.connectionControl = connectionControl; + } + + public IEvent1<IRoundState> getStateUpdateEvent() { + return stateUpdateEvent; + } + + @Override + public void doStartTurn() { + connections.add(connectionControl.getTableUpdateEvent().add(new IListener1<ITable>() { + @Override + public void handle(ITable table) { + view.getTablePanel().setStoneSets(table); + } + })); + connections.add(connectionControl.getTurnEndEvent().add(new IListener1<ITable>() { + @Override + public void handle(ITable table) { + endOfTurn(table); + } + })); + + timer.startTimer(); + } + + private void endOfTurn(ITable newTable) { + cleanUp(); + endOfTurnEvent.emit(newTable); + } + + @Override + protected void timeOut() { + } + + @Override + protected void pauseTurn() { + } +} |