Class: StompFrameRecognizer

Inherits:
Object
  • Object
show all
Defined in:
lib/stomp_frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStompFrameRecognizer

Returns a new instance of StompFrameRecognizer.



29
30
31
32
33
34
# File 'lib/stomp_frame.rb', line 29

def initialize
  @buffer = ''
  @body_length = nil
  @frame = StompFrame.new
  @frames = []
end

Instance Attribute Details

#framesObject

Returns the value of attribute frames.



27
28
29
# File 'lib/stomp_frame.rb', line 27

def frames
  @frames
end

Instance Method Details

#<<(buf) ⇒ Object



88
89
90
91
# File 'lib/stomp_frame.rb', line 88

def<< (buf)
  @buffer << buf
  parse
end

#parseObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stomp_frame.rb', line 71

def parse
  count = @frames.size
  
  parse_header unless @frame.command
  if @frame.command
    if @body_length
      parse_binary_body
    else
      parse_text_body
    end
  end
  
  # parse_XXX_body return the frame if they succeed and nil if they fail
  # the result will fall through
  parse if count != @frames.size
end

#parse_binary_bodyObject



44
45
46
47
48
# File 'lib/stomp_frame.rb', line 44

def parse_binary_body
  if @buffer.length > @body_length
    parse_body(@body_length)
  end
end

#parse_body(len) ⇒ Object

Raises:

  • (RuntimeError)


36
37
38
39
40
41
42
# File 'lib/stomp_frame.rb', line 36

def parse_body(len)
  raise RuntimeError.new("Invalid stompframe (missing null term)") unless @buffer[len] == 0
  @frame.body = @buffer[0...len]
  @buffer = @buffer[len+1..-1]
  @frames << @frame
  @frame = StompFrame.new
end

#parse_headerObject



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

def parse_header
  if match = @buffer.match(/^\s*(\S+)$((?:\s*.*?\s*:\s*.*?\s*$)*)$\r?\n$\r?\n/)
    @frame.command, headers = match.captures
    @buffer = match.post_match
    headers.split(/\n/).each do |data|
      if data =~ /^\s*(\S+)\s*:\s*(.*?)\s*$/
        @frame.headers[$1] = $2
      end
    end
    
    # body_length is nil, if there is no content-length, otherwise it is the length (as in integer)
    @body_length = @frame.headers['content-length'] && @frame.headers['content-length'].to_i
  end
end

#parse_text_bodyObject



50
51
52
53
54
# File 'lib/stomp_frame.rb', line 50

def parse_text_body
  if pos = @buffer.index(0)
    parse_body(pos)
  end
end