Search in sources :

Example 1 with Association

use of com.sun.nio.sctp.Association in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method invokeNotificationHandler.

private <T> HandlerResult invokeNotificationHandler(ResultContainer resultContainer, NotificationHandler<T> handler, T attachment) {
    HandlerResult result;
    SctpNotification notification = resultContainer.notification();
    notification.setAssociation(lookupAssociation(notification.assocId()));
    if (!(handler instanceof AbstractNotificationHandler)) {
        result = handler.handleNotification(notification, attachment);
    } else {
        /* AbstractNotificationHandler */
        AbstractNotificationHandler<T> absHandler = (AbstractNotificationHandler<T>) handler;
        switch(resultContainer.type()) {
            case ASSOCIATION_CHANGED:
                result = absHandler.handleNotification(resultContainer.getAssociationChanged(), attachment);
                break;
            case PEER_ADDRESS_CHANGED:
                result = absHandler.handleNotification(resultContainer.getPeerAddressChanged(), attachment);
                break;
            case SEND_FAILED:
                result = absHandler.handleNotification(resultContainer.getSendFailed(), attachment);
                break;
            case SHUTDOWN:
                result = absHandler.handleNotification(resultContainer.getShutdown(), attachment);
                break;
            default:
                /* implementation specific handlers */
                result = absHandler.handleNotification(resultContainer.notification(), attachment);
        }
    }
    if (!(handler instanceof InternalNotificationHandler)) {
        /* Only remove associations after user handler
             * has finished with them */
        Association assoc = associationToRemove.get();
        if (assoc != null) {
            removeAssociation(assoc);
            associationToRemove.set(null);
        }
    }
    return result;
}
Also used : Association(com.sun.nio.sctp.Association) AbstractNotificationHandler(com.sun.nio.sctp.AbstractNotificationHandler) HandlerResult(com.sun.nio.sctp.HandlerResult)

Example 2 with Association

use of com.sun.nio.sctp.Association in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method removeAssociation.

private void removeAssociation(Association association) {
    synchronized (stateLock) {
        int assocId = association.associationID();
        Set<SocketAddress> addresses = null;
        try {
            addresses = SctpNet.getRemoteAddresses(fdVal, assocId);
        } catch (IOException unused) {
        /* OK, determining connected addresses may not be possible
                 * shutdown, connection lost, etc */
        }
        Set<Association> assocs = associationMap.keySet();
        for (Association a : assocs) {
            if (a.associationID() == assocId) {
                associationMap.remove(a);
                break;
            }
        }
        if (addresses != null) {
            for (SocketAddress addr : addresses) addressMap.remove(addr);
        } else {
            /* We cannot determine the connected addresses */
            Set<java.util.Map.Entry<SocketAddress, Association>> addrAssocs = addressMap.entrySet();
            Iterator<Entry<SocketAddress, Association>> iterator = addrAssocs.iterator();
            while (iterator.hasNext()) {
                Entry<SocketAddress, Association> entry = iterator.next();
                if (entry.getValue().equals(association)) {
                    iterator.remove();
                }
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) Association(com.sun.nio.sctp.Association) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with Association

use of com.sun.nio.sctp.Association in project jdk8u_jdk by JetBrains.

the class Branch method doTest.

void doTest(SocketAddress peerAddress) {
    SctpMultiChannel channel = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
    MessageInfo info = MessageInfo.createOutgoing(null, 0);
    try {
        channel = SctpMultiChannel.open();
        /* setup an association implicitly by sending a small message */
        int streamNumber = 0;
        debug("sending to " + peerAddress + " on stream number: " + streamNumber);
        info = MessageInfo.createOutgoing(peerAddress, streamNumber);
        buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
        buffer.flip();
        int position = buffer.position();
        int remaining = buffer.remaining();
        debug("sending small message: " + buffer);
        int sent = channel.send(buffer, info);
        check(sent == remaining, "sent should be equal to remaining");
        check(buffer.position() == (position + sent), "buffers position should have been incremented by sent");
        /* Receive the COMM_UP */
        buffer.clear();
        BranchNotificationHandler handler = new BranchNotificationHandler();
        info = channel.receive(buffer, null, handler);
        check(handler.receivedCommUp(), "COMM_UP no received");
        Set<Association> associations = channel.associations();
        check(!associations.isEmpty(), "There should be some associations");
        Association bassoc = associations.iterator().next();
        /* TEST 1: branch */
        SctpChannel bchannel = channel.branch(bassoc);
        check(!bchannel.getAllLocalAddresses().isEmpty(), "branched channel should be bound");
        check(!bchannel.getRemoteAddresses().isEmpty(), "branched channel should be connected");
        check(channel.associations().isEmpty(), "there should be no associations since the only one was branched off");
        buffer.clear();
        info = bchannel.receive(buffer, null, null);
        buffer.flip();
        check(info != null, "info is null");
        check(info.streamNumber() == streamNumber, "message not sent on the correct stream");
        check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
        check(info.bytes() == buffer.remaining(), "bytes != remaining");
        check(Util.compare(buffer, Util.SMALL_MESSAGE), "received message not the same as sent message");
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        clientFinishedLatch.countDown();
        try {
            serverFinishedLatch.await(10L, TimeUnit.SECONDS);
        } catch (InterruptedException ie) {
            unexpected(ie);
        }
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                unexpected(e);
            }
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) Association(com.sun.nio.sctp.Association) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 4 with Association

use of com.sun.nio.sctp.Association in project jdk8u_jdk by JetBrains.

the class SocketOptionTests method sctpPrimaryAddr.

/* SCTP_PRIMARY_ADDR */
void sctpPrimaryAddr() throws IOException {
    SocketAddress addrToSet = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
    System.out.println("TESTING SCTP_PRIMARY_ADDR");
    /* create listening channel */
    SctpServerChannel ssc = SctpServerChannel.open().bind(null);
    Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
    if (addrs.isEmpty())
        debug("addrs should not be empty");
    InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();
    /* setup an association implicitly by sending a small message */
    int streamNumber = 0;
    debug("sending to " + serverAddr + " on stream number: " + streamNumber);
    MessageInfo info = MessageInfo.createOutgoing(serverAddr, streamNumber);
    buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
    buffer.flip();
    debug("sending small message: " + buffer);
    SctpMultiChannel smc = SctpMultiChannel.open();
    int sent = smc.send(buffer, info);
    /* Receive the COMM_UP */
    buffer.clear();
    SOTNotificationHandler handler = new SOTNotificationHandler();
    info = smc.receive(buffer, null, handler);
    check(handler.receivedCommUp(), "COMM_UP no received");
    Set<Association> associations = smc.associations();
    check(!associations.isEmpty(), "There should be some associations");
    Association assoc = associations.iterator().next();
    SctpChannel peerChannel = ssc.accept();
    ssc.close();
    Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();
    debug("Peer local Addresses: ");
    for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
        InetSocketAddress addr = (InetSocketAddress) it.next();
        debug("\t" + addr);
        // any of the peer addresses will do!
        addrToSet = addr;
    }
    /* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */
    if ("SunOS".equals(osName)) {
        //smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
        return;
    } else {
        /* Linux */
        SocketAddress primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
        System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
        /* Verify that this is one of the peer addresses */
        boolean found = false;
        // may not have more than one addr
        addrToSet = primaryAddr;
        for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
            InetSocketAddress addr = (InetSocketAddress) it.next();
            if (addr.equals(primaryAddr)) {
                found = true;
            }
            addrToSet = addr;
        }
        check(found, "SCTP_PRIMARY_ADDR returned bogus address!");
        System.out.println("Try SCTP_PRIMARY_ADDR set to: " + addrToSet);
        smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
        System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);
        primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
        System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
        check(addrToSet.equals(primaryAddr), "SCTP_PRIMARY_ADDR not set correctly");
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) InetSocketAddress(java.net.InetSocketAddress) SctpServerChannel(com.sun.nio.sctp.SctpServerChannel) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo) Association(com.sun.nio.sctp.Association) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel)

