Skip to content

Instantly share code, notes, and snippets.

@michalkowol
Last active August 29, 2015 14:13
Show Gist options
  • Save michalkowol/28a751c095a187dfae25 to your computer and use it in GitHub Desktop.
Save michalkowol/28a751c095a187dfae25 to your computer and use it in GitHub Desktop.
trait Cameo {
this: Actor =>
def originalSender: ActorRef
def timeout: FiniteDuration = 60.seconds
def sendResponseAndShutdown(response: AnyRef) = {
timeoutMessenger.cancel()
originalSender ! response
context.stop(self)
}
import context.dispatcher
val timeoutMessenger = context.system.scheduler.scheduleOnce(timeout) {
onTimeout
}
def onTimeout: Unit = sendResponseAndShutdown(Failure(new TimeoutException))
def onError: Receive = LoggingReceive {
case f: Failure => sendResponseAndShutdown(f)
case _ => sendResponseAndShutdown(Failure(new MessageNotSupported))
}
}
class PingActor(val originalSender: ActorRef, networkActor: ActorRef) extends Actor with ActorLogging with Cameo {
networkActor ! Ping
def receive: Receive = LoggingReceive {
case Pong => sendResponseAndShutdown(Up)
} orElse onError
override def onTimeout: Unit = sendResponseAndShutdown(Down)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment