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