Class: CZTop::Beacon

Inherits:
Object
  • Object
show all
Includes:
CZMQ::FFI
Defined in:
lib/cztop/beacon.rb

Overview

Used for LAN discovery and presence.

This is implemented using an Actor.

Constant Summary collapse

ZBEACON_FPTR =

function pointer to the zbeacon() function

::CZMQ::FFI.ffi_libraries.each do |dl|
  fptr = dl.find_function('zbeacon')
  break fptr if fptr
end
MAX_BEACON_DATA =

Returns maximum length of data to #publish.

Returns:

  • (Integer)

    maximum length of data to #publish

255

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeacon

Initialize new Beacon.



21
22
23
# File 'lib/cztop/beacon.rb', line 21

def initialize
  @actor = Actor.new(ZBEACON_FPTR)
end

Instance Attribute Details

#actorActor (readonly)

Returns the actor behind this Beacon.

Returns:

  • (Actor)

    the actor behind this Beacon



26
27
28
# File 'lib/cztop/beacon.rb', line 26

def actor
  @actor
end

Instance Method Details

#configure(port) ⇒ String

Run the beacon on the specified UDP port.

Parameters:

  • port (Integer)

    port number to

Returns:

  • (String)

    hostname, which can be used as endpoint for incoming connections

Raises:

  • (Interrupt)

    if the context was terminated or the process interrupted

  • (NotImplementedError)

    if the system doesn’t support UDP broadcasts



50
51
52
53
54
55
56
57
58
59
60
61
# 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

#listenvoid

This method returns an undefined value.

Just like #subscribe, but subscribe to all peer beacons.



97
98
99
100
# File 'lib/cztop/beacon.rb', line 97

def listen
  @actor.send_picture('sb', :string, 'SUBSCRIBE',
                      :string, nil, :int, 0)
end

#publish(data, interval) ⇒ void

This method returns an undefined value.

Start broadcasting a beacon.

Parameters:

  • data (String)

    data to publish

  • interval (Integer)

    interval in msec

Raises:



71
72
73
74
75
76
# 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

#receiveMessage

Receive next beacon from a peer.

Returns:

  • (Message)

    2-frame message with ([ipaddr, data])



112
113
114
# File 'lib/cztop/beacon.rb', line 112

def receive
  @actor.receive
end

#silencevoid

This method returns an undefined value.

Stop broadcasting the beacon.



81
82
83
# File 'lib/cztop/beacon.rb', line 81

def silence
  @actor << 'SILENCE'
end

#subscribe(filter) ⇒ void

This method returns an undefined value.

Start listening to beacons from peers.

Parameters:

  • filter (String)

    do a prefix match on received beacons



89
90
91
92
# File 'lib/cztop/beacon.rb', line 89

def subscribe(filter)
  @actor.send_picture('sb', :string, 'SUBSCRIBE',
                      :string, filter, :int, filter.bytesize)
end

#terminatevoid

This method returns an undefined value.

Terminates the beacon.



30
31
32
# File 'lib/cztop/beacon.rb', line 30

def terminate
  @actor.terminate
end

#unsubscribevoid

This method returns an undefined value.

Stop listening to other peers.



105
106
107
# File 'lib/cztop/beacon.rb', line 105

def unsubscribe
  @actor << 'UNSUBSCRIBE'
end

#verbose!void

This method returns an undefined value.

Enable verbose logging of commands and activity.



37
38
39
# File 'lib/cztop/beacon.rb', line 37

def verbose!
  @actor << 'VERBOSE'
end