1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Add XMPPTCPConnectionConfiguration.getConnectTimeout()

This commit is contained in:
Florian Schmaus 2015-01-04 10:55:15 +01:00
parent 665e7914f2
commit a24c813ed1
2 changed files with 43 additions and 7 deletions

View file

@ -95,6 +95,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStore; import java.security.KeyStore;
@ -498,7 +499,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
} }
} }
private void connectUsingConfiguration(ConnectionConfiguration config) throws SmackException, IOException { private void connectUsingConfiguration(XMPPTCPConnectionConfiguration config) throws SmackException, IOException {
try { try {
populateHostAddresses(); populateHostAddresses();
} }
@ -512,13 +513,14 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
HostAddress hostAddress = it.next(); HostAddress hostAddress = it.next();
String host = hostAddress.getFQDN(); String host = hostAddress.getFQDN();
int port = hostAddress.getPort(); int port = hostAddress.getPort();
try {
if (config.getSocketFactory() == null) { if (config.getSocketFactory() == null) {
this.socket = new Socket(host, port); socket = new Socket();
} }
else { else {
this.socket = config.getSocketFactory().createSocket(host, port); socket = config.getSocketFactory().createSocket();
} }
try {
socket.connect(new InetSocketAddress(host, port), config.getConnectTimeout());
} catch (Exception e) { } catch (Exception e) {
exception = e; exception = e;
} }

View file

@ -20,11 +20,23 @@ import org.jivesoftware.smack.ConnectionConfiguration;
public class XMPPTCPConnectionConfiguration extends ConnectionConfiguration { public class XMPPTCPConnectionConfiguration extends ConnectionConfiguration {
/**
* The default connect timeout in milliseconds. Preinitialized with 30000 (30 seconds). If this value is changed,
* new Builder instances will use the new value as default.
*/
public static int DEFAULT_CONNECT_TIMEOUT = 30000;
private final boolean compressionEnabled; private final boolean compressionEnabled;
/**
* How long the socket will wait until a TCP connection is established (in milliseconds).
*/
private final int connectTimeout;
private XMPPTCPConnectionConfiguration(Builder builder) { private XMPPTCPConnectionConfiguration(Builder builder) {
super(builder); super(builder);
compressionEnabled = builder.compressionEnabled; compressionEnabled = builder.compressionEnabled;
connectTimeout = builder.connectTimeout;
} }
/** /**
@ -40,12 +52,22 @@ public class XMPPTCPConnectionConfiguration extends ConnectionConfiguration {
return compressionEnabled; return compressionEnabled;
} }
/**
* How long the socket will wait until a TCP connection is established (in milliseconds). Defaults to {@link #DEFAULT_CONNECT_TIMEOUT}.
*
* @return the timeout value in milliseconds.
*/
public int getConnectTimeout() {
return connectTimeout;
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
public static class Builder extends ConnectionConfiguration.Builder<Builder, XMPPTCPConnectionConfiguration> { public static class Builder extends ConnectionConfiguration.Builder<Builder, XMPPTCPConnectionConfiguration> {
private boolean compressionEnabled = false; private boolean compressionEnabled = false;
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
private Builder() { private Builder() {
} }
@ -57,12 +79,24 @@ public class XMPPTCPConnectionConfiguration extends ConnectionConfiguration {
* up to 90%. By default compression is disabled. * up to 90%. By default compression is disabled.
* *
* @param compressionEnabled if the connection is going to use stream compression. * @param compressionEnabled if the connection is going to use stream compression.
* @return a reference to this object.
*/ */
public Builder setCompressionEnabled(boolean compressionEnabled) { public Builder setCompressionEnabled(boolean compressionEnabled) {
this.compressionEnabled = compressionEnabled; this.compressionEnabled = compressionEnabled;
return this; return this;
} }
/**
* Set how long the socket will wait until a TCP connection is established (in milliseconds).
*
* @param connectTimeout the timeout value to be used in milliseconds.
* @return a reference to this object.
*/
public Builder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
@Override @Override
protected Builder getThis() { protected Builder getThis() {
return this; return this;