public
Created

For...yield weirdness

  • Download Gist
EchoServer.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import akka.actor._
import java.net.InetSocketAddress
 
class EchoServer(port: Int) extends Actor {
 
val state = IO.IterateeRef.Map.async[IO.Handle]()(context.dispatcher)
 
override def preStart {
IOManager(context.system) listen new InetSocketAddress(port)
}
 
def receive = {
case IO.NewClient(server) =>
val socket = server.accept()
state(socket) flatMap (_ => EchoServer.processRequest(socket))
case IO.Read(socket, bytes) =>
state(socket)(IO.Chunk(bytes))
case IO.Closed(socket, cause) =>
state(socket)(IO.EOF(None))
state -= socket
}
}
 
object EchoServer extends App
{
def processRequest(socket: IO.SocketHandle): IO.Iteratee[Unit] =
{
println( "In process request")
try {
for {
bs <- IO take 1
} yield {
println("I'll get here")
throw new Exception("Hey-o!")
println("But not here ... as expected")
}
} catch {
case e: Exception => println("And not here ... wtf?"); IO.Done()
}
}
 
ActorSystem().actorOf(Props(new EchoServer(8080)))
}

What about:

  for {
    bs <- IO take 1
  } yield {
    try {
    println("I'll get here")
    throw new Exception("Hey-o!")
    println("But not here ... as expected")
    } catch {
      case e: Exception => println("And not here ... wtf?"); IO.Done()
    }
  }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.