Class: CommandDeserializer

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

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ CommandDeserializer

Returns a new instance of CommandDeserializer.



9
10
11
12
13
14
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 9

def initialize(buffer)
  @buffer = buffer
  @buffer_len = buffer.length
  @command = Command.new(buffer[0], buffer[10], [])
  @position = 11
end

Instance Method Details

#deserializeObject



20
21
22
23
24
25
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 20

def deserialize
  until is_at_end
    @command = @command.add_arg_to_payload(read_object(@buffer[@position]))
  end
  @command
end

#is_at_endObject



16
17
18
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 16

def is_at_end
  @position == @buffer_len
end

#read_boolObject



113
114
115
116
117
118
119
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 113

def read_bool
  size = 1
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_bool(@buffer[p...p + size])
end

#read_byteObject



129
130
131
132
133
134
135
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 129

def read_byte
  size = 1
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_byte(@buffer[p...p + size])
end

#read_charObject



137
138
139
140
141
142
143
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 137

def read_char
  size = 1
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_char(@buffer[p...p + size])
end

#read_commandObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 58

def read_command
  p = @position
  number_of_elements_in_payload = TypeDeserializer.deserialize_int(@buffer[p + 1..p + 4])
  runtime = @buffer[p + 5]
  command_type = @buffer[p + 6]
  @position += 7

  payload = []
  i = 0
  while i < number_of_elements_in_payload
    payload << read_object(@buffer[@position])
    i += 1
  end
  Command.new(runtime, command_type, payload)
end

#read_doubleObject



153
154
155
156
157
158
159
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 153

def read_double
  size = 8
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_double(@buffer[p...p + size])
end

#read_floatObject



121
122
123
124
125
126
127
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 121

def read_float
  size = 4
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_float(@buffer[p...p + size])
end

#read_intObject



105
106
107
108
109
110
111
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 105

def read_int
  size = 4
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_int(@buffer[p...p + size])
end

#read_longlongObject



145
146
147
148
149
150
151
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 145

def read_longlong
  size = 8
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_longlong(@buffer[p...p + size])
end

#read_nilObject



177
178
179
180
181
182
183
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 177

def read_nil
  size = 1
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_nil(@buffer[p...p + size])
end

#read_object(type_num) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 27

def read_object(type_num)
  case type_num
  when Type::COMMAND
    return self.read_command
  when Type::JAVONET_STRING
    return self.read_string
  when Type::JAVONET_INTEGER
    return self.read_int
  when Type::JAVONET_BOOLEAN
    return self.read_bool
  when Type::JAVONET_FLOAT
    return self.read_float
  when Type::JAVONET_BYTE
    return self.read_byte
  when Type::JAVONET_CHAR
    return self.read_char
  when Type::JAVONET_LONG_LONG
    return self.read_longlong
  when Type::JAVONET_DOUBLE
    return self.read_double
  when Type::JAVONET_UNSIGNED_LONG_LONG
    return self.read_ullong
  when Type::JAVONET_UNSIGNED_INTEGER
    return self.read_uint
  when Type::JAVONET_NULL
    return self.read_nil
  else
    raise Exception.new("Type #{type_num} not supported in command deserializer")
  end
end

#read_stringObject

old implementation

def read_command
  p = @position
  number_of_elements_in_payload = TypeDeserializer.deserialize_int(@buffer[p + 1..p + 4])
  runtime = @buffer[p + 5]
  command_type = @buffer[p + 6]
  @position += 7
  return_command = Command.new(runtime, command_type, [])
  read_command_recursively(number_of_elements_in_payload, return_command)
end

def read_command_recursively(number_of_elements_in_payload_left, cmd)
  if number_of_elements_in_payload_left == 0
    cmd
  else
    p = @position
    cmd = cmd.add_arg_to_payload(self.read_object(@buffer[p]))
    read_command_recursively(number_of_elements_in_payload_left - 1, cmd)
  end
end


95
96
97
98
99
100
101
102
103
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 95

def read_string
  p = @position
  string_encoding_mode = @buffer[p + 1]
  size = TypeDeserializer.deserialize_int(@buffer[p + 2..p + 5])
  @position += 6
  p = @position
  @position += size
  TypeDeserializer.deserialize_string(string_encoding_mode, @buffer[p...p + size])
end

#read_uintObject



169
170
171
172
173
174
175
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 169

def read_uint
  size = 4
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_uint(@buffer[p...p + size])
end

#read_ullongObject



161
162
163
164
165
166
167
# File 'lib/javonet-ruby-sdk/core/protocol/command_deserializer.rb', line 161

def read_ullong
  size = 8
  @position += 2
  p = @position
  @position += size
  TypeDeserializer.deserialize_ullong(@buffer[p...p + size])
end