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