Search in sources :

Example 21 with MembershipKey

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

the class DatagramChannelMulticastTest method test_close_invalidatesMembershipKey.

public void test_close_invalidatesMembershipKey() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
    assertTrue(membershipKey.isValid());
    dc.close();
    assertFalse(membershipKey.isValid());
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey)

Example 22 with MembershipKey

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

the class DatagramChannelMulticastTest method check_joinSourceSpecific.

/**
 * Checks that the source-specific membership is correctly source-filtering.
 *
 * @param senderBindAddress the address to bind the sender socket to
 * @param receiverBindAddress the address to bind the receiver socket to
 * @param groupAddress the group address to join
 * @param badSenderAddress a unicast address to join to perform a negative test
 * @param networkInterface The network interface on which to join the multicast group
 */
private void check_joinSourceSpecific(InetAddress senderBindAddress, InetAddress receiverBindAddress, InetAddress groupAddress, InetAddress badSenderAddress, NetworkInterface networkInterface) throws Exception {
    DatagramChannel sendingChannel = DatagramChannel.open();
    // In order to be source-specific 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();
    receivingChannel.bind(new InetSocketAddress(receiverBindAddress, 0));
    configureChannelForReceiving(receivingChannel);
    InetSocketAddress localReceivingAddress = (InetSocketAddress) receivingChannel.getLocalAddress();
    InetSocketAddress groupSocketAddress = new InetSocketAddress(groupAddress, localReceivingAddress.getPort());
    MembershipKey membershipKey1 = receivingChannel.join(groupSocketAddress.getAddress(), networkInterface, senderBindAddress);
    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);
    InetSocketAddress sourceAddress1 = (InetSocketAddress) receiveExpectedDatagram(receivingChannel, receiveBuffer);
    assertEquals(sourceAddress1, sendingAddress);
    assertEquals(msg1, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
    membershipKey1.drop();
    receivingChannel.join(groupSocketAddress.getAddress(), networkInterface, badSenderAddress);
    // Send a message. It should not be received.
    String msg2 = "Hello2";
    channel.sendMulticastMessage(msg2, groupSocketAddress);
    checkNoDatagramReceived(receivingChannel);
    receivingChannel.close();
    sendingChannel.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey) ByteBuffer(java.nio.ByteBuffer)

Example 23 with MembershipKey

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

the class DatagramChannelMulticastTest method check_dropSourceSpecific_twice.

private void check_dropSourceSpecific_twice(InetAddress groupAddress, InetAddress sourceAddress, NetworkInterface networkInterface) throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    MembershipKey membershipKey = dc.join(groupAddress, networkInterface, sourceAddress);
    assertTrue(membershipKey.isValid());
    membershipKey.drop();
    assertFalse(membershipKey.isValid());
    // Try to leave a group we are no longer a member of - should do nothing.
    membershipKey.drop();
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey)

Example 24 with MembershipKey

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

the class DatagramChannelMulticastTest method test_block_multipleBlocksIgnored.

public void test_block_multipleBlocksIgnored() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    MembershipKey membershipKey = dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
    membershipKey.block(UNICAST_IPv4_1);
    MembershipKey membershipKey2 = membershipKey.block(UNICAST_IPv4_1);
    assertSame(membershipKey2, membershipKey);
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey)

Example 25 with MembershipKey

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

the class NioDatagramChannel method joinGroup.

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
@Override
public ChannelFuture joinGroup(InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, ChannelPromise promise) {
    checkJavaVersion();
    ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
    ObjectUtil.checkNotNull(networkInterface, "networkInterface");
    try {
        MembershipKey key;
        if (source == null) {
            key = javaChannel().join(multicastAddress, networkInterface);
        } else {
            key = javaChannel().join(multicastAddress, networkInterface, source);
        }
        synchronized (this) {
            List<MembershipKey> keys = null;
            if (memberships == null) {
                memberships = new HashMap<InetAddress, List<MembershipKey>>();
            } else {
                keys = memberships.get(multicastAddress);
            }
            if (keys == null) {
                keys = new ArrayList<MembershipKey>();
                memberships.put(multicastAddress, keys);
            }
            keys.add(key);
        }
        promise.setSuccess();
    } catch (Throwable e) {
        promise.setFailure(e);
    }
    return promise;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) MembershipKey(java.nio.channels.MembershipKey) InetAddress(java.net.InetAddress) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

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