Module: OMQ::CLI::RoutingHelper

Included in:
RouterRunner, ServerRunner
Defined in:
lib/omq/cli/routing_helper.rb

Overview

Shared routing behaviour for socket types that address peers by ID (ROUTER, SERVER). Include in a runner to get display_routing_id, resolve_target, and a send_targeted_or_eval template method.

Including class must implement #send_to_peer(routing_id, parts).

Instance Method Summary collapse

Instance Method Details

#async_send_loop(task) ⇒ Object

Async sender shared by ROUTER and SERVER monitor mode.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/omq/cli/routing_helper.rb', line 56

def async_send_loop(task)
  task.async do
    n = config.count
    i = 0
    sleep(config.delay) if config.delay
    if config.interval
      interval_send_loop(n, i)
    elsif config.data || config.file
      parts = read_next
      send_targeted_or_eval(parts) if parts
    else
      stdin_send_loop(n, i)
    end
  end
end

#display_routing_id(id) ⇒ Object

Format a raw routing ID for display: printable ASCII as-is, binary as a 0x-prefixed hex string.



15
16
17
18
19
20
21
# File 'lib/omq/cli/routing_helper.rb', line 15

def display_routing_id(id)
  if id.bytes.all? { |b| b >= 0x20 && b <= 0x7E }
    id
  else
    "0x#{id.unpack1("H*")}"
  end
end

#interval_send_loop(n, i) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/omq/cli/routing_helper.rb', line 73

def interval_send_loop(n, i)
  Async::Loop.quantized(interval: config.interval) do
    parts = read_next
    break unless parts
    send_targeted_or_eval(parts)
    i += 1
    break if n && n > 0 && i >= n
  end
end

#resolve_target(target) ⇒ Object

Decode a target string: 0x-prefixed hex is converted to binary, plain strings are returned as-is.



27
28
29
30
31
32
33
# File 'lib/omq/cli/routing_helper.rb', line 27

def resolve_target(target)
  if target.start_with?("0x")
    [target[2..].delete(" ")].pack("H*")
  else
    target
  end
end

#send_targeted_or_eval(parts) ⇒ Object

Send parts to a peer, routing by identity or eval result.

Template method: calls #send_to_peer(id, parts) which the including class must implement for its socket type.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omq/cli/routing_helper.rb', line 41

def send_targeted_or_eval(parts)
  if @send_eval_proc
    parts = eval_send_expr(parts)
    return unless parts
    send_to_peer(resolve_target(parts.shift), parts)
  elsif config.target
    send_to_peer(resolve_target(config.target), parts)
  else
    send_msg(parts)
  end
end

#stdin_send_loop(n, i) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/omq/cli/routing_helper.rb', line 84

def stdin_send_loop(n, i)
  loop do
    parts = read_next
    break unless parts
    send_targeted_or_eval(parts)
    i += 1
    break if n && n > 0 && i >= n
  end
end