class CZTop::Socket

Represents a CZMQ::FFI::Zsock.

Constants

CURVE_server
CURVE_serverkey
TypeNames

All the available type codes, mapped to their Symbol equivalent. @return [Hash<Integer, Symbol>]

Attributes

last_tcp_port[R]

@return [Integer] last automatically selected, bound TCP port, if any @return [nil] if not bound to a TCP port yet

Public Class Methods

new_by_type(type) click to toggle source

@param type [Symbol, Integer] type from {Types} or like :PUB @return [REQ, REP, PUSH, PULL, … ] the new socket @see Types @example Creating a socket by providing its type as a parameter

my_sock = CZTop::Socket.new_by_type(:DEALER, "tcp://example.com:4000")
# File lib/cztop/socket/types.rb, line 33
def self.new_by_type(type)
  case type
  when Integer
    type_code = type
    type_name = TypeNames[type_code] or
      raise ArgumentError, "invalid type %p" % type
    type_class = Socket.const_get(type_name)
  when Symbol
    type_code = Types.const_get(type)
    type_class = Socket.const_get(type)
  else
    raise ArgumentError, "invalid socket type: %p" % type
  end
  ffi_delegate = Zsock.new(type_code)
  sock = type_class.allocate
  sock.attach_ffi_delegate(ffi_delegate)
  sock
end

Public Instance Methods

CURVE_client!(client_cert, server_cert) click to toggle source

Enables CURVE security and makes this socket a CURVE client. @param client_cert [Certificate] client's certificate, to secure

communication (and be authenticated by the server)

@param server_cert [Certificate] the remote server's certificate, so

this socket is able to authenticate the server

@return [void] @raise [SecurityError] if the server's secret key is set in server_cert,

which means it's not secret anymore

@raise [SystemCallError] if there's no secret key in client_cert

# File lib/cztop/socket.rb, line 33
def CURVE_client!(client_cert, server_cert)
  if server_cert.secret_key
    raise SecurityError, "server's secret key not secret"
  end

  client_cert.apply(self) # NOTE: desired: raises if no secret key in cert
  options.CURVE_serverkey = server_cert.public_key
end
CURVE_server!(cert) click to toggle source

Enables CURVE security and makes this socket a CURVE server. @param cert [Certificate] this server's certificate,

so remote clients are able to authenticate this server

@note You'll have to use a {CZTop::Authenticator}. @return [void] @raise [ArgumentError] if there's no secret key in certificate

# File lib/cztop/socket.rb, line 19
def CURVE_server!(cert)
  options.CURVE_server = true
  cert.apply(self) # NOTE: desired: raises if no secret key in cert
end
bind(endpoint) click to toggle source

Binds to an endpoint. @note When binding to an automatically selected TCP port, this will set

{#last_tcp_port}.

@param endpoint [String] @return [void] @raise [SystemCallError] in case of failure

# File lib/cztop/socket.rb, line 85
def bind(endpoint)
  rc = ffi_delegate.bind("%s", :string, endpoint)
  raise_zmq_err("unable to bind to %p" % endpoint) if rc == -1
  @last_tcp_port = rc if rc > 0
end
close() click to toggle source

Closes and destroys the native socket. @return [void] @note Don't try to use it anymore afterwards.

# File lib/cztop/socket.rb, line 71
def close
  ffi_delegate.destroy
end
connect(endpoint) click to toggle source

Connects to an endpoint. @param endpoint [String] @return [void] @raise [ArgumentError] if the endpoint is incorrect

# File lib/cztop/socket.rb, line 54
def connect(endpoint)
  rc = ffi_delegate.connect("%s", :string, endpoint)
  raise ArgumentError, "incorrect endpoint: %p" % endpoint if rc == -1
end
disconnect(endpoint) click to toggle source

Disconnects from an endpoint. @param endpoint [String] @return [void] @raise [ArgumentError] if the endpoint is incorrect

# File lib/cztop/socket.rb, line 63
def disconnect(endpoint)
  rc = ffi_delegate.disconnect("%s", :string, endpoint)
  raise ArgumentError, "incorrect endpoint: %p" % endpoint if rc == -1
end
inspect() click to toggle source

Inspects this {Socket}. @return [String] shows class, native address, and {#last_endpoint}

# File lib/cztop/socket.rb, line 102
def inspect
  "#<%s:0x%x last_endpoint=%p>" % [
    self.class,
    to_ptr.address,
    last_endpoint
  ]
rescue Zsock::DestroyedError
  "#<%s: invalid>" % self.class
end
last_endpoint() click to toggle source

@return [String] last bound endpoint, if any @return [nil] if not bound

# File lib/cztop/socket.rb, line 46
def last_endpoint
  ffi_delegate.endpoint
end
unbind(endpoint) click to toggle source

Unbinds from an endpoint. @param endpoint [String] @return [void] @raise [ArgumentError] if the endpoint is incorrect

# File lib/cztop/socket.rb, line 95
def unbind(endpoint)
  rc = ffi_delegate.unbind("%s", :string, endpoint)
  raise ArgumentError, "incorrect endpoint: %p" % endpoint if rc == -1
end