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.ByteChannel;
26  import java.nio.channels.SelectionKey;
27  import java.nio.channels.SocketChannel;
28  
29  import org.apache.mina.core.RuntimeIoException;
30  import org.apache.mina.core.buffer.IoBuffer;
31  import org.apache.mina.core.file.FileRegion;
32  import org.apache.mina.core.filterchain.IoFilter;
33  import org.apache.mina.core.filterchain.IoFilterChain;
34  import org.apache.mina.core.service.DefaultTransportMetadata;
35  import org.apache.mina.core.service.IoProcessor;
36  import org.apache.mina.core.service.IoService;
37  import org.apache.mina.core.service.TransportMetadata;
38  import org.apache.mina.core.session.IoSession;
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      private Socket getSocket() {
53          return ((SocketChannel) channel).socket();
54      }
55  
56      /**
57       * 
58       * Creates a new instance of NioSocketSession.
59       *
60       * @param service the associated IoService 
61       * @param processor the associated IoProcessor
62       * @param ch the used channel
63       */
64      public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel channel) {
65          super(processor, service, channel);
66          config = new SessionConfigImpl();
67          this.config.setAll(service.getSessionConfig());
68      }
69  
70      public TransportMetadata getTransportMetadata() {
71          return METADATA;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      public SocketSessionConfig getConfig() {
78          return (SocketSessionConfig) config;
79      }
80  
81      @Override
82      SocketChannel getChannel() {
83          return (SocketChannel) channel;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public InetSocketAddress getRemoteAddress() {
90          if (channel == null) {
91              return null;
92          }
93  
94          Socket socket = getSocket();
95  
96          if (socket == null) {
97              return null;
98          }
99  
100         return (InetSocketAddress) socket.getRemoteSocketAddress();
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public InetSocketAddress getLocalAddress() {
107         if (channel == null) {
108             return null;
109         }
110 
111         Socket socket = getSocket();
112 
113         if (socket == null) {
114             return null;
115         }
116 
117         return (InetSocketAddress) socket.getLocalSocketAddress();
118     }
119 
120     protected void destroy(NioSession session) throws Exception {
121         ByteChannel ch = session.getChannel();
122         SelectionKey key = session.getSelectionKey();
123         if (key != null) {
124             key.cancel();
125         }
126         ch.close();
127     }
128 
129     @Override
130     public InetSocketAddress getServiceAddress() {
131         return (InetSocketAddress) super.getServiceAddress();
132     }
133 
134     private class SessionConfigImpl extends AbstractSocketSessionConfig {
135         public boolean isKeepAlive() {
136             try {
137                 return getSocket().getKeepAlive();
138             } catch (SocketException e) {
139                 throw new RuntimeIoException(e);
140             }
141         }
142 
143         public void setKeepAlive(boolean on) {
144             try {
145                 getSocket().setKeepAlive(on);
146             } catch (SocketException e) {
147                 throw new RuntimeIoException(e);
148             }
149         }
150 
151         public boolean isOobInline() {
152             try {
153                 return getSocket().getOOBInline();
154             } catch (SocketException e) {
155                 throw new RuntimeIoException(e);
156             }
157         }
158 
159         public void setOobInline(boolean on) {
160             try {
161                 getSocket().setOOBInline(on);
162             } catch (SocketException e) {
163                 throw new RuntimeIoException(e);
164             }
165         }
166 
167         public boolean isReuseAddress() {
168             try {
169                 return getSocket().getReuseAddress();
170             } catch (SocketException e) {
171                 throw new RuntimeIoException(e);
172             }
173         }
174 
175         public void setReuseAddress(boolean on) {
176             try {
177                 getSocket().setReuseAddress(on);
178             } catch (SocketException e) {
179                 throw new RuntimeIoException(e);
180             }
181         }
182 
183         public int getSoLinger() {
184             try {
185                 return getSocket().getSoLinger();
186             } catch (SocketException e) {
187                 throw new RuntimeIoException(e);
188             }
189         }
190 
191         public void setSoLinger(int linger) {
192             try {
193                 if (linger < 0) {
194                     getSocket().setSoLinger(false, 0);
195                 } else {
196                     getSocket().setSoLinger(true, linger);
197                 }
198             } catch (SocketException e) {
199                 throw new RuntimeIoException(e);
200             }
201         }
202 
203         public boolean isTcpNoDelay() {
204             if (!isConnected()) {
205                 return false;
206             }
207 
208             try {
209                 return getSocket().getTcpNoDelay();
210             } catch (SocketException e) {
211                 throw new RuntimeIoException(e);
212             }
213         }
214 
215         public void setTcpNoDelay(boolean on) {
216             try {
217                 getSocket().setTcpNoDelay(on);
218             } catch (SocketException e) {
219                 throw new RuntimeIoException(e);
220             }
221         }
222 
223         /**
224          * {@inheritDoc}
225          */
226         public int getTrafficClass() {
227             try {
228                 return getSocket().getTrafficClass();
229             } catch (SocketException e) {
230                 throw new RuntimeIoException(e);
231             }
232         }
233 
234         /**
235          * {@inheritDoc}
236          */
237         public void setTrafficClass(int tc) {
238             try {
239                 getSocket().setTrafficClass(tc);
240             } catch (SocketException e) {
241                 throw new RuntimeIoException(e);
242             }
243         }
244 
245         public int getSendBufferSize() {
246             try {
247                 return getSocket().getSendBufferSize();
248             } catch (SocketException e) {
249                 throw new RuntimeIoException(e);
250             }
251         }
252 
253         public void setSendBufferSize(int size) {
254             try {
255                 getSocket().setSendBufferSize(size);
256             } catch (SocketException e) {
257                 throw new RuntimeIoException(e);
258             }
259         }
260 
261         public int getReceiveBufferSize() {
262             try {
263                 return getSocket().getReceiveBufferSize();
264             } catch (SocketException e) {
265                 throw new RuntimeIoException(e);
266             }
267         }
268 
269         public void setReceiveBufferSize(int size) {
270             try {
271                 getSocket().setReceiveBufferSize(size);
272             } catch (SocketException e) {
273                 throw new RuntimeIoException(e);
274             }
275         }
276     }
277 
278     /**
279      * {@inheritDoc}
280      */
281     public final boolean isSecured() {
282         // If the session does not have a SslFilter, we can return false
283         IoFilterChain chain = getFilterChain();
284 
285         IoFilter sslFilter = chain.get(SslFilter.class);
286 
287         if (sslFilter != null) {
288         // Get the SslHandler from the SslFilter
289             return ((SslFilter)sslFilter).isSslStarted(this);
290         } else {
291             return false;
292         }
293     }
294 }