class CZTop::Beacon

Used for LAN discovery and presence.

This is implemented using an {Actor}.

@see api.zeromq.org/czmq3-0:zbeacon

Constants

MAX_BEACON_DATA

@return [Integer] maximum length of data to {#publish}

ZBEACON_FPTR

function pointer to the +zbeacon()+ function

Attributes

actor[R]

@return [Actor] the actor behind this Beacon

Public Class Methods

new() click to toggle source

Initialize new Beacon.

# File lib/cztop/beacon.rb, line 18
def initialize
  @actor = Actor.new(ZBEACON_FPTR)
end

Public Instance Methods

configure(port) click to toggle source

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 45
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
listen() click to toggle source

Just like {#subscribe}, but subscribe to all peer beacons. @return [void]

# File lib/cztop/beacon.rb, line 88
def listen
  @actor.send_picture("sb", :string, "SUBSCRIBE",
                      :string, nil, :int, 0)
end
publish(data, interval) click to toggle source

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 66
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() click to toggle source

Receive next beacon from a peer. @return [Message] 2-frame message with ([ipaddr, data])

# File lib/cztop/beacon.rb, line 101
def receive
  @actor.receive
end
silence() click to toggle source

Stop broadcasting the beacon. @return [void]

# File lib/cztop/beacon.rb, line 74
def silence
  @actor << "SILENCE"
end
subscribe(filter) click to toggle source

Start listening to beacons from peers. @param filter [String] do a prefix match on received beacons @return [void]

# File lib/cztop/beacon.rb, line 81
def subscribe(filter)
  @actor.send_picture("sb", :string, "SUBSCRIBE",
                      :string, filter, :int, filter.bytesize)
end
terminate() click to toggle source

Terminates the beacon. @return [void]

# File lib/cztop/beacon.rb, line 27
def terminate
  @actor.terminate
end
unsubscribe() click to toggle source

Stop listening to other peers. @return [void]

# File lib/cztop/beacon.rb, line 95
def unsubscribe
  @actor << "UNSUBSCRIBE"
end
verbose!() click to toggle source

Enable verbose logging of commands and activity. @return [void]

# File lib/cztop/beacon.rb, line 33
def verbose!
  @actor << "VERBOSE"
end