Exemple #1
0
        public bool TryInit(HttpRequest request)
        {
            if (!request.Query.TryGetValue("type", out var type) || string.IsNullOrWhiteSpace(type))
            {
                return(false);
            }

            if (!Enum.TryParse <ClientType>(type, true, out var clientType))
            {
                return(false);
            }

            _clientType = clientType;
            if (_clientType == ClientType.Provider)
            {
                return(true);
            }

            if (!request.Query.TryGetValue("deposit", out var deposit) ||
                string.IsNullOrWhiteSpace(deposit))
            {
                return(false);
            }

            _depositId = Keccak.TryParse(deposit);

            return(!(_depositId is null));
        }
Exemple #2
0
        public override async Task SubscribeNdmData(NdmDataSubscription request,
                                                    IServerStreamWriter <NdmDataResponse> responseStream, ServerCallContext context)
        {
            var peer = context.Peer;

            if (_logger.IsInfo)
            {
                _logger.Info($"Received SubscribeNdmData() GRPC call from peer: '{peer}' for deposit: '{request.DepositId}'");
            }
            var depositId = Keccak.TryParse(request.DepositId);

            if (depositId is null)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Received an invalid deposit ID from peer: '{peer}'.");
                }
                return;
            }

            var depositPeers = _depositsPeers.AddOrUpdate(depositId, _ => new ConcurrentDictionary <string, BlockingCollection <string> >(),
                                                          (_, p) => p);
            var receivedData = depositPeers.AddOrUpdate(peer, _ => new BlockingCollection <string>(), (_, p) => p);

            try
            {
                while (true)
                {
                    var data = receivedData.Take();
                    await responseStream.WriteAsync(new NdmDataResponse
                    {
                        DepositId = request.DepositId,
                        Data      = data
                    });
                }
            }
            catch (Exception ex)
            {
                if (_logger.IsError)
                {
                    _logger.Error(ex.ToString(), ex);
                }
                depositPeers.TryRemove(peer, out _);
                if (depositPeers.IsEmpty)
                {
                    _depositsPeers.TryRemove(depositId, out _);
                }
            }
        }