Search in sources :

Example 1 with MembershipKey

use of java.nio.channels.MembershipKey in project netty by netty.

the class NioDatagramChannel method leaveGroup.

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
@Override
public ChannelFuture leaveGroup(InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, ChannelPromise promise) {
    checkJavaVersion();
    ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
    ObjectUtil.checkNotNull(networkInterface, "networkInterface");
    synchronized (this) {
        if (memberships != null) {
            List<MembershipKey> keys = memberships.get(multicastAddress);
            if (keys != null) {
                Iterator<MembershipKey> keyIt = keys.iterator();
                while (keyIt.hasNext()) {
                    MembershipKey key = keyIt.next();
                    if (networkInterface.equals(key.networkInterface())) {
                        if (source == null && key.sourceAddress() == null || source != null && source.equals(key.sourceAddress())) {
                            key.drop();
                            keyIt.remove();
                        }
                    }
                }
                if (keys.isEmpty()) {
                    memberships.remove(multicastAddress);
                }
            }
        }
    }
    promise.setSuccess();
    return promise;
}
Also used : MembershipKey(java.nio.channels.MembershipKey) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

Example 2 with MembershipKey

use of java.nio.channels.MembershipKey in project netty by netty.

the class NioDatagramChannel method block.

/**
 * Block the given sourceToBlock address for the given multicastAddress on the given networkInterface
 */
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
@Override
public ChannelFuture block(InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress sourceToBlock, ChannelPromise promise) {
    checkJavaVersion();
    ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
    ObjectUtil.checkNotNull(sourceToBlock, "sourceToBlock");
    ObjectUtil.checkNotNull(networkInterface, "networkInterface");
    synchronized (this) {
        if (memberships != null) {
            List<MembershipKey> keys = memberships.get(multicastAddress);
            for (MembershipKey key : keys) {
                if (networkInterface.equals(key.networkInterface())) {
                    try {
                        key.block(sourceToBlock);
                    } catch (IOException e) {
                        promise.setFailure(e);
                    }
                }
            }
        }
    }
    promise.setSuccess();
    return promise;
}
Also used : MembershipKey(java.nio.channels.MembershipKey) IOException(java.io.IOException) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

Example 3 with MembershipKey

use of java.nio.channels.MembershipKey in project j2objc by google.

the class DatagramChannelMulticastTest method test_block_mixedAddressTypes_IPv4.

public void test_block_mixedAddressTypes_IPv4() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
    try {
        membershipKey.block(UNICAST_IPv6_1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey)

Example 4 with MembershipKey

use of java.nio.channels.MembershipKey in project j2objc by google.

the class DatagramChannelMulticastTest method test_unblock_null.

public void test_unblock_null() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
    membershipKey.block(UNICAST_IPv4_1);
    try {
        membershipKey.unblock(null);
        fail();
    } catch (IllegalStateException expected) {
    // Either of these exceptions are fine
    } catch (NullPointerException expected) {
    // Either of these exception are fine
    }
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey)

Example 5 with MembershipKey

use of java.nio.channels.MembershipKey in project j2objc by google.

the class DatagramChannelMulticastTest method check_block_filtersAsExpected.

private void check_block_filtersAsExpected(InetAddress senderBindAddress, InetAddress receiverBindAddress, InetAddress groupAddress, NetworkInterface networkInterface) throws Exception {
    DatagramChannel sendingChannel = DatagramChannel.open();
    // In order to block a sender the sender's address must be known. The sendingChannel is
    // explicitly bound to a known, non-loopback address.
    sendingChannel.bind(new InetSocketAddress(senderBindAddress, 0));
    InetSocketAddress sendingAddress = (InetSocketAddress) sendingChannel.getLocalAddress();
    DatagramChannel receivingChannel = DatagramChannel.open();
    configureChannelForReceiving(receivingChannel);
    receivingChannel.bind(new InetSocketAddress(receiverBindAddress, 0));
    InetSocketAddress localReceivingAddress = (InetSocketAddress) receivingChannel.getLocalAddress();
    InetSocketAddress groupSocketAddress = new InetSocketAddress(groupAddress, localReceivingAddress.getPort());
    MembershipKey membershipKey = receivingChannel.join(groupSocketAddress.getAddress(), networkInterface);
    ByteBuffer receiveBuffer = ByteBuffer.allocate(10);
    BindableChannel channel = new BindableChannel(sendingChannel, networkInterface);
    // Send a message. It should be received.
    String msg1 = "Hello1";
    channel.sendMulticastMessage(msg1, groupSocketAddress);
    Net.poll(receivingChannel.socket().getFileDescriptor$(), POLLIN, 1000);
    InetSocketAddress sourceAddress1 = (InetSocketAddress) receiveExpectedDatagram(receivingChannel, receiveBuffer);
    assertEquals(sourceAddress1, sendingAddress);
    assertEquals(msg1, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
    // Now block the sender
    membershipKey.block(sendingAddress.getAddress());
    // Send a message. It should be filtered.
    String msg2 = "Hello2";
    channel.sendMulticastMessage(msg2, groupSocketAddress);
    try {
        Net.poll(receivingChannel.socket().getFileDescriptor$(), POLLIN, 1000);
        fail();
    } catch (SocketTimeoutException expected) {
    }
    receiveBuffer.position(0);
    checkNoDatagramReceived(receivingChannel);
    // Now unblock the sender
    membershipKey.unblock(sendingAddress.getAddress());
    // Send a message. It should be received.
    String msg3 = "Hello3";
    channel.sendMulticastMessage(msg3, groupSocketAddress);
    Net.poll(receivingChannel.socket().getFileDescriptor$(), POLLIN, 1000);
    receiveBuffer.position(0);
    InetSocketAddress sourceAddress3 = (InetSocketAddress) receiveExpectedDatagram(receivingChannel, receiveBuffer);
    assertEquals(sourceAddress3, sendingAddress);
    assertEquals(msg3, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
    sendingChannel.close();
    receivingChannel.close();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey) ByteBuffer(java.nio.ByteBuffer)

Aggregations

MembershipKey (java.nio.channels.MembershipKey)25 DatagramChannel (java.nio.channels.DatagramChannel)22 SuppressJava6Requirement (io.netty.util.internal.SuppressJava6Requirement)3 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 ByteBuffer (java.nio.ByteBuffer)2 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1