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