Class: TypeSerializer

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

Class Method Summary collapse

Class Method Details

.double_to_bytes(value) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 198

def self.double_to_bytes(value)
  # IEEE 754 double-precision binary floating-point format (64-bit)
  # Manual implementation without using pack
  bits_low = 0
  bits_high = 0

  if value == 0
    # Zero is a special case
    return [0, 0, 0, 0, 0, 0, 0, 0]
  elsif value.nan?
    # NaN is another special case
    bits_high = 0x7FF80000
    bits_low = 0
  elsif value.infinite?
    # Handle infinity
    bits_high = value > 0 ? 0x7FF00000 : 0xFFF00000
    bits_low = 0
  else
    # Regular number
    sign = value < 0 ? 1 : 0
    value = value.abs

    # Get exponent and fraction
    exponent = Math.log2(value).floor
    fraction = value / (2.0**exponent) - 1.0

    # Normalize exponent for IEEE 754 bias
    biased_exponent = exponent + 1023

    # Clamp exponent to valid range
    if biased_exponent <= 0
      # Denormalized values
      biased_exponent = 0
      fraction = value / (2.0**(-1022))
    elsif biased_exponent >= 2047
      # Overflow, return infinity
      biased_exponent = 2047
      fraction = 0
    end

    # Assemble the bits
    # Convert fraction to 52-bit integer
    fraction_bits = (fraction * 0x10000000000000).round

    # Split into high and low 32-bit words
    bits_high = (sign << 31) | (biased_exponent << 20) | ((fraction_bits >> 32) & 0xFFFFF)
    bits_low = fraction_bits & 0xFFFFFFFF
  end

  # Convert to bytes (little-endian)
  [
    bits_low & 0xFF,
    (bits_low >> 8) & 0xFF,
    (bits_low >> 16) & 0xFF,
    (bits_low >> 24) & 0xFF,
    bits_high & 0xFF,
    (bits_high >> 8) & 0xFF,
    (bits_high >> 16) & 0xFF,
    (bits_high >> 24) & 0xFF
  ]
end

.float_to_bytes(value) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 147

def self.float_to_bytes(value)
  # IEEE 754 single-precision binary floating-point format (32-bit)
  # Manual implementation without using pack
  bits = 0

  if value == 0
    # Zero is a special case
    return [0, 0, 0, 0]
  elsif value.nan?
    # NaN is another special case
    bits = 0x7FC00000
  elsif value.infinite?
    # Handle infinity
    bits = value > 0 ? 0x7F800000 : 0xFF800000
  else
    # Regular number
    sign = value < 0 ? 1 : 0
    value = value.abs

    # Get exponent and fraction
    exponent = Math.log2(value).floor
    fraction = value / (2.0**exponent) - 1.0

    # Normalize exponent for IEEE 754 bias
    biased_exponent = exponent + 127

    # Clamp exponent to valid range
    if biased_exponent <= 0
      # Denormalized values
      biased_exponent = 0
      fraction = value / (2.0**(-126))
    elsif biased_exponent >= 255
      # Overflow, return infinity
      biased_exponent = 255
      fraction = 0
    end

    # Assemble the bits
    fraction_bits = (fraction * 0x800000).round & 0x7FFFFF
    bits = (sign << 31) | (biased_exponent << 23) | fraction_bits
  end

  # Convert to bytes (little-endian)
  [
    bits & 0xFF,
    (bits >> 8) & 0xFF,
    (bits >> 16) & 0xFF,
    (bits >> 24) & 0xFF
  ]
end

.int_to_bytes(value) ⇒ Object

Helper methods for byte conversion



99
100
101
102
103
104
105
106
107
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 99

def self.int_to_bytes(value)
  # Convert to 4-byte (32-bit) integer, little-endian
  [
    value & 0xFF,
    (value >> 8) & 0xFF,
    (value >> 16) & 0xFF,
    (value >> 24) & 0xFF
  ]
end

.longlong_to_bytes(value) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 119

def self.longlong_to_bytes(value)
  # Convert to 8-byte (64-bit) integer, little-endian
  [
    value & 0xFF,
    (value >> 8) & 0xFF,
    (value >> 16) & 0xFF,
    (value >> 24) & 0xFF,
    (value >> 32) & 0xFF,
    (value >> 40) & 0xFF,
    (value >> 48) & 0xFF,
    (value >> 56) & 0xFF
  ]
