From 45656861ab2e618938764f0c46f830184099a71d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 19 Jun 2011 19:46:06 +0200 Subject: 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 --- .../control/network/ConnectionControl.java | 69 +++++++++++++++++++++- .../control/network/IConnectionControl.java | 14 +++++ .../control/network/NetworkRoundControl.java | 55 +++++++++++++++++ .../control/network/NetworkTurnControl.java | 53 +++++++++++++++++ 4 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/jrummikub/control/network/NetworkRoundControl.java create mode 100644 src/jrummikub/control/network/NetworkTurnControl.java (limited to 'src/jrummikub/control/network') 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 tableUpdateEvent = new Event1(); + private Event1 turnEndEvent = new Event1(); + private Event1 turnStartEvent = new Event1(); + private GameData currentGame; private volatile GameData offeredGame; @@ -144,6 +150,21 @@ public class ConnectionControl implements IConnectionControl { return gameStartEvent; } + @Override + public IEvent1 getTableUpdateEvent() { + return tableUpdateEvent; + } + + @Override + public IEvent1 getTurnEndEvent() { + return turnEndEvent; + } + + @Override + public IEvent1 getTurnStartEvent() { + return turnStartEvent; + } + @Override public void offerGame(GameData data) { offeredGame = 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 getTableUpdateEvent(); + + public IEvent1 getTurnEndEvent(); + + public IEvent1 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() { + @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 stateUpdateEvent = new Event1(); + + + public NetworkTurnControl(IConnectionControl connectionControl) { + this.connectionControl = connectionControl; + } + + public IEvent1 getStateUpdateEvent() { + return stateUpdateEvent; + } + + @Override + public void doStartTurn() { + connections.add(connectionControl.getTableUpdateEvent().add(new IListener1() { + @Override + public void handle(ITable table) { + view.getTablePanel().setStoneSets(table); + } + })); + connections.add(connectionControl.getTurnEndEvent().add(new IListener1() { + @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() { + } +} -- cgit v1.2.3