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