Search in sources :

Example 66 with DatagramChannel

use of java.nio.channels.DatagramChannel 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)

Example 67 with DatagramChannel

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

the class DatagramChannelMulticastTest method test_block_cannotBlockWithSourceSpecificMembership.

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

Example 68 with DatagramChannel

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

the class DatagramChannelMulticastTest method test_joinSourceSpecific_nullGroupAddress.

public void test_joinSourceSpecific_nullGroupAddress() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    try {
        dc.join(null, ipv4NetworkInterface, UNICAST_IPv4_1);
        fail();
    } catch (NullPointerException expected) {
    }
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel)

Example 69 with DatagramChannel

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

the class DatagramChannelMulticastTest method test_joinAnySource_nullGroupAddress.

public void test_joinAnySource_nullGroupAddress() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    try {
        dc.join(null, ipv4NetworkInterface);
        fail();
    } catch (NullPointerException expected) {
    }
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel)

Example 70 with DatagramChannel

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

the class DatagramChannelMulticastTest method test_joinAnySource_blockLimit.

public void test_joinAnySource_blockLimit() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    MembershipKey key = dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
    for (byte i = 1; i <= 15; i++) {
        InetAddress sourceAddress = Inet4Address.getByName("10.0.0." + i);
        try {
            key.block(sourceAddress);
        } catch (SocketException e) {
            // There is a limit, that's ok according to the RI docs. For this test a lower bound of 10
            // is used, which appears to be the default linux limit.
            // See /proc/sys/net/ipv4/igmp_max_msf
            assertTrue(i > 10);
            break;
        }
    }
    dc.close();
}
Also used : SocketException(java.net.SocketException) DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey) InetAddress(java.net.InetAddress)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)211 InetSocketAddress (java.net.InetSocketAddress)91 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)57 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 MembershipKey (java.nio.channels.MembershipKey)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5