Esempio n. 1
0
        static void Main(string[] args)
        {
            var system       = ActorSystem.Create("akka-stream-tcp");
            var materializer = system.Materializer();

            Source <Tcp.IncomingConnection, Task <Tcp.ServerBinding> > connections =
                system.TcpStream().Bind("localhost", 8888);

            connections.RunForeach(connection =>
            {
                Console.WriteLine($"New connection from: {connection.RemoteAddress}");

                var echo = Flow.Create <ByteString>()
                           .Via(Framing.Delimiter(
                                    ByteString.FromString("\n"),
                                    maximumFrameLength: 256,
                                    allowTruncation: true))
                           .Select(c => c.ToString())
                           .SelectAsync(1, Sum)
                           .Select(ByteString.FromString);

                connection.HandleWith(echo, materializer);
            }, materializer);

            Console.ReadLine();
        }
Esempio n. 2
0
        private static Flow <ByteString, ByteString, NotUsed> Handle()
        {
            var eol       = ByteString.FromString("\n");
            var delimiter = Framing.Delimiter(eol,
                                              256,
                                              true);

            var receiver = Flow.Create <ByteString>()
                           .Select(bytes =>
            {
                var message = Encoding.UTF8.GetString(bytes.ToArray());
                _logger.Info($"received {message}");
                return(message);
            });

            var responder = Flow.Create <string>()
                            .Select(s =>
            {
                var message = $"Server hereby responds to message: {s}\n";
                return(ByteString.FromString(message));
            });

            return(Flow.Create <ByteString>()
                   .Via(delimiter.Async())
                   .Via(receiver.Async())
                   .Via(responder.Async()));
        }
Esempio n. 3
0
        private static void Main(
            string[] args)
        {
            var system       = (ExtendedActorSystem)ActorSystem.Create("StreamTcpClient");
            var materializer = system.Materializer();
            var connection   = new TcpExt(system).OutgoingConnection(address,
                                                                     port);

            var flow = 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(message);
                return(message);
            }))
                       .Via(Flow.Create <string>()
                            .Select(s => "Hello World"))
                       .Via(Flow.Create <string>()
                            .Select(s => s += "\n"))
                       .Via(Flow.Create <string>()
                            .Select(ByteString.FromString));

            connection.Join(flow)
            .Run(materializer);

            Console.ReadKey();
        }
Esempio n. 4
0
        private void WaitForDelimiter()
        {
            var source = Source.ActorRef <ByteString>(1000, OverflowStrategy.Fail);

            var sink = Sink.ActorRef <string>(Self, new DelimitedStreamComplete());

            var parseLogic = Flow.Create <ByteString>()
                             .Via(Framing.Delimiter(
                                      _delimiter,
                                      maximumFrameLength: 2000))
                             .Select(bs => bs.ToString(Encoding.ASCII));

            var mat = source.Via(parseLogic).To(sink).Run(Context.System.Materializer());

            source.RunForeach(x => Console.WriteLine(x), Context.System.Materializer());

            Receive <ByteString>(msg =>
            {
                mat.Tell(msg);
            });

            Receive <string>(msg =>
            {
                _logger.Info(msg);
            });
        }
Esempio n. 5
0
        private ISourceQueueWithComplete <ByteString> CreateFramingLogic(ByteString delimiter, int maxFrameLength, IActorRef sampleAssembler)
        {
            var delimitLogic = Flow.Create <ByteString>()
                               .Via(Framing.Delimiter(delimiter, maxFrameLength, false))
                               .Where(bs => bs.Count > 0)
                               .To(Sink.ActorRef <ByteString>(sampleAssembler, new FpgaPluginMessages.StreamComplete()));

            var source = Source.Queue <ByteString>(10000, OverflowStrategy.Backpressure);

            return(source.ToMaterialized(delimitLogic, Keep.Left).Run(Context.System.Materializer()));
        }
Esempio n. 6
0
        public void Delimiter_bytes_based_framing_must_work_with_various_delimiters_and_test_sequences()
        {
            for (var i = 1; i <= 100; i++)
            {
                foreach (var delimiter in DelimiterBytes)
                {
                    var testSequence = CompleteTestSequence(delimiter).ToList();
                    var task         = Source.From(testSequence)
                                       .Select(x => x + delimiter)
                                       .Via(Rechunk)
                                       .Via(Framing.Delimiter(delimiter, 256))
                                       .RunWith(Sink.Seq <ByteString>(), Materializer);

                    task.Wait(TimeSpan.FromDays(3)).Should().BeTrue();
                    task.Result.ShouldAllBeEquivalentTo(testSequence);
                }
            }
        }
