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;
21  
22  import java.net.DatagramSocket;
23  import java.net.SocketException;
24  
25  import org.apache.mina.util.ExceptionMonitor;
26  
27  /**
28   * A default implementation of {@link DatagramSessionConfig}.
29   *
30   * @author The Apache MINA Project (dev@mina.apache.org)
31   * @version $Rev: 439913 $, $Date: 2006-09-04 05:12:43 +0200 (mån, 04 sep 2006) $
32   */
33  public class DefaultDatagramSessionConfig extends AbstractDatagramSessionConfig {
34  
35      private static boolean SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
36      private static boolean SET_SEND_BUFFER_SIZE_AVAILABLE = false;
37      private static boolean GET_TRAFFIC_CLASS_AVAILABLE = false;
38      private static final boolean SET_TRAFFIC_CLASS_AVAILABLE = false;
39      private static boolean DEFAULT_BROADCAST = false;
40      private static boolean DEFAULT_REUSE_ADDRESS = false;
41      private static int DEFAULT_RECEIVE_BUFFER_SIZE = 1024;
42      private static int DEFAULT_SEND_BUFFER_SIZE = 1024;
43      private static int DEFAULT_TRAFFIC_CLASS = 0;
44  
45      static {
46          initialize();
47      }
48  
49      private static void initialize() {
50          DatagramSocket socket = null;
51  
52          try {
53              socket = new DatagramSocket();
54              DEFAULT_BROADCAST = socket.getBroadcast();
55              DEFAULT_REUSE_ADDRESS = socket.getReuseAddress();
56              DEFAULT_RECEIVE_BUFFER_SIZE = socket.getReceiveBufferSize();
57              DEFAULT_SEND_BUFFER_SIZE = socket.getSendBufferSize();
58  
59              // Check if setReceiveBufferSize is supported.
60              try {
61                  socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
62                  SET_RECEIVE_BUFFER_SIZE_AVAILABLE = true;
63              } catch (SocketException e) {
64                  SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
65              }
66  
67              // Check if setSendBufferSize is supported.
68              try {
69                  socket.setSendBufferSize(DEFAULT_SEND_BUFFER_SIZE);
70                  SET_SEND_BUFFER_SIZE_AVAILABLE = true;
71              } catch (SocketException e) {
72                  SET_SEND_BUFFER_SIZE_AVAILABLE = false;
73              }
74  
75              // Check if getTrafficClass is supported.
76              try {
77                  DEFAULT_TRAFFIC_CLASS = socket.getTrafficClass();
78                  GET_TRAFFIC_CLASS_AVAILABLE = true;
79              } catch (SocketException e) {
80                  GET_TRAFFIC_CLASS_AVAILABLE = false;
81                  DEFAULT_TRAFFIC_CLASS = 0;
82              }
83          } catch (SocketException e) {
84              ExceptionMonitor.getInstance().exceptionCaught(e);
85          } finally {
86              if (socket != null) {
87                  socket.close();
88              }
89          }
90      }
91  
92      public static boolean isSetReceiveBufferSizeAvailable() {
93          return SET_RECEIVE_BUFFER_SIZE_AVAILABLE;
94      }
95  
96      public static boolean isSetSendBufferSizeAvailable() {
97          return SET_SEND_BUFFER_SIZE_AVAILABLE;
98      }
99  
100     public static boolean isGetTrafficClassAvailable() {
101         return GET_TRAFFIC_CLASS_AVAILABLE;
102     }
103 
104     public static boolean isSetTrafficClassAvailable() {
105         return SET_TRAFFIC_CLASS_AVAILABLE;
106     }
107 
108     private boolean broadcast = DEFAULT_BROADCAST;
109     private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
110     private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
111     private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
112     private int trafficClass = DEFAULT_TRAFFIC_CLASS;
113     
114     /**
115      * Creates a new instance.
116      */
117     public DefaultDatagramSessionConfig() {
118     }
119 
120     /**
121      * @see DatagramSocket#getBroadcast()
122      */
123     public boolean isBroadcast() {
124         return broadcast;
125     }
126 
127     /**
128      * @see DatagramSocket#setBroadcast(boolean)
129      */
130     public void setBroadcast(boolean broadcast) {
131         this.broadcast = broadcast;
132     }
133 
134     /**
135      * @see DatagramSocket#getReuseAddress()
136      */
137     public boolean isReuseAddress() {
138         return reuseAddress;
139     }
140 
141     /**
142      * @see DatagramSocket#setReuseAddress(boolean)
143      */
144     public void setReuseAddress(boolean reuseAddress) {
145         this.reuseAddress = reuseAddress;
146     }
147 
148     /**
149      * @see DatagramSocket#getReceiveBufferSize()
150      */
151     public int getReceiveBufferSize() {
152         return receiveBufferSize;
153     }
154 
155     /**
156      * @see DatagramSocket#setReceiveBufferSize(int)
157      */
158     public void setReceiveBufferSize(int receiveBufferSize) {
159         this.receiveBufferSize = receiveBufferSize;
160     }
161 
162     /**
163      * @see DatagramSocket#getSendBufferSize()
164      */
165     public int getSendBufferSize() {
166         return sendBufferSize;
167     }
168 
169     /**
170      * @see DatagramSocket#setSendBufferSize(int)
171      */
172     public void setSendBufferSize(int sendBufferSize) {
173         this.sendBufferSize = sendBufferSize;
174     }
175 
176     /**
177      * @see DatagramSocket#getTrafficClass()
178      */
179     public int getTrafficClass() {
180         return trafficClass;
181     }
182 
183     /**
184      * @see DatagramSocket#setTrafficClass(int)
185      */
186     public void setTrafficClass(int trafficClass) {
187         this.trafficClass = trafficClass;
188     }
189 
190     @Override
191     protected boolean isBroadcastChanged() {
192         return broadcast != DEFAULT_BROADCAST;
193     }
194 
195     @Override
196     protected boolean isReceiveBufferSizeChanged() {
197         return receiveBufferSize != DEFAULT_RECEIVE_BUFFER_SIZE;
198     }
199 
200     @Override
201     protected boolean isReuseAddressChanged() {
202         return reuseAddress != DEFAULT_REUSE_ADDRESS;
203     }
204 
205     @Override
206     protected boolean isSendBufferSizeChanged() {
207         return sendBufferSize != DEFAULT_SEND_BUFFER_SIZE;
208     }
209 
210     @Override
211     protected boolean isTrafficClassChanged() {
212         return trafficClass != DEFAULT_TRAFFIC_CLASS;
213     }
214     
215 }