Class: CZTop::Socket
- Inherits:
-
Object
- Object
- CZTop::Socket
- 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 collapse
- TypeNames =
All the available type codes, mapped to their Symbol equivalent.
Types.constants.to_h do |name| i = Types.const_get(name) [i, name] end.freeze
Constants included from SendReceiveMethods
CZTop::SendReceiveMethods::FD_TIMEOUT, CZTop::SendReceiveMethods::JIFFY
Instance Attribute Summary collapse
- #last_tcp_port ⇒ Integer? readonly
Attributes included from HasFFIDelegate
CURVE Security collapse
-
#CURVE_client!(client_cert, server_cert) ⇒ void
Enables CURVE security and makes this socket a CURVE client.
-
#CURVE_server!(cert) ⇒ void
Enables CURVE security and makes this socket a CURVE server.
Class Method Summary collapse
-
.new_by_type(type) ⇒ REQ, ...
The new socket.
Instance Method Summary collapse
-
#bind(endpoint) ⇒ void
Binds to an endpoint.
-
#close ⇒ void
Closes and destroys the native socket.
-
#connect(endpoint) ⇒ void
Connects to an endpoint.
-
#disconnect(endpoint) ⇒ void
Disconnects from an endpoint.
-
#initialize(endpoints = nil) ⇒ Socket
constructor
A new instance of Socket.
-
#inspect ⇒ String
Inspects this Socket.
- #last_endpoint ⇒ String?
-
#unbind(endpoint) ⇒ void
Unbinds from an endpoint.
Methods included from HasFFIDelegate::ClassMethods
ffi_delegate, from_ffi_delegate
Methods included from PolymorphicZsockMethods
#set_unbounded, #signal, #wait
Methods included from SendReceiveMethods
#<<, #read_timeout, #receive, #wait_for_fd_signal, #wait_writable, #write_timeout
Methods included from ZsockOptions
#fd, #options, #readable?, #to_io, #writable?
Methods included from HasFFIDelegate
#attach_ffi_delegate, #from_ffi_delegate, raise_zmq_err, #to_ptr
Constructor Details
#initialize(endpoints = nil) ⇒ Socket
Returns a new instance of Socket.
65 |
# File 'lib/cztop/socket/types.rb', line 65 def initialize(endpoints = nil); end |
Instance Attribute Details
#last_tcp_port ⇒ Integer? (readonly)
83 84 85 |
# File 'lib/cztop/socket.rb', line 83 def last_tcp_port @last_tcp_port end |
Class Method Details
.new_by_type(type) ⇒ REQ, ...
Returns the new socket.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cztop/socket/types.rb', line 45 def self.new_by_type(type) case type when Integer type_code = type type_name = TypeNames[type_code] or raise ArgumentError, format('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, format('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
When binding to an automatically selected TCP port, this will set #last_tcp_port.
This method returns an undefined value.
Binds to an endpoint.
91 92 93 94 95 |
# File 'lib/cztop/socket.rb', line 91 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 |
#close ⇒ void
Don’t try to use it anymore afterwards.
This method returns an undefined value.
Closes and destroys the native socket.
77 78 79 |
# File 'lib/cztop/socket.rb', line 77 def close ffi_delegate.destroy end |
#connect(endpoint) ⇒ void
This method returns an undefined value.
Connects to an endpoint.
58 59 60 61 |
# File 'lib/cztop/socket.rb', line 58 def connect(endpoint) rc = ffi_delegate.connect('%s', :string, endpoint) raise ArgumentError, format('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.
37 38 39 40 41 42 |
# File 'lib/cztop/socket.rb', line 37 def CURVE_client!(client_cert, server_cert) raise SecurityError, "server's secret key not secret" if server_cert.secret_key client_cert.apply(self) # NOTE: desired: raises if no secret key in cert .CURVE_serverkey = server_cert.public_key end |
#CURVE_server!(cert) ⇒ void
You’ll have to use a Authenticator.
This method returns an undefined value.
Enables CURVE security and makes this socket a CURVE server.
22 23 24 25 |
# File 'lib/cztop/socket.rb', line 22 def CURVE_server!(cert) .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.
68 69 70 71 |
# File 'lib/cztop/socket.rb', line 68 def disconnect(endpoint) rc = ffi_delegate.disconnect('%s', :string, endpoint) raise ArgumentError, format('incorrect endpoint: %p', endpoint) if rc == -1 end |
#inspect ⇒ String
Inspects this CZTop::Socket.
110 111 112 113 114 |
# File 'lib/cztop/socket.rb', line 110 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_endpoint ⇒ String?
49 50 51 |
# File 'lib/cztop/socket.rb', line 49 def last_endpoint ffi_delegate.endpoint end |
#unbind(endpoint) ⇒ void
This method returns an undefined value.
Unbinds from an endpoint.
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 |