Class: EasyUpnp::SsdpSearcher

Inherits:
Object
  • Object
show all
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

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(options = {})
  unsupported_args = options.keys.reject { |x| DEFAULT_OPTIONS[x] }
  raise RuntimeError.new "Unsupported arguments: #{unsupported_args}" if unsupported_args.any?

  @options = DEFAULT_OPTIONS.merge options
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 parse_message(message)
  lines = message.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

  raw_messages = []

  # Wait for responses. Timeout after a specified number of seconds
  begin
    Timeout::timeout(option :timeout) do
      loop do
        raw_messages.push(socket.recv(4196))
      end
    end
  rescue Timeout::Error
    # This is expected
  ensure
    socket.close
  end

  # Parse messages (extract HTTP headers)
  parsed_messages = raw_messages.map { |x| parse_message 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.
  parsed_messages.reject { |x| !x[:usn] }.group_by { |x| x[:usn].split('::').first }.map do |k, v|
    UpnpDevice.from_ssdp_messages(k, v)
  end
end