Class: OMQ::Routing::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Server

Returns a new instance of Server.

Parameters:



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.options.recv_hwm, :block)
  @connections_by_routing_id  = {}
  @routing_id_by_connection   = {}
  @conn_queues                = {}
end

Instance Attribute Details

#recv_queueAsync::LimitedQueue (readonly)

Returns:

  • (Async::LimitedQueue)


16
17
18
# File 'lib/omq/routing/server.rb', line 16

def recv_queue
  @recv_queue
end

Instance Method Details

#connection_added(connection) ⇒ Object

Parameters:

  • connection (Connection)


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.options.send_hwm, :block)
  @conn_queues[connection] = q
  ConnSendPump.start(@engine, connection, q)
end

#connection_removed(connection) ⇒ Object

Parameters:

  • connection (Connection)


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_recvArray<String>?

Dequeues the next received message. Blocks until one is available.

Returns:

  • (Array<String>, nil)


34
35
36
# File 'lib/omq/routing/server.rb', line 34

def dequeue_recv
  @recv_queue.dequeue
end

#enqueue(parts) ⇒ Object

Parameters:

  • parts (Array<String>)


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.

Returns:

  • (Boolean)


84
85
86
# File 'lib/omq/routing/server.rb', line 84

def send_queues_drained?
  @conn_queues.values.all?(&:empty?)
end

#unblock_recvvoid

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