Created
December 31, 2008 22:01
-
-
Save gnufied/42131 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import eventfax.core._ | |
import eventfax.protocol._ | |
import scala.actors._ | |
import scala.actors.Actor._ | |
import java.io.IOException | |
import java.util.Date | |
case class PingMessage(sessionId: String,message: String) | |
case class PongMessage(sessionId: String,message: String) | |
// An Actor for processing blocking/cpu consuming requests | |
object Bar extends Actor { | |
def act() = loop { | |
react { | |
case x: PingMessage => { | |
println("Got ping message") | |
reply(PongMessage(x.sessionId,"Hey there")) | |
} | |
case Exit(actor: Actor,reason: String) => { | |
println("Got exit message") | |
exit() | |
} | |
} | |
} | |
} | |
// we can subclass ReactorCore class for finer handling of reactor loop | |
class FooReactor extends ReactorCore { | |
onError { | |
case ex: IOException => println("some io error") | |
case ex: Throwable => println("Some other exception") | |
} | |
onMessage { | |
case x: PongMessage => { | |
val tConnection = activeConnections(x.sessionId).asInstanceOf[Foo] | |
tConnection.sendData(x.message + "\n") | |
} | |
case _ => // | |
} | |
} | |
// class where connection is handled | |
class Foo extends Connection { | |
println("Initializing Foo") | |
val buftok: LineProtocol = new LineProtocol("\\n") | |
postInit { | |
println("Someone connected") | |
} | |
def receive(data: FaxData) = { | |
buftok.extract(data) | |
buftok.foreach((str: String) => { | |
Bar ! PingMessage(bindingStr,"Hello from Foo") | |
sendData("fuck heah") | |
}) | |
} | |
unbind { | |
println("Client Disconnected") | |
} | |
} | |
object App { | |
def main(args : Array[String]) : Unit = { | |
Bar.start() | |
val reactor = new FooReactor() | |
reactor.run { | |
reactor.startServer("localhost",3124,new Foo) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment