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.SocketException;
24  import java.nio.channels.SelectionKey;
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.DefaultIoFilterChain;
31  import org.apache.mina.core.filterchain.IoFilterChain;
32  import org.apache.mina.core.service.DefaultTransportMetadata;
33  import org.apache.mina.core.service.IoHandler;
34  import org.apache.mina.core.service.IoProcessor;
35  import org.apache.mina.core.service.IoService;
36  import org.apache.mina.core.service.TransportMetadata;
37  import org.apache.mina.core.session.IoSession;
38  import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
39  import org.apache.mina.transport.socket.DefaultSocketSessionConfig;
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: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
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     public InetSocketAddress getRemoteAddress() {
129         return (InetSocketAddress) ch.socket().getRemoteSocketAddress();
130     }
131 
132     public InetSocketAddress getLocalAddress() {
133         return (InetSocketAddress) ch.socket().getLocalSocketAddress();
134     }
135 
136     @Override
137     public InetSocketAddress getServiceAddress() {
138         return (InetSocketAddress) super.getServiceAddress();
139     }
140 
141     private class SessionConfigImpl extends AbstractSocketSessionConfig {
142         public boolean isKeepAlive() {
143             try {
144                 return ch.socket().getKeepAlive();
145             } catch (SocketException e) {
146                 throw new RuntimeIoException(e);
147             }
148         }
149 
150         public void setKeepAlive(boolean on) {
151             try {
152                 ch.socket().setKeepAlive(on);
153             } catch (SocketException e) {
154                 throw new RuntimeIoException(e);
155             }
156         }
157 
158         public boolean isOobInline() {
159             try {
160                 return ch.socket().getOOBInline();
161             } catch (SocketException e) {
162                 throw new RuntimeIoException(e);
163             }
164         }
165 
166         public void setOobInline(boolean on) {
167             try {
168                 ch.socket().setOOBInline(on);
169             } catch (SocketException e) {
170                 throw new RuntimeIoException(e);
171             }
172         }
173 
174         public boolean isReuseAddress() {
175             try {
176                 return ch.socket().getReuseAddress();
177             } catch (SocketException e) {
178                 throw new RuntimeIoException(e);
179             }
180         }
181 
182         public void setReuseAddress(boolean on) {
183             try {
184                 ch.socket().setReuseAddress(on);
185             } catch (SocketException e) {
186                 throw new RuntimeIoException(e);
187             }
188         }
189 
190         public int getSoLinger() {
191             try {
192                 return ch.socket().getSoLinger();
193             } catch (SocketException e) {
194                 throw new RuntimeIoException(e);
195             }
196         }
197 
198         public void setSoLinger(int linger) {
199             try {
200                 if (linger < 0) {
201                     ch.socket().setSoLinger(false, 0);
202                 } else {
203                     ch.socket().setSoLinger(true, linger);
204                 }
205             } catch (SocketException e) {
206                 throw new RuntimeIoException(e);
207             }
208         }
209 
210         public boolean isTcpNoDelay() {
211             if (!isConnected()) {
212                 return false;
213             }
214 
215             try {
216                 return ch.socket().getTcpNoDelay();
217             } catch (SocketException e) {
218                 throw new RuntimeIoException(e);
219             }
220         }
221 
222         public void setTcpNoDelay(boolean on) {
223             try {
224                 ch.socket().setTcpNoDelay(on);
225             } catch (SocketException e) {
226                 throw new RuntimeIoException(e);
227             }
228         }
229 
230         public int getTrafficClass() {
231             if (DefaultSocketSessionConfig.isGetTrafficClassAvailable()) {
232                 try {
233                     return ch.socket().getTrafficClass();
234                 } catch (SocketException e) {
235                     // Throw an exception only when setTrafficClass is also available.
236                     if (DefaultSocketSessionConfig.isSetTrafficClassAvailable()) {
237                         throw new RuntimeIoException(e);
238                     }
239                 }
240             }
241 
242             return 0;
243         }
244 
245         public void setTrafficClass(int tc) {
246             if (DefaultSocketSessionConfig.isSetTrafficClassAvailable()) {
247                 try {
248                     ch.socket().setTrafficClass(tc);
249                 } catch (SocketException e) {
250                     throw new RuntimeIoException(e);
251                 }
252             }
253         }
254 
255         public int getSendBufferSize() {
256             try {
257                 return ch.socket().getSendBufferSize();
258             } catch (SocketException e) {
259                 throw new RuntimeIoException(e);
260             }
261         }
262 
263         public void setSendBufferSize(int size) {
264             if (DefaultSocketSessionConfig.isSetSendBufferSizeAvailable()) {
265                 try {
266                     ch.socket().setSendBufferSize(size);
267                 } catch (SocketException e) {
268                     throw new RuntimeIoException(e);
269                 }
270             }
271         }
272 
273         public int getReceiveBufferSize() {
274             try {
275                 return ch.socket().getReceiveBufferSize();
276             } catch (SocketException e) {
277                 throw new RuntimeIoException(e);
278             }
279         }
280 
281         public void setReceiveBufferSize(int size) {
282             if (DefaultSocketSessionConfig.isSetReceiveBufferSizeAvailable()) {
283                 try {
284                     ch.socket().setReceiveBufferSize(size);
285                 } catch (SocketException e) {
286                     throw new RuntimeIoException(e);
287                 }
288             }
289         }
290     }
291 }