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