jdk/src/solaris/classes/sun/nio/ch/SctpMultiChannelImpl.java
author chegar
Mon, 29 Jun 2009 14:53:10 +0100
changeset 3072 a801b122142f
parent 2542 d859108aea12
child 3320 a7c037dd2e14
permissions -rw-r--r--
6855335: Several changes in the SCTP implementation. Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     4
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    10
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    15
 * accompanied this code).
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    16
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    20
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    23
 * have any questions.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
package sun.nio.ch;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
import java.net.InetAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
import java.net.SocketAddress;
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
    29
import java.net.SocketException;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.io.FileDescriptor;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.Collections;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.util.Set;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.util.HashSet;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.util.HashMap;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.ByteBuffer;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.nio.channels.SelectionKey;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import java.nio.channels.AlreadyBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import java.nio.channels.NotYetBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import java.nio.channels.spi.SelectorProvider;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import com.sun.nio.sctp.AbstractNotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.Association;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.AssociationChangeNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.HandlerResult;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.IllegalReceiveException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
import com.sun.nio.sctp.InvalidStreamException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
import com.sun.nio.sctp.IllegalUnbindException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
import com.sun.nio.sctp.NotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
import com.sun.nio.sctp.SctpMultiChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
import com.sun.nio.sctp.SctpSocketOption;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
import static com.sun.nio.sctp.SctpStandardSocketOption.*;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
import static sun.nio.ch.SctpResultContainer.*;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
 * An implementation of SctpMultiChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
