lib/rupnp/control_point.rb in rupnp-0.1.0 vs lib/rupnp/control_point.rb in rupnp-0.2.0

- old
+ new

@@ -58,34 +58,40 @@ # @yieldparam bye_device_channel [EM::Channel] # channel on which +byebye+ device notifications are announced # @return [void] def start search_devices_and_listen @search_target, @search_options - yield @new_device_channel, @bye_device_channel + yield @new_device_channel, @bye_device_channel if block_given? end + # Start a search for devices. No listen for update is made. + # + # Found devices are accessible through {#devices}. + # @return [void] def search_only options = @search_options.dup options[:search_only] = true search_devices_and_listen @search_target, options end # Start event server for listening for device events # @param [Integer] port port to listen for # @return [void] def start_event_server(port=EVENT_SUB_DEFAULT_PORT) - @event_port = port - @add_event_url = EM::Channel.new + @event_port ||= port + @add_event_url ||= EM::Channel.new @event_server ||= EM.start_server('0.0.0.0', port, CP::EventServer, @add_event_url) end # Stop event server # @see #start_event_server # @return [void] def stop_event_server + @event_port = nil EM.stop_server @event_server + @event_server = nil end # Add a device to the control point # @param [Device] device device to add # @return [void] @@ -127,17 +133,22 @@ unless options[:search_only] log :info, 'now listening for device advertisement' listener = SSDP.listen listener.notifications.subscribe do |notification| + udn = usn2udn(notification['usn']) + case notification['nts'] - when 'ssdp:alive' - create_device notification + when 'ssdp:alive', 'ssdp:update' + d = @devices.find { |d| d.udn == udn } + if d + d.update notification + else + create_device notification + end when 'ssdp:byebye' - udn = usn2udn(notification['usn']) log :info, "byebye notification sent by device #{udn}" - rejected = @devices.reject! { |d| d.udn == udn } - log :info, "#{rejected.udn} device removed" if rejected + @devices.reject! { |d| d.udn == udn } else log :warn, "Unknown notification type: #{notification['nts']}" end end end