Exemplo n.º 1
0
        private async void TryAddDevice(SSDPMessage notifyMessage)
        {
            if (this.IsDeviceAdded(notifyMessage.UDN))
            {
                this.TryUpdateDevice(notifyMessage);
            }
            else
            {
                try
                {
                    var request = WebRequest.Create(notifyMessage.Location);

                    using (var response = await request.GetResponseAsync())
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            lock (this.devices)
                            {
                                if (this.IsDeviceAdded(notifyMessage.UDN) == false)
                                {
                                    var location = new Uri(notifyMessage.Location);
                                    var host     = "{0}:{1}".F(location.Host, location.Port);

                                    var device = ParseDevice(host, responseStream);
                                    if (device != null)
                                    {
                                        device.MaxAge        = TimeSpan.FromSeconds(notifyMessage.MaxAge);
                                        device.LastCheckTime = DateTime.UtcNow;

                                        this.devices.Add(device);
                                        this.logger.Instance().Info("The device has been added", "DeviceName".As(device.FriendlyName), "DeviceUDN".As(device.UDN));
                                        this.devicesActivity.OnNext(new DeviceActivityEventArgs <TDevice> {
                                            Activity = DeviceActivity.Available, Device = device
                                        });

                                        this.ScheduleNextCheckForExpiredDevices();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (WebException ex)
                {
                    this.logger.Instance().Warning(ex, "Failed to load description for device with UDN '{0}'".F(notifyMessage.UDN));
                }
            }
        }
Exemplo n.º 2
0
        private void TryUpdateDevice(SSDPMessage notifyMessage)
        {
            lock (this.devices)
            {
                var device = GetDevice(notifyMessage.UDN);

                if (device != null)
                {
                    device.MaxAge        = TimeSpan.FromSeconds(notifyMessage.MaxAge);
                    device.LastCheckTime = DateTime.UtcNow;

                    this.ScheduleNextCheckForExpiredDevices();

                    this.logger.Instance().Info("The device lifetime has been updated", "DeviceName".As(device.FriendlyName), "DeviceUDN".As(device.UDN), "MaxAge".As(notifyMessage.MaxAge));
                }
            }
        }