@@ -25,4 +25,60 @@ object TheDemo extends App {
2525 implicit val timeout = Timeout (3 .seconds)
2626 import sys .dispatcher
2727
28+ val numbers = Source (List (1 , 2 , 3 ))
29+ val strings = Source (List (" a" , " b" , " c" ))
30+
31+ val composite = Source () { implicit b =>
32+ val zip = b.add(Zip [Int , String ]())
33+
34+ numbers ~> zip.in0
35+ strings ~> zip.in1
36+
37+ zip.out
38+ }
39+
40+ val fast = Source (() => Iterator from 0 )
41+
42+ val single = Flow [Int ].withAttributes(OperationAttributes .inputBuffer(1 , 1 ))
43+ val f = Flow [ByteString ].mapAsync(x => after(1 .second, sys.scheduler)(Future .successful(x)))
44+ // .via(single)
45+ // .runForeach(println)
46+
47+ import Protocols ._
48+
49+ val codec = BidiFlow () { implicit b =>
50+ val top = b.add(Flow [Message ].map(toBytes))
51+ val bottom = b.add(Flow [ByteString ].map(fromBytes))
52+
53+ BidiShape (top, bottom)
54+ }
55+
56+ val protocol = codec atop framing
57+
58+ val addr = new InetSocketAddress (" localhost" , 0 )
59+ val server = StreamTcp ().bind(addr).to(Sink .foreach { conn =>
60+ conn.flow.join(protocol.reversed).join(Flow [Message ]
61+ .collect {
62+ case Ping (id) => Pong (id)
63+ }).run()
64+ }).run()
65+ val myaddr = Await .result(server, 1 .second)
66+
67+ val client = StreamTcp ().outgoingConnection(myaddr.localAddress)
68+ val stack = protocol join client
69+
70+ Source (0 to 10 ).map(Ping ).via(stack).runForeach(println)
71+
72+ val route =
73+ pathPrefix(" demo" ) {
74+ getFromBrowseableDirectory(" /Users/rkuhn/comp/demo/http" )
75+ } ~
76+ path(" upload" ) {
77+ extractRequest { req =>
78+ req.entity.dataBytes.via(f).to(Sink .ignore).run()
79+ complete(StatusCodes .OK )
80+ }
81+ }
82+
83+ Http ().bind(" localhost" , 8080 ).runForeach(conn => conn.flow.join(route).run())
2884}
0 commit comments