jdk/src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1152 29d6145d1097
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
    // Option adaptor object, created on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private volatile OptionAdaptor opts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Timeout "option" value for receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private volatile int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Traffic-class/Type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private volatile int trafficClass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // ## super will create a useless impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // Invoke the DatagramSocketAdaptor(SocketAddress) constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // passing a dummy DatagramSocketImpl object to aovid any native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // resource allocation in super class and invoking our bind method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // before the dc field is initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(dummyDatagramSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.dc = dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static DatagramSocket create(DatagramChannelImpl dc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return new DatagramSocketAdaptor(dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private void connectInternal(SocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throws SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        InetSocketAddress isa = Net.asInetSocketAddress(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int port = isa.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (port < 0 || port > 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new IllegalArgumentException("connect: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new IllegalArgumentException("connect: null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (!isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            dc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Net.translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void bind(SocketAddress local) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (local == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                local = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            dc.bind(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Net.translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void connect(InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            connectInternal(new InetSocketAddress(address, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (SocketException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // Yes, j.n.DatagramSocket really does this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void connect(SocketAddress remote) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new IllegalArgumentException("Address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        connectInternal(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            dc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return dc.isBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return dc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return (isConnected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ? Net.asInetSocketAddress(dc.remoteAddress()).getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return (isConnected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                ? Net.asInetSocketAddress(dc.remoteAddress()).getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void send(DatagramPacket p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        synchronized (dc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (!dc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    ByteBuffer bb = ByteBuffer.wrap(p.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                                    p.getOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                                    p.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (dc.isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        if (p.getAddress() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            // Legacy DatagramSocket will send in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            // and set address and port of the packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            InetSocketAddress isa = (InetSocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                                    dc.remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            p.setPort(isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            p.setAddress(isa.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            dc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            // Target address may not match connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            dc.send(bb, p.getSocketAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        // Not connected so address must be valid or throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        dc.send(bb, p.getSocketAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Must hold dc.blockingLock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private void receive(ByteBuffer bb) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // Implement timeout with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        SelectionKey sk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        dc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (dc.receive(bb) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            sel = Util.getTemporarySelector(dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            sk = dc.register(sel, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (!dc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                     throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                int ns = sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (ns > 0 && sk.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    if (dc.receive(bb) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (to <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (sk != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (dc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                dc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                Util.releaseTemporarySelector(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public void receive(DatagramPacket p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        synchronized (dc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (!dc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    ByteBuffer bb = ByteBuffer.wrap(p.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                                    p.getOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                                    p.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    p.setLength(bb.position() - p.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return Net.asInetSocketAddress(dc.localAddress()).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return new InetSocketAddress(0).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return Net.asInetSocketAddress(dc.localAddress()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        this.timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private OptionAdaptor opts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (opts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            opts = new OptionAdaptor(dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return opts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public void setSendBufferSize(int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        opts().setSendBufferSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public int getSendBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return opts().getSendBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void setReceiveBufferSize(int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        opts().setReceiveBufferSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public int getReceiveBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return opts().getReceiveBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        opts().setReuseAddress(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return opts().getReuseAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public void setBroadcast(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        opts().setBroadcast(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public boolean getBroadcast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return opts().getBroadcast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void setTrafficClass(int tc) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        opts().setTrafficClass(tc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        trafficClass = tc;
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 int getTrafficClass() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int tc = opts().getTrafficClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (tc < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            tc = trafficClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return tc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
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 boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return !dc.isOpen();
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 DatagramChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    * A dummy implementation of DatagramSocketImpl that can be passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    * DatagramSocket constructor so that no native resources are allocated in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    * super class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
   private static final DatagramSocketImpl dummyDatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
       = new DatagramSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
       protected void create() throws SocketException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
       protected void bind(int lport, InetAddress laddr) throws SocketException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
       protected void send(DatagramPacket p) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
       protected int peek(InetAddress i) throws IOException { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
       protected int peekData(DatagramPacket p) throws IOException { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
       protected void receive(DatagramPacket p) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
       protected void setTTL(byte ttl) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
       protected byte getTTL() throws IOException { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
       protected void setTimeToLive(int ttl) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
       protected int getTimeToLive() throws IOException { return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
       protected void join(InetAddress inetaddr) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
       protected void leave(InetAddress inetaddr) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
       protected void joinGroup(SocketAddress mcastaddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                 NetworkInterface netIf) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
       protected void leaveGroup(SocketAddress mcastaddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                 NetworkInterface netIf) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
       protected void close() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
       public Object getOption(int optID) throws SocketException { return null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
       public void setOption(int optID, Object value) throws SocketException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}