Class: CommandSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertube-ruby-sdk/core/protocol/command_serializer.rb

Class Method Summary collapse

Class Method Details

.serialize(root_command, connection_data = nil, runtime_version = 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hypertube-ruby-sdk/core/protocol/command_serializer.rb', line 10

def self.serialize(root_command, connection_data = nil, runtime_version = 0)
  buffer = ''.dup
  buffer << [root_command.runtime_name, runtime_version].pack('C*')

  buffer << if connection_data
              connection_data.serialize_connection_data.pack('C*')
            else
              [0, 0, 0, 0, 0, 0, 0].pack('C*')
            end
  buffer << [RuntimeNameHt::RUBY, root_command.command_type].pack('C*')

  # Payload (recursive)
  serialize_recursively(root_command, buffer)

  buffer.bytes
end

.serialize_recursively(command, buffer) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hypertube-ruby-sdk/core/protocol/command_serializer.rb', line 29

def self.serialize_recursively(command, buffer)
  command.payload.each do |item|
    if item.is_a?(Command)
      buffer << TypeSerializer.serialize_command(item).pack('C*')
      serialize_recursively(item, buffer)
    elsif TypesHandler.primitive_or_none?(item)
      buffer << TypeSerializer.serialize_primitive(item).pack('C*')
    else
      cached_reference = ReferencesCache.instance.cache_reference(item)
      ref_command = Command.new(RuntimeNameHt::RUBY, CommandType::REFERENCE, cached_reference)
      buffer << TypeSerializer.serialize_command(ref_command).pack('C*')
      serialize_recursively(ref_command, buffer)
    end
  end
end