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.



18
19
20
# File 'lib/cztop/beacon.rb', line 18

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



23
24
25
# File 'lib/cztop/beacon.rb', line 23

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



45
46
47
48
49
50
51
52
53
54
55
56
# 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

#listenvoid

This method returns an undefined value.

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



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

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:



66
67
68
69
70
# 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

#receiveMessage

Receive next beacon from a peer.

Returns:

  • (Message)

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



101
102
103
# File 'lib/cztop/beacon.rb', line 101

def receive
  @actor.receive
end

#silencevoid

This method returns an undefined value.

Stop broadcasting the beacon.



74
75
76
# File 'lib/cztop/beacon.rb', line 74

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



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

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.



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

def terminate
  @actor.terminate
end

#unsubscribevoid

This method returns an undefined value.

Stop listening to other peers.



95
96
97
# File 'lib/cztop/beacon.rb', line 95

def unsubscribe
  @actor << "UNSUBSCRIBE"
end

#verbose!void

This method returns an undefined value.

Enable verbose logging of commands and activity.



33
34
35
# File 'lib/cztop/beacon.rb', line 33

def verbose!
  @actor << "VERBOSE"
end