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