1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30
31
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
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
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
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
116
117 public DefaultDatagramSessionConfig() {
118 }
119
120
121
122
123 public boolean isBroadcast() {
124 return broadcast;
125 }
126
127
128
129
130 public void setBroadcast(boolean broadcast) {
131 this.broadcast = broadcast;
132 }
133
134
135
136
137 public boolean isReuseAddress() {
138 return reuseAddress;
139 }
140
141
142
143
144 public void setReuseAddress(boolean reuseAddress) {
145 this.reuseAddress = reuseAddress;
146 }
147
148
149
150
151 public int getReceiveBufferSize() {
152 return receiveBufferSize;
153 }
154
155
156
157
158 public void setReceiveBufferSize(int receiveBufferSize) {
159 this.receiveBufferSize = receiveBufferSize;
160 }
161
162
163
164
165 public int getSendBufferSize() {
166 return sendBufferSize;
167 }
168
169
170
171
172 public void setSendBufferSize(int sendBufferSize) {
173 this.sendBufferSize = sendBufferSize;
174 }
175
176
177
178
179 public int getTrafficClass() {
180 return trafficClass;
181 }
182
183
184
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 }