Exemplo n.º 1
0
        private void DoSendWork(object state)
        {
            Message message = _messageStorage.GetNext();

            while (message != null)
            {
                if (message != null)
                {
                    var connections = _connectionStorage.GetConnectionsByTopic(message.Topic);

                    foreach (var connection in connections)
                    {
                        var client  = new Notifier.NotifierClient(connection.GrpcChannel);
                        var request = new NotifyRequest()
                        {
                            Content = message.Content
                        };

                        try
                        {
                            if (connection.Limit <= 0)
                            {
                                client.Notify(new NotifyRequest()
                                {
                                    Content = "Message limit is out. You are disconnected."
                                });
                                _connectionStorage.Remove(connection.Address);
                                continue;
                            }

                            var reply = client.Notify(request);
                            _connectionStorage.UpdateLimit(connection.Address);
                            Console.WriteLine($"Notified subscriber {connection.Address} with {message.Content}. Response: {reply}");
                        }
                        catch (RpcException re)
                        {
                            if (re.StatusCode == StatusCode.Internal)
                            {
                                _connectionStorage.Remove(connection.Address);
                            }
                            Console.WriteLine(re.Message);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Error notifying subscriber {connection.Address}. {e.Message}");
                        }
                    }
                }

                message = _messageStorage.GetNext();
            }
        }
Exemplo n.º 2
0
        private async Task DoSendWork(CancellationToken cancellationToken)
        {
            var isEmptyMessages = await _messageRepository.IsEmpty(cancellationToken);

            if (isEmptyMessages)
            {
                return;
            }

            var message = await _messageRepository.GetNext(cancellationToken);

            if (message != null)
            {
                var connections = await _connectionRepository.GetConnectionsByBank(message.Bank, cancellationToken);

                if (connections.Count == 0)
                {
                    await _messageRepository.Add(message, cancellationToken);
                }

                foreach (var connection in connections)
                {
                    var channel = GrpcChannel.ForAddress(connection.Address, new GrpcChannelOptions {
                        HttpHandler = _httpHandler
                    });
                    var client  = new Notifier.NotifierClient(channel);
                    var request = new NotifyRequest {
                        Bank = message.Bank, Content = message.Rates
                    };

                    try
                    {
                        var reply = client.Notify(request);
                        Console.WriteLine($"Notified subscriber {connection.Address} with {message.Rates}. Response: {reply.IsSuccess}");
                    }
                    catch (RpcException e)
                    {
                        if (e.StatusCode == StatusCode.Internal)
                        {
                            await _connectionRepository.Remove(connection.Address, cancellationToken);
                        }

                        Console.WriteLine($"Details: {e.Status.Detail}");
                        Console.WriteLine($"Status code: {e.Status.StatusCode}");
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void DoSendWork(object state)
        {
            while (!_messageStorege.IsEmpty() &&
                   _connectionStorage.GetConnectionsByTopic(_messageStorege.CheckNextTopic()).Count != 0)
            {
                var message = _messageStorege.GetNext();

                if (message != null)
                {
                    var connections = _connectionStorage.GetConnectionsByTopic(message.Topic);

                    foreach (var connection in connections)
                    {
                        var client  = new Notifier.NotifierClient(connection.Channel);
                        var request = new NotifyRequest()
                        {
                            Content = message.Content
                        };

                        try
                        {
                            var reply = client.Notify(request);
                            Console.WriteLine($"Notified subscriber {connection.Address} with {message.Content}. Response: {reply.IsSuccess}");
                        }

                        catch (RpcException rpcException)
                        {
                            if (rpcException.StatusCode == StatusCode.Internal)
                            {
                                _connectionStorage.Remove(connection.Address);
                            }

                            Console.WriteLine($"RPC Error notifying subscriber {connection.Address}. {rpcException.Message}");
                        }


                        catch (Exception exception)
                        {
                            Console.WriteLine($"Error notifying subscriber {connection.Address}. {exception.Message}");
                        }
                    }
                }
            }
        }