Last active
August 29, 2015 14:13
-
-
Save michalkowol/28a751c095a187dfae25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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