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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations