Exemple #1
0
        public async Task StartAsync()
        {
            _startTime.Start();
            long pagingTokenLong = await GetCurrentCursorFromDatabase("operation");

            string pagingToken = pagingTokenLong.ToString();


            _logger.LogDebug($"Starting page token is {pagingToken}");

            _operationsRequestBuilder = _server.Operations.Cursor(pagingToken).Limit(200);
            _eventSource = _operationsRequestBuilder.Stream((sender, response) =>
            {
                _operationsToHandleQueue.Enqueue(response);

                if (_sendQueueInfoMessageTime == null || DateTime.Now >= _sendQueueInfoMessageTime)
                {
                    _sendQueueInfoMessageTime = DateTime.Now.AddMinutes(1);

                    _logger.LogInformation($"Total operations parsed {_totalRequest}");
                    _logger.LogInformation($"Currently queued operations {_operationsToHandleQueue.Count}");
                    _logger.LogInformation($"Current paging token '{response.PagingToken}");
                    var rpm = _startTime.Elapsed.Minutes > 0 ? $"{_totalRequest / (int)_startTime.Elapsed.TotalMinutes} request handled per minute ({(int)_startTime.Elapsed.TotalMinutes}m)" : "";

                    if (!string.IsNullOrEmpty(rpm))
                    {
                        _logger.LogInformation($"{rpm}");
                    }
                }
            });

            _eventSource.Error += (sender, args) => { _logger.LogError(args.Exception.Message); };
            _eventSource.Connect().Wait();
        }
Exemple #2
0
        public async Task ListenPaymentsTest()
        {
            var result = (payment : false, deposit : false);

            using (var stellarServer = new Server(StellarNodeUri))
            {
                KeyPair account1 = KeyPair.FromSecretSeed(Account1SecretSeed);
                KeyPair account2 = KeyPair.FromSecretSeed(Account2SecretSeed);

                var tcs = new TaskCompletionSource <(bool payment, bool deposit)>();

                IEventSource eventSource = stellarServer
                                           .Payments
                                           .ForAccount(account2)
                                           .Cursor("now")
                                           .Stream((sender, operation) =>
                {
                    if (operation is PaymentOperationResponse payment)
                    {
                        if (payment.To.Address == account2.Address)
                        {
                            result.deposit = true;
                        }
                        else if (payment.From.Address == account2.Address)
                        {
                            result.payment = true;
                        }

                        if (result.payment && result.deposit)
                        {
                            tcs.SetResult(result);
                        }
                    }
                });

                eventSource.Connect();

                await SendPayment(account1, account2, 1, stellarServer);
                await SendPayment(account2, account1, 1, stellarServer);

                await Task.WhenAny(tcs.Task, Task.Delay(10000));

                eventSource.Dispose();
            }

            Assert.IsTrue(result.payment);
            Assert.IsTrue(result.deposit);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                StringHelper.PrintHelp();
                return;
            }

            Connections = new ActiveConnections();
            string userName = args[0];


            string       url            = "http://localhost:5000/communicator";
            var          jsonSerializer = new JsonSerializer();
            IEventSource source         = EventSourceFactory.Get(url);

            source.Connect().GetAwaiter().GetResult();
            string connectionId = source.GetConnectionId().GetAwaiter().GetResult();

            List <KeyValue> mtdt = new List <KeyValue>();

            mtdt.Set("user", userName);
            mtdt.Set("id", connectionId);

            var sender      = source.GetEventSender();
            var observables = source.GetObservablesFactory();

            sender.String(new EventInfo("UserConnected"), new StringMessage(connectionId, mtdt));
            Connections.AddUser(mtdt);
            Console.WriteLine($"Welcome: {userName}!");
            Action help = () => Console.WriteLine("keywords [ help, users, clear, file, to, person, quit ]");

            help();

            var onDisconnected = observables.GetOnDisconnected();
            var onConnected    = observables.GetString("UserConnected");
            var onChat         = observables.GetString("Chat");
            var onFile         = observables.GetBinary("File");
            var onPerson       = observables.GetSerialized <Person>("Person", jsonSerializer);

            onConnected.Subscribe(m => Connections.AddUser(m.MetaData));
            onChat.Subscribe(m => Connections.AddUser(m.MetaData));
            onFile.Subscribe(m => Connections.AddUser(m.MetaData));
            onPerson.Subscribe(m => Connections.AddUser(m.MetaData));

            onConnected.Subscribe(msg => {
                string user = msg.MetaData.Get("user");
                Console.WriteLine($" User {user} joined!");
            });


            onDisconnected.Subscribe(id => Connections.Remove(id));

            onChat.Subscribe((msg) =>
            {
                var md      = msg.MetaData;
                string user = md.Get("user");
                $"{user}: {msg.Data}".Print();
            });

            onFile.Subscribe((msg) =>
            {
                var md          = msg.MetaData;
                string user     = md.Get("user");
                string fileName = md.Get("fileName");
                $"{user}: {fileName} length {msg.Data.Length}".Print();
            });

            onPerson.Subscribe((msg) =>
            {
                var md      = msg.MetaData;
                string user = md.Get("user");
                $"{user}: Person {{ Name = {msg.Data.Name}, Age = {msg.Data.Age} }}".Print();
            });


            string message = "Connected";
            string path    = string.Empty;
            string to      = string.Empty;
            bool   exit    = false;

            do
            {
                message = $"{Environment.NewLine}{StringHelper.DefaultPrompt}".Prompt().Trim();

                switch (message)
                {
                case "help":
                    help();
                    break;

                case "users":
                    Connections.Print();
                    break;

                case "clear":
                    Console.Clear();
                    break;

                case "quit":
                    sender.String(new EventInfo("Chat"), new StringMessage("Disconnected", mtdt));
                    return;

                case "file":
                    path = "Enter path: ".Prompt();
                    if (File.Exists(path))
                    {
                        byte[] bytes = System.IO.File.ReadAllBytes(path);
                        mtdt.SetFileInfo(path);
                        sender.Binary(new EventInfo("File"), new BinaryMessage(bytes, mtdt));
                    }
                    break;

                case "to":
                    string user = "******".Prompt();
                    to = Connections.FindId(user);
                    if (!string.IsNullOrWhiteSpace(to))
                    {
                        message = $"Private message for {user}: ".Prompt();
                        sender.String(new EventInfo("Chat", to, null), new StringMessage(message, mtdt));
                    }
                    else
                    {
                        Console.WriteLine($"User not found: {user}");
                    }
                    break;

                case "person":
                    string name   = "Person Name: ".Prompt();
                    string ageStr = "Person Age: ".Prompt();
                    int    age    = 0;
                    int.TryParse(ageStr.Trim(), out age);
                    Person p = new Person()
                    {
                        Name = name, Age = age
                    };
                    sender.Serialized(new EventInfo("Person"), new StringSerializedMessage <Person>(p, mtdt), jsonSerializer);
                    break;

                default:
                    sender.String(new EventInfo("Chat"), new StringMessage(message, mtdt));
                    break;
                }
            }while (!exit);
        }