From 4860867fe8bf45432cd9b7967bc34398af8d013f Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Fri, 17 Jun 2011 22:24:23 +0200 Subject: halbfertige netzwerkdinge git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@450 72836036-5685-4462-b002-a69064685172 --- .../control/network/ConnectionControl.java | 37 ++++++++++++++-------- src/jrummikub/control/network/GameJoinControl.java | 37 +++++++++++++++++++--- .../control/network/NetworkSettingsControl.java | 27 ++++++++++++---- 3 files changed, 76 insertions(+), 25 deletions(-) (limited to 'src/jrummikub/control/network') diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 6cd3fc0..1844922 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -136,6 +136,10 @@ class ConnectionControl { new Thread(new SendGameOfferRunner(offeredGame)).start(); } + private void sendChangeColor(Color color) { + new Thread(new SendChangeColorRunner(color)).start(); + } + private static void emitLater(final Event event) { SwingUtilities.invokeLater(new Runnable() { @Override @@ -160,8 +164,8 @@ class ConnectionControl { .getExtension(ELEMENT_NAME, NAMESPACE); if (((Message) packet).getType() == Message.Type.error) { - System.err.println("Received error message from '" + packet.getFrom() - + "'"); + System.err.println("Received error message from '" + + packet.getFrom() + "'"); return; } @@ -172,27 +176,31 @@ class ConnectionControl { if (messageType.equals("game_offer")) { UUID uuid = UUID.fromString(extension.getValue("uuid")); - GameSettings settings = (GameSettings) Base64.decodeToObject(extension - .getValue("gameSettings")); + GameSettings settings = (GameSettings) Base64 + .decodeToObject(extension.getValue("gameSettings")); 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); + gameJoinEvent.emit(UUID.fromString(extension.getValue("uuid")), + sender); } else if (messageType.equals("game_leave")) { - gameLeaveEvent.emit(UUID.fromString(extension.getValue("uuid")), sender); + 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"))); + sender, (Color) Base64.decodeToObject(extension + .getValue("color"))); } else { System.err.println("Received unrecognized message of type '" + messageType + "'"); @@ -204,8 +212,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); } @@ -246,7 +254,8 @@ class ConnectionControl { return createMessage(extension); } - private Message createGameAckMessage(UUID uuid, String recipient, boolean ack) { + private Message createGameAckMessage(UUID uuid, String recipient, + boolean ack) { DefaultPacketExtension extension = createJRummikubExtension(); extension.setValue("messageType", "game_join_ack"); @@ -317,7 +326,8 @@ class ConnectionControl { break; // Join was successful, break the loop } catch (XMPPException e) { XMPPError error = e.getXMPPError(); - if (error.getType() == Type.CANCEL && error.getCode() == 409) { + if (error.getType() == Type.CANCEL + && error.getCode() == 409) { // There was a conflict, try again with another // nickname nickname += "_"; @@ -446,7 +456,8 @@ class ConnectionControl { public void run() { synchronized (ConnectionControl.this) { if (connection != null) { - connection.sendPacket(createGameAckMessage(uuid, recipient, ack)); + connection.sendPacket(createGameAckMessage(uuid, recipient, + ack)); } } } diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 55f584d..fbc18c6 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -6,28 +6,35 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; +import jrummikub.util.Event; import jrummikub.util.GameData; +import jrummikub.util.IListener; import jrummikub.util.IListener1; +import jrummikub.util.IListener2; +import jrummikub.util.IListener3; import jrummikub.view.ISettingsPanel; -import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; +import jrummikub.view.ISettingsPanel.SettingsMode; public class GameJoinControl { private List connections = new ArrayList(); private GameData gameData; private ConnectionControl connectionControl; private IView view; + private Event backEvent = new Event(); public GameJoinControl(final ConnectionControl connectionControl, final GameData gameData, final IView view) { this.connectionControl = connectionControl; this.gameData = gameData; this.view = view; + view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_JOIN); view.getSettingsPanel().enableAddPlayerButton(false); @@ -48,13 +55,29 @@ public class GameJoinControl { } } })); + connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() { + @Override + public void handle() { + //TODO mit game offer control und game data reden (spieler werden nich wieder entfernt) + + abort(); + } + })); + connections.add(view.getSettingsPanel().getChangePlayerColorEvent().add(new IListener2() { + @Override + public void handle(Integer playerNumber, Color color) { + // TODO Auto-generated method stub + + + } + })); } private void updateSettingsPanel(GameSettings settings) { view.getSettingsPanel().setGameSettings(settings); - Set colors = new HashSet( - Arrays.asList(ISettingsPanel.PLAYER_COLORS)); + Set colors = new HashSet(Arrays + .asList(ISettingsPanel.PLAYER_COLORS)); for (PlayerSettings player : settings.getPlayerList()) { colors.remove(player.getColor()); @@ -78,11 +101,15 @@ public class GameJoinControl { view.showSettingsPanel(true); } + /** + * Aborts joining and goes back to game list + */ public void abort() { - // TODO Implement abort - + view.showSettingsPanel(false); + view.showGameListPanel(true); for (Connection c : connections) { c.remove(); } + } } diff --git a/src/jrummikub/control/network/NetworkSettingsControl.java b/src/jrummikub/control/network/NetworkSettingsControl.java index 447bf08..d9871d9 100644 --- a/src/jrummikub/control/network/NetworkSettingsControl.java +++ b/src/jrummikub/control/network/NetworkSettingsControl.java @@ -48,16 +48,16 @@ public class NetworkSettingsControl extends AbstractSettingsControl { addListeners(); - connections.add(view.getSettingsPanel().getOfferGameEvent() - .add(new IListener() { + connections.add(view.getSettingsPanel().getOfferGameEvent().add( + new IListener() { @Override public void handle() { offerGame(); } })); - connections.add(view.getSettingsPanel().getBackEvent() - .add(new IListener() { + connections.add(view.getSettingsPanel().getBackEvent().add( + new IListener() { @Override public void handle() { abort(); @@ -66,15 +66,27 @@ public class NetworkSettingsControl extends AbstractSettingsControl { })); } + /** + * The game offer event is emitted when a player wants to offer a new + * network game + * + * @return the event + */ public Event1 getOfferGameEvent() { return offerGameEvent; } + /** + * The get back event is emitted when the player wants to go back to the + * previous settings panel + * + * @return the event + */ public Event getBackEvent() { return backEvent; } - - private void offerGame(){ + + private void offerGame() { if (!checkSettings()) { return; } @@ -84,7 +96,8 @@ public class NetworkSettingsControl extends AbstractSettingsControl { @Override protected void update() { - view.getSettingsPanel() + view + .getSettingsPanel() .enableAddPlayerButton( settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); -- cgit v1.2.3