mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-02 06:45:59 +01:00
Move DNS resolving into connect()
It was misplaced in ConnectionConfiguration anyways, as the sole instantiation of a ConnectionConfiguration should not cause any network I/O.
This commit is contained in:
parent
dbab9b8995
commit
d349940537
7 changed files with 56 additions and 64 deletions
|
@ -102,8 +102,6 @@ public class ConnectionConfiguration implements Cloneable {
|
|||
* @param serviceName the name of the service provided by an XMPP server.
|
||||
*/
|
||||
public ConnectionConfiguration(String serviceName) {
|
||||
// Perform DNS lookup to get host and port to use
|
||||
hostAddresses = DNSUtil.resolveXMPPDomain(serviceName);
|
||||
init(serviceName, ProxyInfo.forDefaultProxy());
|
||||
}
|
||||
|
||||
|
@ -117,8 +115,6 @@ public class ConnectionConfiguration implements Cloneable {
|
|||
* @param proxy the proxy through which XMPP is to be connected
|
||||
*/
|
||||
public ConnectionConfiguration(String serviceName,ProxyInfo proxy) {
|
||||
// Perform DNS lookup to get host and port to use
|
||||
hostAddresses = DNSUtil.resolveXMPPDomain(serviceName);
|
||||
init(serviceName, proxy);
|
||||
}
|
||||
|
||||
|
@ -203,7 +199,7 @@ public class ConnectionConfiguration implements Cloneable {
|
|||
*
|
||||
* @param serviceName the XMPP domain of the target server.
|
||||
*/
|
||||
public void setServiceName(String serviceName) {
|
||||
void setServiceName(String serviceName) {
|
||||
this.serviceName = serviceName;
|
||||
}
|
||||
|
||||
|
@ -569,14 +565,17 @@ public class ConnectionConfiguration implements Cloneable {
|
|||
this.resource = resource;
|
||||
}
|
||||
|
||||
void maybeResolveDns() throws Exception {
|
||||
// Abort if we did already resolve the hosts successfully
|
||||
if (hostAddresses != null)
|
||||
return;
|
||||
hostAddresses = DNSUtil.resolveXMPPDomain(serviceName);
|
||||
}
|
||||
|
||||
private void initHostAddresses(String host, int port) {
|
||||
hostAddresses = new ArrayList<HostAddress>(1);
|
||||
HostAddress hostAddress;
|
||||
try {
|
||||
hostAddress = new HostAddress(host, port);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
hostAddress = new HostAddress(host, port);
|
||||
hostAddresses.add(hostAddress);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,8 +77,9 @@ public class DNSUtil {
|
|||
* @param domain the domain.
|
||||
* @return List of HostAddress, which encompasses the hostname and port that the
|
||||
* XMPP server can be reached at for the specified domain.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static List<HostAddress> resolveXMPPDomain(final String domain) {
|
||||
public static List<HostAddress> resolveXMPPDomain(final String domain) throws Exception {
|
||||
if (dnsResolver == null) {
|
||||
List<HostAddress> addresses = new ArrayList<HostAddress>(1);
|
||||
addresses.add(new HostAddress(domain, 5222));
|
||||
|
@ -103,8 +104,9 @@ public class DNSUtil {
|
|||
* @param domain the domain.
|
||||
* @return List of HostAddress, which encompasses the hostname and port that the
|
||||
* XMPP server can be reached at for the specified domain.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static List<HostAddress> resolveXMPPServerDomain(final String domain) {
|
||||
public static List<HostAddress> resolveXMPPServerDomain(final String domain) throws Exception {
|
||||
if (dnsResolver == null) {
|
||||
List<HostAddress> addresses = new ArrayList<HostAddress>(1);
|
||||
addresses.add(new HostAddress(domain, 5269));
|
||||
|
@ -113,7 +115,7 @@ public class DNSUtil {
|
|||
return resolveDomain(domain, 's');
|
||||
}
|
||||
|
||||
private static List<HostAddress> resolveDomain(String domain, char keyPrefix) {
|
||||
private static List<HostAddress> resolveDomain(String domain, char keyPrefix) throws Exception {
|
||||
// Prefix the key with 's' to distinguish him from the client domain lookups
|
||||
String key = keyPrefix + domain;
|
||||
// Return item from cache if it exists.
|
||||
|
|
|
@ -29,6 +29,6 @@ public interface DNSResolver {
|
|||
* @param name The symbolic name of the service.
|
||||
* @return The list of SRV records mapped to the service name.
|
||||
*/
|
||||
List<SRVRecord> lookupSRVRecords(String name);
|
||||
List<SRVRecord> lookupSRVRecords(String name) throws Exception;
|
||||
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ public class HostAddress {
|
|||
this(fqdn);
|
||||
if (port < 0 || port > 65535)
|
||||
throw new IllegalArgumentException(
|
||||
"DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port);
|
||||
"Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port);
|
||||
|
||||
this.port = port;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2013 Florian Schmaus
|
||||
* Copyright 2013-2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +21,7 @@ import java.util.List;
|
|||
|
||||
import org.xbill.DNS.Lookup;
|
||||
import org.xbill.DNS.Record;
|
||||
import org.xbill.DNS.TextParseException;
|
||||
import org.xbill.DNS.Type;
|
||||
|
||||
/**
|
||||
|
@ -39,35 +40,27 @@ public class DNSJavaResolver implements DNSResolver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<SRVRecord> lookupSRVRecords(String name) {
|
||||
public List<SRVRecord> lookupSRVRecords(String name) throws TextParseException {
|
||||
List<SRVRecord> res = new ArrayList<SRVRecord>();
|
||||
|
||||
try {
|
||||
Lookup lookup = new Lookup(name, Type.SRV);
|
||||
Record recs[] = lookup.run();
|
||||
if (recs == null)
|
||||
return res;
|
||||
Lookup lookup = new Lookup(name, Type.SRV);
|
||||
Record recs[] = lookup.run();
|
||||
if (recs == null)
|
||||
return res;
|
||||
|
||||
for (Record record : recs) {
|
||||
org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
|
||||
if (srvRecord != null && srvRecord.getTarget() != null) {
|
||||
String host = srvRecord.getTarget().toString();
|
||||
int port = srvRecord.getPort();
|
||||
int priority = srvRecord.getPriority();
|
||||
int weight = srvRecord.getWeight();
|
||||
for (Record record : recs) {
|
||||
org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
|
||||
if (srvRecord != null && srvRecord.getTarget() != null) {
|
||||
String host = srvRecord.getTarget().toString();
|
||||
int port = srvRecord.getPort();
|
||||
int priority = srvRecord.getPriority();
|
||||
int weight = srvRecord.getWeight();
|
||||
|
||||
SRVRecord r;
|
||||
try {
|
||||
r = new SRVRecord(host, port, priority, weight);
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
res.add(r);
|
||||
}
|
||||
SRVRecord r = new SRVRecord(host, port, priority, weight);
|
||||
res.add(r);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2013 Florian Schmaus
|
||||
* Copyright 2013-2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +21,7 @@ import java.util.Hashtable;
|
|||
import java.util.List;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
|
@ -68,32 +69,23 @@ public class JavaxResolver implements DNSResolver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<SRVRecord> lookupSRVRecords(String name) {
|
||||
public List<SRVRecord> lookupSRVRecords(String name) throws NamingException {
|
||||
List<SRVRecord> res = new ArrayList<SRVRecord>();
|
||||
|
||||
try {
|
||||
Attributes dnsLookup = dirContext.getAttributes(name, new String[]{"SRV"});
|
||||
Attribute srvAttribute = dnsLookup.get("SRV");
|
||||
@SuppressWarnings("unchecked")
|
||||
NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
|
||||
while (srvRecords.hasMore()) {
|
||||
String srvRecordString = srvRecords.next();
|
||||
String[] srvRecordEntries = srvRecordString.split(" ");
|
||||
int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
|
||||
int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
|
||||
int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
|
||||
String host = srvRecordEntries[srvRecordEntries.length - 1];
|
||||
|
||||
SRVRecord srvRecord;
|
||||
try {
|
||||
srvRecord = new SRVRecord(host, port, priority, weight);
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
res.add(srvRecord);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Attributes dnsLookup = dirContext.getAttributes(name, new String[] { "SRV" });
|
||||
Attribute srvAttribute = dnsLookup.get("SRV");
|
||||
@SuppressWarnings("unchecked")
|
||||
NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
|
||||
while (srvRecords.hasMore()) {
|
||||
String srvRecordString = srvRecords.next();
|
||||
String[] srvRecordEntries = srvRecordString.split(" ");
|
||||
int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
|
||||
int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
|
||||
int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
|
||||
String host = srvRecordEntries[srvRecordEntries.length - 1];
|
||||
|
||||
SRVRecord srvRecord = new SRVRecord(host, port, priority, weight);
|
||||
res.add(srvRecord);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -420,6 +420,12 @@ public class TCPConnection extends XMPPConnection {
|
|||
|
||||
private void connectUsingConfiguration(ConnectionConfiguration config) throws SmackException, IOException {
|
||||
Exception exception = null;
|
||||
try {
|
||||
config.maybeResolveDns();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new SmackException(e);
|
||||
}
|
||||
Iterator<HostAddress> it = config.getHostAddresses().iterator();
|
||||
List<HostAddress> failedAddresses = new LinkedList<HostAddress>();
|
||||
while (it.hasNext()) {
|
||||
|
|
Loading…
Reference in a new issue