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