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.transport.socket.AbstractDatagramSessionConfig;
32  import org.apache.mina.transport.socket.DatagramSessionConfig;
33  import org.apache.tomcat.jni.Socket;
34  
35  /**
36   * An {@link IoSession} for APR UDP datagram based session.
37   * It's implementing the usual common features for {@link DatagramSessionConfig}. 
38   *
39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
40   */
41  class AprDatagramSession extends AprSession {
42  
43      static final TransportMetadata METADATA = new DefaultTransportMetadata("apr", "datagram", true, false,
44              InetSocketAddress.class, DatagramSessionConfig.class, IoBuffer.class);
45  
46      /**
47       * Create an instance of {@link AprDatagramSession}. 
48       * 
49       * {@inheritDoc} 
50       */
51      AprDatagramSession(IoService service, IoProcessor<AprSession> processor, long descriptor,
52              InetSocketAddress remoteAddress) throws Exception {
53          super(service, processor, descriptor, remoteAddress);
54          config = new SessionConfigImpl();
55          this.config.setAll(service.getSessionConfig());
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public DatagramSessionConfig getConfig() {
62          return (DatagramSessionConfig) config;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      public TransportMetadata getTransportMetadata() {
69          return METADATA;
70      }
71  
72      /**
73       * The implementation for the {@link org.apache.mina.core.session.IoSessionConfig} related to APR UDP socket.
74       * @author <a href="http://mina.apache.org">Apache MINA Project</a>
75       */
76      private class SessionConfigImpl extends AbstractDatagramSessionConfig {
77          /**
78           * {@inheritDoc}
79           */
80          public boolean isReuseAddress() {
81              try {
82                  return Socket.optGet(getDescriptor(), Socket.APR_SO_REUSEADDR) == 1;
83              } catch (Exception e) {
84                  throw new RuntimeIoException("Failed to get SO_REUSEADDR.", e);
85              }
86          }
87  
88          /**
89           * {@inheritDoc}
90           */
91          public void setReuseAddress(boolean on) {
92              Socket.optSet(getDescriptor(), Socket.APR_SO_REUSEADDR, on ? 1 : 0);
93          }
94  
95          /**
96           * {@inheritDoc}
97           */
98          public int getTrafficClass() {
99              return 0;
100         }
101 
102         /**
103          * {@inheritDoc}
104          */
105         public void setTrafficClass(int tc) {
106         }
107 
108         /**
109          * {@inheritDoc}
110          */
111         public int getSendBufferSize() {
112             try {
113                 return Socket.optGet(getDescriptor(), Socket.APR_SO_SNDBUF);
114             } catch (Exception e) {
115                 throw new RuntimeException("APR Exception", e);
116             }
117         }
118 
119         /**
120          * {@inheritDoc}
121          */
122         public void setSendBufferSize(int size) {
123             Socket.optSet(getDescriptor(), Socket.APR_SO_SNDBUF, size);
124         }
125 
126         /**
127          * {@inheritDoc}
128          */
129         public int getReceiveBufferSize() {
130             try {
131                 return Socket.optGet(getDescriptor(), Socket.APR_SO_RCVBUF);
132             } catch (Exception e) {
133                 throw new RuntimeException("APR Exception", e);
134             }
135         }
136 
137         /**
138          * {@inheritDoc}
139          */
140         public void setReceiveBufferSize(int size) {
141             Socket.optSet(getDescriptor(), Socket.APR_SO_RCVBUF, size);
142         }
143 
144         /**
145          * {@inheritDoc}
146          */
147         public boolean isBroadcast() {
148             return false;
149         }
150 
151         /**
152          * {@inheritDoc}
153          */
154         public void setBroadcast(boolean broadcast) {
155         }
156     }
157 }