Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.milosn.asynckit.core._
- object myFactory extends Function0[Protocol] {
- def apply() = {
- new MyProtocol()
- }
- }
- class MyProtocol extends Protocol {
- override def connectionMade() = {
- transport.write("Greeting! įŽäŊå!\n".getBytes())
- }
- override def dataReceived(data: Array[Byte]) = {
- def _writeSomeMore(result:Any, arg: Any): Deferred = {
- def _success(result:Any, arg: Any): Deferred = {
- throw new Exception("My exception!")
- println("Inner _success() got: " + result)
- return transport.write("And some more!".getBytes())
- }
- def _fail(result:Any, arg: Any) = {
- println("Inner _fail() got: " + result)
- }
- println("_writeSomeMore() got: " + result)
- val d = transport.write("Some more!".getBytes())
- d.addCallbacks(_success _, _fail _)
- return d
- }
- def _success(result:Any, arg: Any) = {
- println("_success() got: " + result)
- }
- def _fail(result:Any, arg: Any) = {
- println("_fail() got: " + result)
- }
- val d = transport.write(data)
- d.addCallbacks(_writeSomeMore _)
- d.addCallbacks(_success _, _fail _)
- /* list */
- def _success_list(result:Any, arg: Any) = {
- println("_success_list() got: " + result)
- }
- def _fail_list(result:Any, arg: Any) = {
- println("_fail_list() got: " + result)
- }
- val d1 = transport.write("1st".getBytes())
- val d2 = transport.write("2nd".getBytes())
- val d3 = transport.write("3rd".getBytes())
- val dl = new DeferredList(List(d1,d2,d3), consume_errors = true)
- dl.addCallbacks(_success_list _, _fail_list _)
- }
- }
- object Main {
- def main(args: Array[String]) = {
- val reactor = Reactor
- reactor.listenTCP(9090, myFactory)
- reactor.listenTCP(9091, myFactory)
- reactor.run()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement