Class: OMQ::Routing::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/omq/routing/router.rb

Overview

ROUTER socket routing: identity-based routing.

Prepends peer identity frame on receive. Uses first frame as routing identity on send.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Router

Returns a new instance of Router.

Parameters:



21
22
23
24
25
26
27
# File 'lib/omq/routing/router.rb', line 21

def initialize(engine)
  @engine                  = engine
  @recv_queue              = Routing.build_queue(engine.options.recv_hwm, :block)
  @connections_by_identity = {}
  @identity_by_connection  = {}
  @conn_queues             = {}
end

Instance Attribute Details

#recv_queueAsync::LimitedQueue (readonly)

Returns:

  • (Async::LimitedQueue)


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

def recv_queue
  @recv_queue
end

Instance Method Details

#connection_added(connection) ⇒ Object

Parameters:

  • connection (Protocol::ZMTP::Connection)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omq/routing/router.rb', line 50

def connection_added(connection)
  identity = connection.peer_identity
  identity = SecureRandom.bytes(5) if identity.nil? || identity.empty?
  @connections_by_identity[identity] = connection
  @identity_by_connection[connection] = identity

  @engine.start_recv_pump(connection, @recv_queue) { |msg| [identity, *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 (Protocol::ZMTP::Connection)


66
67
68
69
70
# File 'lib/omq/routing/router.rb', line 66

def connection_removed(connection)
  identity = @identity_by_connection.delete(connection)
  @connections_by_identity.delete(identity) if identity
  @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/router.rb', line 34

def dequeue_recv
  @recv_queue.dequeue
end

#enqueue(parts) ⇒ Object

Enqueues a message for sending. The first frame is the routing identity.

Parameters:

  • parts (Array<String>)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/omq/routing/router.rb', line 77

def enqueue(parts)
  identity = parts.first
  if @engine.options.router_mandatory?
    unless @connections_by_identity[identity]
      raise SocketError, "no route to identity #{identity.inspect}"
    end
  end
  conn = @connections_by_identity[identity]
  return unless conn  # silently drop if peer disconnected
  @conn_queues[conn]&.enqueue(parts[1..])
end

#send_queues_drained?Boolean

Returns true when all per-connection send queues are empty.

Returns:

  • (Boolean)

    true when all per-connection send queues are empty



92
93
94
# File 'lib/omq/routing/router.rb', line 92

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/router.rb', line 43

def unblock_recv
  @recv_queue.enqueue(nil)
end