end

.serialize_bool(bool_value) ⇒ Object



46
47
48
49
50
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 46

def self.serialize_bool(bool_value)
  encoded_bool_list = bool_value ? [1] : [0]
  length = encoded_bool_list.length
  return [Type::JAVONET_BOOLEAN, length] + encoded_bool_list
end

.serialize_byte(byte_value) ⇒ Object



58
59
60
61
62
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 58

def self.serialize_byte(byte_value)
  encoded_byte_list = [byte_value & 0xFF]
  length = encoded_byte_list.length
  return [Type::JAVONET_BYTE, length] + encoded_byte_list
end

.serialize_char(char_value) ⇒ Object



64
65
66
67
68
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 64

def self.serialize_char(char_value)
  encoded_char_list = [char_value & 0xFF]
  length = encoded_char_list.length
  return [Type::JAVONET_CHAR, length] + encoded_char_list
end

.serialize_command(command) ⇒ Object



29
30
31
32
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 29

def self.serialize_command(command)
  length = int_to_bytes(command.payload.length)
  return [Type::COMMAND] + length + [command.runtime_name, command.command_type]
end

.serialize_double(double_value) ⇒ Object



76
77
78
79
80
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 76

def self.serialize_double(double_value)
  encoded_double_list = double_to_bytes(double_value)
  length = encoded_double_list.length
  return [Type::JAVONET_DOUBLE, length] + encoded_double_list
end

.serialize_float(float_value) ⇒ Object



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

def self.serialize_float(float_value)
  encoded_float_list = float_to_bytes(float_value)
  length = encoded_float_list.length
  return [Type::JAVONET_FLOAT, length] + encoded_float_list
end

.serialize_int(int_value) ⇒ Object



40
41
42
43
44
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 40

def self.serialize_int(int_value)
  encoded_int_list = int_to_bytes(int_value)
  length = encoded_int_list.length
  return [Type::JAVONET_INTEGER, length] + encoded_int_list
end

.serialize_longlong(longlong_value) ⇒ Object



70
71
72
73
74
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 70

def self.serialize_longlong(longlong_value)
  encoded_longlong_list = longlong_to_bytes(longlong_value)
  length = encoded_longlong_list.length
  return [Type::JAVONET_LONG_LONG, length] + encoded_longlong_list
end

.serialize_nilObject



94
95
96
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 94

def self.serialize_nil
  return [Type::JAVONET_NULL, 1, 0]
end

.serialize_primitive(payload_item) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 6

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

.serialize_string(string_value) ⇒ Object



34
35
36
37
38
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 34

def self.serialize_string(string_value)
  encoded_string_list = string_value.bytes
  length = int_to_bytes(encoded_string_list.length)
  return [Type::JAVONET_STRING, StringEncodingMode::UTF8] + length + encoded_string_list
end

.serialize_uint(uint_value) ⇒ Object



82
83
84
85
86
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 82

def self.serialize_uint(uint_value)
  encoded_uint_list = uint_to_bytes(uint_value)
  length = encoded_uint_list.length
  return [Type::JAVONET_UNSIGNED_INTEGER, length] + encoded_uint_list
end

.serialize_ullong(ullong_value) ⇒ Object



88
89
90
91
92
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 88

def self.serialize_ullong(ullong_value)
  encoded_ullong_list = ullong_to_bytes(ullong_value)
  length = encoded_ullong_list.length
  return [Type::JAVONET_UNSIGNED_LONG_LONG, length] + encoded_ullong_list
end

.uint_to_bytes(value) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 109

def self.uint_to_bytes(value)
  # Convert to 4-byte unsigned integer, little-endian
  [
    value & 0xFF,
    (value >> 8) & 0xFF,
    (value >> 16) & 0xFF,
    (value >> 24) & 0xFF
  ]
end

.ullong_to_bytes(value) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 133

def self.ullong_to_bytes(value)
  # Convert to 8-byte unsigned long long, little-endian
  [
    value & 0xFF,
    (value >> 8) & 0xFF,
    (value >> 16) & 0xFF,
    (value >> 24) & 0xFF,
    (value >> 32) & 0xFF,
    (value >> 40) & 0xFF,
    (value >> 48) & 0xFF,
    (value >> 56) & 0xFF
  ]
end