Class: CZTop::Socket

Inherits:
Object
  • Object
show all
Extended by:
HasFFIDelegate::ClassMethods
Includes:
CZMQ::FFI, HasFFIDelegate, ZsockOptions
Defined in:
lib/cztop/socket.rb,
lib/cztop/socket/pub.rb,
lib/cztop/socket/rep.rb,
lib/cztop/socket/req.rb,
lib/cztop/socket/sub.rb,
lib/cztop/socket/pair.rb,
lib/cztop/socket/pull.rb,
lib/cztop/socket/push.rb,
lib/cztop/socket/xpub.rb,
lib/cztop/socket/xsub.rb,
lib/cztop/socket/types.rb,
lib/cztop/socket/dealer.rb,
lib/cztop/socket/router.rb,
lib/cztop/socket/stream.rb,
lib/cztop/socket/fd_wait.rb,
lib/cztop/socket/readable.rb,
lib/cztop/socket/writable.rb

Overview

Represents a CZMQ::FFI::Zsock.

Direct Known Subclasses

DEALER, PAIR, PUB, PULL, PUSH, REP, REQ, ROUTER, STREAM, SUB, XPUB, XSUB

Defined Under Namespace

Modules: FdWait, Readable, Types, Writable Classes: DEALER, PAIR, PUB, PULL, PUSH, REP, REQ, ROUTER, STREAM, SUB, XPUB, XSUB

Constant Summary collapse

TypeNames =

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

Returns:

  • (Hash<Integer, Symbol>)
Types.constants.to_h do |name|
  i = Types.const_get(name)
  [i, name]
end.freeze

Constants included from CZMQ::FFI

CZMQ::FFI::CZMQ_VERSION, CZMQ::FFI::ZFRAME_DONTWAIT, CZMQ::FFI::ZFRAME_MORE, CZMQ::FFI::ZMONITOR_FN, CZMQ::FFI::ZMQ_VERSION

Constants included from ZsockOptions

ZsockOptions::POLLIN, ZsockOptions::POLLOUT

Instance Attribute Summary collapse

Attributes included from HasFFIDelegate

#ffi_delegate

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasFFIDelegate::ClassMethods

ffi_delegate, from_ffi_delegate

Methods included from ZsockOptions

#conflate=, #conflate?, #fd, #heartbeat_ivl, #heartbeat_ivl=, #heartbeat_timeout, #heartbeat_timeout=, #heartbeat_ttl, #heartbeat_ttl=, #identity, #identity=, #immediate=, #immediate?, #ipv6=, #ipv6?, #linger, #linger=, #max_msg_size, #max_msg_size=, #rcvhwm, #rcvhwm=, #readable?, #reconnect_ivl, #reconnect_ivl=, #reconnect_ivl_max, #reconnect_ivl_max=, #recv_timeout, #recv_timeout=, #router_mandatory=, #router_mandatory?, #send_timeout, #send_timeout=, #sndhwm, #sndhwm=, #tcp_keepalive, #tcp_keepalive=, #tcp_keepalive_cnt, #tcp_keepalive_cnt=, #tcp_keepalive_idle, #tcp_keepalive_idle=, #tcp_keepalive_intvl, #tcp_keepalive_intvl=, #to_io, #tos, #tos=, #writable?

Methods included from HasFFIDelegate

#attach_ffi_delegate, #from_ffi_delegate, raise_zmq_err, #to_ptr

Constructor Details

#initialize(endpoints = nil, curve: nil, linger: 0) ⇒ Socket

Returns a new instance of Socket.



36
# File 'lib/cztop/socket.rb', line 36

def initialize(endpoints = nil, curve: nil, linger: 0); end

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



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

def last_tcp_port
  @last_tcp_port
end

Class Method Details

.bind(endpoint, **opts) ⇒ Socket

Creates a new socket and binds it to the given endpoint.

Parameters:

  • endpoint (String)

    endpoint to bind to

  • opts (Hash)

    keyword arguments forwarded to #initialize (e.g. curve:, prefix: for SUB)

Returns:

  • (Socket)

    the new, bound socket



20
21
22
# File 'lib/cztop/socket.rb', line 20

def self.bind(endpoint, **opts)
  new(nil, **opts).tap { |s| s.bind(endpoint) }
end

.connect(endpoint, **opts) ⇒ Socket

Creates a new socket and connects it to the given endpoint.

Parameters:

  • endpoint (String)

    endpoint to connect to

  • opts (Hash)

    keyword arguments forwarded to #initialize (e.g. curve:, prefix: for SUB)

Returns:

  • (Socket)

    the new, connected socket



31
32
33
# File 'lib/cztop/socket.rb', line 31

def self.connect(endpoint, **opts)
  new(nil, **opts).tap { |s| s.connect(endpoint) }
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



90
91
92
93
94
# File 'lib/cztop/socket.rb', line 90

def bind(endpoint)
  rc = ffi_delegate.bind('%s', :string, endpoint)
  raise_zmq_err(format('unable to bind to %p', endpoint)) if rc == -1
  @last_tcp_port = rc if rc.positive?
end

#closevoid

Note:

Don't try to use it anymore afterwards.

This method returns an undefined value.

Closes and destroys the native socket.



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

def close
  ffi_delegate.destroy
  nil
end

#connect(endpoint) ⇒ void

This method returns an undefined value.

Connects to an endpoint.

Parameters:

  • endpoint (String)

Raises:

  • (ArgumentError)

    if the endpoint is incorrect



52
53
54
55
# File 'lib/cztop/socket.rb', line 52

def connect(endpoint)
  rc = ffi_delegate.connect('%s', :string, endpoint)
  raise ArgumentError, format('incorrect endpoint: %p', endpoint) if rc == -1
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, format('incorrect endpoint: %p', endpoint) if rc == -1
end

#inspectString

Inspects this CZTop::Socket.

Returns:



120
121
122
123
124
# File 'lib/cztop/socket.rb', line 120

def inspect
  format('#<%s:0x%x last_endpoint=%p>', self.class, to_ptr.address, last_endpoint)
rescue Zsock::DestroyedError
  format('#<%s: invalid>', self.class)
end

#last_endpointString?

Returns:

  • (String)

    last bound endpoint, if any

  • (nil)

    if not bound



42
43
44
# File 'lib/cztop/socket.rb', line 42

def last_endpoint
  ffi_delegate.endpoint
end

#set_unboundedObject

Set socket to use unbounded pipes (HWM=0); use this in cases when you are totally certain the message volume can fit in memory.



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

def set_unbounded
  ::CZMQ::FFI::Zsock.set_unbounded(ffi_delegate)
  nil
end

#unbind(endpoint) ⇒ void

This method returns an undefined value.

Unbinds from an endpoint.

Parameters:

  • endpoint (String)

Raises:

  • (ArgumentError)

    if the endpoint is incorrect



102
103
104
105
# File 'lib/cztop/socket.rb', line 102

def unbind(endpoint)
  rc = ffi_delegate.unbind('%s', :string, endpoint)
  raise ArgumentError, format('incorrect endpoint: %p', endpoint) if rc == -1
end