Class: MarilynRPC::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/marilyn-rpc/envelope.rb

Overview

This class handles the envelope parsing and encoding which will be used by the server to handle multiple writes into the envelope.

Constant Summary collapse

HEADER_SIZE =
5
HEADER_ENCODING =
"NC".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil, type = nil) ⇒ Envelope

create a new envelope instance

Parameters:

  • content (String) (defaults to: nil)

    the content of the new envelope

  • type (String) (defaults to: nil)

    the type of content



16
17
18
19
# File 'lib/marilyn-rpc/envelope.rb', line 16

def initialize(content = nil, type = nil)
  self.content = content
  @type = type
end

Instance Attribute Details

#sizeObject (readonly)

size of the envelope content



8
9
10
# File 'lib/marilyn-rpc/envelope.rb', line 8

def size
  @size
end

#typeObject

the type of mail that is in the envelope



11
12
13
# File 'lib/marilyn-rpc/envelope.rb', line 11

def type
  @type
end

Instance Method Details

#contentString

Note:

should only be requested when the message if #finished?.

returns the content of the envelope

Returns:

  • (String)

    the content of the envelope



56
57
58
# File 'lib/marilyn-rpc/envelope.rb', line 56

def content
  @buffer
end

#content=(content) ⇒ Object

sets the content of the envelope. If nil was passed an empty string will be set.

Parameters:

  • data (String)

    the new content



63
64
65
66
# File 'lib/marilyn-rpc/envelope.rb', line 63

def content=(content)
  @buffer = content || ""
  @size = content.nil? ? nil : content.size
end

#encodeString

encodes the envelope to be send over the wire

Returns:

  • (String)

    encoded envelope



70
71
72
# File 'lib/marilyn-rpc/envelope.rb', line 70

def encode
  [@size, @type].pack(HEADER_ENCODING) + @buffer
end

#finished?Boolean

checks if the complete envelope was allready parsed

Returns:

  • (Boolean)

    true if the message was parsed



76
77
78
# File 'lib/marilyn-rpc/envelope.rb', line 76

def finished?
  @buffer.size == @size
end

#parse!(data) ⇒ String?

parses the passed data

Parameters:

  • data (String)

    parses the parsed data

Returns:

  • (String, nil)

    returns data that is not part of this string (in case the) parser gets more data than the length of the envelope. In case there are no data it will return nil.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/marilyn-rpc/envelope.rb', line 31

def parse!(data)
  @buffer += data

  # parse the length field of the 
  if @size == nil && @buffer.size >= HEADER_SIZE
    parse_header!(@buffer.slice!(0...HEADER_SIZE))
  end
  
  # envelope is complete and contains overhang
  if @size && @buffer.size > @size
    return @buffer.slice!(@size, @buffer.size) # returns the overhang
  end
end

#parse_header!(header) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

parses the header without having much checking overhead. This is useful in situations where we can assure the size beforehand

Parameters:

  • header (String)

    the header byte string of size HEADER_SIZE



49
50
51
# File 'lib/marilyn-rpc/envelope.rb', line 49

def parse_header!(header)
  @size, @type = header.unpack(HEADER_ENCODING)
end

#reset!Object

resets the envelope object to contain no data, like if it was newly created



22
23
24
# File 'lib/marilyn-rpc/envelope.rb', line 22

def reset!
  @buffer, @size, @type = "", nil, nil
end