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.apr;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.RuntimeIoException;
25  import org.apache.mina.core.buffer.IoBuffer;
26  import org.apache.mina.core.service.DefaultTransportMetadata;
27  import org.apache.mina.core.service.IoProcessor;
28  import org.apache.mina.core.service.IoService;
29  import org.apache.mina.core.service.TransportMetadata;
30  import org.apache.mina.core.session.IoSession;
31  import org.apache.mina.core.session.IoSessionConfig;
32  import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
33  import org.apache.mina.transport.socket.SocketSessionConfig;
34  import org.apache.tomcat.jni.Socket;
35  
36  /**
37   * An {@link IoSession} for APR TCP socket based session.
38   * It's implementing the usual common features for {@SocketSession}. 
39   *
40   * @author The Apache MINA Project (dev@mina.apache.org)
41   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
42   */
43  class AprSocketSession extends AprSession {
44  
45  	
46      static final TransportMetadata METADATA =
47          new DefaultTransportMetadata(
48                  "apr", "socket", false, true,
49                  InetSocketAddress.class,
50                  SocketSessionConfig.class,
51                  IoBuffer.class);
52  
53      private final SocketSessionConfig config = new SessionConfigImpl();
54      
55      /**
56       * Create an instance of {@link AprSocketSession}. 
57       * 
58       * {@inheritDoc} 
59       */
60      AprSocketSession(
61              IoService service, IoProcessor<AprSession> processor, long descriptor) throws Exception {
62          super(service, processor, descriptor);
63          this.config.setAll(service.getSessionConfig());
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public SocketSessionConfig getConfig() {
70          return config;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      public TransportMetadata getTransportMetadata() {
77          return METADATA;
78      }
79  
80      /**
81       * The implementation for the {@link IoSessionConfig} related to APR TCP socket.
82       * @author The Apache MINA Project (dev@mina.apache.org)
83       */
84      private class SessionConfigImpl extends AbstractSocketSessionConfig {
85      	/**
86           * {@inheritDoc}
87           */
88          public boolean isKeepAlive() {
89              try {
90                  return Socket.optGet(getDescriptor(), Socket.APR_SO_KEEPALIVE) == 1;
91              } catch (Exception e) {
92                  throw new RuntimeIoException("Failed to get SO_KEEPALIVE.", e);
93              }
94          }
95          
96          /**
97           * {@inheritDoc}
98           */
99          public void setKeepAlive(boolean on) {
100             Socket.optSet(getDescriptor(), Socket.APR_SO_KEEPALIVE, on ? 1 : 0);
101         }
102 
103         /**
104          * {@inheritDoc}
105          * not supported for the moment
106          */
107         public boolean isOobInline() {
108             return false;
109         }
110 
111         /**
112          * {@inheritDoc}
113          * not supported for the moment
114          */
115         public void setOobInline(boolean on) {
116         }
117 
118         /**
119          * {@inheritDoc}
120          */
121         public boolean isReuseAddress() {
122             try {
123                 return Socket.optGet(getDescriptor(), Socket.APR_SO_REUSEADDR) == 1;
124             } catch (Exception e) {
125                 throw new RuntimeIoException("Failed to get SO_REUSEADDR.", e);
126             }
127         }
128 
129         /**
130          * {@inheritDoc}
131          */
132         public void setReuseAddress(boolean on) {
133             Socket.optSet(getDescriptor(), Socket.APR_SO_REUSEADDR, on ? 1 : 0);
134         }
135 
136         /**
137          * {@inheritDoc}
138          */
139         public int getSoLinger() {
140             try {
141                 return Socket.optGet(getDescriptor(), Socket.APR_SO_LINGER);
142             } catch (Exception e) {
143                 throw new RuntimeIoException("Failed to get SO_LINGER.", e);
144             }
145         }
146 
147         /**
148          * {@inheritDoc}
149          */
150         public void setSoLinger(int linger) {
151             // TODO: Figure out how to disable this.
152             Socket.optSet(getDescriptor(), Socket.APR_SO_LINGER, linger);
153         }
154 
155         /**
156          * {@inheritDoc}
157          */
158         public boolean isTcpNoDelay() {
159             try {
160                 return Socket.optGet(getDescriptor(), Socket.APR_TCP_NODELAY) == 1;
161             } catch (Exception e) {
162                 throw new RuntimeIoException("Failed to get TCP_NODELAY.", e);
163             }
164         }
165 
166         /**
167          * {@inheritDoc}
168          */
169         public void setTcpNoDelay(boolean on) {
170             Socket.optSet(getDescriptor(), Socket.APR_TCP_NODELAY, on ? 1 : 0);
171         }
172 
173         /**
174          * {@inheritDoc}
175          */
176         public int getTrafficClass() {
177             return 0;
178         }
179 
180         /**
181          * {@inheritDoc}
182          */
183         public void setTrafficClass(int tc) {
184         }
185 
186         /**
187          * {@inheritDoc}
188          */
189         public int getSendBufferSize() {
190             try {
191                 return Socket.optGet(getDescriptor(), Socket.APR_SO_SNDBUF);
192             } catch (Exception e) {
193                 throw new RuntimeException("APR Exception", e);
194             }
195         }
196         
197         /**
198          * {@inheritDoc}
199          */
200         public void setSendBufferSize(int size) {
201             Socket.optSet(getDescriptor(), Socket.APR_SO_SNDBUF, size);
202         }
203 
204         /**
205          * {@inheritDoc}
206          */
207         public int getReceiveBufferSize() {
208             try {
209                 return Socket.optGet(getDescriptor(), Socket.APR_SO_RCVBUF);
210             } catch (Exception e) {
211                 throw new RuntimeException("APR Exception", e);
212             }
213         }
214 
215         /**
216          * {@inheritDoc}
217          */
218         public void setReceiveBufferSize(int size) {
219             Socket.optSet(getDescriptor(), Socket.APR_SO_RCVBUF, size);
220         }
221     }
222 }