Class: PingPongPear::Broadcaster
- Inherits:
-
Object
- Object
- PingPongPear::Broadcaster
- Defined in:
- lib/ping_pong_pear.rb
Class Method Summary collapse
- .my_public_address ⇒ Object
- .send_location(name, address, port) ⇒ Object
- .send_update(args) ⇒ Object
- .where_is?(name) ⇒ Boolean
Instance Method Summary collapse
-
#initialize ⇒ Broadcaster
constructor
A new instance of Broadcaster.
- #send(message) ⇒ Object
Constructor Details
#initialize ⇒ Broadcaster
Returns a new instance of Broadcaster.
40 41 42 43 44 45 |
# File 'lib/ping_pong_pear.rb', line 40 def initialize @multicast_addr = MULTICAST_ADDR @port = UDP_PORT @socket = UDPSocket.open @socket.setsockopt :IPPROTO_IP, :IP_MULTICAST_TTL, 1 end |
Class Method Details
.my_public_address ⇒ Object
11 12 13 14 15 |
# File 'lib/ping_pong_pear.rb', line 11 def self.my_public_address Socket.ip_address_list.reject { |addr| addr.ipv4_loopback? || addr.ipv6_loopback? || addr.ipv6_linklocal? }.first end |
.send_location(name, address, port) ⇒ Object
21 22 23 |
# File 'lib/ping_pong_pear.rb', line 21 def self.send_location name, address, port new.send ['source', name, address, port] end |
.send_update(args) ⇒ Object
17 18 19 |
# File 'lib/ping_pong_pear.rb', line 17 def self.send_update args new.send ['commit'] + args end |
.where_is?(name) ⇒ Boolean
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ping_pong_pear.rb', line 25 def self.where_is? name listener_t = Thread.new { listener = Listener.new listener.start { |cmd, n, *rest| break(rest) if cmd == 'source' && n == name } } broadcast = new while listener_t.alive? broadcast.send ['locate', name] end addr, port = listener_t.value "http://#{addr}:#{port}/" end |
Instance Method Details
#send(message) ⇒ Object
47 48 49 |
# File 'lib/ping_pong_pear.rb', line 47 def send @socket.send(JSON.dump(), 0, @multicast_addr, @port) end |