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.InetSocketAddress;
23  import java.net.Socket;
24  import java.net.SocketException;
25  import java.nio.channels.SocketChannel;
26  
27  import org.apache.mina.core.RuntimeIoException;
28  import org.apache.mina.core.buffer.IoBuffer;
29  import org.apache.mina.core.file.FileRegion;
30  import org.apache.mina.core.filterchain.IoFilter;
31  import org.apache.mina.core.filterchain.IoFilterChain;
32  import org.apache.mina.core.service.DefaultTransportMetadata;
33  import org.apache.mina.core.service.IoProcessor;
34  import org.apache.mina.core.service.IoService;
35  import org.apache.mina.core.service.TransportMetadata;
36  import org.apache.mina.core.session.IoSession;
37  import org.apache.mina.filter.ssl.SslFilter;
38  import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
39  import org.apache.mina.transport.socket.SocketSessionConfig;
40  
41  /**
42   * An {@link IoSession} for socket transport (TCP/IP).
43   *
44   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
45   */
46  class NioSocketSession extends NioSession {
47      static final TransportMetadata METADATA = new DefaultTransportMetadata("nio", "socket", false, true,
48              InetSocketAddress.class, SocketSessionConfig.class, IoBuffer.class, FileRegion.class);
49  
50      /**
51       * 
52       * Creates a new instance of NioSocketSession.
53       *
54       * @param service the associated IoService 
55       * @param processor the associated IoProcessor
56       * @param channel the used channel
57       */
58      public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel channel) {
59          super(processor, service, channel);
60          config = new SessionConfigImpl();
61          config.setAll(service.getSessionConfig());
62      }
63  
64      private Socket getSocket() {
65          return ((SocketChannel) channel).socket();
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public TransportMetadata getTransportMetadata() {
73          return METADATA;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public SocketSessionConfig getConfig() {
81          return (SocketSessionConfig) config;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      SocketChannel getChannel() {
89          return (SocketChannel) channel;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public InetSocketAddress getRemoteAddress() {
97          if (channel == null) {
98              return null;
99          }
100 
101         Socket socket = getSocket();
102 
103         if (socket == null) {
104             return null;
105         }
106 
107         return (InetSocketAddress) socket.getRemoteSocketAddress();
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public InetSocketAddress getLocalAddress() {
115         if (channel == null) {
116             return null;
117         }
118 
119         Socket socket = getSocket();
120 
121         if (socket == null) {
122             return null;
123         }
124 
125         return (InetSocketAddress) socket.getLocalSocketAddress();
126     }
127 
128     @Override
129     public InetSocketAddress getServiceAddress() {
130         return (InetSocketAddress) super.getServiceAddress();
131     }
132 
133     /**
134      * A private class storing a copy of the IoService configuration when the IoSession
135      * is created. That allows the session to have its own configuration setting, over
136      * the IoService default one.
137      */
138     private class SessionConfigImpl extends AbstractSocketSessionConfig {
139         /**
140          * {@inheritDoc}
141          */
142         @Override
143         public boolean isKeepAlive() {
144             try {
145                 return getSocket().getKeepAlive();
146             } catch (SocketException e) {
147                 throw new RuntimeIoException(e);
148             }
149         }
150 
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         public void setKeepAlive(boolean on) {
156             try {
157                 getSocket().setKeepAlive(on);
158             } catch (SocketException e) {
159                 throw new RuntimeIoException(e);
160             }
161         }
162 
163         /**
164          * {@inheritDoc}
165          */
166         @Override
167         public boolean isOobInline() {
168             try {
169                 return getSocket().getOOBInline();
170             } catch (SocketException e) {
171                 throw new RuntimeIoException(e);
172             }
173         }
174 
175         /**
176          * {@inheritDoc}
177          */
178         @Override
179         public void setOobInline(boolean on) {
180             try {
181                 getSocket().setOOBInline(on);
182             } catch (SocketException e) {
183                 throw new RuntimeIoException(e);
184             }
185         }
186 
187         /**
188          * {@inheritDoc}
189          */
190         @Override
191         public boolean isReuseAddress() {
192             try {
193                 return getSocket().getReuseAddress();
194             } catch (SocketException e) {
195                 throw new RuntimeIoException(e);
196             }
197         }
198 
199         /**
200          * {@inheritDoc}
201          */
202         @Override
203         public void setReuseAddress(boolean on) {
204             try {
205                 getSocket().setReuseAddress(on);
206             } catch (SocketException e) {
207                 throw new RuntimeIoException(e);
208             }
209         }
210 
211         /**
212          * {@inheritDoc}
213          */
214         @Override
215         public int getSoLinger() {
216             try {
217                 return getSocket().getSoLinger();
218             } catch (SocketException e) {
219                 throw new RuntimeIoException(e);
220             }
221         }
222 
223         /**
224          * {@inheritDoc}
225          */
226         @Override
227         public void setSoLinger(int linger) {
228             try {
229                 if (linger < 0) {
230                     getSocket().setSoLinger(false, 0);
231                 } else {
232                     getSocket().setSoLinger(true, linger);
233                 }
234             } catch (SocketException e) {
235                 throw new RuntimeIoException(e);
236             }
237         }
238 
239         /**
240          * {@inheritDoc}
241          */
242         @Override
243         public boolean isTcpNoDelay() {
244             if (!isConnected()) {
245                 return false;
246             }
247 
248             try {
249                 return getSocket().getTcpNoDelay();
250             } catch (SocketException e) {
251                 throw new RuntimeIoException(e);
252             }
253         }
254 
255         /**
256          * {@inheritDoc}
257          */
258         @Override
259         public void setTcpNoDelay(boolean on) {
260             try {
261                 getSocket().setTcpNoDelay(on);
262             } catch (SocketException e) {
263                 throw new RuntimeIoException(e);
264             }
265         }
266 
267         /**
268          * {@inheritDoc}
269          */
270         @Override
271         public int getTrafficClass() {
272             try {
273                 return getSocket().getTrafficClass();
274             } catch (SocketException e) {
275                 throw new RuntimeIoException(e);
276             }
277         }
278 
279         /**
280          * {@inheritDoc}
281          */
282         @Override
283         public void setTrafficClass(int tc) {
284             try {
285                 getSocket().setTrafficClass(tc);
286             } catch (SocketException e) {
287                 throw new RuntimeIoException(e);
288             }
289         }
290 
291         /**
292          * {@inheritDoc}
293          */
294         @Override
295         public int getSendBufferSize() {
296             try {
297                 return getSocket().getSendBufferSize();
298             } catch (SocketException e) {
299                 throw new RuntimeIoException(e);
300             }
301         }
302 
303         /**
304          * {@inheritDoc}
305          */
306         @Override
307         public void setSendBufferSize(int size) {
308             try {
309                 getSocket().setSendBufferSize(size);
310             } catch (SocketException e) {
311                 throw new RuntimeIoException(e);
312             }
313         }
314 
315         /**
316          * {@inheritDoc}
317          */
318         @Override
319         public int getReceiveBufferSize() {
320             try {
321                 return getSocket().getReceiveBufferSize();
322             } catch (SocketException e) {
323                 throw new RuntimeIoException(e);
324             }
325         }
326 
327         /**
328          * {@inheritDoc}
329          */
330         @Override
331         public void setReceiveBufferSize(int size) {
332             try {
333                 getSocket().setReceiveBufferSize(size);
334             } catch (SocketException e) {
335                 throw new RuntimeIoException(e);
336             }
337         }
338     }
339 
340     /**
341      * {@inheritDoc}
342      */
343     @Override
344     public final boolean isSecured() {
345         // If the session does not have a SslFilter, we can return false
346         IoFilterChain chain = getFilterChain();
347 
348         IoFilter sslFilter = chain.get(SslFilter.class);
349 
350         if (sslFilter != null) {
351         // Get the SslHandler from the SslFilter
352             return ((SslFilter)sslFilter).isSecured(this);
353         } else {
354             return false;
355         }
356     }
357 }