Class: CZTop::Socket

Inherits:
Object
  • Object
show all
Extended by:
HasFFIDelegate::ClassMethods
Includes:
CZMQ::FFI, HasFFIDelegate, PolymorphicZsockMethods, SendReceiveMethods, ZsockOptions
Defined in:
lib/cztop/socket.rb,
lib/cztop/socket/types.rb

Overview

Represents a CZMQ::FFI::Zsock.

Direct Known Subclasses

CLIENT, DEALER, DISH, GATHER, PAIR, PUB, PULL, PUSH, RADIO, REP, REQ, ROUTER, SCATTER, SERVER, STREAM, SUB, XPUB, XSUB

Defined Under Namespace

Modules: Types Classes: CLIENT, DEALER, DISH, GATHER, PAIR, PUB, PULL, PUSH, RADIO, REP, REQ, ROUTER, SCATTER, SERVER, STREAM, SUB, XPUB, XSUB

Constant Summary

TypeNames =

All the available type codes, mapped to their Symbol equivalent.

Returns:

  • (Hash<Integer, Symbol>)
Hash[
  Types.constants.map { |name| i = Types.const_get(name); [ i, name ] }
].freeze

Instance Attribute Summary collapse

Attributes included from HasFFIDelegate

#ffi_delegate

CURVE Security collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasFFIDelegate::ClassMethods

ffi_delegate, from_ffi_delegate

Methods included from PolymorphicZsockMethods

#set_unbounded, #signal, #wait

Methods included from SendReceiveMethods

#<<, #receive

Methods included from ZsockOptions

#fd, #options, #readable?, #writable?

Methods included from HasFFIDelegate

#attach_ffi_delegate, #from_ffi_delegate, #raise_zmq_err, raise_zmq_err, #to_ptr

Instance Attribute Details

#last_tcp_portInteger? (readonly)

Returns:

  • (Integer)

    last automatically selected, bound TCP port, if any

  • (nil)

    if not bound to a TCP port yet



77
78
79
# File 'lib/cztop/socket.rb', line 77

def last_tcp_port
  @last_tcp_port
end

Class Method Details

.new_by_type(type) ⇒ REQ, ...

Returns the new socket

Examples:

Creating a socket by providing its type as a parameter

my_sock = CZTop::Socket.new_by_type(:DEALER, "tcp://example.com:4000")

Parameters:

  • type (Symbol, Integer)

    type from Types or like :PUB

Returns:

See Also:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#bind(endpoint) ⇒ void

Note:

When binding to an automatically selected TCP port, this will set #last_tcp_port.

This method returns an undefined value.

Binds to an endpoint.

Parameters:

  • endpoint (String)

Raises:

  • (SystemCallError)

    in case of failure



85
86
87
88
89
# 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

#closevoid

Note:

Don't try to use it anymore afterwards.

This method returns an undefined value.

Closes and destroys the native socket.



71
72
73
# File 'lib/cztop/socket.rb', line 71

def close
  ffi_delegate.destroy
end

#connect(endpoint) ⇒ void

This method returns an undefined value.

Connects to an endpoint.

Parameters:

  • endpoint (String)

Raises:

  • (ArgumentError)

    if the endpoint is incorrect



54
55
56
57
# 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

#CURVE_client!(client_cert, server_cert) ⇒ void

This method returns an undefined value.

Enables CURVE security and makes this socket a CURVE client.

Parameters:

  • client_cert (Certificate)

    client's certificate, to secure communication (and be authenticated by the server)

  • server_cert (Certificate)

    the remote server's certificate, so this socket is able to authenticate the server

Raises:

  • (SecurityError)

    if the server's secret key is set in server_cert, which means it's not secret anymore

  • (SystemCallError)

    if there's no secret key in client_cert



33
34
35
36
37
38
39
40
# 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) ⇒ void

Note:

You'll have to use a Authenticator.

This method returns an undefined value.

Enables CURVE security and makes this socket a CURVE server.

Parameters:

  • cert (Certificate)

    this server's certificate, so remote clients are able to authenticate this server

Raises:

  • (ArgumentError)

    if there's no secret key in certificate



19
20
21
22
# 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

#disconnect(endpoint) ⇒ void

This method returns an undefined value.

Disconnects from an endpoint.

Parameters:

  • endpoint (String)

Raises:

  • (ArgumentError)

    if the endpoint is incorrect



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

#inspectString

Inspects this CZTop::Socket.

Returns:



102
103
104
105
106
107
108
109
110
# 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_endpointString?

Returns:

  • (String)

    last bound endpoint, if any

  • (nil)

    if not bound



46
47
48
# File 'lib/cztop/socket.rb', line 46

def last_endpoint
  ffi_delegate.endpoint
end

#unbind(endpoint) ⇒ void

This method returns an undefined value.

Unbinds from an endpoint.

Parameters:

  • endpoint (String)

Raises:

  • (ArgumentError)

    if the endpoint is incorrect



95
96
97
98
# 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