Class: EasyUpnp::SsdpSearcher
- Inherits:
-
Object
- Object
- EasyUpnp::SsdpSearcher
- Defined in:
- lib/easy_upnp/ssdp_searcher.rb
Constant Summary collapse
- MULTICAST_ADDR =
These are dictated by the SSDP protocol and cannot be changed
'239.255.255.250'
- MULTICAST_PORT =
1900
- DEFAULT_OPTIONS =
{ # Number of seconds to wait for responses :timeout => 2, # Part of the SSDP protocol. Servers should delay a random amount of time between 0 and N # seconds before sending the response. :mx => 1, # Sometimes recommended to send repeat M-SEARCH queries. Control that here. :repeat_queries => 1 }
Instance Method Summary collapse
- #construct_msearch_packet(urn) ⇒ Object
-
#initialize(options = {}) ⇒ SsdpSearcher
constructor
A new instance of SsdpSearcher.
- #option(key) ⇒ Object
- #parse_message(message) ⇒ Object
- #search(urn = 'ssdp:all') ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SsdpSearcher
Returns a new instance of SsdpSearcher.
25 26 27 28 29 30 |
# File 'lib/easy_upnp/ssdp_searcher.rb', line 25 def initialize( = {}) unsupported_args = .keys.reject { |x| DEFAULT_OPTIONS[x] } raise RuntimeError.new "Unsupported arguments: #{unsupported_args}" if unsupported_args.any? @options = DEFAULT_OPTIONS.merge end |
Instance Method Details
#construct_msearch_packet(urn) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/easy_upnp/ssdp_searcher.rb', line 71 def construct_msearch_packet(urn) <<-MSEARCH M-SEARCH * HTTP/1.1\r HOST: #{MULTICAST_ADDR}:#{MULTICAST_PORT}\r MAN: "ssdp:discover"\r MX: #{option :mx}\r ST: #{urn}\r \r MSEARCH end |
#option(key) ⇒ Object
32 33 34 |
# File 'lib/easy_upnp/ssdp_searcher.rb', line 32 def option(key) @options[key] end |
#parse_message(message) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/easy_upnp/ssdp_searcher.rb', line 82 def () lines = .split "\r\n" headers = lines.map do |line| if !(match = line.match(/([^:]+):\s?(.*)/i)).nil? header, value = match.captures key = header. downcase. gsub('-', '_'). to_sym [key, value] end end Hash[headers.reject(&:nil?)] end |
#search(urn = 'ssdp:all') ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/easy_upnp/ssdp_searcher.rb', line 36 def search(urn = 'ssdp:all') socket = build_socket packet = construct_msearch_packet(urn) # Send M-SEARCH packet over UDP socket option(:repeat_queries).times do socket.send packet, 0, MULTICAST_ADDR, MULTICAST_PORT end = [] # Wait for responses. Timeout after a specified number of seconds begin Timeout::timeout(option :timeout) do loop do .push(socket.recv(4196)) end end rescue Timeout::Error # This is expected ensure socket.close end # Parse messages (extract HTTP headers) = .map { |x| x } # Group messages by device they come from (identified by a UUID in the 'USN' header), # and create UpnpDevices for them. This wrap the services advertized by the SSDP # results. .reject { |x| !x[:usn] }.group_by { |x| x[:usn].split('::').first }.map do |k, v| UpnpDevice.(k, v) end end |