class Discovery::UDP

Attributes

discoverables[R]
group[RW]
port[RW]
thread[R]

Public Class Methods

beacon_to_hash(beacon) click to toggle source

Convert the incoming beacon to a Hash containing all necessary info Implement to something nontrivial in the inherited module

# File lib/discovery/udp.rb, line 49
def self.beacon_to_hash beacon
  {beacon:beacon.to_s, sender:beacon.source}
end
halt() click to toggle source

Halt the discovery thread started by listen, but do not unregister the callback

# File lib/discovery/udp.rb, line 38
def self.halt; @thread.kill; end
listen(&block) click to toggle source

Register a callback to be used in the event of a recognized beacon. Each non-nil response object for each UDP beacon will be yielded to it.

# File lib/discovery/udp.rb, line 20
def self.listen &block
  @blocks ||= []
  (@blocks << block).uniq!
  
  @thread ||= Thread.new do
    loop do
      @discoverables.values.each_with_object(next_beacon) do |blk,beacon|
        obj = blk.call beacon
        @blocks.each do |callback|
          callback.call(obj)
        end unless obj.nil?
      end
    end
  end
end
new() click to toggle source
# File lib/discovery/udp.rb, line 16
def self.new; raise NotImplementedError nil end
next_beacon(timeout:nil) click to toggle source

Return the info hash of the next DDDP beacon detected - blocking Optional :timeout keyword argument specifies maximum time to block

# File lib/discovery/udp.rb, line 42
def self.next_beacon timeout:nil
  @udp_rx ||= Util::UDP::RX.new(@group, @port)
  beacon_to_hash(Timeout.timeout(timeout) { @udp_rx.gets })
end
register(cls, &block) click to toggle source
# File lib/discovery/udp.rb, line 58
def self.register cls, &block
  @discoverables ||= {}
  @discoverables[cls] = block
end