Class: OMQ::Routing::Server
- Inherits:
-
Object
- Object
- OMQ::Routing::Server
- Defined in:
- lib/omq/routing/server.rb
Overview
SERVER socket routing: identity-based routing with auto-generated 4-byte routing IDs.
Prepends routing ID on receive. Strips routing ID on send and routes to the identified connection.
Instance Attribute Summary collapse
- #recv_queue ⇒ Async::LimitedQueue readonly
Instance Method Summary collapse
- #connection_added(connection) ⇒ Object
- #connection_removed(connection) ⇒ Object
-
#dequeue_recv ⇒ Array<String>?
Dequeues the next received message.
- #enqueue(parts) ⇒ Object
-
#initialize(engine) ⇒ Server
constructor
A new instance of Server.
-
#send_queues_drained? ⇒ Boolean
True when all per-connection send queues are empty.
-
#unblock_recv ⇒ void
Wakes a blocked #dequeue_recv with a nil sentinel.
Constructor Details
#initialize(engine) ⇒ Server
Returns a new instance of Server.
21 22 23 24 25 26 27 |
# File 'lib/omq/routing/server.rb', line 21 def initialize(engine) @engine = engine @recv_queue = Routing.build_queue(engine..recv_hwm, :block) @connections_by_routing_id = {} @routing_id_by_connection = {} @conn_queues = {} end |
Instance Attribute Details
#recv_queue ⇒ Async::LimitedQueue (readonly)
16 17 18 |
# File 'lib/omq/routing/server.rb', line 16 def recv_queue @recv_queue end |
Instance Method Details
#connection_added(connection) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/omq/routing/server.rb', line 50 def connection_added(connection) routing_id = SecureRandom.bytes(4) @connections_by_routing_id[routing_id] = connection @routing_id_by_connection[connection] = routing_id @engine.start_recv_pump(connection, @recv_queue) { |msg| [routing_id, *msg] } q = Routing.build_queue(@engine..send_hwm, :block) @conn_queues[connection] = q ConnSendPump.start(@engine, connection, q) end |
#connection_removed(connection) ⇒ Object
65 66 67 68 69 |
# File 'lib/omq/routing/server.rb', line 65 def connection_removed(connection) routing_id = @routing_id_by_connection.delete(connection) @connections_by_routing_id.delete(routing_id) if routing_id @conn_queues.delete(connection) end |
#dequeue_recv ⇒ Array<String>?
Dequeues the next received message. Blocks until one is available.
34 35 36 |
# File 'lib/omq/routing/server.rb', line 34 def dequeue_recv @recv_queue.dequeue end |
#enqueue(parts) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/omq/routing/server.rb', line 74 def enqueue(parts) routing_id = parts.first conn = @connections_by_routing_id[routing_id] return unless conn @conn_queues[conn]&.enqueue(parts[1..]) end |
#send_queues_drained? ⇒ Boolean
True when all per-connection send queues are empty.
84 85 86 |
# File 'lib/omq/routing/server.rb', line 84 def send_queues_drained? @conn_queues.values.all?(&:empty?) end |
#unblock_recv ⇒ void
This method returns an undefined value.
Wakes a blocked #dequeue_recv with a nil sentinel.
43 44 45 |
# File 'lib/omq/routing/server.rb', line 43 def unblock_recv @recv_queue.enqueue(nil) end |