Exemplo n.º 1
0
        /// <inheritdoc/>
        public override Task Connect(grpc.PullConnector request, IServerStreamWriter <grpc.PullRequest> responseStream, ServerCallContext context)
        {
            var id            = request.Id.ToGuid();
            var pullConnector = new PullConnector(id, request.Name, request.Interval);

            ITimer timer = null;

            try
            {
                _pullConnectors.Register(pullConnector);

                timer = _timers.Every(pullConnector.Interval, () =>
                {
                    var pullRequest = new grpc.PullRequest();
                    responseStream.WriteAsync(pullRequest);
                });

                context.CancellationToken.ThrowIfCancellationRequested();
                context.CancellationToken.WaitHandle.WaitOne();
            }
            finally
            {
                timer?.Stop();
                timer?.Dispose();

                _pullConnectors.Unregister(pullConnector);
            }

            return(Task.CompletedTask);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DataPointsStatePuller"/> class.
        /// </summary>
        /// <param name="endPoint"><see cref="DataPointsStateEndPoint"/> to pull from.</param>
        /// <param name="processors"><see cref="IDataPointProcessors"/> to involve when <see cref="DataPoint">data points</see> are pulled.</param>
        /// <param name="timers"><see cref="ITimers"/> for scheduling.</param>
        /// <param name="logger"><see cref="ILogger"/> for logging.</param>
        public DataPointsStatePuller(
            DataPointsStateEndPoint endPoint,
            IDataPointProcessors processors,
            ITimers timers,
            ILogger logger)
        {
            var channel = new Channel(endPoint.Target, ChannelCredentials.Insecure);

            _client = new DataPointsStateClient(channel);

            timers.Every(endPoint.Interval, async() => await Pull().ConfigureAwait(false));
            _processors = processors;
            _logger     = logger;
            _endPoint   = endPoint;
        }