2
|
1 |
/*
|
|
2 |
* Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.nio.ch;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.net.*;
|
|
30 |
import java.nio.*;
|
|
31 |
import java.nio.channels.*;
|
|
32 |
|
|
33 |
|
|
34 |
// Make a datagram-socket channel look like a datagram socket.
|
|
35 |
//
|
|
36 |
// The methods in this class are defined in exactly the same order as in
|
|
37 |
// java.net.DatagramSocket so as to simplify tracking future changes to that
|
|
38 |
// class.
|
|
39 |
//
|
|
40 |
|
|
41 |
public class DatagramSocketAdaptor
|
|
42 |
extends DatagramSocket
|
|
43 |
{
|
|
44 |
|
|
45 |
// The channel being adapted
|
|
46 |
private final DatagramChannelImpl dc;
|
|
47 |
|
|
48 |
// Option adaptor object, created on demand
|
|
49 |
private volatile OptionAdaptor opts = null;
|
|
50 |
|
|
51 |
// Timeout "option" value for receives
|
|
52 |
private volatile int timeout = 0;
|
|
53 |
|
|
54 |
// Traffic-class/Type-of-service
|
|
55 |
private volatile int trafficClass = 0;
|
|
56 |
|
|
57 |
|
|
58 |
// ## super will create a useless impl
|
|
59 |
private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException {
|
|
60 |
// Invoke the DatagramSocketAdaptor(SocketAddress) constructor,
|
|
61 |
// passing a dummy DatagramSocketImpl object to aovid any native
|
|
62 |
// resource allocation in super class and invoking our bind method
|
|
63 |
// before the dc field is initialized.
|
|
64 |
super(dummyDatagramSocket);
|
|
65 |
this.dc = dc;
|
|
66 |
}
|
|
67 |
|
|
68 |
public static DatagramSocket create(DatagramChannelImpl dc) {
|
|
69 |
try {
|
|
70 |
return new DatagramSocketAdaptor(dc);
|
|
71 |
} catch (IOException x) {
|
|
72 |
throw new Error(x);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
private void connectInternal(SocketAddress remote)
|
|
77 |
throws SocketException
|
|
78 |
{
|
|
79 |
InetSocketAddress isa = Net.asInetSocketAddress(remote);
|
|
80 |
int port = isa.getPort();
|
|
81 |
if (port < 0 || port > 0xFFFF)
|
|
82 |
throw new IllegalArgumentException("connect: " + port);
|
|
83 |
if (remote == null)
|
|
84 |
throw new IllegalArgumentException("connect: null address");
|
|
85 |
if (!isClosed())
|
|
86 |
return;
|
|
87 |
try {
|
|
88 |
dc.connect(remote);
|
|
89 |
} catch (Exception x) {
|
|
90 |
Net.translateToSocketException(x);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
public void bind(SocketAddress local) throws SocketException {
|
|
95 |
try {
|
|
96 |
if (local == null)
|
|
97 |
local = new InetSocketAddress(0);
|
|
98 |
dc.bind(local);
|
|
99 |
} catch (Exception x) {
|
|
100 |
Net.translateToSocketException(x);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
public void connect(InetAddress address, int port) {
|
|
105 |
try {
|
|
106 |
connectInternal(new InetSocketAddress(address, port));
|
|
107 |
} catch (SocketException x) {
|
|
108 |
// Yes, j.n.DatagramSocket really does this
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
public void connect(SocketAddress remote) throws SocketException {
|
|
113 |
if (remote == null)
|
|
114 |
throw new IllegalArgumentException("Address can't be null");
|
|
115 |
connectInternal(remote);
|
|
116 |
}
|
|
117 |
|
|
118 |
public void disconnect() {
|
|
119 |
try {
|
|
120 |
dc.disconnect();
|
|
121 |
} catch (IOException x) {
|
|
122 |
throw new Error(x);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
public boolean isBound() {
|
|
127 |
return dc.isBound();
|
|
128 |
}
|
|
129 |
|
|
130 |
public boolean isConnected() {
|
|
131 |
return dc.isConnected();
|
|
132 |
}
|
|
133 |
|
|
134 |
public InetAddress getInetAddress() {
|
|
135 |
return (isConnected()
|
|
136 |
? Net.asInetSocketAddress(dc.remoteAddress()).getAddress()
|
|
137 |
: null);
|
|
138 |
}
|
|
139 |
|
|
140 |
public int getPort() {
|
|
141 |
return (isConnected()
|
|
142 |
? Net.asInetSocketAddress(dc.remoteAddress()).getPort()
|
|
143 |
: -1);
|
|
144 |
}
|
|
145 |
|
|
146 |
public void send(DatagramPacket p) throws IOException {
|
|
147 |
synchronized (dc.blockingLock()) {
|
|
148 |
if (!dc.isBlocking())
|
|
149 |
throw new IllegalBlockingModeException();
|
|
150 |
try {
|
|
151 |
synchronized (p) {
|
|
152 |
ByteBuffer bb = ByteBuffer.wrap(p.getData(),
|
|
153 |
p.getOffset(),
|
|
154 |
p.getLength());
|
|
155 |
if (dc.isConnected()) {
|
|
156 |
if (p.getAddress() == null) {
|
|
157 |
// Legacy DatagramSocket will send in this case
|
|
158 |
// and set address and port of the packet
|
|
159 |
InetSocketAddress isa = (InetSocketAddress)
|
|
160 |
dc.remoteAddress;
|
|
161 |
p.setPort(isa.getPort());
|
|
162 |
p.setAddress(isa.getAddress());
|
|
163 |
dc.write(bb);
|
|
164 |
} else {
|
|
165 |
// Target address may not match connected address
|
|
166 |
dc.send(bb, p.getSocketAddress());
|
|
167 |
}
|
|
168 |
} else {
|
|
169 |
// Not connected so address must be valid or throw
|
|
170 |
dc.send(bb, p.getSocketAddress());
|
|
171 |
}
|
|
172 |
}
|
|
173 |
} catch (IOException x) {
|
|
174 |
Net.translateException(x);
|
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
// Must hold dc.blockingLock()
|
|
180 |
//
|
|
181 |
private void receive(ByteBuffer bb) throws IOException {
|
|
182 |
if (timeout == 0) {
|
|
183 |
dc.receive(bb);
|
|
184 |
return;
|
|
185 |
}
|
|
186 |
|
|
187 |
// Implement timeout with a selector
|
|
188 |
SelectionKey sk = null;
|
|
189 |
Selector sel = null;
|
|
190 |
dc.configureBlocking(false);
|
|
191 |
try {
|
|
192 |
int n;
|
|
193 |
if (dc.receive(bb) != null)
|
|
194 |
return;
|
|
195 |
sel = Util.getTemporarySelector(dc);
|
|
196 |
sk = dc.register(sel, SelectionKey.OP_READ);
|
|
197 |
long to = timeout;
|
|
198 |
for (;;) {
|
|
199 |
if (!dc.isOpen())
|
|
200 |
throw new ClosedChannelException();
|
|
201 |
long st = System.currentTimeMillis();
|
|
202 |
int ns = sel.select(to);
|
|
203 |
if (ns > 0 && sk.isReadable()) {
|
|
204 |
if (dc.receive(bb) != null)
|
|
205 |
return;
|
|
206 |
}
|
|
207 |
sel.selectedKeys().remove(sk);
|
|
208 |
to -= System.currentTimeMillis() - st;
|
|
209 |
if (to <= 0)
|
|
210 |
throw new SocketTimeoutException();
|
|
211 |
|
|
212 |
}
|
|
213 |
} finally {
|
|
214 |
if (sk != null)
|
|
215 |
sk.cancel();
|
|
216 |
if (dc.isOpen())
|
|
217 |
dc.configureBlocking(true);
|
|
218 |
if (sel != null)
|
|
219 |
Util.releaseTemporarySelector(sel);
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
public void receive(DatagramPacket p) throws IOException {
|
|
224 |
synchronized (dc.blockingLock()) {
|
|
225 |
if (!dc.isBlocking())
|
|
226 |
throw new IllegalBlockingModeException();
|
|
227 |
try {
|
|
228 |
synchronized (p) {
|
|
229 |
ByteBuffer bb = ByteBuffer.wrap(p.getData(),
|
|
230 |
p.getOffset(),
|
|
231 |
p.getLength());
|
|
232 |
receive(bb);
|
|
233 |
p.setLength(bb.position() - p.getOffset());
|
|
234 |
}
|
|
235 |
} catch (IOException x) {
|
|
236 |
Net.translateException(x);
|
|
237 |
}
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
public InetAddress getLocalAddress() {
|
|
242 |
if (isClosed())
|
|
243 |
return null;
|
|
244 |
try {
|
|
245 |
return Net.asInetSocketAddress(dc.localAddress()).getAddress();
|
|
246 |
} catch (Exception x) {
|
|
247 |
return new InetSocketAddress(0).getAddress();
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
public int getLocalPort() {
|
|
252 |
if (isClosed())
|
|
253 |
return -1;
|
|
254 |
try {
|
|
255 |
return Net.asInetSocketAddress(dc.localAddress()).getPort();
|
|
256 |
} catch (Exception x) {
|
|
257 |
return 0;
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
public void setSoTimeout(int timeout) throws SocketException {
|
|
262 |
this.timeout = timeout;
|
|
263 |
}
|
|
264 |
|
|
265 |
public int getSoTimeout() throws SocketException {
|
|
266 |
return timeout;
|
|
267 |
}
|
|
268 |
|
|
269 |
private OptionAdaptor opts() {
|
|
270 |
if (opts == null)
|
|
271 |
opts = new OptionAdaptor(dc);
|
|
272 |
return opts;
|
|
273 |
}
|
|
274 |
|
|
275 |
public void setSendBufferSize(int size) throws SocketException {
|
|
276 |
opts().setSendBufferSize(size);
|
|
277 |
}
|
|
278 |
|
|
279 |
public int getSendBufferSize() throws SocketException {
|
|
280 |
return opts().getSendBufferSize();
|
|
281 |
}
|
|
282 |
|
|
283 |
public void setReceiveBufferSize(int size) throws SocketException {
|
|
284 |
opts().setReceiveBufferSize(size);
|
|
285 |
}
|
|
286 |
|
|
287 |
public int getReceiveBufferSize() throws SocketException {
|
|
288 |
return opts().getReceiveBufferSize();
|
|
289 |
}
|
|
290 |
|
|
291 |
public void setReuseAddress(boolean on) throws SocketException {
|
|
292 |
opts().setReuseAddress(on);
|
|
293 |
}
|
|
294 |
|
|
295 |
public boolean getReuseAddress() throws SocketException {
|
|
296 |
return opts().getReuseAddress();
|
|
297 |
}
|
|
298 |
|
|
299 |
public void setBroadcast(boolean on) throws SocketException {
|
|
300 |
opts().setBroadcast(on);
|
|
301 |
}
|
|
302 |
|
|
303 |
public boolean getBroadcast() throws SocketException {
|
|
304 |
return opts().getBroadcast();
|
|
305 |
}
|
|
306 |
|
|
307 |
public void setTrafficClass(int tc) throws SocketException {
|
|
308 |
opts().setTrafficClass(tc);
|
|
309 |
trafficClass = tc;
|
|
310 |
}
|
|
311 |
|
|
312 |
public int getTrafficClass() throws SocketException {
|
|
313 |
int tc = opts().getTrafficClass();
|
|
314 |
if (tc < 0) {
|
|
315 |
tc = trafficClass;
|
|
316 |
}
|
|
317 |
return tc;
|
|
318 |
}
|
|
319 |
|
|
320 |
public void close() {
|
|
321 |
try {
|
|
322 |
dc.close();
|
|
323 |
} catch (IOException x) {
|
|
324 |
throw new Error(x);
|
|
325 |
}
|
|
326 |
}
|
|
327 |
|
|
328 |
public boolean isClosed() {
|
|
329 |
return !dc.isOpen();
|
|
330 |
}
|
|
331 |
|
|
332 |
public DatagramChannel getChannel() {
|
|
333 |
return dc;
|
|
334 |
}
|
|
335 |
|
|
336 |
/*
|
|
337 |
* A dummy implementation of DatagramSocketImpl that can be passed to the
|
|
338 |
* DatagramSocket constructor so that no native resources are allocated in
|
|
339 |
* super class.
|
|
340 |
*/
|
|
341 |
private static final DatagramSocketImpl dummyDatagramSocket
|
|
342 |
= new DatagramSocketImpl()
|
|
343 |
{
|
|
344 |
protected void create() throws SocketException {}
|
|
345 |
|
|
346 |
protected void bind(int lport, InetAddress laddr) throws SocketException {}
|
|
347 |
|
|
348 |
protected void send(DatagramPacket p) throws IOException {}
|
|
349 |
|
|
350 |
protected int peek(InetAddress i) throws IOException { return 0; }
|
|
351 |
|
|
352 |
protected int peekData(DatagramPacket p) throws IOException { return 0; }
|
|
353 |
|
|
354 |
protected void receive(DatagramPacket p) throws IOException {}
|
|
355 |
|
|
356 |
protected void setTTL(byte ttl) throws IOException {}
|
|
357 |
|
|
358 |
protected byte getTTL() throws IOException { return 0; }
|
|
359 |
|
|
360 |
protected void setTimeToLive(int ttl) throws IOException {}
|
|
361 |
|
|
362 |
protected int getTimeToLive() throws IOException { return 0;}
|
|
363 |
|
|
364 |
protected void join(InetAddress inetaddr) throws IOException {}
|
|
365 |
|
|
366 |
protected void leave(InetAddress inetaddr) throws IOException {}
|
|
367 |
|
|
368 |
protected void joinGroup(SocketAddress mcastaddr,
|
|
369 |
NetworkInterface netIf) throws IOException {}
|
|
370 |
|
|
371 |
protected void leaveGroup(SocketAddress mcastaddr,
|
|
372 |
NetworkInterface netIf) throws IOException {}
|
|
373 |
|
|
374 |
protected void close() {}
|
|
375 |
|
|
376 |
public Object getOption(int optID) throws SocketException { return null;}
|
|
377 |
|
|
378 |
public void setOption(int optID, Object value) throws SocketException {}
|
|
379 |
};
|
|
380 |
}
|