Module: NNQ::Routing::Backtrace

Included in:
Rep, RepRaw, ReqRaw, Respondent, RespondentRaw, SurveyorRaw
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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.too_many_hops?(header) ⇒ Boolean

Raw-mode TTL check: returns true if header contains at least MAX_HOPS 4-byte words (i.e. forwarding it would push total hops over the cap). Cheap: just bytesize arithmetic.

Returns:

  • (Boolean)


41
42
43
# File 'lib/nnq/routing/backtrace.rb', line 41

def self.too_many_hops?(header)
  header.bytesize >= MAX_HOPS * 4
end

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