Class: RUPNP::SSDP::Searcher

Inherits:
MulticastConnection show all
Includes:
HTTP
Defined in:
lib/rupnp/ssdp/searcher.rb

Overview

Searcher class for searching devices

Author:

  • Sylvain Daubert

Constant Summary collapse

DEFAULT_M_SEARCH_TRY =

Number of SEARCH datagrams to send

2

Constants included from LogMixin

LogMixin::LOG_LEVEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTP

#get_http_headers, #get_http_verb, #is_http_status_ok?

Methods inherited from MulticastConnection

#peer_info

Methods included from LogMixin

#log

Constructor Details

#initialize(options = {}) ⇒ Searcher

Returns a new instance of Searcher.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :search_target (String)
  • :response_wait_time (Integer)
  • :try_number (Integer)
  • :ttl (Integer)


22
23
24
25
26
27
28
# File 'lib/rupnp/ssdp/searcher.rb', line 22

def initialize(options={})
  @options = options
  @options[:try_number] ||= DEFAULT_M_SEARCH_TRY
  @discovery_responses = EM::Channel.new

  super options[:ttl]
end

Instance Attribute Details

#discovery_responsesEM::Channel (readonly)

Channel to receive discovery responses

Returns:

  • (EM::Channel)


14
15
16
# File 'lib/rupnp/ssdp/searcher.rb', line 14

def discovery_responses
  @discovery_responses
end

Instance Method Details

#post_initObject



31
32
33
34
35
36
37
# File 'lib/rupnp/ssdp/searcher.rb', line 31

def post_init
  search = search_request
  @options[:try_number].times do
    send_datagram search, MULTICAST_IP, DISCOVERY_PORT
    log :debug, "send datagram:\n#{search}"
  end
end

#receive_data(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rupnp/ssdp/searcher.rb', line 40

def receive_data(data)
  port, ip = peer_info
  log :debug, "Response from #{ip}:#{port}:"
  log :debug, data

  response = StringIO.new(data)
  if !is_http_status_ok?(response)
    log :error, "bad HTTP response:\n #{data}"
    return
  end

  @discovery_responses << get_http_headers(response)
end