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