Moved filtering for UUIDs into ConnectionControl

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@452 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-06-18 00:40:41 +02:00
parent 9fe061f21c
commit 4db4ec03d7
4 changed files with 69 additions and 57 deletions

View file

@ -48,12 +48,14 @@ class ConnectionControl {
private Event1<GameData> gameOfferEvent = new Event1<GameData>(); private Event1<GameData> gameOfferEvent = new Event1<GameData>();
private Event1<UUID> gameWithdrawalEvent = new Event1<UUID>(); private Event1<UUID> gameWithdrawalEvent = new Event1<UUID>();
private Event2<UUID, String> gameJoinEvent = new Event2<UUID, String>(); private Event1<String> gameJoinEvent = new Event1<String>();
private Event2<UUID, String> gameLeaveEvent = new Event2<UUID, String>(); private Event1<String> gameLeaveEvent = new Event1<String>();
private Event2<UUID, Boolean> gameJoinAckEvent = new Event2<UUID, Boolean>(); private Event1<Boolean> gameJoinAckEvent = new Event1<Boolean>();
private Event3<UUID, String, Color> changeColorEvent = new Event3<UUID, String, Color>(); private Event2<String, Color> changeColorEvent = new Event2<String, Color>();
private GameData currentGame;
private volatile GameData offeredGame; private volatile GameData offeredGame;
@ -92,44 +94,54 @@ class ConnectionControl {
return gameWithdrawalEvent; return gameWithdrawalEvent;
} }
IEvent2<UUID, String> getGameJoinEvent() { IEvent1<String> getGameJoinEvent() {
return gameJoinEvent; return gameJoinEvent;
} }
IEvent2<UUID, String> getGameLeaveEvent() { IEvent1<String> getGameLeaveEvent() {
return gameLeaveEvent; return gameLeaveEvent;
} }
IEvent2<UUID, Boolean> getGameJoinAckEvent() { IEvent1<Boolean> getGameJoinAckEvent() {
return gameJoinAckEvent; return gameJoinAckEvent;
} }
IEvent3<UUID, String, Color> getChangeColorEvent() { IEvent2<String, Color> getChangeColorEvent() {
return changeColorEvent; return changeColorEvent;
} }
void offerGame(GameData data) { void offerGame(GameData data) {
offeredGame = data; offeredGame = data;
currentGame = data;
sendGameOffer(); sendGameOffer();
} }
void withdrawGame(UUID uuid) { void withdrawGame() {
offeredGame = null; offeredGame = null;
currentGame = null;
new Thread(new SendGameWithdrawRunner(uuid)).start(); new Thread(new SendGameWithdrawRunner(currentGame.getGameID())).start();
}
GameData getCurrentGame() {
return currentGame;
} }
void joinGame(UUID uuid) { void setCurrentGame(GameData game) {
new Thread(new SendGameJoinRunner(uuid)).start(); this.currentGame = game;
} }
void leaveGame(UUID uuid) { void joinGame(GameData game) {
new Thread(new SendGameLeaveRunner(uuid)).start(); setCurrentGame(game);
new Thread(new SendGameJoinRunner(currentGame.getGameID())).start();
} }
void ackJoinGame(UUID uuid, String recipient, boolean ack) { void leaveGame() {
new Thread(new SendGameJoinAckRunner(uuid, recipient, ack)).start(); currentGame = null;
new Thread(new SendGameLeaveRunner(currentGame.getGameID())).start();
}
void ackJoinGame(String recipient, boolean ack) {
new Thread(new SendGameJoinAckRunner(currentGame.getGameID(), recipient, ack)).start();
} }
private void sendGameOffer() { private void sendGameOffer() {
@ -182,28 +194,31 @@ class ConnectionControl {
GameData gameData = new GameData(uuid, settings, sender); GameData gameData = new GameData(uuid, settings, sender);
gameOfferEvent.emit(gameData); gameOfferEvent.emit(gameData);
} else if (messageType.equals("game_withdrawal")) { } else if (messageType.equals("game_withdrawal")) {
gameWithdrawalEvent.emit(UUID gameWithdrawalEvent
.fromString(extension.getValue("uuid"))); .emit(UUID.fromString(extension.getValue("uuid")));
} else if (messageType.equals("game_request")) { } else if (messageType.equals("game_request")) {
if (offeredGame != null) { if (offeredGame != null) {
sendGameOffer(); sendGameOffer();
} }
} else if (messageType.equals("game_join")) { } else if (currentGame != null) {
gameJoinEvent.emit(UUID.fromString(extension.getValue("uuid")), UUID uuid = UUID.fromString(extension.getValue("uuid"));
sender); if (!currentGame.getGameID().equals(uuid)) {
} else if (messageType.equals("game_leave")) { return;
gameLeaveEvent.emit(UUID.fromString(extension.getValue("uuid")), }
sender); if (messageType.equals("game_join")) {
} else if (messageType.equals("game_join_ack")) { gameJoinEvent.emit(sender);
gameJoinAckEvent.emit(UUID.fromString(extension.getValue("uuid")), } else if (messageType.equals("game_leave")) {
Boolean.valueOf(extension.getValue("ack"))); gameLeaveEvent.emit(sender);
} else if (messageType.equals("changeColor")) { } else if (messageType.equals("game_join_ack")) {
changeColorEvent.emit(UUID.fromString(extension.getValue("uuid")), gameJoinAckEvent
sender, (Color) Base64.decodeToObject(extension .emit(Boolean.valueOf(extension.getValue("ack")));
.getValue("color"))); } else if (messageType.equals("changeColor")) {
} else { changeColorEvent.emit(sender, (Color) Base64
System.err.println("Received unrecognized message of type '" .decodeToObject(extension.getValue("color")));
+ messageType + "'"); } else {
System.err.println("Received unrecognized message of type '"
+ messageType + "'");
}
} }
} }
@ -212,8 +227,8 @@ class ConnectionControl {
extension.setValue("messageType", "game_offer"); extension.setValue("messageType", "game_offer");
extension.setValue("uuid", data.getGameID().toString()); extension.setValue("uuid", data.getGameID().toString());
extension.setValue("gameSettings", Base64.encodeObject(data extension.setValue("gameSettings",
.getGameSettings(), Base64.GZIP)); Base64.encodeObject(data.getGameSettings(), Base64.GZIP));
return createMessage(extension); return createMessage(extension);
} }

