From 2d08646187f514032d3d7eba7aed3767e1505d6f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 20 Jun 2011 19:17:27 +0200 Subject: Various ConnectionControl fixes git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@513 72836036-5685-4462-b002-a69064685172 --- .../control/network/ConnectionControl.java | 94 ++++++++++++++-------- src/jrummikub/view/LoginError.java | 2 - src/jrummikub/view/impl/ConnectPanel.java | 3 - 3 files changed, 61 insertions(+), 38 deletions(-) (limited to 'src/jrummikub') diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 7c3ed77..76b430a 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -23,6 +23,7 @@ import jrummikub.util.LoginData; import jrummikub.view.LoginError; import org.jivesoftware.smack.Connection; +import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; @@ -36,6 +37,7 @@ import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError.Type; import org.jivesoftware.smack.util.Base64; +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.muc.DiscussionHistory; import org.jivesoftware.smackx.muc.MultiUserChat; @@ -487,7 +489,13 @@ public class ConnectionControl implements IConnectionControl { @Override public void run() { synchronized (ConnectionControl.this) { - connection = new XMPPConnection(loginData.getServerName()); + ConnectionConfiguration config = new ConnectionConfiguration( + loginData.getServerName()); + config.setSendPresence(false); + config.setRosterLoadedAtLogin(false); + config.setCompressionEnabled(true); + + connection = new XMPPConnection(config); connection.addPacketListener(new PacketListener() { @Override @@ -503,40 +511,55 @@ public class ConnectionControl implements IConnectionControl { }, new AndFilter(new PacketTypeFilter(Message.class), new PacketExtensionFilter(ELEMENT_NAME, NAMESPACE))); - try { - doConnect(); - } catch (XMPPException e) { + LoginError error = doConnect(); + if (error == null) { + error = doLogin(); + } + if (error == null) { + error = doJoin(); + } + + if (error == null) { + requestGames(); + emitLater(connectedEvent); + } else { connection.disconnect(); connection = null; - XMPPError xmppError = e.getXMPPError(); + emitLater(connectionFailedEvent, error); + } + } + } - 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; - } + private LoginError doConnect() { + try { + connection.connect(); + return null; + } catch (XMPPException e) { + XMPPError xmppError = e.getXMPPError(); + + if (xmppError != null) { + if (xmppError.getType() == Type.CANCEL && xmppError.getCode() == 504) { + return LoginError.UNKNOWN_HOST; } - e.printStackTrace(); - emitLater(connectionFailedEvent, LoginError.UNKNOWN_ERROR); } + + e.printStackTrace(); + return LoginError.UNKNOWN_ERROR; + } + } + + private LoginError doLogin() { + try { + connection.login(loginData.getUserName(), loginData.getPassword(), + "JRummikub-" + StringUtils.randomString(8)); + return null; + } catch (XMPPException e) { + return LoginError.AUTHENTICATION_FAILED; } } - private void doConnect() throws XMPPException { - connection.connect(); - connection.login(loginData.getUserName(), loginData.getPassword(), - "JRummikub"); + private LoginError doJoin() { muc = new MultiUserChat(connection, loginData.getChannelName()); DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(0); @@ -546,24 +569,29 @@ public class ConnectionControl implements IConnectionControl { while (true) { try { muc.join(nickname, null, history, 10000); - break; // Join was successful, break the loop + return null; // Join was successful, break the loop } catch (XMPPException e) { - XMPPError error = e.getXMPPError(); - if (error.getType() == Type.CANCEL && error.getCode() == 409) { + XMPPError xmppError = e.getXMPPError(); + + if (xmppError != null && xmppError.getType() == Type.CANCEL + && xmppError.getCode() == 409) { // There was a conflict, try again with another // nickname nickname += "_"; continue; } else { // An unknown error has occurred, cancel connect - throw e; + if (xmppError != null && xmppError.getType() == Type.CANCEL + && xmppError.getCode() == 404) { + return LoginError.UNKNOWN_CHANNEL; + } + + e.printStackTrace(); + return LoginError.UNKNOWN_ERROR; } } } - emitLater(connectedEvent); - - requestGames(); } } diff --git a/src/jrummikub/view/LoginError.java b/src/jrummikub/view/LoginError.java index 7176693..9b61f4a 100644 --- a/src/jrummikub/view/LoginError.java +++ b/src/jrummikub/view/LoginError.java @@ -12,8 +12,6 @@ public enum LoginError { CONNECTION_REFUSED, /** Password or username incorrect */ AUTHENTICATION_FAILED, - /** Username already occurs in the same channel */ - RESOURCE_CONFLICT, /** Server not found */ UNKNOWN_HOST, /** Channel not found */ diff --git a/src/jrummikub/view/impl/ConnectPanel.java b/src/jrummikub/view/impl/ConnectPanel.java index fc30bb0..6613fd3 100644 --- a/src/jrummikub/view/impl/ConnectPanel.java +++ b/src/jrummikub/view/impl/ConnectPanel.java @@ -91,9 +91,6 @@ public class ConnectPanel extends JPanel implements IConnectPanel { case CONNECTION_REFUSED: text = "Die Verbindung wurde abgelehnt"; break; - case RESOURCE_CONFLICT: - text = "Es gibt bereits einen solchen Spieler"; - break; case TIMEOUT: text = "Der Server reagiert nicht"; break; -- cgit v1.2.3