Class: CZTop::Beacon
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.
255
Instance Attribute Summary collapse
-
#actor ⇒ Actor
readonly
The actor behind this Beacon.
Instance Method Summary collapse
-
#configure(port) ⇒ String
Run the beacon on the specified UDP port.
-
#initialize ⇒ Beacon
constructor
Initialize new Beacon.
-
#listen ⇒ void
Just like #subscribe, but subscribe to all peer beacons.
-
#publish(data, interval) ⇒ void
Start broadcasting a beacon.
-
#receive ⇒ Message
Receive next beacon from a peer.
-
#silence ⇒ void
Stop broadcasting the beacon.
-
#subscribe(filter) ⇒ void
Start listening to beacons from peers.
-
#terminate ⇒ void
Terminates the beacon.
-
#unsubscribe ⇒ void
Stop listening to other peers.
-
#verbose! ⇒ void
Enable verbose logging of commands and activity.
Constructor Details
#initialize ⇒ Beacon
Initialize new Beacon.
21 22 23 |
# File 'lib/cztop/beacon.rb', line 21 def initialize @actor = Actor.new(ZBEACON_FPTR) end |
Instance Attribute Details
#actor ⇒ Actor (readonly)
Returns 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.
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 |
#listen ⇒ void
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.
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 |
#receive ⇒ Message
Receive next beacon from a peer.
112 113 114 |
# File 'lib/cztop/beacon.rb', line 112 def receive @actor.receive end |
#silence ⇒ void
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.
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 |
#terminate ⇒ void
This method returns an undefined value.
Terminates the beacon.
30 31 32 |
# File 'lib/cztop/beacon.rb', line 30 def terminate @actor.terminate end |
#unsubscribe ⇒ void
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 |