Class: CZTop::Monitor

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

Overview

Note:

This works only on connection oriented transports, like TCP, IPC, and TIPC.

CZMQ monitor. Listen for socket events.

This is implemented using an Actor.

Constant Summary collapse

ZMONITOR_FPTR =

function pointer to the zmonitor() function

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

Returns types of valid events.

Returns:

  • (Array<String>)

    types of valid events

%w[
  ALL
  CONNECTED
  CONNECT_DELAYED
  CONNECT_RETRIED
  LISTENING
  BIND_FAILED
  ACCEPTED
  ACCEPT_FAILED
  CLOSED
  CLOSE_FAILED
  DISCONNECTED
  MONITOR_STOPPED
  HANDSHAKE_SUCCEEDED
  HANDSHAKE_FAILED_NO_DETAIL
  HANDSHAKE_FAILED_PROTOCOL
  HANDSHAKE_FAILED_AUTH
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Monitor

Returns a new instance of Monitor.

Parameters:

  • socket (Socket, Actor)

    the socket or actor to monitor



24
25
26
# File 'lib/cztop/monitor.rb', line 24

def initialize(socket)
  @actor = Actor.new(ZMONITOR_FPTR, socket)
end

Instance Attribute Details

#actorActor (readonly)

Returns the actor behind this monitor.

Returns:

  • (Actor)

    the actor behind this monitor



29
30
31
# File 'lib/cztop/monitor.rb', line 29

def actor
  @actor
end

Instance Method Details

#fdInteger

Useful for registration in an event-loop.

Returns:

  • (Integer)

    the FD

See Also:



87
88
89
# File 'lib/cztop/monitor.rb', line 87

def fd
  @actor.fd
end

#listen(*events) ⇒ void

This method returns an undefined value.

Configure monitor to listen for specific events.

Parameters:

  • events (String)

    one or more events from EVENTS



67
68
69
70
71
72
73
# 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

#nextMessage

Get next event. This blocks until the next event is available.

Examples:

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

Returns:

  • (Message)

    one of the events from (after #to_a it’s something like ["ACCEPTED", "73", "tcp://127.0.0.1:55585"])



120
121
122
# File 'lib/cztop/monitor.rb', line 120

def next
  @actor.receive
end

#readable?Boolean

Returns whether there’s at least one event available.

Returns:

  • (Boolean)

    whether there’s at least one event available



93
94
95
# File 'lib/cztop/monitor.rb', line 93

def readable?
  @actor.readable?
end

#startvoid

This method returns an undefined value.

Start the monitor. After this, you can read events using #next.



78
79
80
81
# File 'lib/cztop/monitor.rb', line 78

def start
  @actor << 'START'
  @actor.wait
end

#terminatevoid

This method returns an undefined value.

Terminates the monitor.



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

def terminate
  @actor.terminate
end

#verbose!void

This method returns an undefined value.

Enable verbose logging of commands and activity.



40
41
42
# File 'lib/cztop/monitor.rb', line 40

def verbose!
  @actor << 'VERBOSE'
end