Class: Druzy::Upnp::Ssdp

Inherits:
Object
  • Object
show all
Defined in:
lib/druzy/upnp/ssdp.rb

Constant Summary collapse

@@port =
1900
@@host =
"239.255.255.250"

Instance Method Summary collapse

Constructor Details

#initializeSsdp

Returns a new instance of Ssdp.



14
15
16
# File 'lib/druzy/upnp/ssdp.rb', line 14

def initialize
  
end

Instance Method Details

#search(st = "ssdp:all", delay = 10) ⇒ Object

search only device (not service)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/druzy/upnp/ssdp.rb', line 19

def search(st = "ssdp:all", delay = 10)
  message = <<-MESSAGE
M-SEARCH * HTTP/1.1\r
HOST: #{@@host}:#{@@port}\r
MAN: "ssdp:discover"\r
MX: #{delay}\r
ST: #{st}\r
USER-AGENT: #{RbConfig::CONFIG["host_os"]}/ UPnP/1.1 ruby-druzy-upnp/#{Druzy::Upnp::VERSION}\r
  MESSAGE
  
  s = UDPSocket.new
  s.send(message,0,@@host,@@port)
  devices = []
  begin
    Timeout::timeout(delay) do
      loop do
        message = s.recv(4196)
        location = message.split("\n").reject{|line| line==nil || line["LOCATION"]==nil}.first
        location = location[location.index(" ")+1..location.size-2]
        if !devices.include?(location)
          devices << location
          if block_given?
            yield UpnpDevice.new(:location => location)
          end
        end
      end
    end
  rescue
    
  ensure
    s.close
  end
  
end