mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 18:59:41 +02: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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue