/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.transport.amqp.client.transport;

/**
 * Encapsulates all the TCP Transport options in one configuration object.
 */
public class NettyTransportOptions implements Cloneable {

   public static final int DEFAULT_SEND_BUFFER_SIZE = 64 * 1024;
   public static final int DEFAULT_RECEIVE_BUFFER_SIZE = DEFAULT_SEND_BUFFER_SIZE;
   public static final int DEFAULT_TRAFFIC_CLASS = 0;
   public static final boolean DEFAULT_TCP_NO_DELAY = true;
   public static final boolean DEFAULT_TCP_KEEP_ALIVE = false;
   public static final int DEFAULT_SO_LINGER = Integer.MIN_VALUE;
   public static final int DEFAULT_SO_TIMEOUT = -1;
   public static final int DEFAULT_CONNECT_TIMEOUT = 60000;
   public static final int DEFAULT_TCP_PORT = 5672;

   public static final NettyTransportOptions INSTANCE = new NettyTransportOptions();

   private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
   private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
   private int trafficClass = DEFAULT_TRAFFIC_CLASS;
   private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
   private int soTimeout = DEFAULT_SO_TIMEOUT;
   private int soLinger = DEFAULT_SO_LINGER;
   private boolean tcpKeepAlive = DEFAULT_TCP_KEEP_ALIVE;
   private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
   private int defaultTcpPort = DEFAULT_TCP_PORT;

   /**
    * @return the currently set send buffer size in bytes.
    */
   public int getSendBufferSize() {
      return sendBufferSize;
   }

   /**
    * Sets the send buffer size in bytes, the value must be greater than zero
    * or an {@link IllegalArgumentException} will be thrown.
    *
    * @param sendBufferSize the new send buffer size for the TCP Transport.
    * @throws IllegalArgumentException if the value given is not in the valid range.
    */
   public void setSendBufferSize(int sendBufferSize) {
      if (sendBufferSize <= 0) {
         throw new IllegalArgumentException("The send buffer size must be > 0");
      }

      this.sendBufferSize = sendBufferSize;
   }

   /**
    * @return the currently configured receive buffer size in bytes.
    */
   public int getReceiveBufferSize() {
      return receiveBufferSize;
   }

   /**
    * Sets the receive buffer size in bytes, the value must be greater than zero
    * or an {@link IllegalArgumentException} will be thrown.
    *
    * @param receiveBufferSize the new receive buffer size for the TCP Transport.
    * @throws IllegalArgumentException if the value given is not in the valid range.
    */
   public void setReceiveBufferSize(int receiveBufferSize) {
      if (receiveBufferSize <= 0) {
         throw new IllegalArgumentException("The send buffer size must be > 0");
      }

      this.receiveBufferSize = receiveBufferSize;
   }

   /**
    * @return the currently configured traffic class value.
    */
   public int getTrafficClass() {
      return trafficClass;
   }

   /**
    * Sets the traffic class value used by the TCP connection, valid
    * range is between 0 and 255.
    *
    * @param trafficClass the new traffic class value.
    * @throws IllegalArgumentException if the value given is not in the valid range.
    */
   public void setTrafficClass(int trafficClass) {
      if (trafficClass < 0 || trafficClass > 255) {
         throw new IllegalArgumentException("Traffic class must be in the range [0..255]");
      }

      this.trafficClass = trafficClass;
   }

   public int getSoTimeout() {
      return soTimeout;
   }

   public void setSoTimeout(int soTimeout) {
      this.soTimeout = soTimeout;
   }

   public boolean isTcpNoDelay() {
      return tcpNoDelay;
   }

   public void setTcpNoDelay(boolean tcpNoDelay) {
      this.tcpNoDelay = tcpNoDelay;
   }

   public int getSoLinger() {
      return soLinger;
   }

   public void setSoLinger(int soLinger) {
      this.soLinger = soLinger;
   }

   public boolean isTcpKeepAlive() {
      return tcpKeepAlive;
   }

   public void setTcpKeepAlive(boolean keepAlive) {
      this.tcpKeepAlive = keepAlive;
   }

   public int getConnectTimeout() {
      return connectTimeout;
   }

   public void setConnectTimeout(int connectTimeout) {
      this.connectTimeout = connectTimeout;
   }

   public int getDefaultTcpPort() {
      return defaultTcpPort;
   }

   public void setDefaultTcpPort(int defaultTcpPort) {
      this.defaultTcpPort = defaultTcpPort;
   }

   @Override
   public NettyTransportOptions clone() {
      return copyOptions(new NettyTransportOptions());
   }

   protected NettyTransportOptions copyOptions(NettyTransportOptions copy) {
      copy.setConnectTimeout(getConnectTimeout());
      copy.setReceiveBufferSize(getReceiveBufferSize());
      copy.setSendBufferSize(getSendBufferSize());
      copy.setSoLinger(getSoLinger());
      copy.setSoTimeout(getSoTimeout());
      copy.setTcpKeepAlive(isTcpKeepAlive());
      copy.setTcpNoDelay(isTcpNoDelay());
      copy.setTrafficClass(getTrafficClass());

      return copy;
   }
}
