Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.05 KB | None | 0 0
  1. import net.milosn.asynckit.core._
  2.  
  3.  
  4. object myFactory extends Function0[Protocol] {
  5.     def apply() = {
  6.         new MyProtocol()
  7.     }
  8. }
  9.  
  10.  
  11. class MyProtocol extends Protocol {
  12.     override def connectionMade() = {
  13.         transport.write("Greeting! įŽ€äŊ“å­—!\n".getBytes())
  14.     }
  15.  
  16.     override def dataReceived(data: Array[Byte]) = {
  17.         def _writeSomeMore(result:Any, arg: Any): Deferred = {
  18.             def _success(result:Any, arg: Any): Deferred = {
  19.                 throw new Exception("My exception!")
  20.                 println("Inner _success() got: " + result)
  21.                 return transport.write("And some more!".getBytes())
  22.             }
  23.             def _fail(result:Any, arg: Any) = {
  24.                 println("Inner _fail() got: " + result)
  25.             }
  26.  
  27.             println("_writeSomeMore() got: " + result)
  28.             val d = transport.write("Some more!".getBytes())
  29.             d.addCallbacks(_success _, _fail _)
  30.             return d
  31.         }
  32.         def _success(result:Any, arg: Any) = {
  33.             println("_success() got: " + result)
  34.         }
  35.         def _fail(result:Any, arg: Any) = {
  36.             println("_fail() got: " + result)
  37.         }
  38.         val d = transport.write(data)
  39.         d.addCallbacks(_writeSomeMore _)
  40.         d.addCallbacks(_success _, _fail _)
  41.        
  42.         /* list */
  43.         def _success_list(result:Any, arg: Any) = {
  44.             println("_success_list() got: " + result)
  45.         }
  46.         def _fail_list(result:Any, arg: Any) = {
  47.             println("_fail_list() got: " + result)
  48.         }
  49.         val d1 = transport.write("1st".getBytes())
  50.         val d2 = transport.write("2nd".getBytes())
  51.         val d3 = transport.write("3rd".getBytes())
  52.         val dl = new DeferredList(List(d1,d2,d3), consume_errors = true)
  53.         dl.addCallbacks(_success_list _, _fail_list _)
  54.     }
  55. }
  56.  
  57.  
  58. object Main {
  59.     def main(args: Array[String]) = {
  60.         val reactor = Reactor
  61.         reactor.listenTCP(9090, myFactory)
  62.         reactor.listenTCP(9091, myFactory)
  63.         reactor.run()
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement