better exception handling in XmppConnection.connect. (never return without throwing exception. use finally to release wake lock. use status.server_not_found instead of status.offline when necessary
This commit is contained in:
parent
d2a4855a1e
commit
090e6ecf09
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.net.ConnectException;
|
||||||
import java.net.IDN;
|
import java.net.IDN;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
@ -158,9 +159,7 @@ public class XmppConnection implements Runnable {
|
||||||
Bundle result = DNSHelper.getSRVRecord(account.getServer());
|
Bundle result = DNSHelper.getSRVRecord(account.getServer());
|
||||||
ArrayList<Parcelable> values = result.getParcelableArrayList("values");
|
ArrayList<Parcelable> values = result.getParcelableArrayList("values");
|
||||||
if ("timeout".equals(result.getString("error"))) {
|
if ("timeout".equals(result.getString("error"))) {
|
||||||
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": dns timeout");
|
throw new IOException("timeout in dns");
|
||||||
this.changeStatus(Account.State.OFFLINE);
|
|
||||||
return;
|
|
||||||
} else if (values != null) {
|
} else if (values != null) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
boolean socketError = true;
|
boolean socketError = true;
|
||||||
|
@ -200,23 +199,13 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (socketError) {
|
if (socketError) {
|
||||||
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
throw new UnknownHostException();
|
||||||
if (wakeLock.isHeld()) {
|
|
||||||
try {
|
|
||||||
wakeLock.release();
|
|
||||||
} catch (final RuntimeException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else if (result.containsKey("error")
|
} else if (result.containsKey("error")
|
||||||
&& "nosrv".equals(result.getString("error", null))) {
|
&& "nosrv".equals(result.getString("error", null))) {
|
||||||
socket = new Socket(account.getServer().getDomainpart(), 5222);
|
socket = new Socket(account.getServer().getDomainpart(), 5222);
|
||||||
} else {
|
} else {
|
||||||
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
throw new IOException("timeout in dns");
|
||||||
+ ": timeout in DNS resolution");
|
|
||||||
changeStatus(Account.State.OFFLINE);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
OutputStream out = socket.getOutputStream();
|
OutputStream out = socket.getOutputStream();
|
||||||
tagWriter.setOutputStream(out);
|
tagWriter.setOutputStream(out);
|
||||||
|
@ -230,9 +219,7 @@ public class XmppConnection implements Runnable {
|
||||||
processStream(nextTag);
|
processStream(nextTag);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
Log.d(Config.LOGTAG,
|
throw new IOException("unknown tag on connect");
|
||||||
"found unexpected tag: " + nextTag.getName());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (socket.isConnected()) {
|
if (socket.isConnected()) {
|
||||||
|
@ -240,25 +227,15 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
||||||
if (wakeLock.isHeld()) {
|
} catch (final ConnectException e) {
|
||||||
try {
|
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
||||||
wakeLock.release();
|
|
||||||
} catch (final RuntimeException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (final IOException | XmlPullParserException e) {
|
} catch (final IOException | XmlPullParserException e) {
|
||||||
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
||||||
this.changeStatus(Account.State.OFFLINE);
|
this.changeStatus(Account.State.OFFLINE);
|
||||||
if (wakeLock.isHeld()) {
|
|
||||||
try {
|
|
||||||
wakeLock.release();
|
|
||||||
} catch (final RuntimeException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
||||||
this.changeStatus(Account.State.OFFLINE);
|
this.changeStatus(Account.State.OFFLINE);
|
||||||
Log.d(Config.LOGTAG, "compression exception " + e.getMessage());
|
} finally {
|
||||||
if (wakeLock.isHeld()) {
|
if (wakeLock.isHeld()) {
|
||||||
try {
|
try {
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
|
@ -266,7 +243,6 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -534,7 +510,7 @@ public class XmppConnection implements Runnable {
|
||||||
NoSuchAlgorithmException {
|
NoSuchAlgorithmException {
|
||||||
tagReader.readTag(); // read tag close
|
tagReader.readTag(); // read tag close
|
||||||
tagWriter.setOutputStream(new ZLibOutputStream(tagWriter
|
tagWriter.setOutputStream(new ZLibOutputStream(tagWriter
|
||||||
.getOutputStream()));
|
.getOutputStream()));
|
||||||
tagReader
|
tagReader
|
||||||
.setInputStream(new ZLibInputStream(tagReader.getInputStream()));
|
.setInputStream(new ZLibInputStream(tagReader.getInputStream()));
|
||||||
|
|
||||||
|
@ -726,23 +702,23 @@ public class XmppConnection implements Runnable {
|
||||||
&& (packet.query().hasChild("password"))) {
|
&& (packet.query().hasChild("password"))) {
|
||||||
IqPacket register = new IqPacket(IqPacket.TYPE_SET);
|
IqPacket register = new IqPacket(IqPacket.TYPE_SET);
|
||||||
Element username = new Element("username")
|
Element username = new Element("username")
|
||||||
.setContent(account.getUsername());
|
.setContent(account.getUsername());
|
||||||
Element password = new Element("password")
|
Element password = new Element("password")
|
||||||
.setContent(account.getPassword());
|
.setContent(account.getPassword());
|
||||||
register.query("jabber:iq:register").addChild(username);
|
register.query("jabber:iq:register").addChild(username);
|
||||||
register.query().addChild(password);
|
register.query().addChild(password);
|
||||||
sendIqPacket(register, new OnIqPacketReceived() {
|
sendIqPacket(register, new OnIqPacketReceived() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onIqPacketReceived(Account account,
|
public void onIqPacketReceived(Account account,
|
||||||
IqPacket packet) {
|
IqPacket packet) {
|
||||||
if (packet.getType() == IqPacket.TYPE_RESULT) {
|
if (packet.getType() == IqPacket.TYPE_RESULT) {
|
||||||
account.setOption(Account.OPTION_REGISTER,
|
account.setOption(Account.OPTION_REGISTER,
|
||||||
false);
|
false);
|
||||||
changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
|
changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
|
||||||
} else if (packet.hasChild("error")
|
} else if (packet.hasChild("error")
|
||||||
&& (packet.findChild("error")
|
&& (packet.findChild("error")
|
||||||
.hasChild("conflict"))) {
|
.hasChild("conflict"))) {
|
||||||
changeStatus(Account.State.REGISTRATION_CONFLICT);
|
changeStatus(Account.State.REGISTRATION_CONFLICT);
|
||||||
} else {
|
} else {
|
||||||
changeStatus(Account.State.REGISTRATION_FAILED);
|
changeStatus(Account.State.REGISTRATION_FAILED);
|
||||||
|
@ -785,7 +761,7 @@ public class XmppConnection implements Runnable {
|
||||||
stanzasSent = 0;
|
stanzasSent = 0;
|
||||||
messageReceipts.clear();
|
messageReceipts.clear();
|
||||||
} else if (streamFeatures.hasChild("sm",
|
} else if (streamFeatures.hasChild("sm",
|
||||||
"urn:xmpp:sm:2")) {
|
"urn:xmpp:sm:2")) {
|
||||||
smVersion = 2;
|
smVersion = 2;
|
||||||
EnablePacket enable = new EnablePacket(smVersion);
|
EnablePacket enable = new EnablePacket(smVersion);
|
||||||
tagWriter.writeStanzaAsync(enable);
|
tagWriter.writeStanzaAsync(enable);
|
||||||
|
@ -906,7 +882,7 @@ public class XmppConnection implements Runnable {
|
||||||
account.setResource(resource + "." + nextRandomId());
|
account.setResource(resource + "." + nextRandomId());
|
||||||
Log.d(Config.LOGTAG,
|
Log.d(Config.LOGTAG,
|
||||||
account.getJid().toBareJid() + ": switching resource due to conflict ("
|
account.getJid().toBareJid() + ": switching resource due to conflict ("
|
||||||
+ account.getResource() + ")");
|
+ account.getResource() + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue