From 4db4ec03d78382648edaa25f03aa66ef88a03c7c Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Sat, 18 Jun 2011 00:40:41 +0200 Subject: Moved filtering for UUIDs into ConnectionControl git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@452 72836036-5685-4462-b002-a69064685172 --- .../control/network/ConnectionControl.java | 89 +++++++++++++--------- src/jrummikub/control/network/GameJoinControl.java | 2 +- .../control/network/GameOfferControl.java | 21 +++-- src/jrummikub/control/network/NetworkControl.java | 12 +-- 4 files changed, 68 insertions(+), 56 deletions(-) (limited to 'src/jrummikub/control/network') diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 1844922..2d97558 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -48,12 +48,14 @@ class ConnectionControl { private Event1 gameOfferEvent = new Event1(); private Event1 gameWithdrawalEvent = new Event1(); - private Event2 gameJoinEvent = new Event2(); - private Event2 gameLeaveEvent = new Event2(); + private Event1 gameJoinEvent = new Event1(); + private Event1 gameLeaveEvent = new Event1(); - private Event2 gameJoinAckEvent = new Event2(); + private Event1 gameJoinAckEvent = new Event1(); - private Event3 changeColorEvent = new Event3(); + private Event2 changeColorEvent = new Event2(); + + private GameData currentGame; private volatile GameData offeredGame; @@ -92,44 +94,54 @@ class ConnectionControl { return gameWithdrawalEvent; } - IEvent2 getGameJoinEvent() { + IEvent1 getGameJoinEvent() { return gameJoinEvent; } - IEvent2 getGameLeaveEvent() { + IEvent1 getGameLeaveEvent() { return gameLeaveEvent; } - IEvent2 getGameJoinAckEvent() { + IEvent1 getGameJoinAckEvent() { return gameJoinAckEvent; } - IEvent3 getChangeColorEvent() { + IEvent2 getChangeColorEvent() { return changeColorEvent; } void offerGame(GameData data) { offeredGame = data; - + currentGame = data; sendGameOffer(); } - void withdrawGame(UUID uuid) { + void withdrawGame() { offeredGame = null; + currentGame = null; + new Thread(new SendGameWithdrawRunner(currentGame.getGameID())).start(); + } + + GameData getCurrentGame() { + return currentGame; + } - new Thread(new SendGameWithdrawRunner(uuid)).start(); + void setCurrentGame(GameData game) { + this.currentGame = game; } - void joinGame(UUID uuid) { - new Thread(new SendGameJoinRunner(uuid)).start(); + void joinGame(GameData game) { + setCurrentGame(game); + new Thread(new SendGameJoinRunner(currentGame.getGameID())).start(); } - void leaveGame(UUID uuid) { - new Thread(new SendGameLeaveRunner(uuid)).start(); + void leaveGame() { + currentGame = null; + new Thread(new SendGameLeaveRunner(currentGame.getGameID())).start(); } - void ackJoinGame(UUID uuid, String recipient, boolean ack) { - new Thread(new SendGameJoinAckRunner(uuid, recipient, ack)).start(); + void ackJoinGame(String recipient, boolean ack) { + new Thread(new SendGameJoinAckRunner(currentGame.getGameID(), recipient, ack)).start(); } private void sendGameOffer() { @@ -182,28 +194,31 @@ class ConnectionControl { GameData gameData = new GameData(uuid, settings, sender); gameOfferEvent.emit(gameData); } else if (messageType.equals("game_withdrawal")) { - gameWithdrawalEvent.emit(UUID - .fromString(extension.getValue("uuid"))); + gameWithdrawalEvent + .emit(UUID.fromString(extension.getValue("uuid"))); } else if (messageType.equals("game_request")) { if (offeredGame != null) { sendGameOffer(); } - } else if (messageType.equals("game_join")) { - gameJoinEvent.emit(UUID.fromString(extension.getValue("uuid")), - sender); - } else if (messageType.equals("game_leave")) { - gameLeaveEvent.emit(UUID.fromString(extension.getValue("uuid")), - sender); - } else if (messageType.equals("game_join_ack")) { - gameJoinAckEvent.emit(UUID.fromString(extension.getValue("uuid")), - Boolean.valueOf(extension.getValue("ack"))); - } else if (messageType.equals("changeColor")) { - changeColorEvent.emit(UUID.fromString(extension.getValue("uuid")), - sender, (Color) Base64.decodeToObject(extension - .getValue("color"))); - } else { - System.err.println("Received unrecognized message of type '" - + messageType + "'"); + } else if (currentGame != null) { + UUID uuid = UUID.fromString(extension.getValue("uuid")); + if (!currentGame.getGameID().equals(uuid)) { + return; + } + if (messageType.equals("game_join")) { + gameJoinEvent.emit(sender); + } else if (messageType.equals("game_leave")) { + gameLeaveEvent.emit(sender); + } else if (messageType.equals("game_join_ack")) { + gameJoinAckEvent + .emit(Boolean.valueOf(extension.getValue("ack"))); + } else if (messageType.equals("changeColor")) { + changeColorEvent.emit(sender, (Color) Base64 + .decodeToObject(extension.getValue("color"))); + } else { + System.err.println("Received unrecognized message of type '" + + messageType + "'"); + } } } @@ -212,8 +227,8 @@ class ConnectionControl { extension.setValue("messageType", "game_offer"); extension.setValue("uuid", data.getGameID().toString()); - extension.setValue("gameSettings", Base64.encodeObject(data - .getGameSettings(), Base64.GZIP)); + extension.setValue("gameSettings", + Base64.encodeObject(data.getGameSettings(), Base64.GZIP)); return createMessage(extension); } diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 83df0ed..afb78d4 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -108,7 +108,7 @@ public class GameJoinControl { */ private void goBack() { abort(); - connectionControl.leaveGame(gameData.getGameID()); + connectionControl.leaveGame(); view.showSettingsPanel(false); backEvent.emit(); } diff --git a/src/jrummikub/control/network/GameOfferControl.java b/src/jrummikub/control/network/GameOfferControl.java index fd55fc1..f0dc526 100644 --- a/src/jrummikub/control/network/GameOfferControl.java +++ b/src/jrummikub/control/network/GameOfferControl.java @@ -14,6 +14,7 @@ import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.GameData; import jrummikub.util.IListener; +import jrummikub.util.IListener1; import jrummikub.util.IListener2; import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel.SettingsMode; @@ -37,25 +38,21 @@ public class GameOfferControl { updateSettingsPanel(settings); connections.add(connectionControl.getGameJoinEvent().add( - new IListener2() { + new IListener1() { @Override - public void handle(UUID uuid, String sender) { - if (!uuid.equals(gameData.getGameID())) { - return; - } - + public void handle(String sender) { for (PlayerSettings player : settings.getPlayerList()) { if (player.getType() == Type.VACANT) { player.setName(sender); player.setType(Type.NETWORK); updateSettingsPanel(settings); - connectionControl.ackJoinGame(uuid, sender, true); + connectionControl.ackJoinGame(sender, true); connectionControl.offerGame(gameData); return; } } - connectionControl.ackJoinGame(uuid, sender, false); + connectionControl.ackJoinGame(sender, false); } })); connections.add(view.getSettingsPanel().getChangePlayerColorEvent() @@ -71,13 +68,13 @@ public class GameOfferControl { updateSettingsPanel(settings); } })); - connections.add(connectionControl.getGameLeaveEvent().add(new IListener2() { + connections.add(connectionControl.getGameLeaveEvent().add(new IListener1() { @Override - public void handle(UUID value1, String value2) { + public void handle(String sender) { List players = gameData.getGameSettings().getPlayerList(); int index=0; for(PlayerSettings s:players){ - if (s.getName().equals(value2)){ + if (s.getName().equals(sender)){ break; } index++; @@ -122,6 +119,6 @@ public class GameOfferControl { } public void abort() { - connectionControl.withdrawGame(gameData.getGameID()); + connectionControl.withdrawGame(); } } diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java index 0ae3808..0e8381b 100644 --- a/src/jrummikub/control/network/NetworkControl.java +++ b/src/jrummikub/control/network/NetworkControl.java @@ -75,7 +75,7 @@ public class NetworkControl { private void join(GameData gameData) { view.showGameListPanel(false); - connectionControl.joinGame(gameData.getGameID()); + connectionControl.joinGame(gameData); } /** @@ -135,11 +135,11 @@ public class NetworkControl { } })); connections.add(connectionControl.getGameJoinAckEvent().add( - new IListener2() { + new IListener1() { @Override - public void handle(UUID uuid, Boolean ack) { + public void handle(Boolean ack) { if (ack) { - createGameJoinControl(uuid); + createGameJoinControl(); } else { // TODO Error message view.showGameListPanel(true); @@ -158,12 +158,12 @@ public class NetworkControl { view.getGameListPanel().setGameList(gameList); } - private void createGameJoinControl(UUID uuid) { + private void createGameJoinControl() { if (gameJoinControl != null) { return; } - GameData gameData = gameMap.get(uuid); + GameData gameData = connectionControl.getCurrentGame(); gameJoinControl = new GameJoinControl(connectionControl, gameData, view); gameJoinControl.getBackEvent().add(new IListener() { @Override -- cgit v1.2.3