class Discovery::Util::UDP::RX

Public Instance Methods

configure() click to toggle source
# File lib/discovery/util/udp.rb, line 71
def configure
  # Add membership to the multicast group
  @socket.setsockopt Socket::IPPROTO_IP,
                     Socket::IP_ADD_MEMBERSHIP,
                     IPAddr.new(@group).hton + IPAddr.new("0.0.0.0").hton
  # Don't prevent future listening peers on the same machine
  @socket.setsockopt(Socket::SOL_SOCKET,
                     Socket::SO_REUSEADDR,
                     [1].pack('i')) unless @selfish
  # Bind the socket to the specified port or any open port on the machine
  @socket.bind Socket::INADDR_ANY, @port
end
gets() click to toggle source
# File lib/discovery/util/udp.rb, line 84
def gets
  msg, addrinfo = @socket.recvfrom(UDP.max_length)
  msg.instance_variable_set :@source, addrinfo[3].to_s+':'+addrinfo[1].to_s
  class << msg;  attr_reader :source;  end
  msg
end
test!(message=" click to toggle source
# File lib/discovery/util/udp.rb, line 91
def test!(message="#{self}.test!")
  tx = UDP::TX.new @group, @port
  rx = self
  
  outer_thread = Thread.current
  passed = false
  thr = Thread.new do
    rx.gets
    passed = true
    outer_thread.wakeup
  end
  Thread.pass
  
  tx.puts message
  sleep 1 if thr.status
  thr.kill
  tx.close
  
  passed
end