class CZTop::ZsockOptions::OptionsAccessor

Used to access the options of a {Socket} or {Actor}.

Constants

MECHANISMS

supported security mechanisms and their macro value equivalent

Attributes

zocket[R]

@return [Socket, Actor] whose options this {OptionsAccessor} instance

is accessing

Public Class Methods

new(zocket) click to toggle source

@param zocket [Socket, Actor]

# File lib/cztop/zsock_options.rb, line 60
def initialize(zocket)
  @zocket = zocket
end

Public Instance Methods

CURVE_key(key_name) click to toggle source

Get one of the CURVE keys. @param key_name [Symbol] something like :curve_serverkey @return [String, nil] key, if CURVE is supported and active, or nil

# File lib/cztop/zsock_options.rb, line 182
def CURVE_key(key_name)
  return nil if mechanism != :CURVE

  ptr = Zsock.__send__(key_name, @zocket)
  return nil if ptr.null?

  ptr.read_string
end
CURVE_publickey() click to toggle source

@return [String] Z85 encoded public key set @return [nil] if the current mechanism isn’t CURVE or CURVE isn’t

supported
# File lib/cztop/zsock_options.rb, line 220
def CURVE_publickey
  CURVE_key(:curve_publickey)
end
CURVE_secretkey() click to toggle source

@return [String] Z85 encoded secret key set @return [nil] if the current mechanism isn’t CURVE or CURVE isn’t

supported
# File lib/cztop/zsock_options.rb, line 212
def CURVE_secretkey
  CURVE_key(:curve_secretkey)
end
CURVE_server=(bool) click to toggle source

Make this zocket a CURVE server. @param bool [Boolean] @note You’ll have to use a {CZTop::Authenticator}.

# File lib/cztop/zsock_options.rb, line 166
def CURVE_server=(bool)
  Zsock.set_curve_server(@zocket, bool ? 1 : 0)
end
CURVE_server?() click to toggle source

@return [Boolean] whether this zocket is a CURVE server

# File lib/cztop/zsock_options.rb, line 158
def CURVE_server?
  Zsock.curve_server(@zocket).positive?
end
CURVE_serverkey() click to toggle source

@return [String] Z85 encoded server key set @return [nil] if the current mechanism isn’t CURVE or CURVE isn’t

supported
# File lib/cztop/zsock_options.rb, line 174
def CURVE_serverkey
  CURVE_key(:curve_serverkey)
end
CURVE_serverkey=(key) click to toggle source

Sets the server’s public key, so the zocket can authenticate the remote server. @param key [String] Z85 (40 bytes) or binary (32 bytes) server key @raise [ArgumentError] if key has wrong size

# File lib/cztop/zsock_options.rb, line 196
def CURVE_serverkey=(key)
  case key.bytesize
  when 40
    Zsock.set_curve_serverkey(@zocket, key)
  when 32
    ptr = ::FFI::MemoryPointer.from_string(key)
    Zsock.set_curve_serverkey_bin(@zocket, ptr)
  else
    raise ArgumentError, format('invalid server key: %p', key)
  end
end
PLAIN_password() click to toggle source

@return [String] password set for PLAIN mechanism @return [nil] if the current mechanism isn’t PLAIN

# File lib/cztop/zsock_options.rb, line 294
def PLAIN_password
  return nil if mechanism != :PLAIN

  Zsock.plain_password(@zocket).read_string
end
PLAIN_password=(password) click to toggle source

@param password [String] password for PLAIN mechanism

# File lib/cztop/zsock_options.rb, line 302
def PLAIN_password=(password)
  Zsock.set_plain_password(@zocket, password)
end
PLAIN_server=(bool) click to toggle source

Make this zocket a PLAIN server. @param bool [Boolean] @note You’ll have to use a {CZTop::Authenticator}.

# File lib/cztop/zsock_options.rb, line 271
def PLAIN_server=(bool)
  Zsock.set_plain_server(@zocket, bool ? 1 : 0)