View file

@ -108,7 +108,7 @@ public class GameJoinControl {
*/ */
private void goBack() { private void goBack() {
abort(); abort();
connectionControl.leaveGame(gameData.getGameID()); connectionControl.leaveGame();
view.showSettingsPanel(false); view.showSettingsPanel(false);
backEvent.emit(); backEvent.emit();
} }

View file

@ -14,6 +14,7 @@ import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Connection; import jrummikub.util.Connection;
import jrummikub.util.GameData; import jrummikub.util.GameData;
import jrummikub.util.IListener; import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener2; import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel;
import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.ISettingsPanel.SettingsMode;
@ -37,25 +38,21 @@ public class GameOfferControl {
updateSettingsPanel(settings); updateSettingsPanel(settings);
connections.add(connectionControl.getGameJoinEvent().add( connections.add(connectionControl.getGameJoinEvent().add(
new IListener2<UUID, String>() { new IListener1<String>() {
@Override @Override
public void handle(UUID uuid, String sender) { public void handle(String sender) {
if (!uuid.equals(gameData.getGameID())) {
return;
}
for (PlayerSettings player : settings.getPlayerList()) { for (PlayerSettings player : settings.getPlayerList()) {
if (player.getType() == Type.VACANT) { if (player.getType() == Type.VACANT) {
player.setName(sender); player.setName(sender);
player.setType(Type.NETWORK); player.setType(Type.NETWORK);
updateSettingsPanel(settings); updateSettingsPanel(settings);
connectionControl.ackJoinGame(uuid, sender, true); connectionControl.ackJoinGame(sender, true);
connectionControl.offerGame(gameData); connectionControl.offerGame(gameData);
return; return;
} }
} }
connectionControl.ackJoinGame(uuid, sender, false); connectionControl.ackJoinGame(sender, false);
} }
})); }));
connections.add(view.getSettingsPanel().getChangePlayerColorEvent() connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
@ -71,13 +68,13 @@ public class GameOfferControl {
updateSettingsPanel(settings); updateSettingsPanel(settings);
} }
})); }));
connections.add(connectionControl.getGameLeaveEvent().add(new IListener2<UUID, String>() { connections.add(connectionControl.getGameLeaveEvent().add(new IListener1<String>() {
@Override @Override
public void handle(UUID value1, String value2) { public void handle(String sender) {
List<PlayerSettings> players = gameData.getGameSettings().getPlayerList(); List<PlayerSettings> players = gameData.getGameSettings().getPlayerList();
int index=0; int index=0;
for(PlayerSettings s:players){ for(PlayerSettings s:players){
if (s.getName().equals(value2)){ if (s.getName().equals(sender)){
break; break;
} }
index++; index++;
@ -122,6 +119,6 @@ public class GameOfferControl {
} }
public void abort() { public void abort() {
connectionControl.withdrawGame(gameData.getGameID()); connectionControl.withdrawGame();
} }
} }

View file

@ -75,7 +75,7 @@ public class NetworkControl {
private void join(GameData gameData) { private void join(GameData gameData) {
view.showGameListPanel(false); view.showGameListPanel(false);
connectionControl.joinGame(gameData.getGameID()); connectionControl.joinGame(gameData);
} }
/** /**
@ -135,11 +135,11 @@ public class NetworkControl {
} }
})); }));
connections.add(connectionControl.getGameJoinAckEvent().add( connections.add(connectionControl.getGameJoinAckEvent().add(
new IListener2<UUID, Boolean>() { new IListener1<Boolean>() {
@Override @Override
public void handle(UUID uuid, Boolean ack) { public void handle(Boolean ack) {
if (ack) { if (ack) {
createGameJoinControl(uuid); createGameJoinControl();
} else { } else {
// TODO Error message // TODO Error message
view.showGameListPanel(true); view.showGameListPanel(true);
@ -158,12 +158,12 @@ public class NetworkControl {
view.getGameListPanel().setGameList(gameList); view.getGameListPanel().setGameList(gameList);
} }
private void createGameJoinControl(UUID uuid) { private void createGameJoinControl() {
if (gameJoinControl != null) { if (gameJoinControl != null) {
return; return;
} }
GameData gameData = gameMap.get(uuid); GameData gameData = connectionControl.getCurrentGame();
gameJoinControl = new GameJoinControl(connectionControl, gameData, view); gameJoinControl = new GameJoinControl(connectionControl, gameData, view);
gameJoinControl.getBackEvent().add(new IListener() { gameJoinControl.getBackEvent().add(new IListener() {
@Override @Override