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