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