Module: NNQ::Routing::Backtrace

Included in:
Rep, Respondent
Defined in:
lib/nnq/routing/backtrace.rb

Overview

Shared backtrace parsing for SP protocols that use the request-id / hop-stack wire format (REQ/REP, SURVEYOR/RESPONDENT).

Wire format: one or more 4-byte big-endian words. The terminal word (request or survey id) has its high bit set (0x80). Preceding words (hop ids added by devices) have the high bit clear.

Constant Summary collapse

MAX_HOPS =

nng’s default ttl

8

Instance Method Summary collapse

Instance Method Details

#parse_backtrace(body) ⇒ Object

Reads 4-byte BE words off the front of body, stopping at the first one whose top byte has its high bit set. Returns

backtrace_bytes, remaining_payload

or nil on malformed input.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nnq/routing/backtrace.rb', line 18

def parse_backtrace(body)
  offset = 0
  hops   = 0

  while hops < MAX_HOPS
    return nil if body.bytesize - offset < 4

    word    = body.byteslice(offset, 4)
    offset += 4
    hops   += 1

    if word.getbyte(0) & 0x80 != 0
      return [body.byteslice(0, offset), body.byteslice(offset..)]
    end
  end

  nil # exceeded TTL without finding terminator
end