Exemple #1
0
        public RegistryResponse addSensorRecord(
            string sensorName,
            string sensorAddr,
            int sensorPort,
            int readIndex)
        {
            ServiceConfiguration config = ServiceConfiguration.Instance;

            var newRecord = new SensorRegistryRecord(
                sensorName,
                sensorAddr,
                sensorPort,
                readIndex);

            if (Registry.TryAdd(sensorName, newRecord))
            {
                var newEvent = new SensorRegistryEvent(
                    config.serviceId,
                    SensorRegEventType.NewSensor,
                    newRecord);

                this.broker.publishRegistryEvent(
                    newEvent,
                    config.newSensorFilter);

                return(this.okResponse(newRecord));
            }

            return(this.badResponse(RegistryStatus.sensorAlreadyExists));
        }
Exemple #2
0
        public void publishRegistryEvent(SensorRegistryEvent newEvent, string filter)
        {
            ServiceConfiguration config = ServiceConfiguration.Instance;

            this.PublishEvent(newEvent,
                              config.sensorRegistryTopic,
                              filter);
        }
Exemple #3
0
        protected override void Handle(SensorRegistryUpdateRequest request)
        {
            SensorRegistryEvent registryEvent = request.registryEvent;

            if (registryEvent.eventType == SensorRegEventType.NewSensor)
            {
                Console.WriteLine("Registry update: add");
                localRegistry.AddNewRecord(registryEvent.sensorRecord);
            }
            else if (registryEvent.eventType == SensorRegEventType.SensorUpdated)
            {
                // there is gonna be a lot of this so lets just comment them ...
                // Console.WriteLine("Registry update: update");
                localRegistry.UpdateRecord(registryEvent.sensorRecord);
            }
            else if (registryEvent.eventType == SensorRegEventType.SensorRemoved)
            {
                Console.WriteLine("Registry update: remove");
                localRegistry.RemoveRecord(registryEvent.sensorRecord);
            }
        }
Exemple #4
0
        public RegistryResponse removeSensorRecord(string sensorName)
        {
            ServiceConfiguration config = ServiceConfiguration.Instance;

            SensorRegistryRecord record = null;

            if (Registry.TryRemove(sensorName, out record))
            {
                var newEvent = new SensorRegistryEvent(
                    config.serviceId,
                    SensorRegEventType.SensorRemoved,
                    record);

                this.broker.publishRegistryEvent(
                    newEvent,
                    config.sensorRemovedFilter);

                return(this.okResponse(record));
            }

            return(this.badResponse(RegistryStatus.noSuchRecord));
        }
Exemple #5
0
        private void SetupRegistryEventConsumer()
        {
            string sensorRegistryQueue = this.channel.QueueDeclare().QueueName;

            channel.QueueBind(sensorRegistryQueue,
                              config.sensorRegistryTopic,
                              config.allFilter,
                              null);

            EventingBasicConsumer regEventConsumer = new EventingBasicConsumer(this.channel);

            regEventConsumer.Received += (srcChannel, eventArg) =>
            {
                string txtContent             = Encoding.UTF8.GetString(eventArg.Body.ToArray());
                SensorRegistryEvent eventData =
                    JsonConvert.DeserializeObject <SensorRegistryEvent>(txtContent);

                mediator.Send(new SensorRegistryUpdateRequest(eventData));
            };

            this.channel.BasicConsume(sensorRegistryQueue,
                                      true,
                                      regEventConsumer);
        }
Exemple #6
0
        // ATTENTION this method may not be thread safe
        public RegistryResponse updateSensorRecord(string name,
                                                   string address,
                                                   int port,
                                                   int readIndex)

        {
            ServiceConfiguration config = ServiceConfiguration.Instance;

            SensorRegistryRecord oldRecord = null;

            if (Registry.TryRemove(name, out oldRecord))
            {
                var newRecord = new SensorRegistryRecord(
                    name,
                    address,
                    port,
                    readIndex);

                Registry.TryAdd(name, newRecord);

                SensorRegistryEvent newEvent = new SensorRegistryEvent(
                    config.serviceId,
                    SensorRegEventType.SensorUpdated,
                    newRecord);

                // TODO this should be published from mediator request
                // and not from registry class
                // this class should be used just as cache record ... nothing else
                this.broker.publishRegistryEvent(newEvent,
                                                 config.sensorUpdateFilter);

                return(this.okResponse(newRecord));
            }

            return(this.badResponse(RegistryStatus.noSuchRecord));
        }
Exemple #7
0
 public SensorRegistryUpdateRequest(SensorRegistryEvent registryEvent)
 {
     this.registryEvent = registryEvent;
 }