src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java
author michaelm
Wed, 13 Nov 2019 11:06:17 +0000
branchunixdomainchannels
changeset 59052 15e9a570c6e6
parent 58847 692de65ab293
child 59076 998df1368cca
permissions -rw-r--r--
unixdomainchannels: added some unit tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     1
/*
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     4
 *
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    10
 *
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    15
 * accompanied this code).
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    16
 *
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    20
 *
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    23
 * questions.
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    24
 */
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    25
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    26
package sun.nio.ch;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    27
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    28
import java.io.FileDescriptor;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    29
import java.io.IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    30
import java.io.UncheckedIOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    31
import java.net.ProtocolFamily;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    32
import java.net.Socket;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    33
import java.net.SocketAddress;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    34
import java.net.SocketException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    35
import java.net.SocketOption;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    36
import java.net.SocketTimeoutException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    37
import java.net.StandardProtocolFamily;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    38
import java.net.StandardSocketOptions;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    39
import java.nio.ByteBuffer;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    40
import java.nio.channels.AlreadyBoundException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    41
import java.nio.channels.AlreadyConnectedException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    42
import java.nio.channels.AsynchronousCloseException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    43
import java.nio.channels.ClosedChannelException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    44
import java.nio.channels.ConnectionPendingException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    45
import java.nio.channels.IllegalBlockingModeException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    46
import java.nio.channels.NoConnectionPendingException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    47
import java.nio.channels.NotYetConnectedException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    48
import java.nio.channels.SelectionKey;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    49
import java.nio.channels.SocketChannel;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    50
import java.nio.channels.UnixDomainSocketAddress;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    51
import java.nio.channels.spi.SelectorProvider;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    52
import java.security.AccessController;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    53
import java.util.Collections;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    54
import java.util.HashSet;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    55
import java.util.Objects;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    56
import java.util.Set;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    57
import java.util.concurrent.locks.ReentrantLock;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    58
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    59
import sun.net.ConnectionResetException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    60
import sun.net.NetHooks;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    61
import sun.net.ext.ExtendedSocketOptions;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    62
import sun.net.util.SocketExceptions;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    63
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    64
/**
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    65
 * An implementation of SocketChannels
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    66
 */
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    67
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    68
public class UnixDomainSocketChannelImpl extends SocketChannelImpl
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    69
{
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    70
    public UnixDomainSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    71
        throws IOException
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    72
    {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    73
        super(sp, fd);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    74
        if (bound) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    75
            synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    76
                this.localAddress = Net.localUnixAddress(fd);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    77
            }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    78
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    79
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    80
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    81
    // Constructor for sockets obtained from server sockets
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    82
    //
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    83
    UnixDomainSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, SocketAddress isa)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    84
        throws IOException
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    85
    {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    86
        super(sp, fd);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    87
        synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    88
            this.localAddress = Net.localUnixAddress(fd);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    89
            this.remoteAddress = isa;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    90
            this.state = ST_CONNECTED;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    91
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    92
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    93
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    94
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    95
    public SocketAddress getLocalAddress() throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    96
        synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    97
            ensureOpen();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    98
            return localAddress; // TODO: revealed local address?
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
    99
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   100
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   101
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   102
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   103
    public SocketAddress getRemoteAddress() throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   104
        synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   105
            ensureOpen();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   106
            return remoteAddress;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   107
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   108
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   109
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   110
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   111
    public <T> SocketChannel setOption(SocketOption<T> name, T value)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   112
        throws IOException
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   113
    {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   114
        Objects.requireNonNull(name);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   115
        if (!supportedOptions().contains(name))
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   116
            throw new UnsupportedOperationException("'" + name + "' not supported");
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   117
        if (!name.type().isInstance(value))
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   118
            throw new IllegalArgumentException("Invalid value '" + value + "'");
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   119
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   120
        synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   121
            ensureOpen();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   122
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   123
            // no options that require special handling
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   124
            Net.setSocketOption(fd, name, value);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   125
            return this;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   126
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   127
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   128
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   129
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   130
    @SuppressWarnings("unchecked")
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   131
    public <T> T getOption(SocketOption<T> name)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   132
        throws IOException
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   133
    {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   134
        Objects.requireNonNull(name);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   135
        if (!supportedOptions().contains(name))
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   136
            throw new UnsupportedOperationException("'" + name + "' not supported");
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   137
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   138
        synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   139
            ensureOpen();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   140
            // no options that require special handling
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   141
            return (T) Net.getSocketOption(fd, name); // AF_UNIX
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   142
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   143
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   144
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   145
    private static class DefaultOptionsHolder {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   146
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   147
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   148
        private static Set<SocketOption<?>> defaultOptions() {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   149
            HashSet<SocketOption<?>> set = new HashSet<>();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   150
            set.add(StandardSocketOptions.SO_SNDBUF);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   151
            set.add(StandardSocketOptions.SO_RCVBUF);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   152
            set.add(StandardSocketOptions.SO_KEEPALIVE);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   153
            set.add(StandardSocketOptions.SO_LINGER);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   154
            return Collections.unmodifiableSet(set);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   155
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   156
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   157
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   158
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   159
    public final Set<SocketOption<?>> supportedOptions() {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   160
        return DefaultOptionsHolder.defaultOptions;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   161
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   162
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   163
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   164
    public SocketChannel bind(SocketAddress local) throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   165
        readLock.lock();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   166
        try {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   167
            writeLock.lock();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   168
            try {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   169
                synchronized (stateLock) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   170
                    ensureOpen();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   171
                    if (state == ST_CONNECTIONPENDING)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   172
                        throw new ConnectionPendingException();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   173
                    if (localAddress != null)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   174
                        throw new AlreadyBoundException();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   175
                    if (local == null)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   176
                        throw new NullPointerException(); // TODO: ??
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   177
                    UnixDomainSocketAddress usa = Net.checkUnixAddress(local);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   178
                    Net.unixDomainBind(fd, usa);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   179
                    localAddress = Net.localUnixAddress(fd);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   180
                }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   181
            } finally {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   182
                writeLock.unlock();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   183
            }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   184
        } finally {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   185
            readLock.unlock();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   186
        }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   187
        return this;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   188
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   189
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   190
    public Socket socket() {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   191
        throw new UnsupportedOperationException("socket not supported");
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   192
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   193
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   194
    /**
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   195
     * Checks the remote address to which this channel is to be connected.
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   196
     */
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   197
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   198
    SocketAddress checkRemote(SocketAddress sa) throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   199
        SocketAddress isa = Net.checkUnixAddress(sa);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   200
        return isa; // TODO
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   201
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   202
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   203
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   204
    int connectImpl(FileDescriptor fd, SocketAddress sa) throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   205
        UnixDomainSocketAddress isa = (UnixDomainSocketAddress)sa;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   206
        return Net.unixDomainConnect(fd, isa);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   207
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   208
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   209
    @Override
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   210
    SocketAddress localAddressImpl(FileDescriptor fd) throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   211
        return Net.localUnixAddress(fd);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   212
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   213
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   214
    String getRevealedLocalAddressAsString(SocketAddress sa) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   215
        return sa.toString(); // TODO
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   216
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   217
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
diff changeset
   218
}