class CZTop::Monitor
CZMQ monitor. Listen for socket events.
This is implemented using an {Actor}.
@note This works only on connection oriented transports, like TCP, IPC,
and TIPC.
@see api.zeromq.org/czmq3-0:zmonitor @see api.zeromq.org/4-1:zmq-socket-monitor
Constants
- EVENTS
@return [Array<String>] types of valid events
- ZMONITOR_FPTR
function pointer to the +zmonitor()+ function
Attributes
@return [Actor] the actor behind this monitor
Public Class Methods
@param socket [Socket, Actor] the socket or actor to monitor
# File lib/cztop/monitor.rb, line 24 def initialize(socket) @actor = Actor.new(ZMONITOR_FPTR, socket) end
Public Instance Methods
Useful for registration in an event-loop. @return [Integer] the FD @see ZsockOptions#fd
# File lib/cztop/monitor.rb, line 87 def fd @actor.fd end
Configure monitor to listen for specific events. @param events [String] one or more events from {EVENTS} @return [void]
# File lib/cztop/monitor.rb, line 67 def listen(*events) events.each do |event| EVENTS.include?(event) or raise ArgumentError, "invalid event: #{event.inspect}" end @actor << ['LISTEN', *events] end
Get next event. This blocks until the next event is available. @example
socket = CZTop::Socket::ROUTER.new("tcp://127.0.0.1:5050") # ... normal stuff with socket # do this in another thread, or using a Poller, so it's possible to # interact with the socket and the monitor Thread.new do monitor = CZTop::Monitor.new(socket) monitor.listen "CONNECTED", "DISCONNECTED" while event = monitor.next case event[0] when "CONNECTED" puts "a client has connected" when "DISCONNECTED" puts "a client has disconnected" end end end
@return [Message] one of the events from (after {#to_a} it’s something like
<tt>["ACCEPTED", "73", "tcp://127.0.0.1:55585"]</tt>)
# File lib/cztop/monitor.rb, line 120 def next @actor.receive end
@return [Boolean] whether there’s at least one event available
# File lib/cztop/monitor.rb, line 93 def readable? @actor.readable? end
Start the monitor. After this, you can read events using {#next}. @return [void]
# File lib/cztop/monitor.rb, line 78 def start @actor << 'START' @actor.wait end
Terminates the monitor. @return [void]
# File lib/cztop/monitor.rb, line 33 def terminate @actor.terminate end
Enable verbose logging of commands and activity. @return [void]
# File lib/cztop/monitor.rb, line 40 def verbose! @actor << 'VERBOSE' end