Class: Discovery::UDP

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.discoverablesObject (readonly)

Returns the value of attribute discoverables.



51
52
53
# File 'lib/discovery/udp.rb', line 51

def discoverables
  @discoverables
end

Class Method Details

.beacon_to_hash(beacon) ⇒ Object

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



43
44
45
# File 'lib/discovery/udp.rb', line 43

def self.beacon_to_hash beacon
  {beacon:beacon}
end

.haltObject

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



32
# File 'lib/discovery/udp.rb', line 32

def self.halt; @thread.kill; end

.listen(&block) ⇒ Object

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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/discovery/udp.rb', line 14

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

.newObject



10
# File 'lib/discovery/udp.rb', line 10

def self.new; raise NotImplementedError nil end

.next_beacon(timeout: nil) ⇒ Object

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



36
37
38
39
# File 'lib/discovery/udp.rb', line 36

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) ⇒ Object



52
53
54
55
# File 'lib/discovery/udp.rb', line 52

def self.register cls, &block
  @discoverables ||= {}
  @discoverables[cls] = block
end