public class SctpMultiChannelImpl extends SctpMultiChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
    implements SelChImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
    /* Used to make native close and preClose calls */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
    private static NativeDispatcher nd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
    private final FileDescriptor fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
    private final int fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
    /* IDs of native threads doing send and receives, for signalling */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
    private volatile long receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
    private volatile long senderThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
    /* Lock held by current receiving thread */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
    private final Object receiveLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
    /* Lock held by current sending thread */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    private final Object sendLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
    /* Lock held by any thread that modifies the state fields declared below
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
     * DO NOT invoke a blocking I/O operation while holding this lock! */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
    private final Object stateLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
    private enum ChannelState {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
        UNINITIALIZED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
        KILLPENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
        KILLED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
    /* -- The following fields are protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
    private ChannelState state = ChannelState.UNINITIALIZED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
    /* Binding: Once bound the port will remain constant. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
    int port = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
    private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
    /* Has the channel been bound to the wildcard address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
    private boolean wildcard; /* false */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
    /* Keeps a map of addresses to association, and visa versa */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
    private HashMap<SocketAddress, Association> addressMap =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
                         new HashMap<SocketAddress, Association>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
    private HashMap<Association, Set<SocketAddress>> associationMap =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
                         new HashMap<Association, Set<SocketAddress>>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
    /* -- End of fields protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
    /* If an association has been shutdown mark it for removal after
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
     * the user handler has been invoked */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
    private final ThreadLocal<Association> associationToRemove =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
        new ThreadLocal<Association>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
             @Override protected Association initialValue() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                 return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
    };
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
    /* A notification handler cannot invoke receive */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
    private final ThreadLocal<Boolean> receiveInvoked =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
        new ThreadLocal<Boolean>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
             @Override protected Boolean initialValue() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
                 return Boolean.FALSE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
    };
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
    public SctpMultiChannelImpl(SelectorProvider provider)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
        //TODO: update provider, remove public modifier
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
        this.fd = SctpNet.socket(false /*one-to-many*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
    public SctpMultiChannel bind(SocketAddress local, int backlog)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
                    ensureOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
                    if (isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
                        throw new AlreadyBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
                    InetSocketAddress isa = (local == null) ?
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
                        new InetSocketAddress(0) : Net.checkAddress(local);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
                    SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
                    if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
                        sm.checkListen(isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
                    Net.bind(fd, isa.getAddress(), isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                    InetSocketAddress boundIsa = Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
                    port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                    localAddresses.add(isa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
                    if (isa.getAddress().isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
                        wildcard = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
                    Net.listen(fd, backlog < 1 ? 50 : backlog);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
    public SctpMultiChannel bindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
        return bindUnbindAddress(address, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
    public SctpMultiChannel unbindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
        return bindUnbindAddress(address, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
    private SctpMultiChannel bindUnbindAddress(InetAddress address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
                                               boolean add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
        if (address == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
            throw new IllegalArgumentException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
                    if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                        throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                    if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                        throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                    if (wildcard)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                        throw new IllegalStateException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
                                "Cannot add or remove addresses from a channel that is bound to the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
                    if (address.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
                        throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
                                "Cannot add or remove the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
                    if (add) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
                                throw new AlreadyBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
                    } else { /*removing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
                        /* Verify that there is more than one address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
                         * and that address is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
                        if (localAddresses.size() <= 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
                            throw new IllegalUnbindException("Cannot remove address from a channel with only one address bound");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
                        boolean foundAddress = false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
                                foundAddress = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
                        if (!foundAddress )
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
                            throw new IllegalUnbindException("Cannot remove address from a channel that is not bound to that address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
                    SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                    /* Update our internal Set to reflect the addition/removal */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
                    if (add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                        localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
                    else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
                                localAddresses.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
    public Set<Association> associations()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
            throws ClosedChannelException, NotYetBoundException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
                throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
            return Collections.unmodifiableSet(associationMap.keySet());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
    private boolean isBound() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
            return port == -1 ? false : true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
    private void ensureOpen() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
    private void receiverCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
            receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
    private void senderCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
            senderThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
    protected void implConfigureBlocking(boolean block) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
        IOUtil.configureBlocking(fd, block);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
    public void implCloseSelectableChannel() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
            nd.preClose(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
            if (receiverThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
                NativeThread.signal(receiverThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
            if (senderThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
                NativeThread.signal(senderThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
            if (!isRegistered())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
    public FileDescriptor getFD() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
        return fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   302
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   303
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
    public int getFDVal() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
        return fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
     * Translates native poll revent ops into a ready operation ops
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
    private boolean translateReadyOps(int ops, int initialOps,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
                                      SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
        int intOps = sk.nioInterestOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
        int oldOps = sk.nioReadyOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
        int newOps = initialOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
            /* This should only happen if this channel is pre-closed while a
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
             * selection operation is in progress
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
             * ## Throw an error if this channel has not been pre-closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   324
        if ((ops & (PollArrayWrapper.POLLERR
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
                    | PollArrayWrapper.POLLHUP)) != 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
            newOps = intOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
            sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
            return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
            ((intOps & SelectionKey.OP_READ) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
            newOps |= SelectionKey.OP_READ;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
            ((intOps & SelectionKey.OP_WRITE) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
            newOps |= SelectionKey.OP_WRITE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
        sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
        return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   348
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
        return translateReadyOps(ops, 0, sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   354
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   355
        int newOps = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
        if ((ops & SelectionKey.OP_READ) != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
            newOps |= PollArrayWrapper.POLLIN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
        if ((ops & SelectionKey.OP_WRITE) != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
            newOps |= PollArrayWrapper.POLLOUT;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
        sk.selector.putEventOps(sk, newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   362
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   363
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   364
    public void kill() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   365
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   366
            if (state == ChannelState.KILLED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   367
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   368
            if (state == ChannelState.UNINITIALIZED) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   369
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   370
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   371
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   372
            assert !isOpen() && !isRegistered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   373
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   374
            /* Postpone the kill if there is a thread sending or receiving. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   375
            if (receiverThread == 0 && senderThread == 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   376
                nd.close(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   377
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   378
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   379
                state = ChannelState.KILLPENDING;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   380
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   381
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   382
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   383
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   384
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   385
    public <T> SctpMultiChannel setOption(SctpSocketOption<T> name,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   386
                                          T value,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   387
                                          Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   388
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   389
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   390
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   391
        if (!(supportedOptions().contains(name)))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   392
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   393
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   394
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   395
            if (association != null && (name.equals(SCTP_PRIMARY_ADDR) ||
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   396
                    name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   397
                checkAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   398
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   399
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   400
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   401
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   402
            int assocId = association == null ? 0 : association.associationID();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   403
            SctpNet.setSocketOption(fdVal, name, value, assocId);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   404
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   405
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   406
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   407
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   408
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   409
    @SuppressWarnings("unchecked")
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   410
    public <T> T getOption(SctpSocketOption<T> name, Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   411
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   412
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   413
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   414
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   415
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   416
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   417
        synchronized (stateLock) {
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   418
            if (association != null && (name.equals(SCTP_PRIMARY_ADDR) ||
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   419
                    name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   420
                checkAssociation(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   421
            }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   422
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   423
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   424
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   425
            int assocId = association == null ? 0 : association.associationID();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   426
            return (T)SctpNet.getSocketOption(fdVal, name, assocId);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   427
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   428
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   429
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   430
    private static class DefaultOptionsHolder {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   431
        static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   432
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   433
        private static Set<SctpSocketOption<?>> defaultOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   434
            HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(10);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   435
            set.add(SCTP_DISABLE_FRAGMENTS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   436
            set.add(SCTP_EXPLICIT_COMPLETE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   437
            set.add(SCTP_FRAGMENT_INTERLEAVE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   438
            set.add(SCTP_INIT_MAXSTREAMS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   439
            set.add(SCTP_NODELAY);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   440
            set.add(SCTP_PRIMARY_ADDR);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   441
            set.add(SCTP_SET_PEER_PRIMARY_ADDR);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   442
            set.add(SO_SNDBUF);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   443
            set.add(SO_RCVBUF);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   444
            set.add(SO_LINGER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   445
            return Collections.unmodifiableSet(set);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   446
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   447
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   448
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   449
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   450
    public final Set<SctpSocketOption<?>> supportedOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   451
        return DefaultOptionsHolder.defaultOptions;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   452
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   453
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   454
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   455
    public <T> MessageInfo receive(ByteBuffer buffer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   456
                                   T attachment,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   457
                                   NotificationHandler<T> handler)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   458
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   459
        if (buffer == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   460
            throw new IllegalArgumentException("buffer cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   461
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   462
        if (buffer.isReadOnly())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   463
            throw new IllegalArgumentException("Read-only buffer");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   464
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   465
        if (receiveInvoked.get())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   466
            throw new IllegalReceiveException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   467
                    "cannot invoke receive from handler");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   468
        receiveInvoked.set(Boolean.TRUE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   469
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   470
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   471
            SctpResultContainer resultContainer = new SctpResultContainer();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   472
            do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   473
                resultContainer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   474
                synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   475
                    ensureOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   476
                    if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   477
                        throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   478
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   479
                    int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   480
                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   481
                        begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   482
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   483
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   484
                            if(!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   485
                                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   486
                            receiverThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   487
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   488
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   489
                        do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   490
                            n = receive(fdVal, buffer, resultContainer);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   491
                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   492
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   493
                    } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   494
                        receiverCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   495
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   496
                        assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   497
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   498
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   499
                    if (!resultContainer.isNotification()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   500
                        /* message or nothing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   501
                        if (resultContainer.hasSomething()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   502
                            /* Set the association before returning */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   503
                            SctpMessageInfoImpl info =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   504
                                    resultContainer.getMessageInfo();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   505
                            info.setAssociation(lookupAssociation(info.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   506
                                    associationID()));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   507
                            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   508
                            if (sm != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   509
                                InetSocketAddress isa  = (InetSocketAddress)info.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   510
                                if (!addressMap.containsKey(isa)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   511
                                    /* must be a new association */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   512
                                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   513
                                        sm.checkAccept(isa.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   514
                                                       isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   515
                                    } catch (SecurityException se) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   516
                                        buffer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   517
                                        throw se;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   518
                                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   519
                                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   520
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   521
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   522
                            assert info.association() != null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   523
                            return info;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   524
                        } else  {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   525
                          /* Non-blocking may return null if nothing available*/
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   526
                            return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   527
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   528
                    } else { /* notification */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   529
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   530
                            handleNotificationInternal(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   531
                                    resultContainer);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   532
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   533
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   534
                } /* receiveLock */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   535
            } while (handler == null ? true :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   536
                (invokeNotificationHandler(resultContainer, handler, attachment)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   537
                 == HandlerResult.CONTINUE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   538
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   539
            receiveInvoked.set(Boolean.FALSE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   540
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   541
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   542
        return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   543
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   544
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   545
    private int receive(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   546
                        ByteBuffer dst,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   547
                        SctpResultContainer resultContainer)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   548
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   549
        int pos = dst.position();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   550
        int lim = dst.limit();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   551
        assert (pos <= lim);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   552
        int rem = (pos <= lim ? lim - pos : 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   553
        if (dst instanceof DirectBuffer && rem > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   554
            return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   555
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   556
        /* Substitute a native buffer. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   557
        int newSize = Math.max(rem, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   558
        ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   559
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   560
            int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   561
            bb.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   562
            if (n > 0 && rem > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   563
                dst.put(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   564
            return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   565
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   566
            Util.releaseTemporaryDirectBuffer(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   567
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   568
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   569
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   570
    private int receiveIntoNativeBuffer(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   571
                                        SctpResultContainer resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   572
                                        ByteBuffer bb,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   573
                                        int rem,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   574
                                        int pos)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   575
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   576
        int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   577
        if (n > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   578
            bb.position(pos + n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   579
        return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   580
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   581
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   582
    private InternalNotificationHandler internalNotificationHandler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   583
            new InternalNotificationHandler();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   584
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   585
    private void handleNotificationInternal(SctpResultContainer resultContainer)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   586
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   587
        invokeNotificationHandler(resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   588
                internalNotificationHandler, null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   589
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   590
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   591
    private class InternalNotificationHandler<T>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   592
            extends AbstractNotificationHandler<T>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   593
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   594
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   595
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   596
                AssociationChangeNotification not, T unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   597
            SctpAssocChange sac = (SctpAssocChange) not;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   598
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   599
            /* Update map to reflect change in association */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   600
            switch (not.event()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   601
                case COMM_UP :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   602
                    Association newAssociation = new SctpAssociationImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   603
                       (sac.assocId(), sac.maxInStreams(), sac.maxOutStreams());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   604
                    addAssociation(newAssociation);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   605
                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   606
                case SHUTDOWN :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   607
                case COMM_LOST :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   608
                //case RESTART: ???
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   609
                    /* mark association for removal after user handler invoked*/
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   610
                    associationToRemove.set(lookupAssociation(sac.assocId()));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   611
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   612
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   613
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   614
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   615
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   616
    private <T> HandlerResult invokeNotificationHandler(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   617
                                   SctpResultContainer resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   618
                                   NotificationHandler<T> handler,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   619
                                   T attachment) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   620
        HandlerResult result;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   621
        SctpNotification notification = resultContainer.notification();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   622
        notification.setAssociation(lookupAssociation(notification.assocId()));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   623
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   624
        if (!(handler instanceof AbstractNotificationHandler)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   625
            result = handler.handleNotification(notification, attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   626
        } else { /* AbstractNotificationHandler */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   627
            AbstractNotificationHandler absHandler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   628
                    (AbstractNotificationHandler)handler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   629
            switch(resultContainer.type()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   630
                case ASSOCIATION_CHANGED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   631
                    result = absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   632
                            resultContainer.getAssociationChanged(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   633
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   634
                case PEER_ADDRESS_CHANGED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   635
                    result = absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   636
                            resultContainer.getPeerAddressChanged(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   637
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   638
                case SEND_FAILED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   639
                    result = absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   640
                            resultContainer.getSendFailed(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   641
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   642
                case SHUTDOWN :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   643
                    result =  absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   644
                            resultContainer.getShutdown(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   645
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   646
                default :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   647
                    /* implementation specific handlers */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   648
                    result =  absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   649
                            resultContainer.notification(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   650
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   651
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   652
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   653
        if (!(handler instanceof InternalNotificationHandler)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   654
            /* Only remove associations after user handler
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   655
             * has finished with them */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   656
            Association assoc = associationToRemove.get();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   657
            if (assoc != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   658
                removeAssociation(assoc);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   659
                associationToRemove.set(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   660
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   661
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   662
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   663
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   664
        return result;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   665
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   666
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   667
    private Association lookupAssociation(int assocId) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   668
        /* Lookup the association in our internal map */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   669
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   670
            Set<Association> assocs = associationMap.keySet();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   671
            for (Association a : assocs) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   672
                if (a.associationID() == assocId) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   673
                    return a;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   674
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   675
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   676
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   677
        return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   678
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   679
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   680
    private void addAssociation(Association association) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   681
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   682
            int assocId = association.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   683
            Set<SocketAddress> addresses = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   684
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   685
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   686
                addresses = SctpNet.getRemoteAddresses(fdVal, assocId);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   687
            } catch (IOException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   688
                /* OK, determining connected addresses may not be possible
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   689
                 * shutdown, connection lost, etc */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   690
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   691
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   692
            associationMap.put(association, addresses);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   693
            if (addresses != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   694
                for (SocketAddress addr : addresses)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   695
                    addressMap.put(addr, association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   696
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   697
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   698
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   699
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   700
    private void removeAssociation(Association association) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   701
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   702
            int assocId = association.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   703
            Set<SocketAddress> addresses = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   704
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   705
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   706
                addresses = SctpNet.getRemoteAddresses(fdVal, assocId);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   707
            } catch (IOException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   708
                /* OK, determining connected addresses may not be possible
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   709
                 * shutdown, connection lost, etc */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   710
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   711
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   712
            Set<Association> assocs = associationMap.keySet();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   713
            for (Association a : assocs) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   714
                if (a.associationID() == assocId) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   715
                    associationMap.remove(a);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   716
                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   717
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   718
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   719
            if (addresses != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   720
                for (SocketAddress addr : addresses)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   721
                    addressMap.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   722
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   723
                /* We cannot determine the connected addresses */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   724
                Set<java.util.Map.Entry<SocketAddress, Association>> addrAssocs =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   725
                        addressMap.entrySet();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   726
                for (java.util.Map.Entry<SocketAddress, Association> entry : addrAssocs) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   727
                    if (entry.getValue().equals(association)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   728
                        addressMap.remove(entry.getKey());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   729
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   730
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   731
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   732
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   733
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   734
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   735
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   736
     * @throws  IllegalArgumentException
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   737
     *          If the given association is not controlled by this channel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   738
     *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   739
     * @return  {@code true} if, and only if, the given association is one
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   740
     *          of the current associations controlled by this channel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   741
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   742
    private boolean checkAssociation(Association messageAssoc) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   743
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   744
            for (Association association : associationMap.keySet()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   745
                if (messageAssoc.equals(association)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   746
                    return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   747
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   748
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   749
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   750
        throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   751
              "Given Association is not controlled by this channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   752
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   753
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   754
    private void checkStreamNumber(Association assoc, int streamNumber) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   755
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   756
            if (streamNumber < 0 || streamNumber >= assoc.maxOutboundStreams())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   757
                throw new InvalidStreamException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   758
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   759
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   760
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   761
    /* TODO: Add support for ttl and isComplete to both 121 12M
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   762
     *       SCTP_EOR not yet supported on reference platforms
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   763
     *       TTL support limited...
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   764
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   765
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   766
    public int send(ByteBuffer buffer, MessageInfo messageInfo)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   767
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   768
        if (buffer == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   769
            throw new IllegalArgumentException("buffer cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   770
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   771
        if (messageInfo == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   772
            throw new IllegalArgumentException("messageInfo cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   773
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   774
        synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   775
            ensureOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   776
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   777
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   778
                bind(null, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   779
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   780
            int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   781
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   782
                int assocId = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   783
                SocketAddress address = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   784
                begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   785
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   786
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   787
                    if(!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   788
                        return 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   789
                    senderThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   790
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   791
                    /* Determine what address or association to send to */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   792
                    Association assoc = messageInfo.association();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   793
                    InetSocketAddress addr = (InetSocketAddress)messageInfo.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   794
                    if (assoc != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   795
                        checkAssociation(assoc);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   796
                        checkStreamNumber(assoc, messageInfo.streamNumber());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   797
                        assocId = assoc.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   798
                        /* have we also got a preferred address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   799
                        if (addr != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   800
                            if (!assoc.equals(addressMap.get(addr)))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   801
                                throw new IllegalArgumentException("given preferred address is not part of this association");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   802
                            address = addr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   803
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   804
                    } else if (addr != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   805
                        address = addr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   806
                        Association association = addressMap.get(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   807
                        if (association != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   808
                            checkStreamNumber(association, messageInfo.streamNumber());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   809
                            assocId = association.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   810
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   811
                        } else { /* must be new association */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   812
                            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   813
                            if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   814
                                sm.checkConnect(addr.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   815
                                                addr.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   816
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   817
                    } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   818
                        throw new AssertionError(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   819
                            "Both association and address cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   820
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   821
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   822
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   823
                do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   824
                    n = send(fdVal, buffer, assocId, address, messageInfo);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   825
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   826
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   827
                return IOStatus.normalize(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   828
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   829
                senderCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   830
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   831
                assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   832
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   833
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   834
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   835
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   836
    private int send(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   837
                     ByteBuffer src,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   838
                     int assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   839
                     SocketAddress target,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   840
                     MessageInfo messageInfo)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   841
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   842
        int streamNumber = messageInfo.streamNumber();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   843
        boolean unordered = messageInfo.isUnordered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   844
        int ppid = messageInfo.payloadProtocolID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   845
        int pos = src.position();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   846
        int lim = src.limit();
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   847
        assert (pos <= lim && streamNumber >= 0);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   848
        int rem = (pos <= lim ? lim - pos : 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   849
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   850
        if (src instanceof DirectBuffer)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   851
            return sendFromNativeBuffer(fd, src, rem, pos, target, assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   852
                    streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   853
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   854
        /* Substitute a native buffer */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   855
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   856
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   857
            bb.put(src);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   858
            bb.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   859
            /* Do not update src until we see how many bytes were written */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   860
            src.position(pos);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   861
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   862
            int n = sendFromNativeBuffer(fd, bb, rem, pos, target, assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   863
                    streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   864
            if (n > 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   865
                /* now update src */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   866
                src.position(pos + n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   867
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   868
            return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   869
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   870
            Util.releaseTemporaryDirectBuffer(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   871
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   872
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   873
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   874
    private int sendFromNativeBuffer(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   875
                                     ByteBuffer bb,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   876
                                     int rem,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   877
                                     int pos,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   878
                                     SocketAddress target,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   879
                                     int assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   880
                                     int streamNumber,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   881
                                     boolean unordered,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   882
                                     int ppid)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   883
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   884
        int written = send0(fd, ((DirectBuffer)bb).address() + pos,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   885
                            rem, target, assocId, streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   886
        if (written > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   887
            bb.position(pos + written);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   888
        return written;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   889
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   890
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   891
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   892
    public SctpMultiChannel shutdown(Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   893
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   894
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   895
            checkAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   896
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   897
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   898
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   899
            SctpNet.shutdown(fdVal, association.associationID());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   900
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   901
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   902
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   903
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   904
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   905
    public Set<SocketAddress> getAllLocalAddresses()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   906
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   907
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   908
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   909
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   910
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   911
                return Collections.EMPTY_SET;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   912
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   913
            return SctpNet.getLocalAddresses(fdVal);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   914
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   915
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   916
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   917
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   918
    public Set<SocketAddress> getRemoteAddresses(Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   919
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   920
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   921
            checkAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   922
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   923
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   924
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   925
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   926
                return SctpNet.getRemoteAddresses(fdVal, association.associationID());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   927
            } catch (SocketException se) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   928
                /* a valid association should always have remote addresses */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   929
                Set<SocketAddress> addrs = associationMap.get(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   930
                return addrs != null ? addrs : Collections.EMPTY_SET;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   931
            }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   932
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   933
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   934
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   935
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   936
    public SctpChannel branch(Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   937
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   938
        synchronized (stateLock) {
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   939
            checkAssociation(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   940
            if (!isOpen())
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   941
                throw new ClosedChannelException();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   942
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   943
            FileDescriptor bFd = SctpNet.branch(fdVal,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   944
                                                association.associationID());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   945
            /* successfully branched, we can now remove it from assoc list */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   946
            removeAssociation(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   947
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   948
            return new SctpChannelImpl(provider(), bFd, association);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   949
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   950
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   951
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   952
    /* Use common native implementation shared between
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   953
     * one-to-one and one-to-many */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   954
    private static int receive0(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   955
                                SctpResultContainer resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   956
                                long address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   957
                                int length)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   958
            throws IOException{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   959
        return SctpChannelImpl.receive0(fd, resultContainer, address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   960
                length);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   961
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   962
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   963
    private static int send0(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   964
                             long address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   965
                             int length,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   966
                             SocketAddress target,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   967
                             int assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   968
                             int streamNumber,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   969
                             boolean unordered,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   970
                             int ppid)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   971
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   972
        return SctpChannelImpl.send0(fd, address, length, target, assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   973
                streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   974
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   975
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   976
    static {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   977
        Util.load();   /* loads nio & net native libraries */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   978
        java.security.AccessController.doPrivileged(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   979
                new sun.security.action.LoadLibraryAction("sctp"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   980
        nd = new SctpSocketDispatcher();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   981
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   982
}