Class: CommandSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/javonet-ruby-sdk/core/protocol/command_serializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/protocol/command_serializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/protocol/command_serializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/core/protocol/command_serializer.rb

Instance Method Summary collapse

Constructor Details

#initializeCommandSerializer

Returns a new instance of CommandSerializer.



8
9
10
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 8

def initialize
  @byte_buffer = []
end

Instance Method Details

#insert_into_buffer(arguments) ⇒ Object



52
53
54
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 52

def insert_into_buffer(arguments)
  @byte_buffer = @byte_buffer + arguments
end

#serialize(root_command, connection_type = ConnectionType::IN_MEMORY, tcp_connection_data = nil, runtime_version = 0) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 12

def serialize(root_command, connection_type = ConnectionType::IN_MEMORY, tcp_connection_data = nil, runtime_version = 0)
  queue = []
  queue.unshift(root_command)
  self.insert_into_buffer([root_command.runtime_name, runtime_version])
  self.insert_into_buffer([connection_type])
  self.insert_into_buffer(self.serialize_tcp(tcp_connection_data))
  self.insert_into_buffer([RuntimeName::RUBY, root_command.command_type])
  self.serialize_recursively(queue)
end

#serialize_primitive(payload_item) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 29

def serialize_primitive(payload_item)
  if payload_item.nil?
    return TypeSerializer.serialize_nil
  elsif [true, false].include? payload_item
    return TypeSerializer.serialize_bool(payload_item)
  elsif payload_item.is_a? Integer
    if (-2 ** 31..2 ** 31).include?(payload_item)
      return TypeSerializer.serialize_int(payload_item)
    elsif (-2 ** 63..2 ** 63).include?(payload_item)
      return TypeSerializer.serialize_longlong(payload_item)
    else
      return TypeSerializer.serialize_ullong(payload_item)
    end
  elsif payload_item.is_a? String
    return TypeSerializer.serialize_string(payload_item)
  elsif payload_item.is_a? Float
    return TypeSerializer.serialize_double(payload_item)
  elsif payload_item.is_a?
  else
    raise Exception.new("Payload not supported in command serializer")
  end
end

#serialize_recursively(queue) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 56

def serialize_recursively(queue)
  if queue.length == 0
    return @byte_buffer
  end
  command = queue.shift
  queue.unshift(command.drop_first_payload_argument)
  if command.payload.length > 0
    if command.payload[0].is_a? Command
      inner_command = command.payload[0]
      self.insert_into_buffer(TypeSerializer.serialize_command(inner_command))
      queue.unshift(inner_command)
    else
      result = self.serialize_primitive(command.payload[0])
      self.insert_into_buffer(result)
      return self.serialize_recursively(queue)
    end
  else
    queue.shift
  end
  self.serialize_recursively(queue)
end

#serialize_tcp(tcp_connection_data) ⇒ Object



22
23
24
25
26
27
# File 'lib/javonet-ruby-sdk/core/protocol/command_serializer.rb', line 22

def serialize_tcp(tcp_connection_data)
  if tcp_connection_data.nil?
    return [0, 0, 0, 0, 0, 0]
  end
  tcp_connection_data.get_address_bytes + tcp_connection_data.get_port_bytes
end