Beispiel #1
0
        private static Task StartProcessing(
            SensorDataConsumer[] consumers,
            CancellationTokenSource cancellationToken,
            SensorsHub sensorsHub)
        {
            var consumptionJobs = consumers
                                  .Select(c => c.StartConsuming(cancellationToken.Token))
                                  .ToArray();

            sensorsHub.StartToListen().Wait();

            return(Task.WhenAll(consumptionJobs));
        }
Beispiel #2
0
        private static void StopClient(
            CancellationTokenSource cancellationToken,
            SensorsHub sensorsHub,
            SensorDataProducer producer,
            Task processingJob,
            Channel <SensorDataDto> channel)
        {
            cancellationToken.Cancel();

            sensorsHub.StopToListen().Wait();
            producer.Complete();


            Console.WriteLine("Sensor client closing...");

            processingJob.Wait();
            channel.Reader.Completion.Wait();

            Console.WriteLine("Sensor client closed...");
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var configuration = Configuration.Read();

            var database = new Database(
                configuration);

            var channel = Channel.CreateUnbounded <SensorDataDto>();

            var consumers = PrepareConsumers(
                channel,
                database,
                ParallelConsumersCount);

            var producer = new SensorDataProducer(
                channel.Writer);

            var sensorsHub = new SensorsHub(
                producer);

            var cancellationToken = new CancellationTokenSource();

            var processingJob = StartProcessing(
                consumers,
                cancellationToken,
                sensorsHub);

            Console.WriteLine("Press any key to to exit.");
            Console.ReadLine();

            StopClient(
                cancellationToken,
                sensorsHub,
                producer,
                processingJob,
                channel);
        }