summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/ConnectionControl.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control/network/ConnectionControl.java')
-rw-r--r--src/jrummikub/control/network/ConnectionControl.java61
1 files changed, 42 insertions, 19 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java
index acccbdc..54c9512 100644
--- a/src/jrummikub/control/network/ConnectionControl.java
+++ b/src/jrummikub/control/network/ConnectionControl.java
@@ -16,6 +16,7 @@ import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
import jrummikub.util.LoginData;
+import jrummikub.view.LoginError;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
@@ -46,7 +47,7 @@ public class ConnectionControl implements IConnectionControl {
private volatile MultiUserChat muc;
private Event connectedEvent = new Event();
- private Event connectionFailedEvent = new Event();
+ private Event1<LoginError> connectionFailedEvent = new Event1<LoginError>();
private Event1<GameData> gameOfferEvent = new Event1<GameData>();
private Event1<UUID> gameWithdrawalEvent = new Event1<UUID>();
@@ -72,7 +73,7 @@ public class ConnectionControl implements IConnectionControl {
* Creates new connection control
*
* @param loginData
- * player's login data
+ * player's login data
*/
public ConnectionControl(LoginData loginData) {
this.loginData = loginData;
@@ -91,7 +92,7 @@ public class ConnectionControl implements IConnectionControl {
@Override
public void disconnect() {
connectedEvent = new Event();
- connectionFailedEvent = new Event();
+ connectionFailedEvent = new Event1<LoginError>();
run(new Runnable() {
@Override
public void run() {
@@ -111,7 +112,7 @@ public class ConnectionControl implements IConnectionControl {
}
@Override
- public IEvent getConnectionFailedEvent() {
+ public IEvent1<LoginError> getConnectionFailedEvent() {
return connectionFailedEvent;
}
@@ -252,8 +253,7 @@ public class ConnectionControl implements IConnectionControl {
protected void addData(DefaultPacketExtension extension) {
extension.setValue("messageType", "change_color");
extension.setValue("uuid", uuid.toString());
- extension.setValue("color",
- Base64.encodeObject(color, Base64.GZIP));
+ extension.setValue("color", Base64.encodeObject(color, Base64.GZIP));
}
});
}
@@ -316,8 +316,8 @@ public class ConnectionControl implements IConnectionControl {
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));
+ extension.setValue("gameSettings",
+ Base64.encodeObject(data.getGameSettings(), Base64.GZIP));
}
});
}
@@ -340,6 +340,15 @@ public class ConnectionControl implements IConnectionControl {
});
}
+ private static <T> void emitLater(final Event1<T> event, final T arg) {
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ event.emit(arg);
+ }
+ });
+ }
+
private Message createMessage(PacketExtension extension) {
Message message = muc.createMessage();
message.addExtension(extension);
@@ -355,8 +364,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;
}
@@ -372,14 +381,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();
@@ -447,10 +455,26 @@ public class ConnectionControl implements IConnectionControl {
connection.disconnect();
connection = null;
- // TODO Auto-generated catch block
+ XMPPError xmppError = e.getXMPPError();
+
+ if (xmppError != null) {
+ int code = xmppError.getCode();
+ if (code == 504) {
+ emitLater(connectionFailedEvent, LoginError.UNKNOWN_HOST);
+ return;
+ } else if (code == 404) {
+ emitLater(connectionFailedEvent, LoginError.UNKNOWN_CHANNEL);
+ return;
+ } else if (code == 503) {
+ emitLater(connectionFailedEvent, LoginError.RESOURCE_CONFLICT);
+ return;
+ } else if (code == 401 || code == 402 || code == 407) {
+ emitLater(connectionFailedEvent, LoginError.AUTHENTICATION_FAILED);
+ return;
+ }
+ }
e.printStackTrace();
-
- emitLater(connectionFailedEvent);
+ emitLater(connectionFailedEvent, LoginError.UNKNOWN_ERROR);
}
}
}
@@ -471,8 +495,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 += "_";