module Roku::Discover
Constants
- BIND_ADDR
- MULTICAST_ADDR
- PORT
- REQUEST
Public Class Methods
await_response()
click to toggle source
# File lib/roku/discover.rb, line 34 def await_response Timeout.timeout(10) do loop do response, = socket.recvfrom(1024) if response.include?('LOCATION') && response.include?('200 OK') return response end end end end
search()
click to toggle source
# File lib/roku/discover.rb, line 18 def search bind socket.send(REQUEST, 0, MULTICAST_ADDR, PORT) begin parse_address(await_response) rescue Timeout::Error raise DeviceNotFound, "Roku's automatic device discovery failed. If you continue " \ "to receive this message, you should try restarting your " \ "router, and you may want to manually configure this. You " \ "can do this by finding the device's IP via your router " \ "and setting the ROKU_HOST environment variable to " \ "\"http://{{device-ip}}:8060\"." end end
Private Class Methods
bind()
click to toggle source
# File lib/roku/discover.rb, line 47 def bind return if @bound socket.bind(BIND_ADDR, PORT) @bound = true end
bind_address()
click to toggle source
# File lib/roku/discover.rb, line 65 def bind_address IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new(BIND_ADDR).hton end
parse_address(response)
click to toggle source
# File lib/roku/discover.rb, line 53 def parse_address(response) response.scan(/LOCATION: (.*)\r/).first.first end
socket()
click to toggle source
# File lib/roku/discover.rb, line 57 def socket @socket ||= UDPSocket.open.tap do |socket| socket.setsockopt(:IPPROTO_IP, :IP_ADD_MEMBERSHIP, bind_address) socket.setsockopt(:IPPROTO_IP, :IP_MULTICAST_TTL, 1) socket.setsockopt(:SOL_SOCKET, :SO_REUSEPORT, 1) end end