001/**
002 *
003 * Copyright 2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.tcp;
018
019import org.jivesoftware.smack.ConnectionConfiguration;
020
021/**
022 * A connection configuration for XMPP connections over TCP (the common case).
023 * <p>
024 * You can get an instance of the configuration builder with {@link #builder()} and build the final immutable connection
025 * configuration with {@link Builder#build()}.
026 * </p>
027 * <pre>
028 * {@code
029 * XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
030 *     .setXmppDomain("example.org").setUsernameAndPassword("user", "password")
031 *     .setCompressionEnabled(false).build();
032 * XMPPTCPConnection connection = new XMPPTCPConnection(conf);
033 * }
034 * </pre>
035 */
036public final class XMPPTCPConnectionConfiguration extends ConnectionConfiguration {
037
038    /**
039     * The default connect timeout in milliseconds. Preinitialized with 30000 (30 seconds). If this value is changed,
040     * new Builder instances will use the new value as default.
041     */
042    public static int DEFAULT_CONNECT_TIMEOUT = 30000;
043
044    /**
045     * How long the socket will wait until a TCP connection is established (in milliseconds).
046     */
047    private final int connectTimeout;
048
049    private XMPPTCPConnectionConfiguration(Builder builder) {
050        super(builder);
051        connectTimeout = builder.connectTimeout;
052    }
053
054    /**
055     * How long the socket will wait until a TCP connection is established (in milliseconds). Defaults to {@link #DEFAULT_CONNECT_TIMEOUT}.
056     *
057     * @return the timeout value in milliseconds.
058     */
059    public int getConnectTimeout() {
060        return connectTimeout;
061    }
062
063    public static Builder builder() {
064        return new Builder();
065    }
066
067    /**
068     * A configuration builder for XMPP connections over TCP. Use {@link XMPPTCPConnectionConfiguration#builder()} to
069     * obtain a new instance and {@link #build} to build the configuration.
070     */
071    public static final class Builder extends ConnectionConfiguration.Builder<Builder, XMPPTCPConnectionConfiguration> {
072        private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
073
074        private Builder() {
075        }
076
077        /**
078         * Set how long the socket will wait until a TCP connection is established (in milliseconds).
079         *
080         * @param connectTimeout the timeout value to be used in milliseconds.
081         * @return a reference to this object.
082         */
083        public Builder setConnectTimeout(int connectTimeout) {
084            this.connectTimeout = connectTimeout;
085            return this;
086        }
087
088        @Override
089        protected Builder getThis() {
090            return this;
091        }
092
093        @Override
094        public XMPPTCPConnectionConfiguration build() {
095            return new XMPPTCPConnectionConfiguration(this);
096        }
097    }
098}