Exemplo n.º 1
0
 public UpnpRootDevice AddDevice(UpnpRootDevice device)
 {
     lock (this.deviceLock)
     {
         UpnpEngineDevice engineDevice = new UpnpEngineDevice(device, this);
         this.devices.Add(engineDevice);
     }
     this.RefreshNotifyer();
     return(device);
 }
Exemplo n.º 2
0
        private void OnHttpRequestItem(PowerLineContext context, Guid itemName, string itemExtention)
        {
            UpnpEngineDevice device = this.GetDevice(itemName);

            if (device == null)
            {
                context.SetResponse(404, "Unable to detect given element, device not found");
            }
            else
            {
                UpnpPlaceHolder placeHolder      = new UpnpPlaceHolder(context.LocalAddress, context.LocalPort, context.RemoteAddress, context.RemotePort);
                string          rawDeviceUpnpXml = device.GetHttpDevice(placeHolder);
                context.SetRepsonseContentType(device.GetHttpDeviceMime());
                context.SetResponse(200, rawDeviceUpnpXml, false);
            }
        }
Exemplo n.º 3
0
        private UpnpEngineDevice GetMinNextNotification()
        {
            UpnpEngineDevice minDevice = null;

            lock (this.deviceLock)
            {
                foreach (UpnpEngineDevice device in this.devices)
                {
                    if (minDevice == null || (minDevice.GetLastNotification() > device.GetLastNotification()))
                    {
                        minDevice = device;
                    }
                }
            }
            return(minDevice);
        }
Exemplo n.º 4
0
        private void OnRunnerDisocveryForDevice(HttpRequest request, UpnpEngineDevice device, UpnpNt searchId, IPEndPoint remoteEndpoint, IPEndPoint localEndpoint, IPAddress localInterfaceIpOfRemoteEndpoint)
        {
            Console.WriteLine("Sending response for disconvery");

            HttpHeader sendDelay = request.Headers.Get("MX", true);

            if (sendDelay != null && int.TryParse(sendDelay.Value, out int maxDelay))
            {
                int currentSleep = random.Next(1, maxDelay * 1000);
                System.Threading.Thread.Sleep(currentSleep);
            }
            UpnpSsdpQueryResponse response = new UpnpSsdpQueryResponse(
                (int)device.Device.NotificationInterval.TotalSeconds,
                searchId, device.Device.IdUsn, this.GetDeviceHttpAddress(device, localInterfaceIpOfRemoteEndpoint));

            this.Send(response, remoteEndpoint);
        }
Exemplo n.º 5
0
 internal void InvokeDeviceNotification(UpnpEngineDevice device)
 {
     for (int i = 0; i < 2; i++)
     {
         this.Brodcast((ipAddress) =>
         {
             return(new UpnpSsdpNotify(
                        (int)device.Device.NotificationInterval.TotalSeconds,
                        SSDP_IP,
                        SSDP_PORT,
                        this.GetDeviceHttpAddress(device, ipAddress),
                        device.Device.UpnpRoot.ToString(),
                        (device.Device.Enable) ? UpnpSsdpNotify.NotifyAlive : UpnpSsdpNotify.NotifyByeBye,
                        device.Device.UpnpRoot.GetUsn(device.Device.Id.id).ToString()).Encode());
         });
         this.Brodcast((ipAddress) =>
         {
             return(new UpnpSsdpNotify(
                        (int)device.Device.NotificationInterval.TotalSeconds,
                        SSDP_IP,
                        SSDP_PORT,
                        this.GetDeviceHttpAddress(device, ipAddress),
                        device.Device.Id.ToString(),
                        (device.Device.Enable) ? UpnpSsdpNotify.NotifyAlive : UpnpSsdpNotify.NotifyByeBye,
                        device.Device.Id.GetUsn(device.Device.Id.id).ToString()).Encode());
         });
         this.Brodcast((ipAddress) =>
         {
             return(new UpnpSsdpNotify(
                        (int)device.Device.NotificationInterval.TotalSeconds,
                        SSDP_IP,
                        SSDP_PORT,
                        this.GetDeviceHttpAddress(device, ipAddress),
                        device.Device.UpnpType.ToString(),
                        (device.Device.Enable) ? UpnpSsdpNotify.NotifyAlive : UpnpSsdpNotify.NotifyByeBye,
                        device.Device.UpnpType.GetUsn(device.Device.Id.id).ToString()).Encode());
         });
         if (!device.Device.Enable)
         {
             return;
         }
         System.Threading.Thread.Sleep(2500);
     }
 }
Exemplo n.º 6
0
 private void RunnerNotifier()
 {
     while (!this.cancelToken.IsCancellationRequested)
     {
         UpnpEngineDevice selectedDevice = this.GetMinNextNotification();
         if (selectedDevice == null)
         {
             this.waitHandle.WaitOne();
         }
         else
         {
             DateTime lastNotification = selectedDevice.GetNextNotification();
             if (lastNotification < DateTime.UtcNow.AddSeconds(5))
             {
                 selectedDevice.Notifiy();
             }
             else
             {
                 this.waitHandle.WaitOne(lastNotification - DateTime.UtcNow);
             }
         }
     }
 }
Exemplo n.º 7
0
 private string GetDeviceHttpAddress(UpnpEngineDevice device, IPAddress localInterfaceIpOfRemoteEndpoint = null)
 {
     return($"{this.GetPowerLineHttpAddress(localInterfaceIpOfRemoteEndpoint)}UPnP/{device.Device.Id.id.ToString()}.xml");
 }