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      private static int DEFAULT_RECEIVE_BUFFER_SIZE = 1024;
32      private static int DEFAULT_SEND_BUFFER_SIZE = 1024;
33      private static int DEFAULT_TRAFFIC_CLASS = 0;
34      private static boolean DEFAULT_KEEP_ALIVE = false;
35      private static boolean DEFAULT_OOB_INLINE = false;
36      private static int DEFAULT_SO_LINGER = -1;
37      private static boolean DEFAULT_TCP_NO_DELAY = false;
38  
39      private IoService parent;
40      private boolean defaultReuseAddress;
41      private int defaultReceiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
42  
43      private boolean reuseAddress;
44      private int receiveBufferSize = defaultReceiveBufferSize;
45      private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
46      private int trafficClass = DEFAULT_TRAFFIC_CLASS;
47      private boolean keepAlive = DEFAULT_KEEP_ALIVE;
48      private boolean oobInline = DEFAULT_OOB_INLINE;
49      private int soLinger = DEFAULT_SO_LINGER;
50      private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
51  
52      /**
53       * Creates a new instance.
54       */
55      public DefaultSocketSessionConfig() {
56          // Do nothing
57      }
58  
59      public void init(IoService parent) {
60          this.parent = parent;
61          if (parent instanceof SocketAcceptor) {
62              defaultReuseAddress = true;
63          } else {
64              defaultReuseAddress = DEFAULT_REUSE_ADDRESS;
65          }
66          reuseAddress = defaultReuseAddress;
67      }
68  
69      public boolean isReuseAddress() {
70          return reuseAddress;
71      }
72  
73      public void setReuseAddress(boolean reuseAddress) {
74          this.reuseAddress = reuseAddress;
75      }
76  
77      public int getReceiveBufferSize() {
78          return receiveBufferSize;
79      }
80  
81      public void setReceiveBufferSize(int receiveBufferSize) {
82          this.receiveBufferSize = receiveBufferSize;
83  
84          // The acceptor configures the SO_RCVBUF value of the
85          // server socket when it is activated.  Consequently,
86          // a newly accepted session doesn't need to update its
87          // SO_RCVBUF parameter.  Therefore, we need to update
88          // the default receive buffer size if the acceptor is
89          // not bound yet to avoid a unnecessary system call
90          // when the acceptor is activated and new sessions are
91          // created.
92          if (!parent.isActive() && parent instanceof SocketAcceptor) {
93              defaultReceiveBufferSize = receiveBufferSize;
94          }
95      }
96  
97      public int getSendBufferSize() {
98          return sendBufferSize;
99      }
100 
101     public void setSendBufferSize(int sendBufferSize) {
102         this.sendBufferSize = sendBufferSize;
103     }
104 
105     public int getTrafficClass() {
106         return trafficClass;
107     }
108 
109     public void setTrafficClass(int trafficClass) {
110         this.trafficClass = trafficClass;
111     }
112 
113     public boolean isKeepAlive() {
114         return keepAlive;
115     }
116 
117     public void setKeepAlive(boolean keepAlive) {
118         this.keepAlive = keepAlive;
119     }
120 
121     public boolean isOobInline() {
122         return oobInline;
123     }
124 
125     public void setOobInline(boolean oobInline) {
126         this.oobInline = oobInline;
127     }
128 
129     public int getSoLinger() {
130         return soLinger;
131     }
132 
133     public void setSoLinger(int soLinger) {
134         this.soLinger = soLinger;
135     }
136 
137     public boolean isTcpNoDelay() {
138         return tcpNoDelay;
139     }
140 
141     public void setTcpNoDelay(boolean tcpNoDelay) {
142         this.tcpNoDelay = tcpNoDelay;
143     }
144 
145     @Override
146     protected boolean isKeepAliveChanged() {
147         return keepAlive != DEFAULT_KEEP_ALIVE;
148     }
149 
150     @Override
151     protected boolean isOobInlineChanged() {
152         return oobInline != DEFAULT_OOB_INLINE;
153     }
154 
155     @Override
156     protected boolean isReceiveBufferSizeChanged() {
157         return receiveBufferSize != defaultReceiveBufferSize;
158     }
159 
160     @Override
161     protected boolean isReuseAddressChanged() {
162         return reuseAddress != defaultReuseAddress;
163     }
164 
165     @Override
166     protected boolean isSendBufferSizeChanged() {
167         return sendBufferSize != DEFAULT_SEND_BUFFER_SIZE;
168     }
169 
170     @Override
171     protected boolean isSoLingerChanged() {
172         return soLinger != DEFAULT_SO_LINGER;
173     }
174 
175     @Override
176     protected boolean isTcpNoDelayChanged() {
177         return tcpNoDelay != DEFAULT_TCP_NO_DELAY;
178     }
179 
180     @Override
181     protected boolean isTrafficClassChanged() {
182         return trafficClass != DEFAULT_TRAFFIC_CLASS;
183     }
184 }