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 = XMPPConnectionConfiguration.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    private final boolean compressionEnabled;
045
046    /**
047     * How long the socket will wait until a TCP connection is established (in milliseconds).
048     */
049    private final int connectTimeout;
050
051    private XMPPTCPConnectionConfiguration(Builder builder) {
052        super(builder);
053        compressionEnabled = builder.compressionEnabled;
054        connectTimeout = builder.connectTimeout;
055    }
056
057    /**
058     * Returns true if the connection is going to use stream compression. Stream compression
059     * will be requested after TLS was established (if TLS was enabled) and only if the server
060     * offered stream compression. With stream compression network traffic can be reduced
061     * up to 90%. By default compression is disabled.
062     *
063     * @return true if the connection is going to use stream compression.
064     */
065    @Override
066    public boolean isCompressionEnabled() {
067        return compressionEnabled;
068    }
069
070    /**
071     * How long the socket will wait until a TCP connection is established (in milliseconds). Defaults to {@link #DEFAULT_CONNECT_TIMEOUT}.
072     *
073     * @return the timeout value in milliseconds.
074     */
075    public int getConnectTimeout() {
076        return connectTimeout;
077    }
078
079    public static Builder builder() {
080        return new Builder();
081    }
082
083    /**
084     * A configuration builder for XMPP connections over TCP. Use {@link XMPPTCPConnectionConfiguration#builder()} to
085     * obtain a new instance and {@link #build} to build the configuration.
086     */
087    public static final class Builder extends ConnectionConfiguration.Builder<Builder, XMPPTCPConnectionConfiguration> {
088        private boolean compressionEnabled = false;
089        private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
090
091        private Builder() {
092        }
093
094        /**
095         * Sets if the connection is going to use stream compression. Stream compression
096         * will be requested after TLS was established (if TLS was enabled) and only if the server
097         * offered stream compression. With stream compression network traffic can be reduced
098         * up to 90%. By default compression is disabled.
099         *
100         * @param compressionEnabled if the connection is going to use stream compression.
101         * @return a reference to this object.
102         */
103        public Builder setCompressionEnabled(boolean compressionEnabled) {
104            this.compressionEnabled = compressionEnabled;
105            return this;
106        }
107
108        /**
109         * Set how long the socket will wait until a TCP connection is established (in milliseconds).
110         *
111         * @param connectTimeout the timeout value to be used in milliseconds.
112         * @return a reference to this object.
113         */
114        public Builder setConnectTimeout(int connectTimeout) {
115            this.connectTimeout = connectTimeout;
116            return this;
117        }
118
119        @Override
120        protected Builder getThis() {
121            return this;
122        }
123
124        @Override
125        public XMPPTCPConnectionConfiguration build() {
126            return new XMPPTCPConnectionConfiguration(this);
127        }
128    }
129}