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



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
259
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 199

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 a 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



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
197
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 148

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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
28
# 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?(FalseClass) || payload_item.is_a?(TrueClass)
    return serialize_bool(payload_item)
  else
    raise TypeError, "Unsupported payload item type: #{payload_item.class} for payload item: #{payload_item}."
  end
end

.serialize_string(string_value) ⇒ Object



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

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



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

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



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

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



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

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



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

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