Sha256: 0eb201dff8750aa524ff2da75186f8d4d00cfce7cb61038d98f78158774836d1

Contents?: true

Size: 1.97 KB

Versions: 68

Compression:

Stored size: 1.97 KB

Contents

# encoding: utf-8

# This will be probably used by all the async libraries like EventMachine.
# It expects the whole frame as one string, so if library of your choice
# gives you input chunk-by-chunk, you'll need to have something like this:
#
# class Client
#   include EventMachine::Deferrable
#
#   def receive_data(chunk)
#     if @payload.nil?
#       self.decode_from_string(chunk[0..6])
#       @payload = ""
#     elsif @payload && chunk[-1] != FINAL_OCTET
#       @payload += chunk
#       @size += chunk.bytesize
#     else
#       check_size(@size, @payload.bytesize)
#       Frame.decode(@payload) # we need the whole payload
#       @size, @payload = nil
#     end
#   end
#
#   NOTE: the client should also implement waiting for another frames, in case that some header/body frames are expected.
# end

require "amq/client/exceptions"

module AMQ
  module Client
    module Framing
      module String
        class Frame < AMQ::Protocol::Frame
          ENCODINGS_SUPPORTED = defined? Encoding
          HEADER_SLICE = (0..6).freeze
          DATA_SLICE = (7..-1).freeze
          PAYLOAD_SLICE = (0..-2).freeze

          def self.decode(string)
            header              = string[HEADER_SLICE]
            type, channel, size = self.decode_header(header)
            data                = string[DATA_SLICE]
            payload             = data[PAYLOAD_SLICE]
            frame_end           = data[-1, 1]

            frame_end.force_encoding(AMQ::Protocol::Frame::FINAL_OCTET.encoding) if ENCODINGS_SUPPORTED

            # 1) the size is miscalculated
            if payload.bytesize != size
              raise BadLengthError.new(size, payload.bytesize)
            end

            # 2) the size is OK, but the string doesn't end with FINAL_OCTET
            raise NoFinalOctetError.new if frame_end != AMQ::Protocol::Frame::FINAL_OCTET

            self.new(type, payload, channel)
          end # self.from
        end # Frame
      end # String
    end # Framing
  end # Client
end # AMQ

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
amq-client-0.7.0.alpha27 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha26 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha25 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha24 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha23 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha22 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha21 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha20 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha19 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha18 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha17 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha16 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha15 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha14 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha13 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha12 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha11 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha10 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha9 lib/amq/client/framing/string/frame.rb
amq-client-0.7.0.alpha8 lib/amq/client/framing/string/frame.rb