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

actor[R]

@return [Actor] the actor behind this monitor

Public Class Methods

new(socket) click to toggle source

@param socket [Socket, Actor] the socket or actor to monitor

# File lib/cztop/monitor.rb, line 21
def initialize(socket)
  @actor = Actor.new(ZMONITOR_FPTR, socket)
end

Public Instance Methods

fd() click to toggle source

Useful for registration in an event-loop. @return [Integer] the FD @see ZsockOptions#fd

# File lib/cztop/monitor.rb, line 81
def fd
  @actor.fd
end
listen(*events) click to toggle source

Configure monitor to listen for specific events. @param events [String] one or more events from {EVENTS} @return [void]

# File lib/cztop/monitor.rb, line 63
def listen(*events)
  events.each do |event|
    EVENTS.include?(event) or
      raise ArgumentError, "invalid event: #{event.inspect}"
  end
  @actor << [ "LISTEN", *events ]
end
next() click to toggle source

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 112
def next
  @actor.receive
end
readable?() click to toggle source

@return [Boolean] whether there's at least one event available

# File lib/cztop/monitor.rb, line 86
def readable?
  @actor.readable?
end
start() click to toggle source

Start the monitor. After this, you can read events using {#next}. @return [void]

# File lib/cztop/monitor.rb, line 73
def start
  @actor << "START"
  @actor.wait
end
terminate() click to toggle source

Terminates the monitor. @return [void]

# File lib/cztop/monitor.rb, line 30
def terminate
  @actor.terminate
end
verbose!() click to toggle source

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

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