001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.transport.socket; 021 022import org.apache.mina.core.service.IoService; 023 024/** 025 * A default implementation of {@link SocketSessionConfig}. 026 * 027 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 028 */ 029public class DefaultSocketSessionConfig extends AbstractSocketSessionConfig { 030 private static final boolean DEFAULT_REUSE_ADDRESS = false; 031 032 private static final int DEFAULT_TRAFFIC_CLASS = 0; 033 034 private static final boolean DEFAULT_KEEP_ALIVE = false; 035 036 private static final boolean DEFAULT_OOB_INLINE = false; 037 038 private static final int DEFAULT_SO_LINGER = -1; 039 040 private static final boolean DEFAULT_TCP_NO_DELAY = false; 041 042 protected IoService parent; 043 044 private boolean defaultReuseAddress; 045 046 private boolean reuseAddress; 047 048 /* The SO_RCVBUF parameter. Set to -1 (ie, will default to OS default) */ 049 private int receiveBufferSize = -1; 050 051 /* The SO_SNDBUF parameter. Set to -1 (ie, will default to OS default) */ 052 private int sendBufferSize = -1; 053 054 private int trafficClass = DEFAULT_TRAFFIC_CLASS; 055 056 private boolean keepAlive = DEFAULT_KEEP_ALIVE; 057 058 private boolean oobInline = DEFAULT_OOB_INLINE; 059 060 private int soLinger = DEFAULT_SO_LINGER; 061 062 private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY; 063 064 /** 065 * Creates a new instance. 066 */ 067 public DefaultSocketSessionConfig() { 068 // Do nothing 069 } 070 071 public void init(IoService parent) { 072 this.parent = parent; 073 074 if (parent instanceof SocketAcceptor) { 075 defaultReuseAddress = true; 076 } else { 077 defaultReuseAddress = DEFAULT_REUSE_ADDRESS; 078 } 079 080 reuseAddress = defaultReuseAddress; 081 } 082 083 public boolean isReuseAddress() { 084 return reuseAddress; 085 } 086 087 public void setReuseAddress(boolean reuseAddress) { 088 this.reuseAddress = reuseAddress; 089 } 090 091 public int getReceiveBufferSize() { 092 return receiveBufferSize; 093 } 094 095 public void setReceiveBufferSize(int receiveBufferSize) { 096 this.receiveBufferSize = receiveBufferSize; 097 } 098 099 public int getSendBufferSize() { 100 return sendBufferSize; 101 } 102 103 public void setSendBufferSize(int sendBufferSize) { 104 this.sendBufferSize = sendBufferSize; 105 } 106 107 public int getTrafficClass() { 108 return trafficClass; 109 } 110 111 public void setTrafficClass(int trafficClass) { 112 this.trafficClass = trafficClass; 113 } 114 115 public boolean isKeepAlive() { 116 return keepAlive; 117 } 118 119 public void setKeepAlive(boolean keepAlive) { 120 this.keepAlive = keepAlive; 121 } 122 123 public boolean isOobInline() { 124 return oobInline; 125 } 126 127 public void setOobInline(boolean oobInline) { 128 this.oobInline = oobInline; 129 } 130 131 public int getSoLinger() { 132 return soLinger; 133 } 134 135 public void setSoLinger(int soLinger) { 136 this.soLinger = soLinger; 137 } 138 139 public boolean isTcpNoDelay() { 140 return tcpNoDelay; 141 } 142 143 public void setTcpNoDelay(boolean tcpNoDelay) { 144 this.tcpNoDelay = tcpNoDelay; 145 } 146 147 @Override 148 protected boolean isKeepAliveChanged() { 149 return keepAlive != DEFAULT_KEEP_ALIVE; 150 } 151 152 @Override 153 protected boolean isOobInlineChanged() { 154 return oobInline != DEFAULT_OOB_INLINE; 155 } 156 157 @Override 158 protected boolean isReceiveBufferSizeChanged() { 159 return receiveBufferSize != -1; 160 } 161 162 @Override 163 protected boolean isReuseAddressChanged() { 164 return reuseAddress != defaultReuseAddress; 165 } 166 167 @Override 168 protected boolean isSendBufferSizeChanged() { 169 return sendBufferSize != -1; 170 } 171 172 @Override 173 protected boolean isSoLingerChanged() { 174 return soLinger != DEFAULT_SO_LINGER; 175 } 176 177 @Override 178 protected boolean isTcpNoDelayChanged() { 179 return tcpNoDelay != DEFAULT_TCP_NO_DELAY; 180 } 181 182 @Override 183 protected boolean isTrafficClassChanged() { 184 return trafficClass != DEFAULT_TRAFFIC_CLASS; 185 } 186}