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.nio;
21  
22  import java.net.SocketAddress;
23  import java.net.SocketException;
24  import java.nio.channels.SelectionKey;
25  import java.nio.channels.SocketChannel;
26  
27  import org.apache.mina.common.IoFilterChain;
28  import org.apache.mina.common.IoHandler;
29  import org.apache.mina.common.IoService;
30  import org.apache.mina.common.IoServiceConfig;
31  import org.apache.mina.common.IoSession;
32  import org.apache.mina.common.IoSessionConfig;
33  import org.apache.mina.common.RuntimeIOException;
34  import org.apache.mina.common.TransportType;
35  import org.apache.mina.common.IoFilter.WriteRequest;
36  import org.apache.mina.common.support.BaseIoSession;
37  import org.apache.mina.common.support.BaseIoSessionConfig;
38  import org.apache.mina.common.support.IoServiceListenerSupport;
39  import org.apache.mina.util.Queue;
40  
41  /**
42   * An {@link IoSession} for socket transport (TCP/IP).
43   *
44   * @author The Apache Directory Project (mina-dev@directory.apache.org)
45   * @version $Rev: 575603 $, $Date: 2007-09-14 19:04:45 +0900 (Fri, 14 Sep 2007) $
46   */
47  class SocketSessionImpl extends BaseIoSession {
48      private final IoService manager;
49  
50      private final IoServiceConfig serviceConfig;
51  
52      private final SocketSessionConfig config = new SessionConfigImpl();
53  
54      private final SocketIoProcessor ioProcessor;
55  
56      private final SocketFilterChain filterChain;
57  
58      private final SocketChannel ch;
59  
60      private final Queue writeRequestQueue;
61  
62      private final IoHandler handler;
63  
64      private final SocketAddress remoteAddress;
65  
66      private final SocketAddress localAddress;
67  
68      private final SocketAddress serviceAddress;
69  
70      private final IoServiceListenerSupport serviceListeners;
71  
72      private SelectionKey key;
73  
74      private int readBufferSize = 1024;
75  
76      /**
77       * Creates a new instance.
78       */
79      SocketSessionImpl(IoService manager, SocketIoProcessor ioProcessor,
80              IoServiceListenerSupport listeners, IoServiceConfig serviceConfig,
81              SocketChannel ch, IoHandler defaultHandler,
82              SocketAddress serviceAddress) {
83          this.manager = manager;
84          this.serviceListeners = listeners;
85          this.ioProcessor = ioProcessor;
86          this.filterChain = new SocketFilterChain(this);
87          this.ch = ch;
88          this.writeRequestQueue = new Queue();
89          this.handler = defaultHandler;
90          this.remoteAddress = ch.socket().getRemoteSocketAddress();
91          this.localAddress = ch.socket().getLocalSocketAddress();
92          this.serviceAddress = serviceAddress;
93          this.serviceConfig = serviceConfig;
94  
95          // Apply the initial session settings
96          IoSessionConfig sessionConfig = serviceConfig.getSessionConfig();
97          if (sessionConfig instanceof SocketSessionConfig) {
98              SocketSessionConfig cfg = (SocketSessionConfig) sessionConfig;
99              this.config.setKeepAlive(cfg.isKeepAlive());
100             this.config.setOobInline(cfg.isOobInline());
101             this.config.setReceiveBufferSize(cfg.getReceiveBufferSize());
102             this.config.setReuseAddress(cfg.isReuseAddress());
103             this.config.setSendBufferSize(cfg.getSendBufferSize());
104             this.config.setSoLinger(cfg.getSoLinger());
105             this.config.setTcpNoDelay(cfg.isTcpNoDelay());
106 
107             if (this.config.getTrafficClass() != cfg.getTrafficClass()) {
108                 this.config.setTrafficClass(cfg.getTrafficClass());
109             }
110         }
111     }
112 
113     public IoService getService() {
114         return manager;
115     }
116 
117     public IoServiceConfig getServiceConfig() {
118         return serviceConfig;
119     }
120 
121     public IoSessionConfig getConfig() {
122         return config;
123     }
124 
125     SocketIoProcessor getIoProcessor() {
126         return ioProcessor;
127     }
128 
129     public IoFilterChain getFilterChain() {
130         return filterChain;
131     }
132 
133     SocketChannel getChannel() {
134         return ch;
135     }
136 
137     IoServiceListenerSupport getServiceListeners() {
138         return serviceListeners;
139     }
140 
141     SelectionKey getSelectionKey() {
142         return key;
143     }
144 
145     void setSelectionKey(SelectionKey key) {
146         this.key = key;
147     }
148 
149     public IoHandler getHandler() {
150         return handler;
151     }
152 
153     protected void close0() {
154         filterChain.fireFilterClose(this);
155     }
156 
157     Queue getWriteRequestQueue() {
158         return writeRequestQueue;
159     }
160 
161     protected void write0(WriteRequest writeRequest) {
162         filterChain.fireFilterWrite(this, writeRequest);
163     }
164 
165     public TransportType getTransportType() {
166         return TransportType.SOCKET;
167     }
168 
169     public SocketAddress getRemoteAddress() {
170         return remoteAddress;
171     }
172 
173     public SocketAddress getLocalAddress() {
174         return localAddress;
175     }
176 
177     public SocketAddress getServiceAddress() {
178         return serviceAddress;
179     }
180 
181     protected void updateTrafficMask() {
182         this.ioProcessor.updateTrafficMask(this);
183     }
184 
185     int getReadBufferSize() {
186         return readBufferSize;
187     }
188     
189     void setReadBufferSize(int readBufferSize) {
190         this.readBufferSize = readBufferSize;
191     }
192 
193     private class SessionConfigImpl extends BaseIoSessionConfig implements
194             SocketSessionConfig {
195         public boolean isKeepAlive() {
196             try {
197                 return ch.socket().getKeepAlive();
198             } catch (SocketException e) {
199                 throw new RuntimeIOException(e);
200             }
201         }
202 
203         public void setKeepAlive(boolean on) {
204             try {
205                 ch.socket().setKeepAlive(on);
206             } catch (SocketException e) {
207                 throw new RuntimeIOException(e);
208             }
209         }
210 
211         public boolean isOobInline() {
212             try {
213                 return ch.socket().getOOBInline();
214             } catch (SocketException e) {
215                 throw new RuntimeIOException(e);
216             }
217         }
218 
219         public void setOobInline(boolean on) {
220             try {
221                 ch.socket().setOOBInline(on);
222             } catch (SocketException e) {
223                 throw new RuntimeIOException(e);
224             }
225         }
226 
227         public boolean isReuseAddress() {
228             try {
229                 return ch.socket().getReuseAddress();
230             } catch (SocketException e) {
231                 throw new RuntimeIOException(e);
232             }
233         }
234 
235         public void setReuseAddress(boolean on) {
236             try {
237                 ch.socket().setReuseAddress(on);
238             } catch (SocketException e) {
239                 throw new RuntimeIOException(e);
240             }
241         }
242 
243         public int getSoLinger() {
244             try {
245                 return ch.socket().getSoLinger();
246             } catch (SocketException e) {
247                 throw new RuntimeIOException(e);
248             }
249         }
250 
251         public void setSoLinger(int linger) {
252             try {
253                 if (linger < 0) {
254                     ch.socket().setSoLinger(false, 0);
255                 } else {
256                     ch.socket().setSoLinger(true, linger);
257                 }
258             } catch (SocketException e) {
259                 throw new RuntimeIOException(e);
260             }
261         }
262 
263         public boolean isTcpNoDelay() {
264             try {
265                 return ch.socket().getTcpNoDelay();
266             } catch (SocketException e) {
267                 throw new RuntimeIOException(e);
268             }
269         }
270 
271         public void setTcpNoDelay(boolean on) {
272             try {
273                 ch.socket().setTcpNoDelay(on);
274             } catch (SocketException e) {
275                 throw new RuntimeIOException(e);
276             }
277         }
278 
279         public int getTrafficClass() {
280             if (SocketSessionConfigImpl.isGetTrafficClassAvailable()) {
281                 try {
282                     return ch.socket().getTrafficClass();
283                 } catch (SocketException e) {
284                     // Throw an exception only when setTrafficClass is also available.
285                     if (SocketSessionConfigImpl.isSetTrafficClassAvailable()) {
286                         throw new RuntimeIOException(e);
287                     }
288                 }
289             }
290 
291             return 0;
292         }
293 
294         public void setTrafficClass(int tc) {
295             if (SocketSessionConfigImpl.isSetTrafficClassAvailable()) {
296                 try {
297                     ch.socket().setTrafficClass(tc);
298                 } catch (SocketException e) {
299                     throw new RuntimeIOException(e);
300                 }
301             }
302         }
303 
304         public int getSendBufferSize() {
305             try {
306                 return ch.socket().getSendBufferSize();
307             } catch (SocketException e) {
308                 throw new RuntimeIOException(e);
309             }
310         }
311 
312         public void setSendBufferSize(int size) {
313             if (SocketSessionConfigImpl.isSetSendBufferSizeAvailable()) {
314                 try {
315                     ch.socket().setSendBufferSize(size);
316                 } catch (SocketException e) {
317                     throw new RuntimeIOException(e);
318                 }
319             }
320         }
321 
322         public int getReceiveBufferSize() {
323             try {
324                 return ch.socket().getReceiveBufferSize();
325             } catch (SocketException e) {
326                 throw new RuntimeIOException(e);
327             }
328         }
329 
330         public void setReceiveBufferSize(int size) {
331             if (SocketSessionConfigImpl.isSetReceiveBufferSizeAvailable()) {
332                 try {
333                     ch.socket().setReceiveBufferSize(size);
334                 } catch (SocketException e) {
335                     throw new RuntimeIOException(e);
336                 }
337             }
338         }
339     }
340 }