Class: BWA::Discovery

Inherits:
Object
  • Object
show all
Defined in:
lib/bwa/discovery.rb

Class Method Summary collapse

Class Method Details



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bwa/discovery.rb', line 30

def advertise
  socket = UDPSocket.new
  socket.bind("0.0.0.0", 30_303)
  msg = "BWGSPA\r\n00-15-27-00-00-01\r\n"
  loop do
    data, addr = socket.recvfrom(32)
    next unless data == "Discovery: Who is out there?"

    ip = addr.last
    BWA.logger.info "Advertising to #{ip}"
    socket.sendmsg(msg, 0, Socket.sockaddr_in(addr[1], ip))
  end
end

.discover(timeout = 5, exhaustive: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bwa/discovery.rb', line 9

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(30_303, "255.255.255.255"))
  spas = {}
  loop do
    break unless socket.wait_readable(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
  end
  spas
end