protected Flow <ByteString, ByteString, NotUsed> Handle(
            Tcp.IncomingConnection connection)
        {
            var graph = Flow.FromGraph(GraphDsl.Create(builder =>
            {
                var welcome =
                    Source.Single(
                        ByteString.FromString($"Welcome port {connection.RemoteAddress}"));

                var logic = builder.Add(Flow.Create <ByteString>()
                                        .Via(Framing.Delimiter(ByteString.FromString("\n"),
                                                               256,
                                                               true))
                                        .Via(
                                            Flow.Create <ByteString>()
                                            .Select(bytes =>
                {
                    var message = Encoding.UTF8.GetString(bytes.ToArray());
                    _logger.Info($"received {message}");
                    return(message);
                })
                                            )
                                        .Via(CloseConnection())
                                        .Via(Flow.Create <string>()
                                             .Select(s =>
                {
                    var message = $"Server hereby responds to message: {s}\n";
                    return(ByteString.FromString(message));
                }))
                                        );

                var concat = builder.Add(Concat.Create <ByteString>());
                var in0    = concat.In(0);

                // welcome.Via(in0);


                return(new FlowShape <ByteString, ByteString>(logic.Inlet,
                                                              concat.Out));
            }));

            return(graph);
        }
Esempio n. 2
0
        public void A_Flow_using_Join_must_allow_for_concat_cycle()
        {
            this.AssertAllStagesStopped(() =>
            {
                var flow = Flow.FromGraph(GraphDsl.Create(TestSource.SourceProbe <string>(this), Sink.First <string>(), Keep.Both, (b, source, sink) =>
                {
                    var concat    = b.Add(Concat.Create <string>(2));
                    var broadcast = b.Add(new Broadcast <string>(2, true));

                    b.From(source).To(concat.In(0));
                    b.From(concat.Out).To(broadcast.In);
                    b.From(broadcast.Out(0)).To(sink);
                    return(new FlowShape <string, string>(concat.In(1), broadcast.Out(1)));
                }));

                var tuple = flow.Join(Flow.Create <string>()).Run(Materializer);
                var probe = tuple.Item1;
                var t     = tuple.Item2;
                probe.SendNext("lonely traveler");
                t.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue();
                t.Result.Should().Be("lonely traveler");
                probe.SendComplete();
            }, Materializer);
        }