Class: Stomper::FrameSerializer

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

Overview

This class serializes Stomp frames to IO streams. Submodule mixins within this class are used to adjust the serialization behavior depending upon the Stomp Protocol version being used.

Defined Under Namespace

Modules: V1_1

Constant Summary collapse

FRAME_TERMINATOR =

The character that must be present at the end of every Stomp frame.

"\000"

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ FrameSerializer

Creates a new frame serializer that will read frames from and write frames to the supplied IO object (typically a TCP or SSL socket.)

Parameters:

  • io (IO)

    IO stream to read from and write to



16
17
18
19
20
# File 'lib/stomper/frame_serializer.rb', line 16

def initialize(io)
  @io = io
  @write_mutex = ::Mutex.new
  @read_mutex = ::Mutex.new
end

Instance Method Details

#escape_header_name(str) ⇒ String

Note:

If the connection is using the 1.1 protocol, this method will be overridden by Stomper::FrameSerializer::V1_1#escape_header_name

Escape a header name to comply with Stomp Protocol 1.0 specifications. All LF (“n”) and “:” characters are replaced with empty strings.

Parameters:

  • str (String)

Returns:

  • (String)

    escaped header name



79
80
81
# File 'lib/stomper/frame_serializer.rb', line 79

def escape_header_name(str)
  str.gsub(/[\n:]/, '')
end

#escape_header_value(str) ⇒ String

Note:

If the connection is using the 1.1 protocol, this method will be overridden by Stomper::FrameSerializer::V1_1#escape_header_value

Escape a header value to comply with Stomp Protocol 1.0 specifications. All LF (“n”) characters are replaced with empty strings.

Parameters:

  • str (String)

Returns:

  • (String)

    escaped header value



89
90
91
# File 'lib/stomper/frame_serializer.rb', line 89

def escape_header_value(str)
  str.gsub(/\n/, '')
end

#extend_for_protocol(version) ⇒ self

Extends the serializer based on the version of the protocol being used.

Parameters:

  • version (String)

    protocol version being used

Returns:

  • (self)


25
26
27
28
29
30
# File 'lib/stomper/frame_serializer.rb', line 25

def extend_for_protocol(version)
  if EXTEND_BY_VERSION[version]
    EXTEND_BY_VERSION[version].each { |m| extend m }
  end
  self
end

#read_frameStomper::Frame

Deserializes and returns a Stomper::Frame read from the underlying IO stream. If the IO stream produces data that violates the Stomp protocol specification, an instance of Errors::FatalProtocolError, or one of its subclasses, will be raised.

Returns:

Raises:



48
49
50
# File 'lib/stomper/frame_serializer.rb', line 48

def read_frame
  @read_mutex.synchronize { __read_frame__ }
end

#serialize_headers(frame) ⇒ String

Converts the headers of the supplied frame into a single “n” delimited string that’s suitable for writing to io.

Parameters:

  • frame (Stomper::Frame)

    the frame whose headers should be serialized

Returns:

  • (String)


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

def serialize_headers(frame)
  serialized = frame.headers.inject('') do |head_str, (k, v)|
    k = escape_header_name(k)
    next head_str if k.empty? || ['content-type', 'content-length'].include?(k)
    head_str << "#{k}:#{escape_header_value(v)}\n"
  end
  if frame.body
    if ct = determine_content_type(frame)
      serialized << "content-type:#{ct}\n"
    end
    if clen = determine_content_length(frame)
      serialized << "content-length:#{clen}\n"
    end
  end
  serialized
end

#unescape_header_name(str) ⇒ String Also known as: unescape_header_value

Note:

If the connection is using the 1.1 protocol, this method will be overridden by Stomper::FrameSerializer::V1_1#unescape_header_name

Return the header name as it was passed. Stomp 1.0 does not provide any means for escaping special characters such as “:” and “n”

Parameters:

  • str (String)

Returns:

  • (String)


99
# File 'lib/stomper/frame_serializer.rb', line 99

def unescape_header_name(str); str; end

#write_frame(frame) ⇒ Stomper::Frame

Serializes and writes a Stomper::Frame to the underlying IO stream. This includes setting the appropriate values for ‘content-length’ and ‘content-type’ headers, if applicable.

Parameters:

Returns:



37
38
39
# File 'lib/stomper/frame_serializer.rb', line 37

def write_frame(frame)
  @write_mutex.synchronize { __write_frame__(frame) }
end