Various ConnectionControl fixes
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@513 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
72a16316dc
commit
2d08646187
3 changed files with 63 additions and 40 deletions
|
@ -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;
|
||||
|
||||
emitLater(connectionFailedEvent, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private LoginError doConnect() {
|
||||
try {
|
||||
connection.connect();
|
||||
return null;
|
||||
} catch (XMPPException e) {
|
||||
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, LoginError.UNKNOWN_ERROR);
|
||||
}
|
||||
if (xmppError.getType() == Type.CANCEL && xmppError.getCode() == 504) {
|
||||
return LoginError.UNKNOWN_HOST;
|
||||
}
|
||||
}
|
||||
|
||||
private void doConnect() throws XMPPException {
|
||||
connection.connect();
|
||||
e.printStackTrace();
|
||||
return LoginError.UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
private LoginError doLogin() {
|
||||
try {
|
||||
connection.login(loginData.getUserName(), loginData.getPassword(),
|
||||
"JRummikub");
|
||||
"JRummikub-" + StringUtils.randomString(8));
|
||||
return null;
|
||||
} catch (XMPPException e) {
|
||||
return LoginError.AUTHENTICATION_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
|
|
Reference in a new issue