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