Ejemplo n.º 1
0
        private void Temperature_ReadingChanged(ITemperatureSensor sender, ITemperatureReading args)
        {
            // Get reading
            var t = args.Temperature;

            Debug.WriteLine($"F: {t.DegreesFahrenheit}  C: {t.DegreesCelsius}");
        }
Ejemplo n.º 2
0
        public void ReceivedResponse(IActorRef deviceActor, ITemperatureReading reading, HashSet <IActorRef> stillWaiting, Dictionary <string, ITemperatureReading> repliesSoFar)
        {
            Console.WriteLine(JsonConvert.SerializeObject(new
            {
                Message     = $"{GetType().Name}.ReceivedResponse",
                deviceActor = deviceActor.ToString(),
                reading,
                stillWaitingCount = stillWaiting.Count,
                repliesSoFarCount = repliesSoFar.Count
            }, Formatting.Indented));
            Context.Unwatch(deviceActor);
            string deviceId = ActorToDeviceId[deviceActor];

            stillWaiting.Remove(deviceActor);

            repliesSoFar.Add(deviceId, reading);

            if (stillWaiting.Count == 0)
            {
                Requester.Tell(new RespondAllTemperatures(RequestId, repliesSoFar));
                Context.Stop(Self);
            }
            else
            {
                Context.Become(WaitingForReplies(repliesSoFar, stillWaiting));
            }
        }
Ejemplo n.º 3
0
        public UntypedReceive WaitingForReplies(
            Dictionary <string, ITemperatureReading> repliesSoFar,
            HashSet <IActorRef> stillWaiting)
        {
            Console.WriteLine(JsonConvert.SerializeObject(new
            {
                Message = $"{GetType().Name}.WaitingForReplies",
                repliesSoFar,
                stillWaitingCount = stillWaiting.Count
            }, Formatting.Indented));
            return(message =>
            {
                switch (message)
                {
                case RespondTemperature response when response.RequestId == 0:
                    IActorRef deviceActor = Sender;
                    ITemperatureReading reading = null;
                    if (response.Value.HasValue)
                    {
                        reading = new Temperature(response.Value.Value);
                    }
                    else
                    {
                        reading = TemperatureNotAvailable.Instance;
                    }
                    ReceivedResponse(deviceActor, reading, stillWaiting, repliesSoFar);
                    break;

                case Terminated t:
                    ReceivedResponse(t.ActorRef, DeviceNotAvailable.Instance, stillWaiting, repliesSoFar);
                    break;

                case CollectionTimeout _:
                    Dictionary <string, ITemperatureReading> replies = new Dictionary <string, ITemperatureReading>(repliesSoFar);
                    foreach (IActorRef actor in stillWaiting)
                    {
                        string deviceId = ActorToDeviceId[actor];
                        replies.Add(deviceId, DeviceTimedOut.Instance);
                    }
                    Requester.Tell(new RespondAllTemperatures(RequestId, replies));
                    Context.Stop(Self);
                    break;
                }
            });
        }
Ejemplo n.º 4
0
        private void ReceivedResponse(IActorRef deviceActor, ITemperatureReading reading, HashSet <IActorRef> stillWaiting, Dictionary <string, ITemperatureReading> repliesSoFar)
        {
            Context.Unwatch(deviceActor);
            var deviceId = ActorToDeviceId[deviceActor];

            repliesSoFar[deviceId] = reading;
            stillWaiting.Remove(deviceActor);

            if (!stillWaiting.Any())
            {
                Requester.Tell(new RespondAllTemperatures(RequestId, repliesSoFar));
                Context.Stop(Self);
            }
            else
            {
                Context.Become(WaitingForReplies(repliesSoFar, stillWaiting));
            }
        }
