2014-08-19 14:02:23 +03:00
|
|
|
/**
|
2015-03-07 22:58:48 -08:00
|
|
|
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
2014-08-19 14:02:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.io;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
2014-09-05 10:17:57 +02:00
|
|
|
import akka.io.Udp;
|
2014-08-19 14:02:23 +03:00
|
|
|
import akka.testkit.JavaTestKit;
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.net.Inet6Address;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.net.NetworkInterface;
|
|
|
|
|
import java.util.Enumeration;
|
2014-09-05 10:17:57 +02:00
|
|
|
import java.util.Random;
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
public class JavaUdpMulticastTest {
|
|
|
|
|
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("JavaUdpMulticastTest");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testUdpMulticast() throws Exception {
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
NetworkInterface ipv6Iface = null;
|
|
|
|
|
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements() && ipv6Iface == null;) {
|
|
|
|
|
NetworkInterface interf = interfaces.nextElement();
|
|
|
|
|
for (Enumeration<InetAddress> addresses = interf.getInetAddresses(); addresses.hasMoreElements() && ipv6Iface == null;) {
|
|
|
|
|
InetAddress address = addresses.nextElement();
|
|
|
|
|
if (address instanceof Inet6Address) {
|
|
|
|
|
ipv6Iface = interf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-05 10:17:57 +02:00
|
|
|
// host assigned link local multicast address http://tools.ietf.org/html/rfc3307#section-4.3.2
|
|
|
|
|
// generate a random 32 bit multicast address with the high order bit set
|
|
|
|
|
final String randomAddress = Long.toHexString(((long) Math.abs(new Random().nextInt())) | (1L << 31)).toUpperCase();
|
|
|
|
|
final StringBuilder groupBuilder = new StringBuilder("FF02:");
|
|
|
|
|
for (int i = 0; i < 2; i += 1) {
|
|
|
|
|
groupBuilder.append(":");
|
|
|
|
|
groupBuilder.append(randomAddress.subSequence(i * 4, i * 4 + 4));
|
|
|
|
|
}
|
|
|
|
|
final String group = groupBuilder.toString();
|
2014-08-19 14:02:23 +03:00
|
|
|
final Integer port = TestUtils.temporaryUdpIpv6Port(ipv6Iface);
|
|
|
|
|
final String msg = "ohi";
|
|
|
|
|
final ActorRef sink = getRef();
|
2014-09-05 10:17:57 +02:00
|
|
|
final String iface = ipv6Iface.getName();
|
2014-08-19 14:02:23 +03:00
|
|
|
|
2014-09-05 10:17:57 +02:00
|
|
|
final ActorRef listener = system.actorOf(Props.create(JavaUdpMulticast.Listener.class, iface, group, port, sink));
|
|
|
|
|
expectMsgClass(Udp.Bound.class);
|
|
|
|
|
final ActorRef sender = system.actorOf(Props.create(JavaUdpMulticast.Sender.class, iface, group, port, msg));
|
2014-08-19 14:02:23 +03:00
|
|
|
expectMsgEquals(msg);
|
|
|
|
|
|
|
|
|
|
// unbind
|
|
|
|
|
system.stop(listener);
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
|
|
|
|
JavaTestKit.shutdownActorSystem(system);
|
|
|
|
|
system = null;
|
|
|
|
|
}
|
|
|
|
|
}
|