DatagramSocketAdaptor.java revision 4216
1173N/A/*
2362N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
1173N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1173N/A *
1173N/A * This code is free software; you can redistribute it and/or modify it
1173N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1173N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1173N/A *
1173N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1173N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1173N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1173N/A * version 2 for more details (a copy is included in the LICENSE file that
1173N/A * accompanied this code).
1173N/A *
1173N/A * You should have received a copy of the GNU General Public License version
1173N/A * 2 along with this work; if not, write to the Free Software Foundation,
1173N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1173N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1173N/A */
1173N/A
1173N/Apackage sun.nio.ch;
1173N/A
1173N/Aimport java.io.*;
1173N/Aimport java.net.*;
1173N/Aimport java.nio.*;
1173N/Aimport java.nio.channels.*;
1173N/A
1173N/A
1173N/A// Make a datagram-socket channel look like a datagram socket.
1173N/A//
1173N/A// The methods in this class are defined in exactly the same order as in
1173N/A// java.net.DatagramSocket so as to simplify tracking future changes to that
1173N/A// class.
1173N/A//
1173N/A
1173N/Apublic class DatagramSocketAdaptor
1173N/A extends DatagramSocket
1173N/A{
1173N/A
1173N/A // The channel being adapted
1173N/A private final DatagramChannelImpl dc;
1173N/A
1173N/A // Timeout "option" value for receives
1173N/A private volatile int timeout = 0;
1173N/A
1173N/A // ## super will create a useless impl
1173N/A private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException {
1173N/A // Invoke the DatagramSocketAdaptor(SocketAddress) constructor,
1173N/A // passing a dummy DatagramSocketImpl object to aovid any native
1173N/A // resource allocation in super class and invoking our bind method
1173N/A // before the dc field is initialized.
1173N/A super(dummyDatagramSocket);
1173N/A this.dc = dc;
1173N/A }
1173N/A
1173N/A public static DatagramSocket create(DatagramChannelImpl dc) {
1173N/A try {
1173N/A return new DatagramSocketAdaptor(dc);
1173N/A } catch (IOException x) {
1173N/A throw new Error(x);
1173N/A }
1173N/A }
1173N/A
1173N/A private void connectInternal(SocketAddress remote)
1173N/A throws SocketException
1173N/A {
1173N/A InetSocketAddress isa = Net.asInetSocketAddress(remote);
1173N/A int port = isa.getPort();
1173N/A if (port < 0 || port > 0xFFFF)
1173N/A throw new IllegalArgumentException("connect: " + port);
1173N/A if (remote == null)
1173N/A throw new IllegalArgumentException("connect: null address");
1173N/A if (isClosed())
1173N/A return;
1173N/A try {
1173N/A dc.connect(remote);
1173N/A } catch (Exception x) {
1173N/A Net.translateToSocketException(x);
1173N/A }
1173N/A }
1173N/A
1450N/A public void bind(SocketAddress local) throws SocketException {
1450N/A try {
1173N/A if (local == null)
1173N/A local = new InetSocketAddress(0);
1173N/A dc.bind(local);
1173N/A } catch (Exception x) {
1173N/A Net.translateToSocketException(x);
1173N/A }
1173N/A }
1173N/A
1173N/A public void connect(InetAddress address, int port) {
1173N/A try {
1173N/A connectInternal(new InetSocketAddress(address, port));
1173N/A } catch (SocketException x) {
1173N/A // Yes, j.n.DatagramSocket really does this
1173N/A }
1173N/A }
1173N/A
1173N/A public void connect(SocketAddress remote) throws SocketException {
1173N/A if (remote == null)
1173N/A throw new IllegalArgumentException("Address can't be null");
1173N/A connectInternal(remote);
1173N/A }
1173N/A
1173N/A public void disconnect() {
1173N/A try {
1173N/A dc.disconnect();
1173N/A } catch (IOException x) {
1173N/A throw new Error(x);
1173N/A }
1173N/A }
1173N/A
1173N/A public boolean isBound() {
1173N/A return dc.localAddress() != null;
1173N/A }
1173N/A
1173N/A public boolean isConnected() {
1173N/A return dc.remoteAddress() != null;
1173N/A }
1173N/A
1173N/A public InetAddress getInetAddress() {
1173N/A return (isConnected()
1173N/A ? Net.asInetSocketAddress(dc.remoteAddress()).getAddress()
1173N/A : null);
1173N/A }
1173N/A
1173N/A public int getPort() {
1173N/A return (isConnected()
1173N/A ? Net.asInetSocketAddress(dc.remoteAddress()).getPort()
1173N/A : -1);
1173N/A }
1173N/A
1173N/A public void send(DatagramPacket p) throws IOException {
1173N/A synchronized (dc.blockingLock()) {
1173N/A if (!dc.isBlocking())
1173N/A throw new IllegalBlockingModeException();
1173N/A try {
1173N/A synchronized (p) {
1173N/A ByteBuffer bb = ByteBuffer.wrap(p.getData(),
1173N/A p.getOffset(),
1173N/A p.getLength());
1173N/A if (dc.isConnected()) {
1173N/A if (p.getAddress() == null) {
1173N/A // Legacy DatagramSocket will send in this case
1173N/A // and set address and port of the packet
1173N/A InetSocketAddress isa = (InetSocketAddress)
1173N/A dc.remoteAddress();
1173N/A p.setPort(isa.getPort());
1173N/A p.setAddress(isa.getAddress());
1173N/A dc.write(bb);
1173N/A } else {
1173N/A // Target address may not match connected address
1173N/A dc.send(bb, p.getSocketAddress());
1173N/A }
1173N/A } else {
1173N/A // Not connected so address must be valid or throw
1173N/A dc.send(bb, p.getSocketAddress());
1173N/A }
1173N/A }
1173N/A } catch (IOException x) {
1173N/A Net.translateException(x);
1173N/A }
1173N/A }
1173N/A }
1173N/A
1173N/A // Must hold dc.blockingLock()
1173N/A //
1173N/A private SocketAddress receive(ByteBuffer bb) throws IOException {
1173N/A if (timeout == 0) {
1173N/A return dc.receive(bb);
1173N/A }
1173N/A
1173N/A // Implement timeout with a selector
1173N/A SelectionKey sk = null;
1173N/A Selector sel = null;
1173N/A dc.configureBlocking(false);
1173N/A try {
1173N/A int n;
1173N/A SocketAddress sender;
1173N/A if ((sender = dc.receive(bb)) != null)
1173N/A return sender;
1173N/A sel = Util.getTemporarySelector(dc);
1173N/A sk = dc.register(sel, SelectionKey.OP_READ);
1173N/A long to = timeout;
1173N/A for (;;) {
1173N/A if (!dc.isOpen())
1173N/A throw new ClosedChannelException();
1173N/A long st = System.currentTimeMillis();
1173N/A int ns = sel.select(to);
1173N/A if (ns > 0 && sk.isReadable()) {
1173N/A if ((sender = dc.receive(bb)) != null)
1173N/A return sender;
1173N/A }
1173N/A sel.selectedKeys().remove(sk);
1173N/A to -= System.currentTimeMillis() - st;
1173N/A if (to <= 0)
1173N/A throw new SocketTimeoutException();
1173N/A
1173N/A }
1173N/A } finally {
1173N/A if (sk != null)
1173N/A sk.cancel();
1173N/A if (dc.isOpen())
1173N/A dc.configureBlocking(true);
1173N/A if (sel != null)
1173N/A Util.releaseTemporarySelector(sel);
1173N/A }
1173N/A }
1173N/A
1173N/A public void receive(DatagramPacket p) throws IOException {
1173N/A synchronized (dc.blockingLock()) {
1173N/A if (!dc.isBlocking())
1173N/A throw new IllegalBlockingModeException();
1173N/A try {
1173N/A synchronized (p) {
1173N/A ByteBuffer bb = ByteBuffer.wrap(p.getData(),
1173N/A p.getOffset(),
1173N/A p.getLength());
1173N/A SocketAddress sender = receive(bb);
p.setSocketAddress(sender);
p.setLength(bb.position() - p.getOffset());
}
} catch (IOException x) {
Net.translateException(x);
}
}
}
public InetAddress getLocalAddress() {
if (isClosed())
return null;
SocketAddress local = dc.localAddress();
if (local == null)
local = new InetSocketAddress(0);
InetAddress result = ((InetSocketAddress)local).getAddress();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkConnect(result.getHostAddress(), -1);
} catch (SecurityException x) {
return new InetSocketAddress(0).getAddress();
}
}
return result;
}
public int getLocalPort() {
if (isClosed())
return -1;
try {
SocketAddress local = dc.getLocalAddress();
if (local != null) {
return ((InetSocketAddress)local).getPort();
}
} catch (Exception x) {
}
return 0;
}
public void setSoTimeout(int timeout) throws SocketException {
this.timeout = timeout;
}
public int getSoTimeout() throws SocketException {
return timeout;
}
private void setBooleanOption(SocketOption<Boolean> name, boolean value)
throws SocketException
{
try {
dc.setOption(name, value);
} catch (IOException x) {
Net.translateToSocketException(x);
}
}
private void setIntOption(SocketOption<Integer> name, int value)
throws SocketException
{
try {
dc.setOption(name, value);
} catch (IOException x) {
Net.translateToSocketException(x);
}
}
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
try {
return dc.getOption(name).booleanValue();
} catch (IOException x) {
Net.translateToSocketException(x);
return false; // keep compiler happy
}
}
private int getIntOption(SocketOption<Integer> name) throws SocketException {
try {
return dc.getOption(name).intValue();
} catch (IOException x) {
Net.translateToSocketException(x);
return -1; // keep compiler happy
}
}
public void setSendBufferSize(int size) throws SocketException {
if (size <= 0)
throw new IllegalArgumentException("Invalid send size");
setIntOption(StandardSocketOptions.SO_SNDBUF, size);
}
public int getSendBufferSize() throws SocketException {
return getIntOption(StandardSocketOptions.SO_SNDBUF);
}
public void setReceiveBufferSize(int size) throws SocketException {
if (size <= 0)
throw new IllegalArgumentException("Invalid receive size");
setIntOption(StandardSocketOptions.SO_RCVBUF, size);
}
public int getReceiveBufferSize() throws SocketException {
return getIntOption(StandardSocketOptions.SO_RCVBUF);
}
public void setReuseAddress(boolean on) throws SocketException {
setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
}
public boolean getReuseAddress() throws SocketException {
return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
}
public void setBroadcast(boolean on) throws SocketException {
setBooleanOption(StandardSocketOptions.SO_BROADCAST, on);
}
public boolean getBroadcast() throws SocketException {
return getBooleanOption(StandardSocketOptions.SO_BROADCAST);
}
public void setTrafficClass(int tc) throws SocketException {
setIntOption(StandardSocketOptions.IP_TOS, tc);
}
public int getTrafficClass() throws SocketException {
return getIntOption(StandardSocketOptions.IP_TOS);
}
public void close() {
try {
dc.close();
} catch (IOException x) {
throw new Error(x);
}
}
public boolean isClosed() {
return !dc.isOpen();
}
public DatagramChannel getChannel() {
return dc;
}
/*
* A dummy implementation of DatagramSocketImpl that can be passed to the
* DatagramSocket constructor so that no native resources are allocated in
* super class.
*/
private static final DatagramSocketImpl dummyDatagramSocket
= new DatagramSocketImpl()
{
protected void create() throws SocketException {}
protected void bind(int lport, InetAddress laddr) throws SocketException {}
protected void send(DatagramPacket p) throws IOException {}
protected int peek(InetAddress i) throws IOException { return 0; }
protected int peekData(DatagramPacket p) throws IOException { return 0; }
protected void receive(DatagramPacket p) throws IOException {}
protected void setTTL(byte ttl) throws IOException {}
protected byte getTTL() throws IOException { return 0; }
protected void setTimeToLive(int ttl) throws IOException {}
protected int getTimeToLive() throws IOException { return 0;}
protected void join(InetAddress inetaddr) throws IOException {}
protected void leave(InetAddress inetaddr) throws IOException {}
protected void joinGroup(SocketAddress mcastaddr,
NetworkInterface netIf) throws IOException {}
protected void leaveGroup(SocketAddress mcastaddr,
NetworkInterface netIf) throws IOException {}
protected void close() {}
public Object getOption(int optID) throws SocketException { return null;}
public void setOption(int optID, Object value) throws SocketException {}
};
}