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