Skip to content

Instantly share code, notes, and snippets.

@bpk-t
Last active March 17, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpk-t/5c77dc0472d1ff796d1a to your computer and use it in GitHub Desktop.
Save bpk-t/5c77dc0472d1ff796d1a to your computer and use it in GitHub Desktop.
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import java.util.concurrent.TimeUnit
import scala.util.{Failure, Success, Random}
import scala.util.control.Breaks.{break, breakable}
import scala.concurrent.duration.Duration
import scala.concurrent.Await
object Main extends App {
val system = ActorSystem("test")
val k = system.actorOf(Props[KiyoshiActor])
//val k = system.actorOf(Props[KiyoshiActor2])
//val k = system.actorOf(Props[KiyoshiActor3])
implicit val timeout = Timeout(5000, TimeUnit.MILLISECONDS)
breakable {
while (true) {
val f = k ? (if (Random.nextBoolean()) {
println("ズン")
Zun
} else {
println("ドコ")
Doko
})
Await.ready(f, Duration.Inf)
f.value.get match {
case Success(ret) => {
ret match {
case Kiyoshi => {
println("キ・ヨ・シ!")
break()
}
case NotKiyoshi =>
}
}
case Failure(e) =>
}
}
}
system.terminate()
}
case object Zun
case object Doko
case object Kiyoshi
case object NotKiyoshi
class KiyoshiActor extends Actor {
def zun2: Receive = {
case Zun => {
sender() ! NotKiyoshi
context.become(zun3)
}
case Doko => init()
}
def zun3: Receive = {
case Zun => {
sender() ! NotKiyoshi
context.become(zun4)
}
case Doko => init()
}
def zun4: Receive = {
case Zun => {
sender() ! NotKiyoshi
context.become(doko)
}
case Doko => init()
}
def doko: Receive = {
case Doko => {
sender() ! Kiyoshi
context.become(receive)
}
case Zun => sender() ! NotKiyoshi
}
def receive: Receive = {
case Zun => {
sender() ! NotKiyoshi
context.become(zun2)
}
case Doko => init()
}
def init(): Unit = {
sender() ! NotKiyoshi
context.become(receive)
}
}
class KiyoshiActor2 extends Actor {
def receive: Receive = {
case Zun => {
sender() ! NotKiyoshi
context.become({
case Zun => {
sender() ! NotKiyoshi
context.become({
case Zun => {
sender() ! NotKiyoshi
context.become({
case Zun => {
sender() ! NotKiyoshi
context.become({
case Zun => sender() ! NotKiyoshi
case Doko => {
sender() ! Kiyoshi
context.become(receive)
}
})
}
case Doko => init()
})
}
case Doko => init()
})
}
case Doko => init()
})
}
case Doko => init()
}
def init(): Unit = {
sender() ! NotKiyoshi
context.become(receive)
}
}
class KiyoshiActor3 extends Actor {
def behavior(count: Integer): Receive = {
case Zun => {
sender() ! NotKiyoshi
context.become(behavior(if (count < 4) count + 1 else count))
}
case Doko => {
sender() ! (if (count == 4) Kiyoshi else NotKiyoshi)
context.become(receive)
}
}
def receive: Receive = behavior(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment