Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.InetSocketAddress
- import akka.actor._
- import akka.io.Tcp
- import akka.util.ByteString
- import org.joda.time.DateTime
- import scala.concurrent.duration._
- class TcpBenchConnectionHandler(remote: InetSocketAddress, connection: ActorRef) extends Actor with ActorLogging {
- import context.dispatcher
- // We need to know when the connection dies without sending a `Tcp.ConnectionClosed`
- context.watch(connection)
- def sendPackets(sender: ActorRef) : Runnable = return new Runnable {
- def run() = {
- val packet = new Packet(DateTime.now())
- sender ! Tcp.Write(packet.toByteString)
- }
- }
- def receive: Receive = {
- case Tcp.Received(data) =>
- val text = data.utf8String.trim
- text match {
- case "close" => {
- context.stop(self)
- }
- case "start" => {
- val exec = sendPackets(sender)
- context.system.scheduler.schedule(1 seconds, 1 seconds) {
- exec.run()
- }
- exec.run()
- }
- case _ => {
- sender ! Tcp.Write(ByteString.fromString("not valid"))
- }
- }
- case _: Tcp.ConnectionClosed =>
- log.debug("Stopping, because connection for remote address {} closed", remote)
- context.stop(self)
- case Terminated(`connection`) =>
- log.debug("Stopping, because connection for remote address {} died", remote)
- context.stop(self)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement