Module: ASIR::Transport::PayloadIO

Included in:
ConnectionOriented, File
Defined in:
lib/asir/transport/payload_io.rb

Overview

!SLIDE Payload IO for Transport

Framing

  • Header line containing the number of bytes in the payload.

  • The payload bytes.

  • Blank line.

  • Footer.

Defined Under Namespace

Classes: UnexpectedResponse

Constant Summary collapse

HEADER =
"# asir_payload_size: "
"\n# asir_payload_end"

Instance Method Summary collapse

Instance Method Details

#_read(stream, state) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/asir/transport/payload_io.rb', line 28

def _read stream, state
  size = /\d+$/.match(stream.readline.chomp)[0].to_i # HEADER (size)
  payload = stream.read(size)
  stream.readline # FOOTER
  stream.readline
  payload
end

#_read_line_and_expect!(stream, regexp, consume = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/asir/transport/payload_io.rb', line 36

def _read_line_and_expect! stream, regexp, consume = nil
  ok = false
  until ok
    line = stream.readline
    _log { "_read_line_and_expect! #{stream} #{line.inspect}" }
    ok = consume && consume.match(line) ? false : true
  end
  unless match = regexp.match(line)
    _log { "_read_line_and_expect! #{stream} #{regexp.inspect} !~ #{line.inspect}" }
    exc = UnexpectedResponse.new("expected #{regexp.inspect}, received #{line.inspect}")
    exc.expected = regexp
    exc.received = line
    raise exc
  end
  match
end

#_write(payload, stream, state) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/asir/transport/payload_io.rb', line 19

def _write payload, stream, state
  stream.write HEADER
  stream.puts payload.size
  stream.write payload
  stream.puts FOOTER
  stream.flush
  stream
end

#closeObject

!SLIDE pause



54
55
56
57
58
59
60
61
# File 'lib/asir/transport/payload_io.rb', line 54

def close
  if @stream
    _before_close! stream if respond_to?(:_before_close!)
    @stream.close
  end
ensure
  @stream = nil unless Channel === @stream
end