Akka IO Extension

The akka part contains an akka-io extension.

Usage

If you’re using sbt just add the following to your build definition:

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.4.12",
  "com.github.mkroli" %% "dns4s-akka" % "0.21.0-SNAPSHOT"
)

Imports

Use the following additional imports to get started:

import com.github.mkroli.dns4s.dsl._
import com.github.mkroli.dns4s.akka._

Server

The following is an excerpt from examples/simple/../DnsServer.scala:

class DnsHandlerActor extends Actor {
  override def receive = {
    case Query(q) ~ Questions(QName(host) ~ TypeA() :: Nil) =>
      sender() ! Response(q) ~ Answers(RRName(host) ~ ARecord("1.2.3.4"))
  }
}

object DnsServer extends App {
  implicit val system = ActorSystem("DnsServer")
  implicit val timeout = Timeout(5 seconds)
  IO(Dns) ? Dns.Bind(system.actorOf(Props[DnsHandlerActor]()), 5354)
}

Client

The following is an excerpt from examples/simple-client/../DnsClient.scala:

object DnsClient extends App {
  implicit val system = ActorSystem("DnsServer")
  implicit val timeout = Timeout(5.seconds)
  import system.dispatcher

  IO(Dns) ? Dns.DnsPacket(Query ~ Questions(QName("google.de")), new java.net.InetSocketAddress("8.8.8.8", 53)) foreach {
    case Response(Answers(answers)) =>
      answers.collect {
        case ARecord(arecord) => println(arecord.address.getHostAddress)
      }
  }
}