Class: NLHue::SSDP::SSDPConnection

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/nlhue/ssdp.rb

Overview

UDP connection used for SSDP by discover().

Instance Method Summary collapse

Constructor Details

#initialize(type, timeout, receiver) ⇒ SSDPConnection

type - the SSDP service type (used in the ST: field) timeout - the number of seconds to wait for responses (used in the MX: field) receiver is the block passed to discover().



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nlhue/ssdp.rb', line 69

def initialize type, timeout, receiver
	super
	@type = type
	@timeout = timeout
	@receiver = receiver
	@msg = "M-SEARCH * HTTP/1.1\r\n" +
	"HOST: #{SSDP_ADDR}:#{SSDP_PORT}\r\n" +
	"MAN: ssdp:discover\r\n" +
	"MX: #{timeout.to_i}\r\n" +
	"ST: #{type}\r\n" +
	"\r\n"
end

Instance Method Details

#closed?Boolean

Indicates whether shutdown has been called.

Returns:

  • (Boolean)


101
102
103
# File 'lib/nlhue/ssdp.rb', line 101

def closed?
	@receiver.nil?
end

#post_initObject



82
83
84
85
86
87
# File 'lib/nlhue/ssdp.rb', line 82

def post_init
	send_datagram @msg, SSDP_ADDR, SSDP_PORT
	EM.add_timer(0.5) do
		send_datagram @msg, SSDP_ADDR, SSDP_PORT
	end
end

#receive_data(data) ⇒ Object



89
90
91
92
# File 'lib/nlhue/ssdp.rb', line 89

def receive_data data
	port, ip = Socket.unpack_sockaddr_in(get_peername)
	@receiver.call Response.new(ip, data) if @receiver
end

#shutdownObject

Closes the UDP socket and ignores any future SSDP messages.



95
96
97
98
# File 'lib/nlhue/ssdp.rb', line 95

def shutdown
	@receiver = nil
	close_connection
end