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 java.net.{Inet6Address, InetSocketAddress, NetworkInterface, StandardProtocolFamily}
|
|
|
|
|
import java.nio.channels.DatagramChannel
|
2014-09-05 10:17:57 +02:00
|
|
|
import scala.util.Random
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
import akka.actor.{ActorSystem, Props}
|
2014-09-05 10:17:57 +02:00
|
|
|
import akka.io.Udp
|
2014-08-19 14:02:23 +03:00
|
|
|
import akka.testkit.TestKit
|
|
|
|
|
import org.scalatest.{BeforeAndAfter, WordSpecLike}
|
|
|
|
|
|
|
|
|
|
import scala.collection.JavaConversions.enumerationAsScalaIterator
|
|
|
|
|
|
|
|
|
|
class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")) with WordSpecLike with BeforeAndAfter {
|
|
|
|
|
|
|
|
|
|
"listener" should {
|
|
|
|
|
"send message back to sink" in {
|
|
|
|
|
val Some(ipv6Iface) = NetworkInterface.getNetworkInterfaces.collectFirst {
|
|
|
|
|
case iface if iface.getInetAddresses.exists(_.isInstanceOf[Inet6Address]) => iface
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
val randomAddress: String = (Random.nextInt().abs.toLong | (1L << 31)).toHexString.toUpperCase
|
|
|
|
|
val group = randomAddress.grouped(4).mkString("FF02::", ":", "")
|
2014-08-19 14:02:23 +03:00
|
|
|
val port = TestUtils.temporaryUdpIpv6Port(ipv6Iface)
|
2014-09-05 10:17:57 +02:00
|
|
|
val msg = "ohi"
|
|
|
|
|
val sink = testActor
|
|
|
|
|
val iface = ipv6Iface.getName
|
2014-08-19 14:02:23 +03:00
|
|
|
|
|
|
|
|
val listener = system.actorOf(Props(classOf[Listener], iface, group, port, sink))
|
2014-09-05 10:17:57 +02:00
|
|
|
expectMsgType[Udp.Bound]
|
|
|
|
|
val sender = system.actorOf(Props(classOf[Sender], iface, group, port, msg))
|
2014-08-19 14:02:23 +03:00
|
|
|
expectMsg(msg)
|
|
|
|
|
|
|
|
|
|
// unbind
|
|
|
|
|
system.stop(listener)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def afterAll = {
|
|
|
|
|
TestKit.shutdownActorSystem(system)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object TestUtils {
|
|
|
|
|
def temporaryUdpIpv6Port(iface: NetworkInterface) = {
|
|
|
|
|
val serverSocket = DatagramChannel.open(StandardProtocolFamily.INET6).socket()
|
|
|
|
|
serverSocket.bind(new InetSocketAddress(iface.getInetAddresses.nextElement(), 0))
|
|
|
|
|
val port = serverSocket.getLocalPort
|
|
|
|
|
serverSocket.close()
|
|
|
|
|
port
|
|
|
|
|
}
|
|
|
|
|
}
|