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

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