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<UUID> gameWithdrawalEvent = new Event1<UUID>();
private Event2<UUID, String> gameJoinEvent = new Event2<UUID, String>();
private Event2<UUID, String> gameLeaveEvent = new Event2<UUID, String>();
private Event1<String> gameJoinEvent = new Event1<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;
@ -92,44 +94,54 @@ class ConnectionControl {
return gameWithdrawalEvent;
}
IEvent2<UUID, String> getGameJoinEvent() {
IEvent1<String> getGameJoinEvent() {
return gameJoinEvent;
}
IEvent2<UUID, String> getGameLeaveEvent() {
IEvent1<String> getGameLeaveEvent() {
return gameLeaveEvent;
}
IEvent2<UUID, Boolean> getGameJoinAckEvent() {
IEvent1<Boolean> getGameJoinAckEvent() {
return gameJoinAckEvent;
}
IEvent3<UUID, String, Color> getChangeColorEvent() {
IEvent2<String, Color> getChangeColorEvent() {
return changeColorEvent;
}
void offerGame(GameData data) {
offeredGame = data;
currentGame = data;
sendGameOffer();
}
void withdrawGame(UUID uuid) {
void withdrawGame() {
offeredGame = null;
new Thread(new SendGameWithdrawRunner(uuid)).start();
currentGame = null;
new Thread(new SendGameWithdrawRunner(currentGame.getGameID())).start();
}
void joinGame(UUID uuid) {
new Thread(new SendGameJoinRunner(uuid)).start();
GameData getCurrentGame() {
return currentGame;
}
void leaveGame(UUID uuid) {
new Thread(new SendGameLeaveRunner(uuid)).start();
void setCurrentGame(GameData game) {
this.currentGame = game;
}
void ackJoinGame(UUID uuid, String recipient, boolean ack) {
new Thread(new SendGameJoinAckRunner(uuid, recipient, ack)).start();
void joinGame(GameData game) {
setCurrentGame(game);
new Thread(new SendGameJoinRunner(currentGame.getGameID())).start();
}
void leaveGame() {
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() {
@ -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);
}

View file

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

View file

@ -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<UUID, String>() {
new IListener1<String>() {
@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<UUID, String>() {
connections.add(connectionControl.getGameLeaveEvent().add(new IListener1<String>() {
@Override
public void handle(UUID value1, String value2) {
public void handle(String sender) {
List<PlayerSettings> 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();
}
}

View file

@ -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<UUID, Boolean>() {
new IListener1<Boolean>() {
@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