parent
67a9e62254
commit
0953e7aee3
41 changed files with 156 additions and 368 deletions
62
akka-docs/rst/scala/code/docs/io/ScalaUdpMulticast.scala
Normal file
62
akka-docs/rst/scala/code/docs/io/ScalaUdpMulticast.scala
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io
|
||||
|
||||
import java.net.{ InetAddress, InetSocketAddress, NetworkInterface, StandardProtocolFamily }
|
||||
import java.net.DatagramSocket
|
||||
import java.nio.channels.DatagramChannel
|
||||
|
||||
import akka.actor.{ Actor, ActorLogging, ActorRef }
|
||||
import akka.io.Inet.{ DatagramChannelCreator, SocketOption, SocketOptionV2 }
|
||||
import akka.io.{ IO, Udp }
|
||||
import akka.util.ByteString
|
||||
|
||||
//#inet6-protocol-family
|
||||
final case class Inet6ProtocolFamily() extends DatagramChannelCreator {
|
||||
override def create() =
|
||||
DatagramChannel.open(StandardProtocolFamily.INET6)
|
||||
}
|
||||
//#inet6-protocol-family
|
||||
|
||||
//#multicast-group
|
||||
final case class MulticastGroup(address: String, interface: String) extends SocketOptionV2 {
|
||||
override def afterBind(s: DatagramSocket) {
|
||||
val group = InetAddress.getByName(address)
|
||||
val networkInterface = NetworkInterface.getByName(interface)
|
||||
s.getChannel.join(group, networkInterface)
|
||||
}
|
||||
}
|
||||
//#multicast-group
|
||||
|
||||
class Listener(iface: String, group: String, port: Int, sink: ActorRef) extends Actor with ActorLogging {
|
||||
//#bind
|
||||
import context.system
|
||||
val opts = List(Inet6ProtocolFamily(), MulticastGroup(group, iface))
|
||||
IO(Udp) ! Udp.Bind(self, new InetSocketAddress(port), opts)
|
||||
//#bind
|
||||
|
||||
def receive = {
|
||||
case b @ Udp.Bound(to) =>
|
||||
log.info("Bound to {}", to)
|
||||
sink ! (b)
|
||||
case Udp.Received(data, remote) =>
|
||||
val msg = data.decodeString("utf-8")
|
||||
log.info("Received '{}' from {}", msg, remote)
|
||||
sink ! msg
|
||||
}
|
||||
}
|
||||
|
||||
class Sender(iface: String, group: String, port: Int, msg: String) extends Actor with ActorLogging {
|
||||
import context.system
|
||||
IO(Udp) ! Udp.SimpleSender(List(Inet6ProtocolFamily()))
|
||||
|
||||
def receive = {
|
||||
case Udp.SimpleSenderReady => {
|
||||
val remote = new InetSocketAddress(s"$group%$iface", port)
|
||||
log.info("Sending message to {}", remote)
|
||||
sender() ! Udp.Send(ByteString(msg), remote)
|
||||
}
|
||||
}
|
||||
}
|
||||
59
akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala
Normal file
59
akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io
|
||||
|
||||
import java.net.{ Inet6Address, InetSocketAddress, NetworkInterface, StandardProtocolFamily }
|
||||
import java.nio.channels.DatagramChannel
|
||||
import scala.util.Random
|
||||
|
||||
import akka.actor.{ ActorSystem, Props }
|
||||
import akka.io.Udp
|
||||
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
|
||||
}
|
||||
|
||||
// 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::", ":", "")
|
||||
val port = TestUtils.temporaryUdpIpv6Port(ipv6Iface)
|
||||
val msg = "ohi"
|
||||
val sink = testActor
|
||||
val iface = ipv6Iface.getName
|
||||
|
||||
val listener = system.actorOf(Props(classOf[Listener], iface, group, port, sink))
|
||||
expectMsgType[Udp.Bound]
|
||||
val sender = system.actorOf(Props(classOf[Sender], iface, group, port, msg))
|
||||
expectMsg(msg)
|
||||
|
||||
// unbind
|
||||
system.stop(listener)
|
||||
}
|
||||
}
|
||||
|
||||
def afterAll(): Unit = {
|
||||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue