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