Module: CZTop::HasFFIDelegate

Included in:
Actor, CertStore, Certificate, Config, Frame, Message, Poller::ZPoller, Socket, Z85
Defined in:
lib/cztop/has_ffi_delegate.rb

Overview

This module is used to attach the low-level objects of classes within the CZMQ::FFI namespace (coming from the czmq-ffi-gen gem) as delegates.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ffi_delegateCZMQ::FFI::* (readonly)

Returns the attached delegate.

Returns:

  • (CZMQ::FFI::*)

    the attached delegate


8
9
10
# File 'lib/cztop/has_ffi_delegate.rb', line 8

def ffi_delegate
  @ffi_delegate
end

Class Method Details

.raise_zmq_err(msg = CZMQ::FFI::Errors.strerror, errno: CZMQ::FFI::Errors.errno) ⇒ Object

Raises the appropriate exception for the reported ZMQ error.

Parameters:

  • msg (String) (defaults to: CZMQ::FFI::Errors.strerror)

    error message

Raises:

  • (ArgumentError)

    if EINVAL was reported

  • (Interrupt)

    if EINTR was reported

  • (SocketError)

    if EHOSTUNREACH was reported

  • (SystemCallError)

    any other reported error (appropriate SystemCallError subclass, if errno is known)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cztop/has_ffi_delegate.rb', line 48

def raise_zmq_err(msg = CZMQ::FFI::Errors.strerror,
                  errno: CZMQ::FFI::Errors.errno)

  case errno
  when Errno::EINVAL::Errno       then raise ArgumentError, msg, caller
  when Errno::EINTR::Errno        then raise Interrupt, msg, caller
  when Errno::EHOSTUNREACH::Errno then raise SocketError, msg, caller

  # If the errno is known, the corresponding Errno::* exception is
  # automatically constructed. Otherwise, it'll be a plain SystemCallError.
  # In any case, #errno will return the corresponding errno.
  else raise SystemCallError.new(msg, errno), msg, caller
  end
end

Instance Method Details

#attach_ffi_delegate(ffi_delegate) ⇒ void

Note:

This only raises the correct exception when the creation of the new CZMQ object was the most recent thing done with the CZMQ library and thus CZMQ::FFI::Errors.errno is still reports the correct error number.

This method returns an undefined value.

Attaches an FFI delegate to the current (probably new) CZTop object.

Parameters:

  • ffi_delegate

    an instance of the corresponding class in the CZMQ::FFI namespace

Raises:

  • (SystemCallError, ArgumentError, ...)

    if the FFI delegate's internal pointer is NULL

See Also:


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

def attach_ffi_delegate(ffi_delegate)
  raise_zmq_err(CZMQ::FFI::Errors.strerror) if ffi_delegate.null?
  @ffi_delegate = ffi_delegate
end

#from_ffi_delegate(ffi_delegate) ⇒ CZTop::*

Same as the counterpart in ClassMethods, but usable from within an instance.

Returns:

See Also:

  • FFIDelegate::ClassMethods#from_ffi_delegate

34
35
36
# File 'lib/cztop/has_ffi_delegate.rb', line 34

def from_ffi_delegate(ffi_delegate)
  self.class.from_ffi_delegate(ffi_delegate)
end

#raise_zmq_err(msg = CZMQ::FFI::Errors.strerror, errno: CZMQ::FFI::Errors.errno) ⇒ Object (private)

Raises the appropriate exception for the reported ZMQ error.

Parameters:

  • msg (String) (defaults to: CZMQ::FFI::Errors.strerror)

    error message

Raises:

  • (ArgumentError)

    if EINVAL was reported

  • (Interrupt)

    if EINTR was reported

  • (SocketError)

    if EHOSTUNREACH was reported

  • (SystemCallError)

    any other reported error (appropriate SystemCallError subclass, if errno is known)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cztop/has_ffi_delegate.rb', line 48

def raise_zmq_err(msg = CZMQ::FFI::Errors.strerror,
                  errno: CZMQ::FFI::Errors.errno)

  case errno
  when Errno::EINVAL::Errno       then raise ArgumentError, msg, caller
  when Errno::EINTR::Errno        then raise Interrupt, msg, caller
  when Errno::EHOSTUNREACH::Errno then raise SocketError, msg, caller

  # If the errno is known, the corresponding Errno::* exception is
  # automatically constructed. Otherwise, it'll be a plain SystemCallError.
  # In any case, #errno will return the corresponding errno.
  else raise SystemCallError.new(msg, errno), msg, caller
  end
end

#to_ptrFFI::Pointer

Returns FFI delegate's pointer.

Returns:

  • (FFI::Pointer)

    FFI delegate's pointer


11
12
13
# File 'lib/cztop/has_ffi_delegate.rb', line 11

def to_ptr
  @ffi_delegate.to_ptr
end