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 */ 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 public int getReceiveBufferSize() { 58 try { 59 return channel.socket().getReceiveBufferSize(); 60 } catch (SocketException e) { 61 throw new RuntimeIoException(e); 62 } 63 } 64 65 /** 66 * Set the Socket receive buffer size for this DatagramChannel. <br> 67 * <br> 68 * Note : The underlying Socket may not accept the new buffer's size. 69 * The user has to check that the new value has been set. 70 * 71 * @param receiveBufferSize the DatagramChannel receive buffer size. 72 * @throws RuntimeIoException if the socket is closed or if we 73 * had a SocketException 74 * 75 * @see DatagramSocket#setReceiveBufferSize() 76 */ 77 public void setReceiveBufferSize(int receiveBufferSize) { 78 try { 79 channel.socket().setReceiveBufferSize(receiveBufferSize); 80 } catch (SocketException e) { 81 throw new RuntimeIoException(e); 82 } 83 } 84 85 /** 86 * Tells if SO_BROADCAST is enabled. 87 * 88 * @return <code>true</code> if SO_BROADCAST is enabled 89 * @throws RuntimeIoException If the socket is closed or if we get an 90 * {@link SocketException} 91 */ 92 public boolean isBroadcast() { 93 try { 94 return channel.socket().getBroadcast(); 95 } catch (SocketException e) { 96 throw new RuntimeIoException(e); 97 } 98 } 99 100 public void setBroadcast(boolean broadcast) { 101 try { 102 channel.socket().setBroadcast(broadcast); 103 } catch (SocketException e) { 104 throw new RuntimeIoException(e); 105 } 106 } 107 108 /** 109 * 110 * @throws RuntimeIoException If the socket is closed or if we get an 111 * {@link SocketException} 112 */ 113 public int getSendBufferSize() { 114 try { 115 return channel.socket().getSendBufferSize(); 116 } catch (SocketException e) { 117 throw new RuntimeIoException(e); 118 } 119 } 120 121 /** 122 * 123 * @throws RuntimeIoException If the socket is closed or if we get an 124 * {@link SocketException} 125 */ 126 public void setSendBufferSize(int sendBufferSize) { 127 try { 128 channel.socket().setSendBufferSize(sendBufferSize); 129 } catch (SocketException e) { 130 throw new RuntimeIoException(e); 131 } 132 } 133 134 /** 135 * Tells if SO_REUSEADDR is enabled. 136 * 137 * @return <code>true</code> if SO_REUSEADDR is enabled 138 * @throws RuntimeIoException If the socket is closed or if we get an 139 * {@link SocketException} 140 */ 141 public boolean isReuseAddress() { 142 try { 143 return channel.socket().getReuseAddress(); 144 } catch (SocketException e) { 145 throw new RuntimeIoException(e); 146 } 147 } 148 149 /** 150 * 151 * @throws RuntimeIoException If the socket is closed or if we get an 152 * {@link SocketException} 153 */ 154 public void setReuseAddress(boolean reuseAddress) { 155 try { 156 channel.socket().setReuseAddress(reuseAddress); 157 } catch (SocketException e) { 158 throw new RuntimeIoException(e); 159 } 160 } 161 162 /** 163 * Get the current Traffic Class for this Socket, if any. As this is 164 * not a mandatory feature, the returned value should be considered as 165 * a hint. 166 * 167 * @return The Traffic Class supported by this Socket 168 * @throws RuntimeIoException If the socket is closed or if we get an 169 * {@link SocketException} 170 */ 171 public int getTrafficClass() { 172 try { 173 return channel.socket().getTrafficClass(); 174 } catch (SocketException e) { 175 throw new RuntimeIoException(e); 176 } 177 } 178 179 /** 180 * {@inheritDoc} 181 * @throws RuntimeIoException If the socket is closed or if we get an 182 * {@link SocketException} 183 */ 184 public void setTrafficClass(int trafficClass) { 185 try { 186 channel.socket().setTrafficClass(trafficClass); 187 } catch (SocketException e) { 188 throw new RuntimeIoException(e); 189 } 190 } 191 }