2014-12-22 16:18:26 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package docs.stream
|
|
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress
|
2015-01-28 18:02:07 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
2014-12-22 16:18:26 +01:00
|
|
|
import akka.actor.ActorSystem
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream._
|
|
|
|
|
import akka.stream.scaladsl._
|
2015-04-09 22:28:16 +02:00
|
|
|
import akka.stream.stage.{ PushStage, SyncDirective, Context }
|
2014-12-22 16:18:26 +01:00
|
|
|
import akka.stream.testkit.AkkaSpec
|
2015-01-28 18:02:07 +01:00
|
|
|
import akka.testkit.TestProbe
|
2014-12-22 16:18:26 +01:00
|
|
|
import akka.util.ByteString
|
|
|
|
|
import cookbook.RecipeParseLines
|
2015-03-26 20:56:49 +02:00
|
|
|
import docs.utils.TestUtils
|
2015-01-28 14:19:50 +01:00
|
|
|
import StreamTcp._
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.Future
|
2014-12-22 16:18:26 +01:00
|
|
|
|
|
|
|
|
class StreamTcpDocSpec extends AkkaSpec {
|
|
|
|
|
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val mat = ActorFlowMaterializer()
|
2014-12-22 16:18:26 +01:00
|
|
|
|
2015-01-28 18:02:07 +01:00
|
|
|
// silence sysout
|
|
|
|
|
def println(s: String) = ()
|
|
|
|
|
|
2015-04-10 07:44:43 +02:00
|
|
|
"simple server connection" in {
|
2015-03-26 20:56:49 +02:00
|
|
|
{
|
|
|
|
|
//#echo-server-simple-bind
|
|
|
|
|
val localhost = new InetSocketAddress("127.0.0.1", 8888)
|
|
|
|
|
//#echo-server-simple-bind
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
val localhost = TestUtils.temporaryServerAddress()
|
|
|
|
|
//#echo-server-simple-bind
|
|
|
|
|
val connections: Source[IncomingConnection, Future[ServerBinding]] =
|
|
|
|
|
StreamTcp().bind(localhost)
|
|
|
|
|
//#echo-server-simple-bind
|
|
|
|
|
|
|
|
|
|
//#echo-server-simple-handle
|
|
|
|
|
connections runForeach { connection =>
|
|
|
|
|
println(s"New connection from: ${connection.remoteAddress}")
|
|
|
|
|
|
|
|
|
|
val echo = Flow[ByteString]
|
|
|
|
|
.transform(() => RecipeParseLines.parseLines("\n", maximumLineBytes = 256))
|
|
|
|
|
.map(_ + "!!!\n")
|
|
|
|
|
.map(ByteString(_))
|
|
|
|
|
|
|
|
|
|
connection.handleWith(echo)
|
|
|
|
|
}
|
|
|
|
|
//#echo-server-simple-handle
|
2014-12-22 16:18:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-10 07:44:43 +02:00
|
|
|
"initial server banner echo server" in {
|
2015-03-26 20:56:49 +02:00
|
|
|
val localhost = TestUtils.temporaryServerAddress()
|
2015-01-28 14:19:50 +01:00
|
|
|
val connections = StreamTcp().bind(localhost)
|
2015-01-28 18:02:07 +01:00
|
|
|
val serverProbe = TestProbe()
|
2014-12-22 16:18:26 +01:00
|
|
|
|
|
|
|
|
//#welcome-banner-chat-server
|
2015-01-28 14:19:50 +01:00
|
|
|
connections runForeach { connection =>
|
2014-12-22 16:18:26 +01:00
|
|
|
|
|
|
|
|
val serverLogic = Flow() { implicit b =>
|
2015-01-28 14:19:50 +01:00
|
|
|
import FlowGraph.Implicits._
|
2014-12-22 16:18:26 +01:00
|
|
|
|
2015-01-28 18:02:07 +01:00
|
|
|
// server logic, parses incoming commands
|
|
|
|
|
val commandParser = new PushStage[String, String] {
|
2015-04-09 22:28:16 +02:00
|
|
|
override def onPush(elem: String, ctx: Context[String]): SyncDirective = {
|
2015-01-28 18:02:07 +01:00
|
|
|
elem match {
|
|
|
|
|
case "BYE" ⇒ ctx.finish()
|
|
|
|
|
case _ ⇒ ctx.push(elem + "!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import connection._
|
|
|
|
|
val welcomeMsg = s"Welcome to: $localAddress, you are: $remoteAddress!\n"
|
2014-12-22 16:18:26 +01:00
|
|
|
|
|
|
|
|
val welcome = Source.single(ByteString(welcomeMsg))
|
2015-01-28 14:19:50 +01:00
|
|
|
val echo = b.add(Flow[ByteString]
|
2014-12-22 16:18:26 +01:00
|
|
|
.transform(() => RecipeParseLines.parseLines("\n", maximumLineBytes = 256))
|
2015-01-28 18:02:07 +01:00
|
|
|
//#welcome-banner-chat-server
|
|
|
|
|
.map { command ⇒ serverProbe.ref ! command; command }
|
|
|
|
|
//#welcome-banner-chat-server
|
|
|
|
|
.transform(() ⇒ commandParser)
|
2015-02-05 15:53:49 +01:00
|
|
|
.map(_ + "\n")
|
2015-01-28 14:19:50 +01:00
|
|
|
.map(ByteString(_)))
|
2014-12-22 16:18:26 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
val concat = b.add(Concat[ByteString]())
|
2014-12-22 16:18:26 +01:00
|
|
|
// first we emit the welcome message,
|
2015-01-28 14:19:50 +01:00
|
|
|
welcome ~> concat.in(0)
|
2014-12-22 16:18:26 +01:00
|
|
|
// then we continue using the echo-logic Flow
|
2015-01-28 14:19:50 +01:00
|
|
|
echo.outlet ~> concat.in(1)
|
2014-12-22 16:18:26 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
(echo.inlet, concat.out)
|
2014-12-22 16:18:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection.handleWith(serverLogic)
|
|
|
|
|
}
|
2015-01-28 18:02:07 +01:00
|
|
|
|
2014-12-22 16:18:26 +01:00
|
|
|
//#welcome-banner-chat-server
|
|
|
|
|
|
2015-01-28 18:02:07 +01:00
|
|
|
val input = new AtomicReference("Hello world" :: "What a lovely day" :: Nil)
|
|
|
|
|
def readLine(prompt: String): String = {
|
|
|
|
|
input.get() match {
|
|
|
|
|
case all @ cmd :: tail if input.compareAndSet(all, tail) ⇒ cmd
|
|
|
|
|
case _ ⇒ "q"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#repl-client
|
2015-01-28 14:19:50 +01:00
|
|
|
val connection = StreamTcp().outgoingConnection(localhost)
|
2015-01-28 18:02:07 +01:00
|
|
|
|
|
|
|
|
val replParser = new PushStage[String, ByteString] {
|
2015-04-09 22:28:16 +02:00
|
|
|
override def onPush(elem: String, ctx: Context[ByteString]): SyncDirective = {
|
2015-01-28 18:02:07 +01:00
|
|
|
elem match {
|
|
|
|
|
case "q" ⇒ ctx.pushAndFinish(ByteString("BYE\n"))
|
|
|
|
|
case _ ⇒ ctx.push(ByteString(s"$elem\n"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val repl = Flow[ByteString]
|
|
|
|
|
.transform(() => RecipeParseLines.parseLines("\n", maximumLineBytes = 256))
|
|
|
|
|
.map(text => println("Server: " + text))
|
|
|
|
|
.map(_ => readLine("> "))
|
|
|
|
|
.transform(() ⇒ replParser)
|
|
|
|
|
|
2015-04-10 07:44:43 +02:00
|
|
|
connection.join(repl).run()
|
2015-01-28 18:02:07 +01:00
|
|
|
//#repl-client
|
|
|
|
|
|
|
|
|
|
serverProbe.expectMsg("Hello world")
|
|
|
|
|
serverProbe.expectMsg("What a lovely day")
|
|
|
|
|
serverProbe.expectMsg("BYE")
|
2014-12-22 16:18:26 +01:00
|
|
|
}
|
|
|
|
|
}
|