From 4df5c0e665991dd19a2f25ee513c3ff85a672c55 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Mon, 20 Jun 2011 03:06:46 +0200 Subject: Implemented login error messages git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@500 72836036-5685-4462-b002-a69064685172 --- .../control/network/ConnectionControl.java | 61 +++++++++++++++------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'src/jrummikub/control/network/ConnectionControl.java') 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 connectionFailedEvent = new Event1(); private Event1 gameOfferEvent = new Event1(); private Event1 gameWithdrawalEvent = new Event1(); @@ -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(); run(new Runnable() { @Override public void run() { @@ -111,7 +112,7 @@ public class ConnectionControl implements IConnectionControl { } @Override - public IEvent getConnectionFailedEvent() { + public IEvent1 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 void emitLater(final Event1 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 += "_"; -- cgit v1.2.3