diff options
author | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-06-18 01:25:26 +0200 |
---|---|---|
committer | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-06-18 01:25:26 +0200 |
commit | fbf5cc3572d9117b18c32c677bc030ab8a8c20ce (patch) | |
tree | 343a00407ca85e18b9b7c40f0bed279a41ebf16f /src/jrummikub | |
parent | 5aa115f4db12ffd68eee6fc0e978d6895901b0ce (diff) | |
download | JRummikub-fbf5cc3572d9117b18c32c677bc030ab8a8c20ce.tar JRummikub-fbf5cc3572d9117b18c32c677bc030ab8a8c20ce.zip |
Simplified ConnectionControl
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@454 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub')
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 315 |
1 files changed, 112 insertions, 203 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index f12497d..21d2c8b 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -74,8 +74,17 @@ class ConnectionControl { void disconnect() { connectedEvent = new Event(); connectionFailedEvent = new Event(); - new Thread(new DisconnectRunner()).start(); - + new Thread(new Runnable() { + @Override + public void run() { + synchronized (ConnectionControl.this) { + if (connection != null) { + connection.disconnect(); + connection = null; + } + } + } + }).start(); } IEvent getConnectedEvent() { @@ -119,9 +128,20 @@ class ConnectionControl { void withdrawGame() { offeredGame = null; currentGame = null; - new Thread(new SendGameWithdrawRunner(currentGame.getGameID())).start(); + final UUID uuid = currentGame.getGameID(); + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "game_withdrawal"); + extension.setValue("uuid", uuid.toString()); + + return createMessage(extension); + } + }); } - + GameData getCurrentGame() { return currentGame; } @@ -130,26 +150,101 @@ class ConnectionControl { this.currentGame = game; } - void joinGame(GameData game) { + void joinGame(final GameData game) { setCurrentGame(game); - new Thread(new SendGameJoinRunner(currentGame.getGameID())).start(); + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "game_join"); + extension.setValue("uuid", game.getGameID().toString()); + + return createMessage(extension); + } + }).start(); } void leaveGame() { - new Thread(new SendGameLeaveRunner(currentGame.getGameID())).start(); + final UUID uuid = currentGame.getGameID(); currentGame = null; + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "game_leave"); + extension.setValue("uuid", uuid.toString()); + + return createMessage(extension); + } + }).start(); } - void ackJoinGame(String recipient, boolean ack) { - new Thread(new SendGameJoinAckRunner(currentGame.getGameID(), recipient, ack)).start(); + void ackJoinGame(final String recipient, final boolean ack) { + final UUID uuid = currentGame.getGameID(); + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "game_join_ack"); + extension.setValue("uuid", uuid.toString()); + extension.setValue("ack", Boolean.toString(ack)); + + Message message = createMessage(extension); + message.setType(Message.Type.normal); + message.setTo(muc.getRoom() + "/" + recipient); + return message; + } + }).start(); } - private void sendGameOffer() { - new Thread(new SendGameOfferRunner(offeredGame)).start(); + void changeColor(final Color color) { + final UUID uuid = currentGame.getGameID(); + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "change_color"); + extension.setValue("uuid", uuid.toString()); + extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); + + return createMessage(extension); + } + }).start(); + } - private void sendChangeColor(Color color) { - new Thread(new SendChangeColorRunner(color)).start(); + private void sendGameOffer() { + final GameData data = offeredGame; + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "game_offer"); + extension.setValue("uuid", data.getGameID().toString()); + extension.setValue("gameSettings", Base64.encodeObject( + data.getGameSettings(), Base64.GZIP)); + + return createMessage(extension); + } + }).start(); + } + + private void requestGames() { + new Thread(new SendRunner() { + @Override + public Message send() { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "game_request"); + + return createMessage(extension); + } + }); } private static void emitLater(final Event event) { @@ -222,75 +317,6 @@ class ConnectionControl { } } - private Message createGameOfferMessage(GameData data) { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "game_offer"); - extension.setValue("uuid", data.getGameID().toString()); - extension.setValue("gameSettings", - Base64.encodeObject(data.getGameSettings(), Base64.GZIP)); - - return createMessage(extension); - } - - private Message createGameWithdrawMessage(UUID uuid) { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "game_withdrawal"); - extension.setValue("uuid", uuid.toString()); - - return createMessage(extension); - } - - private Message createGameJoinMessage(UUID uuid) { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "game_join"); - extension.setValue("uuid", uuid.toString()); - - return createMessage(extension); - } - - private Message createGameLeaveMessage(UUID uuid) { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "game_leave"); - extension.setValue("uuid", uuid.toString()); - - return createMessage(extension); - } - - private Message createChangeColorMessage(Color color) { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "change_color"); - extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); - - return createMessage(extension); - } - - private Message createGameAckMessage(UUID uuid, String recipient, - boolean ack) { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "game_join_ack"); - extension.setValue("uuid", uuid.toString()); - extension.setValue("ack", Boolean.toString(ack)); - - Message message = createMessage(extension); - message.setType(Message.Type.normal); - message.setTo(muc.getRoom() + "/" + recipient); - return message; - } - - private Message createGameRequestMessage() { - DefaultPacketExtension extension = createJRummikubExtension(); - - extension.setValue("messageType", "game_request"); - - return createMessage(extension); - } - private class ConnectRunner implements Runnable { @Override public void run() { @@ -356,137 +382,20 @@ class ConnectionControl { emitLater(connectedEvent); - new Thread(new SendGameRequestRunner()).start(); + requestGames(); } } - private class SendGameOfferRunner implements Runnable { - private GameData data; - - public SendGameOfferRunner(GameData data) { - this.data = data; - } - + private abstract class SendRunner implements Runnable { @Override public void run() { synchronized (ConnectionControl.this) { if (connection != null) { - connection.sendPacket(createGameOfferMessage(data)); + connection.sendPacket(send()); } } } - } - private class SendGameWithdrawRunner implements Runnable { - private UUID uuid; - - public SendGameWithdrawRunner(UUID uuid) { - this.uuid = uuid; - } - - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.sendPacket(createGameWithdrawMessage(uuid)); - } - } - } - } - - private class SendGameRequestRunner implements Runnable { - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.sendPacket(createGameRequestMessage()); - } - } - } - } - - private class SendGameJoinRunner implements Runnable { - private UUID uuid; - - public SendGameJoinRunner(UUID uuid) { - this.uuid = uuid; - } - - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.sendPacket(createGameJoinMessage(uuid)); - } - } - } - } - - private class SendChangeColorRunner implements Runnable { - private Color color; - - public SendChangeColorRunner(Color color) { - this.color = color; - } - - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.sendPacket(createChangeColorMessage(color)); - } - } - } - } - - private class SendGameLeaveRunner implements Runnable { - private UUID uuid; - - public SendGameLeaveRunner(UUID uuid) { - this.uuid = uuid; - } - - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.sendPacket(createGameLeaveMessage(uuid)); - } - } - } - } - - private class SendGameJoinAckRunner implements Runnable { - private UUID uuid; - private String recipient; - private boolean ack; - - public SendGameJoinAckRunner(UUID uuid, String recipient, boolean ack) { - this.uuid = uuid; - this.recipient = recipient; - this.ack = ack; - } - - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.sendPacket(createGameAckMessage(uuid, recipient, - ack)); - } - } - } - } - - private class DisconnectRunner implements Runnable { - @Override - public void run() { - synchronized (ConnectionControl.this) { - if (connection != null) { - connection.disconnect(); - connection = null; - } - } - } + abstract public Message send(); } } |