Ejemplo n.º 5
0
        public UntypedReceive WaitingForReplies(
            Dictionary <string, ITemperatureReading> repliesSoFar,
            HashSet <IActorRef> stillWaiting)
        {
            return(message =>
            {
                switch (message)
                {
                case RespondTemperature response when response.RequestId == 0:
                    var deviceActor = Sender;
                    ITemperatureReading reading = null;
                    if (response.Value.HasValue)
                    {
                        reading = new Temperature(response.Value.Value);
                    }
                    else
                    {
                        reading = TemperatureNotAvailable.Instance;
                    }
                    ReceivedResponse(deviceActor, reading, stillWaiting, repliesSoFar);
                    break;

                case Terminated t:
                    ReceivedResponse(t.ActorRef, DeviceNotAvailable.Instance, stillWaiting, repliesSoFar);
                    break;

                case CollectionTimeout _:
                    var replies = new Dictionary <string, ITemperatureReading>(repliesSoFar);
                    //We have reach te timeout, so we are not going to wait for these actors.
                    foreach (var actor in stillWaiting)
                    {
                        var deviceId = ActorToDeviceId[actor];
                        replies.Add(deviceId, DeviceTimedOut.Instance);
                    }
                    //We reply with what we have, and the actor is stopped.
                    Requester.Tell(new RespondAllTemperatures(RequestId, replies));
                    Context.Stop(Self);
                    break;
                }
            });
        }
Ejemplo n.º 6
0
        public UntypedReceive WaitingForReplies(
            Dictionary <string, ITemperatureReading> repliesSoFar, HashSet <IActorRef> stillWaiting)
        {
            return(message =>
            {
                switch (message)
                {
                case RespondTemperature response when response.RequestId == 0:
                    var deviceActor = Sender;
                    ITemperatureReading temperatureReading = null;
                    if (response.Value.HasValue)
                    {
                        temperatureReading = new Temperature(response.Value.Value);
                    }
                    else
                    {
                        temperatureReading = TemperatureNotAvailable.Instance;
                    }

                    ReceivedResponse(deviceActor, temperatureReading, stillWaiting, repliesSoFar);
                    break;

                case Terminated terminated:
                    ReceivedResponse(terminated.ActorRef, DeviceNotAvailable.Instance, stillWaiting, repliesSoFar);
                    break;

                case CollectionTimeout collectionTimeout:
                    var replies = new Dictionary <string, ITemperatureReading>(repliesSoFar);
                    foreach (var actor in stillWaiting)
                    {
                        var deviceId = ActorToDeviceId[actor];
                        replies.Add(deviceId, DeviceTimeOut.Instance);
                    }
                    Requester.Tell(new RespondAllTemperatures(RequestId, replies));
                    Context.Stop(Self);
                    break;
                }
            });
        }
Ejemplo n.º 7
0
        public void ReceivedResponse(
            IActorRef deviceActor,
            ITemperatureReading reading,
            IImmutableSet <IActorRef> stillWaiting,
            IImmutableDictionary <string, ITemperatureReading> repliesSoFar)
        {
            Context.Unwatch(deviceActor);
            var deviceId = ActorToDeviceId[deviceActor];

            var newStillWaiting = stillWaiting.Remove(deviceActor);
            var newRepliesSoFar = repliesSoFar.Add(deviceId, reading);

            if (newStillWaiting.Count == 0)
            {
                Requester.Tell(new RespondAllTemperatures(RequestId, newRepliesSoFar));
                Context.Stop(Self);
            }
            else
            {
                Context.Become(WaitingForReplies(newRepliesSoFar, newStillWaiting));
            }
        }
Ejemplo n.º 8
0
        public void ReceivedResponse(
            IActorRef deviceActor,
            ITemperatureReading reading,
            HashSet <IActorRef> stillWaiting,
            Dictionary <string, ITemperatureReading> repliesSoFar)
        {
            Context.Unwatch(deviceActor);
            stillWaiting.Remove(deviceActor);

            var deviceId = ActorToDeviceId[deviceActor];

            repliesSoFar.Add(deviceId, reading);

            if (stillWaiting.Count == 0)
            {
                //We have received the responses from all the devices. We repply and the actor is stopped.
                Requester.Tell(new RespondAllTemperatures(RequestId, repliesSoFar));
                Context.Stop(Self);
            }
            else
            {
                Context.Become(WaitingForReplies(repliesSoFar, stillWaiting));
            }
        }
Ejemplo n.º 9
0
        private void Temperature_ReadingChanged(ITemperatureSensor sender, ITemperatureReading args)
        {
            // Get reading
            var t = args.Temperature;

            Debug.WriteLine($"F: {t.DegreesFahrenheit}  C: {t.DegreesCelsius}");
        }