Class: Discovery::Util::UDP::RX
- Defined in:
- lib/discovery/util/udp.rb
Instance Attribute Summary
Attributes inherited from Xceiver
#group, #local_ip, #local_port, #port, #socket
Instance Method Summary collapse
Methods inherited from Xceiver
#close, #initialize, new, #open
Constructor Details
This class inherits a constructor from Discovery::Util::UDP::Xceiver
Instance Method Details
#configure ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# 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 ⇒ Object
84 85 86 87 88 89 |
# 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 = "#{self}.test!") ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/discovery/util/udp.rb', line 91 def test!(="#{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 sleep 1 if thr.status thr.kill tx.close passed end |