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
]

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


21
22
23
# File 'lib/cztop/monitor.rb', line 21

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


26
27
28
# File 'lib/cztop/monitor.rb', line 26

def actor
  @actor
end

Instance Method Details

#fdInteger

Useful for registration in an event-loop.

Returns:

  • (Integer)

    the FD

See Also:


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

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


63
64
65
66
67
68
69
# 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

#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"])


112
113
114
# File 'lib/cztop/monitor.rb', line 112

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


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

def readable?
  @actor.readable?
end

#startvoid

This method returns an undefined value.

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


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

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

#terminatevoid

This method returns an undefined value.

Terminates the monitor.


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

def terminate
  @actor.terminate
end

#verbose!void

This method returns an undefined value.

Enable verbose logging of commands and activity.


36
37
38
# File 'lib/cztop/monitor.rb', line 36

def verbose!
  @actor << "VERBOSE"
end