diff options
Diffstat (limited to 'src/jrummikub/control/network')
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 94 |
1 files changed, 61 insertions, 33 deletions
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(); } } |