View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.transport.socket;
21  
22  import org.apache.mina.core.service.IoService;
23  
24  /**
25   * A default implementation of {@link SocketSessionConfig}.
26   *
27   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
28   */
29  public class DefaultSocketSessionConfig extends AbstractSocketSessionConfig {
30      private static boolean DEFAULT_REUSE_ADDRESS = false;
31  
32      private static int DEFAULT_TRAFFIC_CLASS = 0;
33  
34      private static boolean DEFAULT_KEEP_ALIVE = false;
35  
36      private static boolean DEFAULT_OOB_INLINE = false;
37  
38      private static int DEFAULT_SO_LINGER = -1;
39  
40      private static boolean DEFAULT_TCP_NO_DELAY = false;
41  
42      protected IoService parent;
43  
44      private boolean defaultReuseAddress;
45  
46      private boolean reuseAddress;
47  
48      /* The SO_RCVBUF parameter. Set to -1 (ie, will default to OS default) */
49      private int receiveBufferSize = -1;
50  
51      /* The SO_SNDBUF parameter. Set to -1 (ie, will default to OS default) */
52      private int sendBufferSize = -1;
53  
54      private int trafficClass = DEFAULT_TRAFFIC_CLASS;
55  
56      private boolean keepAlive = DEFAULT_KEEP_ALIVE;
57  
58      private boolean oobInline = DEFAULT_OOB_INLINE;
59  
60      private int soLinger = DEFAULT_SO_LINGER;
61  
62      private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
63  
64      /**
65       * Creates a new instance.
66       */
67      public DefaultSocketSessionConfig() {
68          // Do nothing
69      }
70  
71      public void init(IoService parent) {
72          this.parent = parent;
73  
74          if (parent instanceof SocketAcceptor) {
75              defaultReuseAddress = true;
76          } else {
77              defaultReuseAddress = DEFAULT_REUSE_ADDRESS;
78          }
79  
80          reuseAddress = defaultReuseAddress;
81      }
82  
83      public boolean isReuseAddress() {
84          return reuseAddress;
85      }
86  
87      public void setReuseAddress(boolean reuseAddress) {
88          this.reuseAddress = reuseAddress;
89      }
90  
91      public int getReceiveBufferSize() {
92          return receiveBufferSize;
93      }
94  
95      public void setReceiveBufferSize(int receiveBufferSize) {
96          this.receiveBufferSize = receiveBufferSize;
97      }
98  
99      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 }