Class: CommandSerializer

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

Class Method Summary collapse

Class Method Details

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 7

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

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

  # Payload (recursive)
  serialize_recursively(root_command, buffer)

  buffer.bytes
end

.serialize_recursively(command, buffer) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 26

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)
    else
      buffer << TypeSerializer.serialize_primitive(item).pack('C*')
    end
  end
end