Example 5 with Association

use of com.sun.nio.sctp.Association in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method send.

/* TODO: Add support for ttl and isComplete to both 121 12M
     *       SCTP_EOR not yet supported on reference platforms
     *       TTL support limited...
     */
@Override
public int send(ByteBuffer buffer, MessageInfo messageInfo) throws IOException {
    if (buffer == null)
        throw new IllegalArgumentException("buffer cannot be null");
    if (messageInfo == null)
        throw new IllegalArgumentException("messageInfo cannot be null");
    synchronized (sendLock) {
        ensureOpen();
        if (!isBound())
            bind(null, 0);
        int n = 0;
        try {
            int assocId = -1;
            SocketAddress address = null;
            begin();
            synchronized (stateLock) {
                if (!isOpen())
                    return 0;
                senderThread = NativeThread.current();
                /* Determine what address or association to send to */
                Association assoc = messageInfo.association();
                InetSocketAddress addr = (InetSocketAddress) messageInfo.address();
                if (assoc != null) {
                    checkAssociation(assoc);
                    checkStreamNumber(assoc, messageInfo.streamNumber());
                    assocId = assoc.associationID();
                    /* have we also got a preferred address */
                    if (addr != null) {
                        if (!assoc.equals(addressMap.get(addr)))
                            throw new IllegalArgumentException("given preferred address is not part of this association");
                        address = addr;
                    }
                } else if (addr != null) {
                    address = addr;
                    Association association = addressMap.get(addr);
                    if (association != null) {
                        checkStreamNumber(association, messageInfo.streamNumber());
                        assocId = association.associationID();
                    } else {
                        /* must be new association */
                        SecurityManager sm = System.getSecurityManager();
                        if (sm != null)
                            sm.checkConnect(addr.getAddress().getHostAddress(), addr.getPort());
                    }
                } else {
                    throw new AssertionError("Both association and address cannot be null");
                }
            }
            do {
                n = send(fdVal, buffer, assocId, address, messageInfo);
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
            return IOStatus.normalize(n);
        } finally {
            senderCleanup();
            end((n > 0) || (n == IOStatus.UNAVAILABLE));
            assert IOStatus.check(n);
        }
    }
}
Also used : Association(com.sun.nio.sctp.Association) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

Association (com.sun.nio.sctp.Association)6 MessageInfo (com.sun.nio.sctp.MessageInfo)3 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 SctpChannel (com.sun.nio.sctp.SctpChannel)2 AbstractNotificationHandler (com.sun.nio.sctp.AbstractNotificationHandler)1 HandlerResult (com.sun.nio.sctp.HandlerResult)1 InvalidStreamException (com.sun.nio.sctp.InvalidStreamException)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 Entry (java.util.Map.Entry)1