end
PLAIN_server?() click to toggle source

@return [Boolean] whether this zocket is a PLAIN server

# File lib/cztop/zsock_options.rb, line 263
def PLAIN_server?
  Zsock.plain_server(@zocket).positive?
end
PLAIN_username() click to toggle source

@return [String] username set for PLAIN mechanism @return [nil] if the current mechanism isn’t PLAIN

# File lib/cztop/zsock_options.rb, line 278
def PLAIN_username
  return nil if mechanism != :PLAIN

  Zsock.plain_username(@zocket).read_string
end
PLAIN_username=(username) click to toggle source

@param username [String] username for PLAIN mechanism @note You’ll have to use a {CZTop::Authenticator}.

# File lib/cztop/zsock_options.rb, line 287
def PLAIN_username=(username)
  Zsock.set_plain_username(@zocket, username)
end
[](option_name) click to toggle source

Fuzzy option getter. This is to make it easier when porting applications from CZMQ libraries to CZTop.

@param option_name [Symbol, String] case insensitive option name @raise [NoMethodError] if option name can’t be recognized

# File lib/cztop/zsock_options.rb, line 70
def [](option_name)
  meth1 = :"#{option_name}"
  meth2 = :"#{option_name}?"

  if respond_to? meth1
    meth = meth1
  elsif respond_to? meth2
    meth = meth2
  else
    # NOTE: beware of predicates, especially #CURVE_server? & friends
    meth = public_methods.grep_v(/=$/)
                         .find { |m| m =~ /^#{option_name}\??$/i }
    raise NoMethodError, option_name if meth.nil?
  end

  __send__(meth)
end
[]=(option_name, new_value) click to toggle source

Fuzzy option setter. This is to make it easier when porting applications from CZMQ libraries to CZTop.

@param option_name [Symbol, String] case insensitive option name @param new_value [String, Integer] new value @raise [NoMethodError] if option name can’t be recognized

# File lib/cztop/zsock_options.rb, line 95
def []=(option_name, new_value)
  meth = :"#{option_name}="

  unless respond_to? meth
    meth = public_methods.find { |m| m =~ /^#{option_name}=$/i }
    raise NoMethodError, option_name if meth.nil?
  end

  __send__(meth, new_value)
end
events() click to toggle source

@return [Integer] socket events (readable/writable) @see CZTop::Poller::ZMQ::POLLIN and CZTop::Poller::ZMQ::POLLOUT

# File lib/cztop/zsock_options.rb, line 480
def events
  Zsock.events(@zocket)
end
fd() click to toggle source

@return [Integer] socket file descriptor

# File lib/cztop/zsock_options.rb, line 473
def fd
  Zsock.fd(@zocket)
end
heartbeat_ivl() click to toggle source

@return [Integer] current value of Heartbeat IVL

# File lib/cztop/zsock_options.rb, line 391
def heartbeat_ivl
  Zsock.heartbeat_ivl(@zocket)
end
heartbeat_ivl=(new_value) click to toggle source

@param new_value [Integer] new value for Heartbeat IVL

# File lib/cztop/zsock_options.rb, line 397
def heartbeat_ivl=(new_value)
  raise ArgumentError, 'invalid IVL' unless new_value >= 0

  Zsock.set_heartbeat_ivl(@zocket, new_value)
end
heartbeat_timeout() click to toggle source

@return [Integer] current value of Heartbeat Timeout

# File lib/cztop/zsock_options.rb, line 423
def heartbeat_timeout
  Zsock.heartbeat_timeout(@zocket)
end
heartbeat_timeout=(new_value) click to toggle source

@param new_value [Integer] new value for Heartbeat Timeout

# File lib/cztop/zsock_options.rb, line 429
def heartbeat_timeout=(new_value)
  raise ArgumentError, 'invalid timeout' unless new_value >= 0

  Zsock.set_heartbeat_timeout(@zocket, new_value)
end
heartbeat_ttl() click to toggle source

@return [Integer] current value of Heartbeat TTL, in milliseconds

# File lib/cztop/zsock_options.rb, line 405
def heartbeat_ttl
  Zsock.heartbeat_ttl(@zocket)
end
heartbeat_ttl=(new_value) click to toggle source

@param new_value [Integer] new value for Heartbeat TTL, in

milliseconds

@note The value will internally be rounded to the nearest decisecond.

So a value of less than 100 will have no effect.
# File lib/cztop/zsock_options.rb, line 414
def heartbeat_ttl=(new_value)
  raise ArgumentError, "invalid TTL: #{new_value}" unless new_value.is_a? Integer
  raise ArgumentError, "TTL out of range: #{new_value}" unless (0..65_536).include? new_value

  Zsock.set_heartbeat_ttl(@zocket, new_value)
end
identity() click to toggle source

@return [String] current socket identity

# File lib/cztop/zsock_options.rb, line 360
def identity
  Zsock.identity(@zocket).read_string
end
identity=(identity) click to toggle source

@param identity [String] new socket identity @raise [ArgumentError] if identity is invalid

# File lib/cztop/zsock_options.rb, line 367
def identity=(identity)
  raise ArgumentError, 'zero-length identity' if identity.bytesize.zero?
  raise ArgumentError, 'identity too long' if identity.bytesize > 255
  raise ArgumentError, 'invalid identity' if identity.start_with? "\0"

  Zsock.set_identity(@zocket, identity)
end
ipv6=(new_value) click to toggle source

Set the IPv6 option for the socket. A value of true means IPv6 is enabled on the socket, while false means the socket will use only IPv4. When IPv6 is enabled the socket will connect to, or accept connections from, both IPv4 and IPv6 hosts. Default is false. @param new_value [Boolean] new value for ipv6

# File lib/cztop/zsock_options.rb, line 467
def ipv6=(new_value)
  Zsock.set_ipv6(@zocket, new_value ? 1 : 0)
end
ipv6?() click to toggle source

@return [Boolean] current value of ipv6

# File lib/cztop/zsock_options.rb, line 456
def ipv6?
  Zsock.ipv6(@zocket) != 0
end
linger() click to toggle source

@return [Integer] current value of LINGER

# File lib/cztop/zsock_options.rb, line 437
def linger
  Zsock.linger(@zocket)
end
linger=(new_value) click to toggle source

This defines the number of milliseconds to wait while closing/disconnecting a socket if there are outstanding messages to send.

Default is 0, which means to not wait at all. -1 means to wait indefinitely

@param new_value [Integer] new value for LINGER

# File lib/cztop/zsock_options.rb, line 450
def linger=(new_value)
  Zsock.set_linger(@zocket, new_value)
end
mechanism() click to toggle source

@return [Symbol] the current security mechanism in use @note This is automatically set through the use of CURVE certificates,

etc
# File lib/cztop/zsock_options.rb, line 149
def mechanism
  # int zsock_mechanism (void *self);
  code = Zsock.mechanism(@zocket)
  MECHANISMS[code] or
    raise format('unknown ZMQ security mechanism code: %i', code)
end
rcvhwm() click to toggle source

@return [Integer] the receive high water mark

# File lib/cztop/zsock_options.rb, line 123
def rcvhwm
  Zsock.rcvhwm(@zocket)
end
rcvhwm=(value) click to toggle source

@param value [Integer] the new receive high water mark

# File lib/cztop/zsock_options.rb, line 129
def rcvhwm=(value)
  Zsock.set_rcvhwm(@zocket, value)
end
rcvtimeo() click to toggle source

@return [Integer] the timeout in milliseconds when receiving a message @see Message.receive_from @note -1 means infinite, 0 means nonblocking

# File lib/cztop/zsock_options.rb, line 313
def rcvtimeo
  Zsock.rcvtimeo(@zocket)
end
rcvtimeo=(timeout) click to toggle source

@param timeout [Integer] new timeout in milliseconds @see Message.receive_from @note -1 means infinite, 0 means nonblocking

# File lib/cztop/zsock_options.rb, line 321
def rcvtimeo=(timeout)
  Zsock.set_rcvtimeo(@zocket, timeout)
end
reconnect_ivl() click to toggle source

@return [Integer] current value of RECONNECT_IVL

# File lib/cztop/zsock_options.rb, line 486
def reconnect_ivl
  Zsock.reconnect_ivl(@zocket)
end
reconnect_ivl=(new_value) click to toggle source

This defines the number of milliseconds to wait while closing/disconnecting a socket if there are outstanding messages to send.

Default is 0, which means to not wait at all. -1 means to wait indefinitely

@param new_value [Integer] new value for RECONNECT_IVL

# File lib/cztop/zsock_options.rb, line 499
def reconnect_ivl=(new_value)
  Zsock.set_reconnect_ivl(@zocket, new_value)
end
router_mandatory=(bool) click to toggle source

ZMQ_ROUTER_MANDATORY: Accept only routable messages on ROUTER sockets. Default is off. @param bool [Boolean] whether to raise a SocketError if a message isn’t routable

(either if the that peer isn't connected or its SNDHWM is reached)

@see libzmq.readthedocs.io/en/latest/zmq_setsockopt.html#_zmq_router_mandatory_accept_only_routable_messages_on_router_sockets

# File lib/cztop/zsock_options.rb, line 347
def router_mandatory=(bool)
  Zsock.set_router_mandatory(@zocket, bool ? 1 : 0)
  @router_mandatory = bool # NOTE: no way to read this option, so we need to remember
end
router_mandatory?() click to toggle source

@return [Boolean] whether ZMQ_ROUTER_MANDATORY has been set

# File lib/cztop/zsock_options.rb, line 354
def router_mandatory?
  @router_mandatory
end
sndhwm() click to toggle source

@return [Integer] the send high water mark

# File lib/cztop/zsock_options.rb, line 111
def sndhwm
  Zsock.sndhwm(@zocket)
end
sndhwm=(value) click to toggle source

@param value [Integer] the new send high water mark.

# File lib/cztop/zsock_options.rb, line 117
def sndhwm=(value)
  Zsock.set_sndhwm(@zocket, value)
end
sndtimeo() click to toggle source

@return [Integer] the timeout in milliseconds when sending a message @see Message#send_to @note -1 means infinite, 0 means nonblocking

# File lib/cztop/zsock_options.rb, line 329
def sndtimeo
  Zsock.sndtimeo(@zocket)
end
sndtimeo=(timeout) click to toggle source

@param timeout [Integer] new timeout in milliseconds @see Message#send_to @note -1 means infinite, 0 means nonblocking

# File lib/cztop/zsock_options.rb, line 337
def sndtimeo=(timeout)
  Zsock.set_sndtimeo(@zocket, timeout)
end
tos() click to toggle source

@return [Integer] current value of Type of Service

# File lib/cztop/zsock_options.rb, line 377
def tos
  Zsock.tos(@zocket)
end
tos=(new_value) click to toggle source

@param new_value [Integer] new value for Type of Service

# File lib/cztop/zsock_options.rb, line 383
def tos=(new_value)
  raise ArgumentError, 'invalid TOS' unless new_value >= 0

  Zsock.set_tos(@zocket, new_value)
end
zap_domain() click to toggle source

Gets the ZAP domain used for authentication. @see rfc.zeromq.org/spec:27 @return [String]

# File lib/cztop/zsock_options.rb, line 248
def zap_domain
  Zsock.zap_domain(@zocket).read_string
end
zap_domain=(domain) click to toggle source

Sets the ZAP domain used for authentication. @param domain [String] the new ZAP domain

# File lib/cztop/zsock_options.rb, line 255
def zap_domain=(domain)
  raise ArgumentError, 'domain too long' if domain.bytesize > 254

  Zsock.set_zap_domain(@zocket, domain)
end