在Derek Wyatt的书Akka Concurrency(第13.1节飞机的Telnet服务器)之后,他使用Handle
中名为akka.actor.IO
的内容。但是,我无法在当前Akka API docs中找到它。我无法从以前的akka版本的任何迁移指南中找到它...它是否被移动到某处?或者已弃用?
以下是我需要的代码"转换"目前的akka版本(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
}
}