class BWA::Discovery

Public Class Methods

advertise() click to toggle source
discover(timeout = 5, exhaustive = false) click to toggle source
# File lib/bwa/discovery.rb, line 6
def discover(timeout = 5, exhaustive = false)
  socket = UDPSocket.new
  socket.bind("0.0.0.0", 0)
  socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
  socket.sendmsg("Discovery: Who is out there?", 0, Socket.sockaddr_in(30303, '255.255.255.255'))
  spas = {}
  loop do
    if IO.select([socket], nil, nil, timeout)
      msg, ip = socket.recvfrom(64)
      ip = ip[2]
      name, mac = msg.split("\r\n")
      name.strip!
      if mac.start_with?("00-15-27-")
        spas[ip] = name
        break unless exhaustive
      end
    else
      break
    end
  end
  spas
end