interrupt XMPPConnection Thread

in some cases the the DNS query might take too long (even though we specified a timeout)
if that happens we need a secondary solution (besides killing the socket) to stop the thread
This commit is contained in:
Daniel Gultsch 2016-05-12 21:54:42 +02:00
parent cbdb413613
commit 2014f388b1
2 changed files with 15 additions and 1 deletions

View file

@ -51,14 +51,19 @@ public class DNSHelper {
final String host = jid.getDomainpart(); final String host = jid.getDomainpart();
final List<InetAddress> servers = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDnsServers(context) : getDnsServersPreLollipop(); final List<InetAddress> servers = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDnsServers(context) : getDnsServersPreLollipop();
Bundle b = new Bundle(); Bundle b = new Bundle();
boolean interrupted = false;
for(InetAddress server : servers) { for(InetAddress server : servers) {
if (Thread.currentThread().isInterrupted()) {
interrupted = true;
break;
}
b = queryDNS(host, server); b = queryDNS(host, server);
if (b.containsKey("values")) { if (b.containsKey("values")) {
return b; return b;
} }
} }
if (!b.containsKey("values")) { if (!b.containsKey("values")) {
Log.d(Config.LOGTAG,"all dns queries failed. provide fallback A record"); Log.d(Config.LOGTAG,(interrupted ? "Thread interrupted during DNS query" :"all dns queries failed") + ". provide fallback A record");
ArrayList<Parcelable> values = new ArrayList<>(); ArrayList<Parcelable> values = new ArrayList<>();
values.add(createNamePortBundle(host, 5222, false)); values.add(createNamePortBundle(host, 5222, false));
b.putParcelableArrayList("values",values); b.putParcelableArrayList("values",values);

View file

@ -280,6 +280,10 @@ public class XmppConnection implements Runnable {
final Bundle result = DNSHelper.getSRVRecord(account.getServer(), mXmppConnectionService); final Bundle result = DNSHelper.getSRVRecord(account.getServer(), mXmppConnectionService);
final ArrayList<Parcelable>values = result.getParcelableArrayList("values"); final ArrayList<Parcelable>values = result.getParcelableArrayList("values");
for(Iterator<Parcelable> iterator = values.iterator(); iterator.hasNext();) { for(Iterator<Parcelable> iterator = values.iterator(); iterator.hasNext();) {
if (Thread.currentThread().isInterrupted()) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": Thread was interrupted");
return;
}
final Bundle namePort = (Bundle) iterator.next(); final Bundle namePort = (Bundle) iterator.next();
try { try {
String srvRecordServer; String srvRecordServer;
@ -1334,7 +1338,12 @@ public class XmppConnection implements Runnable {
} }
} }
public void interrupt() {
Thread.currentThread().interrupt();
}
public void disconnect(final boolean force) { public void disconnect(final boolean force) {
interrupt();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force="+Boolean.valueOf(force)); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force="+Boolean.valueOf(force));
if (force) { if (force) {
forceCloseSocket(); forceCloseSocket();