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 The Apache MINA Project (dev@mina.apache.org)
33   * @version $Rev: 718753 $, $Date: 2008-11-18 23:53:14 +0100 (Tue, 18 Nov 2008) $
34   */
35  class NioDatagramSessionConfig extends AbstractDatagramSessionConfig {
36      /** The associated channel */
37      private final DatagramChannel channel;
38  
39      /**
40       * Creates a new instance of NioDatagramSessionConfig, associated
41       * with the given DatagramChannel.
42       *
43       * @param channel The associated DatagramChannel
44       */
45      NioDatagramSessionConfig(DatagramChannel channel) {
46          this.channel = channel;
47      }
48  
49      /**
50       * Get the Socket receive buffer size for this DatagramChannel.
51       * 
52       * @return the DatagramChannel receive buffer size.
53       * @throws RuntimeIoException if the socket is closed or if we 
54       * had a SocketException
55       * 
56       * @see DatagramSocket#getReceiveBufferSize()
57       */
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()
77       */
78      public void setReceiveBufferSize(int receiveBufferSize) {
79          try {
80              channel.socket().setReceiveBufferSize(receiveBufferSize);
81          } catch (SocketException e) {
82              throw new RuntimeIoException(e);
83          }
84      }
85  
86      /**
87       * Tells if SO_BROADCAST is enabled.
88       * 
89       * @return <code>true</code> if SO_BROADCAST is enabled
90       * @throws RuntimeIoException If the socket is closed or if we get an
91       * {@link SocketException} 
92       */
93      public boolean isBroadcast() {
94          try {
95              return channel.socket().getBroadcast();
96          } catch (SocketException e) {
97              throw new RuntimeIoException(e);
98          }
99      }
100 
101     public void setBroadcast(boolean broadcast) {
102         try {
103             channel.socket().setBroadcast(broadcast);
104         } catch (SocketException e) {
105             throw new RuntimeIoException(e);
106         }
107     }
108 
109     /**
110      * 
111      * @throws RuntimeIoException If the socket is closed or if we get an
112      * {@link SocketException} 
113      */
114     public int getSendBufferSize() {
115         try {
116             return channel.socket().getSendBufferSize();
117         } catch (SocketException e) {
118             throw new RuntimeIoException(e);
119         }
120     }
121 
122     /**
123      * 
124      * @throws RuntimeIoException If the socket is closed or if we get an
125      * {@link SocketException} 
126      */
127     public void setSendBufferSize(int sendBufferSize) {
128         try {
129             channel.socket().setSendBufferSize(sendBufferSize);
130         } catch (SocketException e) {
131             throw new RuntimeIoException(e);
132         }
133     }
134 
135     /**
136      * Tells if SO_REUSEADDR is enabled.
137      * 
138      * @return <code>true</code> if SO_REUSEADDR is enabled
139      * @throws RuntimeIoException If the socket is closed or if we get an
140      * {@link SocketException} 
141      */
142     public boolean isReuseAddress() {
143         try {
144             return channel.socket().getReuseAddress();
145         } catch (SocketException e) {
146             throw new RuntimeIoException(e);
147         }
148     }
149 
150     /**
151      * 
152      * @throws RuntimeIoException If the socket is closed or if we get an
153      * {@link SocketException} 
154      */
155     public void setReuseAddress(boolean reuseAddress) {
156         try {
157             channel.socket().setReuseAddress(reuseAddress);
158         } catch (SocketException e) {
159             throw new RuntimeIoException(e);
160         }
161     }
162 
163     /**
164      * Get the current Traffic Class for this Socket, if any. As this is
165      * not a mandatory feature, the returned value should be considered as 
166      * a hint. 
167      * 
168      * @return The Traffic Class supported by this Socket
169      * @throws RuntimeIoException If the socket is closed or if we get an
170      * {@link SocketException} 
171      */
172     public int getTrafficClass() {
173         try {
174             return channel.socket().getTrafficClass();
175         } catch (SocketException e) {
176             throw new RuntimeIoException(e);
177         }
178     }
179 
180     /**
181      * {@inheritDoc}
182      * @throws RuntimeIoException If the socket is closed or if we get an
183      * {@link SocketException} 
184      */
185     public void setTrafficClass(int trafficClass) {
186         try {
187             channel.socket().setTrafficClass(trafficClass);
188         } catch (SocketException e) {
189             throw new RuntimeIoException(e);
190         }
191     }
192 }