문제

Following the book Akka Concurrency by Derek Wyatt (section 13.1 The plane's Telnet server), he uses something called Handle from akka.actor.IO. However, I can't find it in the current Akka API docs. I can't find it in any of the migration guides either from previous akka versions... Did it get moved somewhere? Or deprecated?

Here is the code in question that I need to "convert" to current akka version (2.3):

import akka.actor.{Actor, ActorRef, IO,
                     IOManager, ActorLogging, Props}
  import akka.util.ByteString
  import scala.collection.mutable.Map
  import akka.pattern.ask
  import akka.util.Timeout
  import scala.concurrent.duration._
  import scala.concurrent.ExecutionContext.Implicits.global
  import scala.util.{Success, Failure}

  class TelnetServer(plane: ActorRef) extends Actor
                                         with ActorLogging {
    import TelnetServer._

    // The 'subservers' stores the map of Actors-to-clients that we
    // need in order to route future communications
    val subservers = Map.empty[IO.Handle, ActorRef]

    // Opens the server's socket and starts listening for incoming
    // stuff
    val serverSocket =
      IOManager(context.system).listen("0.0.0.0", 31733)

    def receive = {
      // This message is sent by IO when our server officially
      // starts
      case IO.Listening(server, address) =>
        log.info("Telnet Server listeninig on port {}", address)

      // When a client connects (e.g. telnet) we get this
      // message
      case IO.NewClient(server) =>
        log.info("New incoming client connection on server")
        // You must accept the socket, which can pass to the sub
        // server as well as used as a 'key' into our map to know
        // where future communications come from
        val socket = server.accept()
        socket.write(ByteString(welcome))
        subservers +=
          (socket ->
           context.actorOf(Props(new SubServer(socket, plane))))

      // Every time we get a message it comes in as a ByteString on
      // this message
      case IO.Read(socket, bytes) =>
        // Convert from ByteString to ascii (helper from companion)
        val cmd = ascii(bytes)
        // Send the message to the subserver, looked up by socket
        subservers(socket) ! NewMessage(cmd)

      // Client closed connection, kill the sub server
      case IO.Closed(socket, cause) =>
        context.stop(subservers(socket))
        subservers -= socket
    }
  }
도움이 되었습니까?

해결책

This book use akka 2.1.x, and the io module is still experimental then, so many apis have changed or removed since then. See the migration guide

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top