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