Remove simple line parsing, exposing explicit delimiter stage instead

This commit is contained in:
Endre Sándor Varga 2015-06-23 16:55:27 +02:00
parent 79e24c5976
commit 8702f09f10
7 changed files with 21 additions and 33 deletions

View file

@ -22,7 +22,8 @@ class RecipeParseLines extends RecipeSpec {
//#parse-lines
import akka.stream.io.Framing
val linesStream = rawData.via(Framing.lines("\r\n", maximumLineBytes = 100))
val linesStream = rawData.via(
Framing.delimiter(ByteString("\r\n"), maximumFrameLength = 100, allowTruncation = true)).map(_.utf8String)
//#parse-lines
Await.result(linesStream.grouped(10).runWith(Sink.head), 3.seconds) should be(List(

View file

@ -47,7 +47,8 @@ class StreamTcpDocSpec extends AkkaSpec {
println(s"New connection from: ${connection.remoteAddress}")
val echo = Flow[ByteString]
.via(Framing.lines("\n", maximumLineBytes = 256, allowTruncation = false))
.via(Framing.delimiter(ByteString("\n"), maximumFrameLength = 256, allowTruncation = true))
.map(_.utf8String)
.map(_ + "!!!\n")
.map(ByteString(_))
@ -85,7 +86,8 @@ class StreamTcpDocSpec extends AkkaSpec {
val welcome = Source.single(ByteString(welcomeMsg))
val echo = b.add(Flow[ByteString]
.via(Framing.lines("\n", maximumLineBytes = 256, allowTruncation = false))
.via(Framing.delimiter(ByteString("\n"), maximumFrameLength = 256, allowTruncation = true))
.map(_.utf8String)
//#welcome-banner-chat-server
.map { command serverProbe.ref ! command; command }
//#welcome-banner-chat-server
@ -136,7 +138,8 @@ class StreamTcpDocSpec extends AkkaSpec {
}
val repl = Flow[ByteString]
.via(Framing.lines("\n", maximumLineBytes = 256, allowTruncation = false))
.via(Framing.delimiter(ByteString("\n"), maximumFrameLength = 256, allowTruncation = true))
.map(_.utf8String)
.map(text => println("Server: " + text))
.map(_ => readLine("> "))
.transform(() replParser)

View file

@ -96,8 +96,7 @@ Parsing lines from a stream of ByteStrings
characters (or, alternatively, containing binary frames delimited by a special delimiter byte sequence) which
needs to be parsed.
The :class:`Framing` helper object contains a convenience method to parse messages from a stream of ``ByteStrings``
and in particular it has basic support for parsing text lines:
The :class:`Framing` helper object contains a convenience method to parse messages from a stream of ``ByteStrings``:
.. includecode:: code/docs/stream/cookbook/RecipeParseLines.scala#parse-lines

View file

@ -23,7 +23,7 @@ which will emit an :class:`IncomingConnection` element for each new connection t
Next, we simply handle *each* incoming connection using a :class:`Flow` which will be used as the processing stage
to handle and emit ByteStrings from and to the TCP Socket. Since one :class:`ByteString` does not have to necessarily
correspond to exactly one line of text (the client might be sending the line in chunks) we use the ``Framing.lines``
correspond to exactly one line of text (the client might be sending the line in chunks) we use the ``Framing.delmiter``
helper Flow to chunk the inputs up into actual lines of text. The last boolean
argument indicates that we require an explicit line ending even for the last message before the connection is closed.
In this example we simply add exclamation marks to each incoming text message and push it through the flow: