Refactor ConnectionControl
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@475 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
71f1f20d35
commit
7396a4ee85
2 changed files with 57 additions and 72 deletions
|
@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,9 @@ public class GameJoinControl extends AbstractGameBeginControl {
|
|||
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
|
||||
|
|
Reference in a new issue