001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.transport.socket;
021:
022: import java.net.DatagramSocket;
023: import java.net.SocketException;
024:
025: import org.apache.mina.common.ExceptionMonitor;
026:
027: /**
028: * A default implementation of {@link DatagramSessionConfig}.
029: *
030: * @author The Apache MINA Project (dev@mina.apache.org)
031: * @version $Rev: 439913 $, $Date: 2006-09-04 05:12:43 +0200 (mån, 04 sep 2006) $
032: */
033: public class DefaultDatagramSessionConfig extends
034: AbstractDatagramSessionConfig {
035:
036: private static boolean SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
037:
038: private static boolean SET_SEND_BUFFER_SIZE_AVAILABLE = false;
039:
040: private static boolean GET_TRAFFIC_CLASS_AVAILABLE = false;
041:
042: private static final boolean SET_TRAFFIC_CLASS_AVAILABLE = false;
043:
044: private static boolean DEFAULT_BROADCAST = false;
045:
046: private static boolean DEFAULT_REUSE_ADDRESS = false;
047:
048: private static int DEFAULT_RECEIVE_BUFFER_SIZE = 1024;
049:
050: private static int DEFAULT_SEND_BUFFER_SIZE = 1024;
051:
052: private static int DEFAULT_TRAFFIC_CLASS = 0;
053:
054: static {
055: initialize();
056: }
057:
058: private static void initialize() {
059: DatagramSocket socket = null;
060:
061: try {
062: socket = new DatagramSocket();
063: DEFAULT_BROADCAST = socket.getBroadcast();
064: DEFAULT_REUSE_ADDRESS = socket.getReuseAddress();
065: DEFAULT_RECEIVE_BUFFER_SIZE = socket.getReceiveBufferSize();
066: DEFAULT_SEND_BUFFER_SIZE = socket.getSendBufferSize();
067:
068: // Check if setReceiveBufferSize is supported.
069: try {
070: socket
071: .setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
072: SET_RECEIVE_BUFFER_SIZE_AVAILABLE = true;
073: } catch (SocketException e) {
074: SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
075: }
076:
077: // Check if setSendBufferSize is supported.
078: try {
079: socket.setSendBufferSize(DEFAULT_SEND_BUFFER_SIZE);
080: SET_SEND_BUFFER_SIZE_AVAILABLE = true;
081: } catch (SocketException e) {
082: SET_SEND_BUFFER_SIZE_AVAILABLE = false;
083: }
084:
085: // Check if getTrafficClass is supported.
086: try {
087: DEFAULT_TRAFFIC_CLASS = socket.getTrafficClass();
088: GET_TRAFFIC_CLASS_AVAILABLE = true;
089: } catch (SocketException e) {
090: GET_TRAFFIC_CLASS_AVAILABLE = false;
091: DEFAULT_TRAFFIC_CLASS = 0;
092: }
093: } catch (SocketException e) {
094: ExceptionMonitor.getInstance().exceptionCaught(e);
095: } finally {
096: if (socket != null) {
097: socket.close();
098: }
099: }
100: }
101:
102: public static boolean isSetReceiveBufferSizeAvailable() {
103: return SET_RECEIVE_BUFFER_SIZE_AVAILABLE;
104: }
105:
106: public static boolean isSetSendBufferSizeAvailable() {
107: return SET_SEND_BUFFER_SIZE_AVAILABLE;
108: }
109:
110: public static boolean isGetTrafficClassAvailable() {
111: return GET_TRAFFIC_CLASS_AVAILABLE;
112: }
113:
114: public static boolean isSetTrafficClassAvailable() {
115: return SET_TRAFFIC_CLASS_AVAILABLE;
116: }
117:
118: private boolean broadcast = DEFAULT_BROADCAST;
119:
120: private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
121:
122: private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
123:
124: private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
125:
126: private int trafficClass = DEFAULT_TRAFFIC_CLASS;
127:
128: /**
129: * Creates a new instance.
130: */
131: public DefaultDatagramSessionConfig() {
132: }
133:
134: /**
135: * @see DatagramSocket#getBroadcast()
136: */
137: public boolean isBroadcast() {
138: return broadcast;
139: }
140:
141: /**
142: * @see DatagramSocket#setBroadcast(boolean)
143: */
144: public void setBroadcast(boolean broadcast) {
145: this .broadcast = broadcast;
146: }
147:
148: /**
149: * @see DatagramSocket#getReuseAddress()
150: */
151: public boolean isReuseAddress() {
152: return reuseAddress;
153: }
154:
155: /**
156: * @see DatagramSocket#setReuseAddress(boolean)
157: */
158: public void setReuseAddress(boolean reuseAddress) {
159: this .reuseAddress = reuseAddress;
160: }
161:
162: /**
163: * @see DatagramSocket#getReceiveBufferSize()
164: */
165: public int getReceiveBufferSize() {
166: return receiveBufferSize;
167: }
168:
169: /**
170: * @see DatagramSocket#setReceiveBufferSize(int)
171: */
172: public void setReceiveBufferSize(int receiveBufferSize) {
173: this .receiveBufferSize = receiveBufferSize;
174: }
175:
176: /**
177: * @see DatagramSocket#getSendBufferSize()
178: */
179: public int getSendBufferSize() {
180: return sendBufferSize;
181: }
182:
183: /**
184: * @see DatagramSocket#setSendBufferSize(int)
185: */
186: public void setSendBufferSize(int sendBufferSize) {
187: this .sendBufferSize = sendBufferSize;
188: }
189:
190: /**
191: * @see DatagramSocket#getTrafficClass()
192: */
193: public int getTrafficClass() {
194: return trafficClass;
195: }
196:
197: /**
198: * @see DatagramSocket#setTrafficClass(int)
199: */
200: public void setTrafficClass(int trafficClass) {
201: this.trafficClass = trafficClass;
202: }
203: }
|