class CZTop::Beacon
Used for LAN discovery and presence.
This is implemented using an {Actor}.
Constants
- MAX_BEACON_DATA
@return [Integer] maximum length of data to {#publish}
- ZBEACON_FPTR
function pointer to the +zbeacon()+ function
Attributes
@return [Actor] the actor behind this Beacon
Public Class Methods
Initialize new Beacon
.
# File lib/cztop/beacon.rb, line 21 def initialize @actor = Actor.new(ZBEACON_FPTR) end
Public Instance Methods
Run the beacon on the specified UDP port.
@param port [Integer] port number to @return [String] hostname, which can be used as endpoint for incoming
connections
@raise [Interrupt] if the context was terminated or the process
interrupted
@raise [NotImplementedError] if the system doesn’t support UDP broadcasts
# File lib/cztop/beacon.rb, line 50 def configure(port) @actor.send_picture('si', :string, 'CONFIGURE', :int, port) ptr = Zstr.recv(@actor) # NULL if context terminated or interrupted HasFFIDelegate.raise_zmq_err if ptr.null? hostname = ptr.read_string return hostname unless hostname.empty? raise NotImplementedError, "system doesn't support UDP broadcasts" end
Just like {#subscribe}, but subscribe to all peer beacons. @return [void]
# File lib/cztop/beacon.rb, line 97 def listen @actor.send_picture('sb', :string, 'SUBSCRIBE', :string, nil, :int, 0) end
Start broadcasting a beacon. @param data [String] data to publish @param interval [Integer] interval in msec @raise [ArgumentError] if data is longer than {MAX_BEACON_DATA} bytes @return [void]
# File lib/cztop/beacon.rb, line 71 def publish(data, interval) raise ArgumentError, 'data too long' if data.bytesize > MAX_BEACON_DATA @actor.send_picture('sbi', :string, 'PUBLISH', :string, data, :int, data.bytesize, :int, interval) end
Receive next beacon from a peer. @return [Message] 2-frame message with ([ipaddr, data])
# File lib/cztop/beacon.rb, line 112 def receive @actor.receive end
Stop broadcasting the beacon. @return [void]
# File lib/cztop/beacon.rb, line 81 def silence @actor << 'SILENCE' end
Start listening to beacons from peers. @param filter [String] do a prefix match on received beacons @return [void]
# File lib/cztop/beacon.rb, line 89 def subscribe(filter) @actor.send_picture('sb', :string, 'SUBSCRIBE', :string, filter, :int, filter.bytesize) end
Terminates the beacon. @return [void]
# File lib/cztop/beacon.rb, line 30 def terminate @actor.terminate end
Stop listening to other peers. @return [void]
# File lib/cztop/beacon.rb, line 105 def unsubscribe @actor << 'UNSUBSCRIBE' end
Enable verbose logging of commands and activity. @return [void]
# File lib/cztop/beacon.rb, line 37 def verbose! @actor << 'VERBOSE' end