XMPPTCPConnectionConfiguration.java

  1. /**
  2.  *
  3.  * Copyright 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.tcp;

  18. import org.jivesoftware.smack.ConnectionConfiguration;

  19. /**
  20.  * A connection configuration for XMPP connections over TCP (the common case).
  21.  * <p>
  22.  * You can get an instance of the configuration builder with {@link #builder()} and build the final immutable connection
  23.  * configuration with {@link Builder#build()}.
  24.  * </p>
  25.  * <pre>
  26.  * {@code
  27.  * XMPPTCPConnectionConfiguration conf = XMPPConnectionConfiguration.builder()
  28.  *     .setServiceName("example.org").setUsernameAndPassword("user", "password")
  29.  *     .setCompressionEnabled(false).build();
  30.  * XMPPTCPConnection connection = new XMPPTCPConnection(conf);
  31.  * }
  32.  * </pre>
  33.  */
  34. public class XMPPTCPConnectionConfiguration extends ConnectionConfiguration {

  35.     /**
  36.      * The default connect timeout in milliseconds. Preinitialized with 30000 (30 seconds). If this value is changed,
  37.      * new Builder instances will use the new value as default.
  38.      */
  39.     public static int DEFAULT_CONNECT_TIMEOUT = 30000;

  40.     private final boolean compressionEnabled;

  41.     /**
  42.      * How long the socket will wait until a TCP connection is established (in milliseconds).
  43.      */
  44.     private final int connectTimeout;

  45.     private XMPPTCPConnectionConfiguration(Builder builder) {
  46.         super(builder);
  47.         compressionEnabled = builder.compressionEnabled;
  48.         connectTimeout = builder.connectTimeout;
  49.     }

  50.     /**
  51.      * Returns true if the connection is going to use stream compression. Stream compression
  52.      * will be requested after TLS was established (if TLS was enabled) and only if the server
  53.      * offered stream compression. With stream compression network traffic can be reduced
  54.      * up to 90%. By default compression is disabled.
  55.      *
  56.      * @return true if the connection is going to use stream compression.
  57.      */
  58.     @Override
  59.     public boolean isCompressionEnabled() {
  60.         return compressionEnabled;
  61.     }

  62.     /**
  63.      * How long the socket will wait until a TCP connection is established (in milliseconds). Defaults to {@link #DEFAULT_CONNECT_TIMEOUT}.
  64.      *
  65.      * @return the timeout value in milliseconds.
  66.      */
  67.     public int getConnectTimeout() {
  68.         return connectTimeout;
  69.     }

  70.     public static Builder builder() {
  71.         return new Builder();
  72.     }

  73.     /**
  74.      * A configuration builder for XMPP connections over TCP. Use {@link XMPPTCPConnectionConfiguration#builder()} to
  75.      * obtain a new instance and {@link #build} to build the configuration.
  76.      */
  77.     public static class Builder extends ConnectionConfiguration.Builder<Builder, XMPPTCPConnectionConfiguration> {
  78.         private boolean compressionEnabled = false;
  79.         private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;

  80.         private Builder() {
  81.         }

  82.         /**
  83.          * Sets if the connection is going to use stream compression. Stream compression
  84.          * will be requested after TLS was established (if TLS was enabled) and only if the server
  85.          * offered stream compression. With stream compression network traffic can be reduced
  86.          * up to 90%. By default compression is disabled.
  87.          *
  88.          * @param compressionEnabled if the connection is going to use stream compression.
  89.          * @return a reference to this object.
  90.          */
  91.         public Builder setCompressionEnabled(boolean compressionEnabled) {
  92.             this.compressionEnabled = compressionEnabled;
  93.             return this;
  94.         }

  95.         /**
  96.          * Set how long the socket will wait until a TCP connection is established (in milliseconds).
  97.          *
  98.          * @param connectTimeout the timeout value to be used in milliseconds.
  99.          * @return a reference to this object.
  100.          */
  101.         public Builder setConnectTimeout(int connectTimeout) {
  102.             this.connectTimeout = connectTimeout;
  103.             return this;
  104.         }

  105.         @Override
  106.         protected Builder getThis() {
  107.             return this;
  108.         }

  109.         @Override
  110.         public XMPPTCPConnectionConfiguration build() {
  111.             return new XMPPTCPConnectionConfiguration(this);
  112.         }
  113.     }
  114. }