Class: OMQ::Rust::Socket
- Inherits:
-
Object
- Object
- OMQ::Rust::Socket
- Defined in:
- lib/omq/rs/socket.rb
Overview
Base class for OMQ.rs-backed sockets.
Direct Known Subclasses
CHANNEL, CLIENT, DEALER, DISH, GATHER, PAIR, PEER, PUB, PULL, PUSH, RADIO, REP, REQ, ROUTER, SCATTER, SERVER, STREAM, SUB, XPUB, XSUB
Instance Attribute Summary collapse
-
#socket_type ⇒ Symbol
readonly
Lowercase socket pattern.
Instance Method Summary collapse
-
#bind(endpoint) ⇒ String
Binds socket to an endpoint.
-
#close ⇒ nil
Closes socket and releases native resources.
-
#closed? ⇒ Boolean
Reports whether socket is closed.
-
#connect(endpoint) ⇒ Socket
Connects socket to an endpoint.
-
#disconnect(endpoint) ⇒ Socket
Disconnects socket from an endpoint.
-
#each {|message| ... } ⇒ Enumerator?
Yields received messages until socket closes.
-
#initialize(recv_timeout: nil, send_timeout: nil, curve_auth: nil, **options) ⇒ Socket
constructor
Creates a socket without binding or connecting it.
-
#join(group) ⇒ Socket
Joins DISH group.
-
#leave(group) ⇒ Socket
Leaves DISH group.
-
#monitor ⇒ Monitor
Returns socket lifecycle monitor.
-
#monitor_event(timeout: @recv_timeout) ⇒ Hash?
Receives next monitor event.
-
#monitor_fd ⇒ Integer
Returns monitor notification file descriptor.
-
#peer_info(routing_id) ⇒ Hash?
Returns metadata for live SERVER route.
-
#publish(group, message) ⇒ Socket
Publishes RADIO message to group.
-
#recv ⇒ Array<String, Integer>
(also: #receive)
Receives next message.
-
#send(message, *more) ⇒ Socket
(also: #<<)
Sends message, blocking while send queue is full.
-
#set_curve_auth(authenticator = nil) {|peer| ... } ⇒ Socket
Configures CURVE client authorization on a server before materialization.
-
#subscribe(prefix = "") ⇒ Socket
Adds SUB or XSUB subscription prefix.
-
#try_monitor_event ⇒ Hash?
Attempts to receive monitor event without blocking.
-
#try_recv ⇒ Array<String, Integer>?
Attempts to receive without blocking.
-
#try_send(message, *more) ⇒ Boolean
Attempts to send without blocking.
-
#unbind(endpoint) ⇒ Socket
Stops listening on an endpoint.
-
#unsubscribe(prefix = "") ⇒ Socket
Removes SUB or XSUB subscription prefix.
-
#wait_for_peer(timeout: nil) ⇒ Socket
Waits until first peer completes handshake.
-
#wait_for_subscriber(timeout: nil) ⇒ Socket
Waits until PUB, XPUB, or RADIO receives first subscription.
-
#wait_readable(timeout: @recv_timeout) ⇒ true
Waits for receive notification.
-
#wake_recv ⇒ Socket
Wakes a thread or fiber blocked in #wait_readable.
Constructor Details
#initialize(recv_timeout: nil, send_timeout: nil, curve_auth: nil, **options) ⇒ Socket
Creates a socket without binding or connecting it.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/omq/rs/socket.rb', line 273 def initialize(recv_timeout: nil, send_timeout: nil, curve_auth: nil, **) socket_type = self.class.const_get(:SOCKET_TYPE, false) @socket_type = socket_type.to_s.downcase.to_sym unless SOCKET_TYPES.include?(@socket_type) raise ArgumentError, "unknown socket type: #{socket_type}" end @recv_timeout = recv_timeout @send_timeout = send_timeout @recv_batch = [] @request_waiting = false @reply_ready = false @native = Native::Socket.new(@socket_type.to_s.upcase) @native.(()) @materialize_lock = Mutex.new @materialized = false @recv_io = nil @send_io = nil @peer_connected = false @subscriber_joined = false set_curve_auth(curve_auth) unless curve_auth.nil? end |
Instance Attribute Details
#socket_type ⇒ Symbol (readonly)
Returns lowercase socket pattern.
218 219 220 |
# File 'lib/omq/rs/socket.rb', line 218 def socket_type @socket_type end |
Instance Method Details
#bind(endpoint) ⇒ String
Binds socket to an endpoint.
300 301 302 303 |
# File 'lib/omq/rs/socket.rb', line 300 def bind(endpoint) ensure_materialized @native.bind(String(endpoint)) end |
#close ⇒ nil
Closes socket and releases native resources.
625 626 627 628 629 630 631 632 |
# File 'lib/omq/rs/socket.rb', line 625 def close return if closed? @native.close close_wrapper(@recv_io) close_wrapper(@send_io) nil end |
#closed? ⇒ Boolean
Reports whether socket is closed.
637 638 639 |
# File 'lib/omq/rs/socket.rb', line 637 def closed? @native.closed? end |
#connect(endpoint) ⇒ Socket
Connects socket to an endpoint.
309 310 311 312 313 |
# File 'lib/omq/rs/socket.rb', line 309 def connect(endpoint) ensure_materialized @native.connect(String(endpoint)) self end |
#disconnect(endpoint) ⇒ Socket
Disconnects socket from an endpoint.
319 320 321 322 323 |
# File 'lib/omq/rs/socket.rb', line 319 def disconnect(endpoint) ensure_materialized @native.disconnect(String(endpoint)) self end |
#each {|message| ... } ⇒ Enumerator?
Yields received messages until socket closes.
485 486 487 488 489 490 491 |
# File 'lib/omq/rs/socket.rb', line 485 def each return enum_for(__method__) unless block_given? loop { yield recv } rescue IOError raise unless closed? end |
#join(group) ⇒ Socket
Joins DISH group.
517 518 519 520 521 |
# File 'lib/omq/rs/socket.rb', line 517 def join(group) ensure_materialized @native.join(String(group).b) self end |
#leave(group) ⇒ Socket
Leaves DISH group.
527 528 529 530 531 |
# File 'lib/omq/rs/socket.rb', line 527 def leave(group) ensure_materialized @native.leave(String(group).b) self end |
#monitor ⇒ Monitor
Returns socket lifecycle monitor.
581 582 583 584 |
# File 'lib/omq/rs/socket.rb', line 581 def monitor ensure_materialized @monitor ||= Monitor.new(self) end |
#monitor_event(timeout: @recv_timeout) ⇒ Hash?
Receives next monitor event.
601 602 603 604 605 606 607 608 609 610 |
# File 'lib/omq/rs/socket.rb', line 601 def monitor_event(timeout: @recv_timeout) return if closed? ensure_materialized event = @native.try_recv_monitor return event if event wait_for_native_fd(@native.monitor_fd, timeout, "monitor receive timed out") @native.try_recv_monitor end |
#monitor_fd ⇒ Integer
Returns monitor notification file descriptor.
Intended for event-loop adapters; use #monitor otherwise.
591 592 593 594 |
# File 'lib/omq/rs/socket.rb', line 591 def monitor_fd ensure_materialized @native.monitor_fd end |
#peer_info(routing_id) ⇒ Hash?
Returns metadata for live SERVER route.
340 341 342 343 |
# File 'lib/omq/rs/socket.rb', line 340 def peer_info(routing_id) ensure_materialized @native.peer_info(routing_id) end |
#publish(group, message) ⇒ Socket
Publishes RADIO message to group.
538 539 540 |
# File 'lib/omq/rs/socket.rb', line 538 def publish(group, ) send(group, ) end |
#recv ⇒ Array<String, Integer> Also known as: receive
Receives next message.
422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/omq/rs/socket.rb', line 422 def recv ensure_materialized = try_recv return if loop do wait_for(@recv_io, @recv_timeout, "receive timed out") = try_recv return if raise IOError, "socket closed" if closed? end end |
#send(message, *more) ⇒ Socket Also known as: <<
Sends message, blocking while send queue is full.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/omq/rs/socket.rb', line 379 def send(, *more) ensure_materialized parts = normalize_parts(, more) validate_send_parts!(parts) validate_pattern_state_before_send! loop do result = enqueue(parts) if result == :ok sent! return self end wait_for(@send_io, @send_timeout, "send timed out") raise IOError, "socket closed" if closed? end end |
#set_curve_auth(authenticator = nil) {|peer| ... } ⇒ Socket
Configures CURVE client authorization on a server before materialization.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/omq/rs/socket.rb', line 353 def set_curve_auth(authenticator = nil, &block) raise RuntimeError, "CURVE authentication must be configured before bind or connect" if @materialized authenticator = block if block case authenticator when nil @native.clear_curve_auth when Array @native.set_curve_auth_keys(authenticator) else unless authenticator.respond_to?(:call) raise TypeError, "CURVE authenticator must be an Array, callable, or nil" end @native.set_curve_auth_callback(Rust.wrap_curve_authenticator(authenticator)) end self end |
#subscribe(prefix = "") ⇒ Socket
Adds SUB or XSUB subscription prefix.
497 498 499 500 501 |
# File 'lib/omq/rs/socket.rb', line 497 def subscribe(prefix = "") ensure_materialized @native.subscribe(String(prefix).b) self end |
#try_monitor_event ⇒ Hash?
Attempts to receive monitor event without blocking.
615 616 617 618 619 620 |
# File 'lib/omq/rs/socket.rb', line 615 def try_monitor_event return if closed? ensure_materialized @native.try_recv_monitor end |
#try_recv ⇒ Array<String, Integer>?
Attempts to receive without blocking.
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/omq/rs/socket.rb', line 441 def try_recv ensure_materialized unless @recv_batch.empty? = @recv_batch.shift received! return end = if ROUTED_TYPES.include?(@socket_type) @native.try_recv_routed elsif (batch = @native.try_recv_batch) first = batch.shift @recv_batch = batch first end received! if end |
#try_send(message, *more) ⇒ Boolean
Attempts to send without blocking.
406 407 408 409 410 411 412 413 414 415 |
# File 'lib/omq/rs/socket.rb', line 406 def try_send(, *more) ensure_materialized parts = normalize_parts(, more) validate_send_parts!(parts) validate_pattern_state_before_send! return false unless enqueue(parts) == :ok sent! true end |
#unbind(endpoint) ⇒ Socket
Stops listening on an endpoint.
329 330 331 332 333 |
# File 'lib/omq/rs/socket.rb', line 329 def unbind(endpoint) ensure_materialized @native.unbind(String(endpoint)) self end |
#unsubscribe(prefix = "") ⇒ Socket
Removes SUB or XSUB subscription prefix.
507 508 509 510 511 |
# File 'lib/omq/rs/socket.rb', line 507 def unsubscribe(prefix = "") ensure_materialized @native.unsubscribe(String(prefix).b) self end |
#wait_for_peer(timeout: nil) ⇒ Socket
Waits until first peer completes handshake.
548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/omq/rs/socket.rb', line 548 def wait_for_peer(timeout: nil) raise IOError, "socket closed" if closed? return self if @peer_connected ensure_materialized wait_for_native_fd(@native.peer_connected_fd, timeout, "peer connection timed out") raise IOError, "socket closed" if closed? @peer_connected = true self end |
#wait_for_subscriber(timeout: nil) ⇒ Socket
Waits until PUB, XPUB, or RADIO receives first subscription.
566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/omq/rs/socket.rb', line 566 def wait_for_subscriber(timeout: nil) raise IOError, "socket closed" if closed? return self if @subscriber_joined ensure_materialized wait_for_native_fd(@native.subscriber_joined_fd, timeout, "subscriber timed out") raise IOError, "socket closed" if closed? @subscriber_joined = true self end |
#wait_readable(timeout: @recv_timeout) ⇒ true
Waits for receive notification.
Notification may represent a message, close, or explicit #wake_recv.
467 468 469 470 471 |
# File 'lib/omq/rs/socket.rb', line 467 def wait_readable(timeout: @recv_timeout) ensure_materialized wait_for(@recv_io, timeout, "receive timed out") true end |
#wake_recv ⇒ Socket
Wakes a thread or fiber blocked in #wait_readable.
476 477 478 479 |
# File 'lib/omq/rs/socket.rb', line 476 def wake_recv @native.wake_recv if @materialized self end |