Esempio n. 7
0
        public static void Example(IMaterializer materializer)
        {
            Source <Tuple <string, WatcherChangeTypes>, NotUsed> directoryTrigger = null;

            var delimeter = ByteString.FromString("\r\n");
            var getLines  =
                Flow.Create <ByteString>()
                .Via(Framing.Delimiter(delimeter, maximumFrameLength: 256, allowTruncation: true))
                .Select(bytes => bytes.DecodeString());

            var newFilesLines =
                directoryTrigger
                .Where(t => t.Item2 == WatcherChangeTypes.Created)
                .Select(t => t.Item1)
                .MergeMany(breadth: 100, flatten: fileName =>
                           FileIO.FromFile(new FileInfo(fileName))
                           .Via(getLines)
                           .MapMaterializedValue(_ => NotUsed.Instance));
        }
        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. 9
0
        public void Simple_server_connection_must_handle_connection()
        {
            #region echo-server-simple-handle
            Source <Tcp.IncomingConnection, Task <Tcp.ServerBinding> > connections =
                Sys.TcpStream().Bind("127.0.0.1", 8888);

            connections.RunForeach(connection =>
            {
                Console.WriteLine($"New connection from: {connection.RemoteAddress}");

                var echo = Flow.Create <ByteString>()
                           .Via(Framing.Delimiter(
                                    ByteString.FromString("\n"),
                                    maximumFrameLength: 256,
                                    allowTruncation: true))
                           .Select(c => c.ToString())
                           .Select(c => c + "!!!\n")
                           .Select(ByteString.FromString);

                connection.HandleWith(echo, Materializer);
            }, Materializer);
            #endregion
        }
Esempio n. 10
0
 private static Flow <ByteString, string, NotUsed> SimpleLines(string delimiter, int maximumBytes, bool allowTruncation = true)
 {
     return(Framing.Delimiter(ByteString.FromString(delimiter), maximumBytes, allowTruncation)
            .Select(x => x.ToString(Encoding.UTF8)).Named("LineFraming"));
 }
Esempio n. 11
0
        public void Simple_server_must_initial_server_banner_echo_server()
        {
            var connections = Sys.TcpStream().Bind("127.0.0.1", 8888);
            var serverProbe = CreateTestProbe();

            #region welcome-banner-chat-server
            connections.RunForeach(connection =>
            {
                // server logic, parses incoming commands
                var commandParser = Flow.Create <string>().TakeWhile(c => c != "BYE").Select(c => c + "!");

                var welcomeMessage = $"Welcome to: {connection.LocalAddress}, you are: {connection.RemoteAddress}!";
                var welcome        = Source.Single(welcomeMessage);

                var serverLogic = Flow.Create <ByteString>()
                                  .Via(Framing.Delimiter(
                                           ByteString.FromString("\n"),
                                           maximumFrameLength: 256,
                                           allowTruncation: true))
                                  .Select(c => c.ToString())
                                  .Select(command =>
                {
                    serverProbe.Tell(command);
                    return(command);
                })
                                  .Via(commandParser)
                                  .Merge(welcome)
                                  .Select(c => c + "\n")
                                  .Select(ByteString.FromString);

                connection.HandleWith(serverLogic, Materializer);
            }, Materializer);
            #endregion

            var input = new ConcurrentQueue <string>(new[] { "Hello world", "What a lovely day" });

            string ReadLine(string prompt) => input.TryDequeue(out var cmd) ? cmd : "q";

            {
                var connection = Sys.TcpStream().OutgoingConnection("127.0.0.1", 8888);
            }

            {
                #region repl-client
                var connection = Sys.TcpStream().OutgoingConnection("127.0.0.1", 8888);

                var replParser = Flow.Create <string>().TakeWhile(c => c != "q")
                                 .Concat(Source.Single("BYE"))
                                 .Select(elem => ByteString.FromString($"{elem}\n"));

                var repl = Flow.Create <ByteString>()
                           .Via(Framing.Delimiter(
                                    ByteString.FromString("\n"),
                                    maximumFrameLength: 256,
                                    allowTruncation: true))
                           .Select(c => c.ToString())
                           .Select(text =>
                {
                    Output.WriteLine($"Server: {text}");
                    return(text);
                })
                           .Select(text => ReadLine("> "))
                           .Via(replParser);

                connection.Join(repl).Run(Materializer);
                #endregion
            }

            serverProbe.ExpectMsg("Hello world", TimeSpan.FromSeconds(20));
            serverProbe.ExpectMsg("What a lovely day");
            serverProbe.ExpectMsg("BYE");
        }