コード例 #1
0
        protected override void OnReceive(object message)
        {
            switch (message)
            {
            case RequestRegisterTemperatureSensor m when m.FloorId == _floorId:
                if (_sensorIdToActorRefs.TryGetValue(m.SensorId, out var existingSensorActorRef))
                {
                    existingSensorActorRef.Forward(m);
                }
                else
                {
                    var newSensorActor = Context.ActorOf(TemperatureSensor.Props(_floorId, m.SensorId),
                                                         $"temperature-sensor-{m.SensorId}");
                    Context.Watch(newSensorActor);
                    _sensorIdToActorRefs.Add(m.SensorId, newSensorActor);
                    newSensorActor.Forward(m);
                }
                break;

            case RequestTemperatureSensorIds m:
                Sender.Tell(new RespondTemperatureSensorIds(m.RequestId, ImmutableHashSet.CreateRange(_sensorIdToActorRefs.Keys)));
                break;

            case Terminated m:
                var terminatedTemperatureSensorId = _sensorIdToActorRefs.First(x => x.Value == m.ActorRef).Key;
                _sensorIdToActorRefs.Remove(terminatedTemperatureSensorId);
                break;

            default:
                Unhandled(message);
                break;
            }
        }
コード例 #2
0
        protected override void OnReceive(object message)
        {
            switch (message)
            {
            case RequestRegisterTemperatureSensor m when m.FloorId == FloorId:
                if (!Sensors.TryGetValue(m.SensorId, out var existingSensorActorRef))
                {
                    existingSensorActorRef = Context.ActorOf(TemperatureSensor.Props(FloorId, m.SensorId), $"temperature-sensor{m.SensorId}");
                    Sensors.Add(m.SensorId, existingSensorActorRef);
                    Context.Watch(existingSensorActorRef);
                }
                existingSensorActorRef.Forward(m);
                break;

            case RequestTemperatureSensorIds m:
                Sender.Tell(new RespondTemperatureSensorIds(m.RequestId, ImmutableHashSet.CreateRange <string>(Sensors.Keys)));
                break;

            case RequestAllTemperatures m:
                var actorRefToSensorIdMap = new Dictionary <IActorRef, string>();
                foreach (var item in Sensors)
                {
                    actorRefToSensorIdMap.Add(item.Value, item.Key);
                }
                Context.ActorOf(FloorQuery.Props(actorRefToSensorIdMap, m.RequestId, Sender, TimeSpan.FromSeconds(3)));
                break;

            case Terminated m:
                var termintedTemperatureSensorId = Sensors.First(x => x.Value == m.ActorRef).Key;
                Sensors.Remove(termintedTemperatureSensorId);
                break;

            default:
                Unhandled(message);
                break;
            }
        }