diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-19 00:58:06 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-19 00:58:06 +0200 |
commit | 7396a4ee85052fc0593cf10f2d7cc66fa20d8420 (patch) | |
tree | f764976c7d0ca994b3a1064c173e3f26eb4529aa /src/jrummikub/control/network | |
parent | 71f1f20d3583fb7e279690774fe88870883db7f7 (diff) | |
download | JRummikub-7396a4ee85052fc0593cf10f2d7cc66fa20d8420.tar JRummikub-7396a4ee85052fc0593cf10f2d7cc66fa20d8420.zip |
Refactor ConnectionControl
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@475 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control/network')
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 120 | ||||
-rw-r--r-- | src/jrummikub/control/network/GameJoinControl.java | 9 |
2 files changed, 57 insertions, 72 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 81f6095..86322b2 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -68,14 +68,14 @@ public class ConnectionControl implements IConnectionControl { @Override public void connect() { - new Thread(new ConnectRunner()).start(); + run(new ConnectRunner()); } @Override public void disconnect() { connectedEvent = new Event(); connectionFailedEvent = new Event(); - new Thread(new Runnable() { + run(new Runnable() { @Override public void run() { synchronized (ConnectionControl.this) { @@ -85,7 +85,7 @@ public class ConnectionControl implements IConnectionControl { } } } - }).start(); + }); } @Override @@ -140,17 +140,13 @@ public class ConnectionControl implements IConnectionControl { offeredGame = null; final UUID uuid = currentGame.getGameID(); currentGame = null; - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_withdrawal"); extension.setValue("uuid", uuid.toString()); - - return createMessage(extension); } - }).start(); + }); } @Override @@ -163,104 +159,86 @@ public class ConnectionControl implements IConnectionControl { this.currentGame = game; } + private void run(Runnable runner) { + new Thread(runner).start(); + } + @Override public void joinGame(final GameData game) { setCurrentGame(game); - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_join"); extension.setValue("uuid", game.getGameID().toString()); - - return createMessage(extension); } - }).start(); + }); } @Override public void leaveGame() { final UUID uuid = currentGame.getGameID(); currentGame = null; - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_leave"); extension.setValue("uuid", uuid.toString()); - - return createMessage(extension); } - }).start(); + }); } @Override public void ackJoinGame(final String recipient, final boolean ack) { final UUID uuid = currentGame.getGameID(); - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_join_ack"); extension.setValue("uuid", uuid.toString()); extension.setValue("ack", Boolean.toString(ack)); + } - Message message = createMessage(extension); + @Override + protected void modifyMessage(Message message) { message.setType(Message.Type.normal); message.setTo(muc.getRoom() + "/" + recipient); - return message; } - }).start(); + }); } @Override public void changeColor(final Color color) { final UUID uuid = currentGame.getGameID(); - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "change_color"); extension.setValue("uuid", uuid.toString()); - extension.setValue("color", - Base64.encodeObject(color, Base64.GZIP)); - - return createMessage(extension); + extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); } - }).start(); + }); } private void sendGameOffer() { final GameData data = offeredGame; - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_offer"); extension.setValue("uuid", data.getGameID().toString()); - extension.setValue("gameSettings", Base64.encodeObject( - data.getGameSettings(), Base64.GZIP)); - - return createMessage(extension); + extension.setValue("gameSettings", + Base64.encodeObject(data.getGameSettings(), Base64.GZIP)); } - }).start(); + }); } private void requestGames() { - new Thread(new SendRunner() { + run(new SendRunner() { @Override - public Message send() { - DefaultPacketExtension extension = createJRummikubExtension(); - + protected void addData(DefaultPacketExtension extension) { extension.setValue("messageType", "game_request"); - - return createMessage(extension); } }); } @@ -289,8 +267,8 @@ public class ConnectionControl implements IConnectionControl { .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; } @@ -306,14 +284,13 @@ public class ConnectionControl implements IConnectionControl { String sender, String messageType) { 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(); @@ -334,11 +311,10 @@ public class ConnectionControl implements IConnectionControl { } else if (messageType.equals("game_leave")) { gameLeaveEvent.emit(sender); } else if (messageType.equals("game_join_ack")) { - gameJoinAckEvent - .emit(Boolean.valueOf(extension.getValue("ack"))); + gameJoinAckEvent.emit(Boolean.valueOf(extension.getValue("ack"))); } else if (messageType.equals("change_color")) { - changeColorEvent.emit(sender, (Color) Base64 - .decodeToObject(extension.getValue("color"))); + changeColorEvent.emit(sender, + (Color) Base64.decodeToObject(extension.getValue("color"))); } else { System.err.println("Received unrecognized message of type '" + messageType + "'"); @@ -395,8 +371,7 @@ public class ConnectionControl implements IConnectionControl { 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 += "_"; @@ -419,11 +394,18 @@ public class ConnectionControl implements IConnectionControl { public void run() { synchronized (ConnectionControl.this) { if (connection != null) { - connection.sendPacket(send()); + DefaultPacketExtension extension = createJRummikubExtension(); + addData(extension); + Message message = createMessage(extension); + modifyMessage(message); + connection.sendPacket(message); } } } - abstract public Message send(); + abstract protected void addData(DefaultPacketExtension extension); + + protected void modifyMessage(Message message) { + } } } diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 489199e..184ce75 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -19,16 +19,19 @@ public class GameJoinControl extends AbstractGameBeginControl { * Creates new game join control * * @param connectionControl - * the current connection control for events and messages + * the current connection control for events and messages * @param gameData - * the game data for settings, game id + * the game data for settings, game id * @param view - * the view + * the view */ public GameJoinControl(final IConnectionControl connectionControl, final GameData gameData, final IView view) { super(connectionControl, view, gameData, SettingsMode.NETWORK_JOIN); + fixGameSettings(gameData.getGameSettings()); + updateSettingsPanel(); + connections.add(connectionControl.getGameOfferEvent().add( new IListener1<GameData>() { @Override |