Class: Krakow::FrameType Abstract

Inherits:
Object
  • Object
show all
Extended by:
Utils::Lazy::ClassMethods
Includes:
Utils::Lazy, Utils::Lazy::InstanceMethods
Defined in:
lib/krakow/frame_type.rb,
lib/krakow/frame_type/error.rb,
lib/krakow/frame_type/message.rb,
lib/krakow/frame_type/response.rb

Overview

This class is abstract.

Received message

Direct Known Subclasses

Error, Message, Response

Defined Under Namespace

Classes: Error, Message, Response

Constant Summary collapse

FRAME_TYPE_MAP =

Registered frame types

[
  FrameType::Response,
  FrameType::Error,
  FrameType::Message
]
SIZE_BYTES =

Size bytes

4

Instance Attribute Summary

Attributes included from Utils::Lazy::InstanceMethods

#arguments

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Lazy::ClassMethods

attribute, attributes, set_attributes

Methods included from Utils::Lazy::InstanceMethods

#initialize, #inspect, #to_s

Methods included from Utils::Lazy

included

Methods included from Utils::Logging

level=, #log

Class Method Details

.build(args = {}) ⇒ FrameType

Build proper FrameType instance based on args

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :type (FrameType)

    class of frame

  • :data (String)
  • :size (Integer)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/krakow/frame_type.rb', line 41

def build(args={})
  klass = FRAME_TYPE_MAP[args[:type].to_i]
  if(klass == FrameType::Response)
    klass.new(:response => args[:data])
  elsif(klass == FrameType::Error)
    klass.new(:error => args[:data])
  elsif(klass == FrameType::Message)
    unpacked = args[:data].unpack("Q>s>a16a#{args[:size]}")
    klass.new(
      Hash[*([:timestamp, :attempts, :message_id, :message].zip(unpacked).flatten)]
    )
  else
    raise TypeError.new "Unknown frame type received: #{args[:type].inspect} - #{klass.inspect}"
  end
end

.decode(bytes) ⇒ Hash

Information about incoming frame

Parameters:

  • bytes (String)

Returns:

  • (Hash)


30
31
32
33
# File 'lib/krakow/frame_type.rb', line 30

def decode(bytes)
  size, type = bytes.unpack('l>l>')
  {:size => size - SIZE_BYTES, :type => type}
end

Instance Method Details

#contentString

Content of message

Returns:

  • (String)


61
62
63
# File 'lib/krakow/frame_type.rb', line 61

def content
  raise NotImplementedError.new 'Content method not properly defined!'
end