Merge pull request #19802 from ktoso/wip-framing-made-proper-ktoso

!str split Framing into javadsl and scaladsl
This commit is contained in:
Konrad Malawski 2016-02-16 20:53:34 +01:00
commit 650e94ba30
15 changed files with 218 additions and 26 deletions

View file

@ -7,7 +7,7 @@ import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentLinkedQueue;
import akka.NotUsed;
import akka.stream.io.Framing;
import akka.stream.javadsl.Framing;
import docs.AbstractJavaTest;
import docs.stream.SilenceSystemOut;
import java.net.InetSocketAddress;
@ -79,7 +79,7 @@ public class StreamTcpDocTest extends AbstractJavaTest {
System.out.println("New connection from: " + connection.remoteAddress());
final Flow<ByteString, ByteString, NotUsed> echo = Flow.of(ByteString.class)
.via(Framing.delimiter(ByteString.fromString("\n"), 256, false))
.via(Framing.delimiter(ByteString.fromString("\n"), 256, FramingTruncation.DISALLOW))
.map(ByteString::utf8String)
.map(s -> s + "!!!\n")
.map(ByteString::fromString);
@ -113,7 +113,7 @@ public class StreamTcpDocTest extends AbstractJavaTest {
final Source<String, NotUsed> welcome = Source.single(welcomeMsg);
final Flow<ByteString, ByteString, NotUsed> serverLogic =
Flow.of(ByteString.class)
.via(Framing.delimiter(ByteString.fromString("\n"), 256, false))
.via(Framing.delimiter(ByteString.fromString("\n"), 256, FramingTruncation.DISALLOW))
.map(ByteString::utf8String)
//#welcome-banner-chat-server
.map(command -> {
@ -149,7 +149,7 @@ public class StreamTcpDocTest extends AbstractJavaTest {
.map(elem -> ByteString.fromString(elem + "\n"));
final Flow<ByteString, ByteString, NotUsed> repl = Flow.of(ByteString.class)
.via(Framing.delimiter(ByteString.fromString("\n"), 256, false))
.via(Framing.delimiter(ByteString.fromString("\n"), 256, FramingTruncation.DISALLOW))
.map(ByteString::utf8String)
.map(text -> {System.out.println("Server: " + text); return "next";})
.map(elem -> readLine("> "))

View file

@ -7,7 +7,8 @@ import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.io.Framing;
import akka.stream.javadsl.Framing;
import akka.stream.javadsl.FramingTruncation;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import akka.testkit.JavaTestKit;
@ -48,7 +49,7 @@ public class RecipeParseLines extends RecipeTest {
//#parse-lines
final Source<String, NotUsed> lines = rawData
.via(Framing.delimiter(ByteString.fromString("\r\n"), 100, true))
.via(Framing.delimiter(ByteString.fromString("\r\n"), 100, FramingTruncation.ALLOW))
.map(b -> b.utf8String());
//#parse-lines
lines.limit(10).runWith(Sink.seq(), mat).toCompletableFuture().get(1, TimeUnit.SECONDS);

View file

@ -185,3 +185,39 @@ The old behaviour can be achieved by explicitly draining the entity:
response.entity().getDataBytes().runWith(Sink.ignore())
Websocket now consistently named WebSocket
------------------------------------------
Previously we had a mix of methods and classes called ``websocket`` or ``Websocket``, which was in contradiction with
how the word is spelled in the spec and some other places of Akka HTTP.
Methods and classes using the word WebSocket now consistently use it as ``WebSocket``, so updating is as simple as
find-and-replacing the lower-case ``s`` to an upper-case ``S`` wherever the word WebSocket appeared.
Java DSL for Http binding and connections changed
-------------------------------------------------
In order to minimise the number of needed overloads for each method defined on the ``Http`` extension
a new mini-DSL has been introduced for connecting to hosts given a hostname, port and optional ``ConnectionContext``.
The availability of the connection context (if it's set to ``HttpsConnectionContext``) makes the server be bound
as an HTTPS server, and for outgoing connections those settings are used instead of the default ones if provided.
Was::
http.cachedHostConnectionPool(toHost("akka.io"), materializer());
http.cachedHostConnectionPool("akka.io", 80, httpsConnectionContext, materializer()); // does not work anymore
Replace with::
http.cachedHostConnectionPool(toHostHttps("akka.io", 8081), materializer());
http.cachedHostConnectionPool(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext), materializer());
Framing moved to akka.stream.[javadsl/scaladsl]
-----------------------------------------------
The ``Framing`` object which can be used to chunk up ``ByteString`` streams into
framing dependent chunks (such as lines) has moved to ``akka.stream.scaladsl.Framing``,
and has gotten a Java DSL equivalent type in ``akka.stream.javadsl.Framing``.

View file

@ -24,7 +24,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 ``delimiter``
helper Flow from ``akka.stream.io.Framing`` to chunk the inputs up into actual lines of text. The last boolean
helper Flow from ``akka.stream.javadsl.Framing`` 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:

View file

@ -8,7 +8,7 @@ import java.io.File
import akka.Done
import akka.actor.ActorRef
import akka.http.scaladsl.model.Multipart.FormData.BodyPart
import akka.stream.io.{ Framing }
import akka.stream.scaladsl.Framing
import akka.stream.scaladsl._
import akka.http.scaladsl.model.Multipart
import akka.util.ByteString

View file

@ -4,7 +4,7 @@
package docs.http.scaladsl.server.directives
import akka.http.scaladsl.model._
import akka.stream.io.Framing
import akka.stream.scaladsl.Framing
import akka.util.ByteString
import docs.http.scaladsl.server.RoutingSpec
import scala.concurrent.Future

View file

@ -21,7 +21,7 @@ class RecipeParseLines extends RecipeSpec {
ByteString("\r\n\r\n")))
//#parse-lines
import akka.stream.io.Framing
import akka.stream.scaladsl.Framing
val linesStream = rawData.via(Framing.delimiter(
ByteString("\r\n"), maximumFrameLength = 100, allowTruncation = true))
.map(_.utf8String)

View file

@ -39,7 +39,7 @@ class StreamTcpDocSpec extends AkkaSpec {
{
val (host, port) = TestUtils.temporaryServerHostnameAndPort()
//#echo-server-simple-handle
import akka.stream.io.Framing
import akka.stream.scaladsl.Framing
val connections: Source[IncomingConnection, Future[ServerBinding]] =
Tcp().bind(host, port)
@ -66,7 +66,7 @@ class StreamTcpDocSpec extends AkkaSpec {
val connections = Tcp().bind(localhost.getHostName, localhost.getPort) // TODO getHostString in Java7
val serverProbe = TestProbe()
import akka.stream.io.Framing
import akka.stream.scaladsl.Framing
//#welcome-banner-chat-server
connections.runForeach { connection =>
@ -97,7 +97,7 @@ class StreamTcpDocSpec extends AkkaSpec {
}
//#welcome-banner-chat-server
import akka.stream.io.Framing
import akka.stream.scaladsl.Framing
val input = new AtomicReference("Hello world" :: "What a lovely day" :: Nil)
def readLine(prompt: String): String = {

View file

@ -189,3 +189,10 @@ Replace with::
http.cachedHostConnectionPool(toHostHttps("akka.io", 8081), materializer());
http.cachedHostConnectionPool(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext), materializer());
Framing moved to akka.stream.[javadsl/scaladsl]
-----------------------------------------------
The ``Framing`` object which can be used to chunk up ``ByteString`` streams into
framing dependent chunks (such as lines) has moved to ``akka.stream.scaladsl.Framing``,
and has gotten a Java DSL equivalent type in ``akka.stream.javadsl.Framing``.