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.nio;
21  
22  import java.net.DatagramSocket;
23  import java.net.SocketException;
24  import java.nio.channels.DatagramChannel;
25  
26  import org.apache.mina.core.RuntimeIoException;
27  import org.apache.mina.transport.socket.AbstractDatagramSessionConfig;
28  
29  /**
30   * Define the configuration for a Datagram based session. 
31   * 
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  class NioDatagramSessionConfig extends AbstractDatagramSessionConfig {
35      /** The associated channel */
36      private final DatagramChannel channel;
37  
38      /**
39       * Creates a new instance of NioDatagramSessionConfig, associated
40       * with the given DatagramChannel.
41       *
42       * @param channel The associated DatagramChannel
43       */
44      NioDatagramSessionConfig(DatagramChannel channel) {
45          this.channel = channel;
46      }
47  
48      /**
49       * Get the Socket receive buffer size for this DatagramChannel.
50       * 
51       * @return the DatagramChannel receive buffer size.
52       * @throws RuntimeIoException if the socket is closed or if we 
53       * had a SocketException
54       * 
55       * @see DatagramSocket#getReceiveBufferSize()
56       */
57      @Override
58      public int getReceiveBufferSize() {
59          try {
60              return channel.socket().getReceiveBufferSize();
61          } catch (SocketException e) {
62              throw new RuntimeIoException(e);
63          }
64      }
65  
66      /**
67       * Set the Socket receive buffer size for this DatagramChannel. <br>
68       * <br>
69       * Note : The underlying Socket may not accept the new buffer's size.
70       * The user has to check that the new value has been set. 
71       * 
72       * @param receiveBufferSize the DatagramChannel receive buffer size.
73       * @throws RuntimeIoException if the socket is closed or if we 
74       * had a SocketException
75       * 
76       * @see DatagramSocket#setReceiveBufferSize(int)
77       */
78      @Override
79      public void setReceiveBufferSize(int receiveBufferSize) {
80          try {
81              channel.socket().setReceiveBufferSize(receiveBufferSize);
82          } catch (SocketException e) {
83              throw new RuntimeIoException(e);
84          }
85      }
86  
87      /**
88       * Tells if SO_BROADCAST is enabled.
89       * 
90       * @return <tt>true</tt> if SO_BROADCAST is enabled
91       * @throws RuntimeIoException If the socket is closed or if we get an
92       * {@link SocketException} 
93       */
94      @Override
95      public boolean isBroadcast() {
96          try {
97              return channel.socket().getBroadcast();
98          } catch (SocketException e) {
99              throw new RuntimeIoException(e);
100         }
101     }
102 
103     @Override
104     public void setBroadcast(boolean broadcast) {
105         try {
106             channel.socket().setBroadcast(broadcast);
107         } catch (SocketException e) {
108             throw new RuntimeIoException(e);
109         }
110     }
111 
112     /**
113      * 
114      * @throws RuntimeIoException If the socket is closed or if we get an
115      * {@link SocketException} 
116      */
117     @Override
118     public int getSendBufferSize() {
119         try {
120             return channel.socket().getSendBufferSize();
121         } catch (SocketException e) {
122             throw new RuntimeIoException(e);
123         }
124     }
125 
126     /**
127      * 
128      * @throws RuntimeIoException If the socket is closed or if we get an
129      * {@link SocketException} 
130      */
131     @Override
132     public void setSendBufferSize(int sendBufferSize) {
133         try {
134             channel.socket().setSendBufferSize(sendBufferSize);
135         } catch (SocketException e) {
136             throw new RuntimeIoException(e);
137         }
138     }
139 
140     /**
141      * Tells if SO_REUSEADDR is enabled.
142      * 
143      * @return <tt>true</tt> if SO_REUSEADDR is enabled
144      * @throws RuntimeIoException If the socket is closed or if we get an
145      * {@link SocketException} 
146      */
147     @Override
148     public boolean isReuseAddress() {
149         try {
150             return channel.socket().getReuseAddress();
151         } catch (SocketException e) {
152             throw new RuntimeIoException(e);
153         }
154     }
155 
156     /**
157      * 
158      * @throws RuntimeIoException If the socket is closed or if we get an
159      * {@link SocketException} 
160      */
161     @Override
162     public void setReuseAddress(boolean reuseAddress) {
163         try {
164             channel.socket().setReuseAddress(reuseAddress);
165         } catch (SocketException e) {
166             throw new RuntimeIoException(e);
167         }
168     }
169 
170     /**
171      * Get the current Traffic Class for this Socket, if any. As this is
172      * not a mandatory feature, the returned value should be considered as 
173      * a hint. 
174      * 
175      * @return The Traffic Class supported by this Socket
176      * @throws RuntimeIoException If the socket is closed or if we get an
177      * {@link SocketException} 
178      */
179     @Override
180     public int getTrafficClass() {
181         try {
182             return channel.socket().getTrafficClass();
183         } catch (SocketException e) {
184             throw new RuntimeIoException(e);
185         }
186     }
187 
188     /**
189      * {@inheritDoc}
190      * @throws RuntimeIoException If the socket is closed or if we get an
191      * {@link SocketException} 
192      */
193     @Override
194     public void setTrafficClass(int trafficClass) {
195         try {
196             channel.socket().setTrafficClass(trafficClass);
197         } catch (SocketException e) {
198             throw new RuntimeIoException(e);
199         }
200     }
201 }