class Sftui::Frame
Len Address (4 bytes) Data
----
—-----
—-----
————————----
—-+ | | | | | | |0xff|0xff| ----
—-----
—-----
————————----
—-+
Constants
- ADDRESS_LENGTH
- BITWISE_MASK
- LENGTH_LENGTH
- TERMINATE_CHAR
- TERMINATE_LENGTH
- TRANSACTION_ENDING_CHAR
Attributes
bytes[RW]
Public Class Methods
new(bytes)
click to toggle source
# File lib/sftui/frame.rb, line 21 def initialize(bytes) @bytes = bytes end
Public Instance Methods
+(other)
click to toggle source
# File lib/sftui/frame.rb, line 35 def +(other) # rubocop:disable Metrics/AbcSize raise unless other.completed? && other.machine_id == machine_id combined_data_bytes = data_bytes + other.data_bytes combined_length = LENGTH_LENGTH + ADDRESS_LENGTH + TERMINATE_LENGTH + combined_data_bytes.length Frame.new( to_bytes(combined_length, LENGTH_LENGTH) + addr_bytes + combined_data_bytes + terminate_bytes ) end
addr_bytes()
click to toggle source
# File lib/sftui/frame.rb, line 67 def addr_bytes bytes[addr_offset..addr_offset + ADDRESS_LENGTH - 1] end
completed?()
click to toggle source
# File lib/sftui/frame.rb, line 25 def completed? bytes.length == length && bytes[(-1 - TERMINATE_LENGTH + 1)..-1] == terminate_bytes end
data_bytes()
click to toggle source
# File lib/sftui/frame.rb, line 53 def data_bytes return [] unless completed? bytes[data_offset..(-1 - TERMINATE_LENGTH)] end
display()
click to toggle source
# File lib/sftui/frame.rb, line 75 def display # rubocop:disable Metrics/AbcSize table = TTY::Table.new(header: %w[Length Address Data Terminate]) table << [ length_bytes.map { |byte| to_hex(byte) }.join(' '), addr_bytes.map { |byte| to_hex(byte) }.join(' '), data_bytes.map { |byte| to_hex(byte) }.join(' '), terminate_bytes.map { |byte| to_hex(byte) }.join(' ') ] table.render(:unicode, alignments: %i[center center]) end
last_frame_in_transaction?()
click to toggle source
# File lib/sftui/frame.rb, line 59 def last_frame_in_transaction? data_bytes[-1] == TRANSACTION_ENDING_CHAR end
length_bytes()
click to toggle source
# File lib/sftui/frame.rb, line 63 def length_bytes bytes[0..LENGTH_LENGTH - 1] end
machine_id()
click to toggle source
# File lib/sftui/frame.rb, line 29 def machine_id return unless completed? to_integer(addr_bytes) end
sanitized_data()
click to toggle source
# File lib/sftui/frame.rb, line 49 def sanitized_data last_frame_in_transaction? ? data_bytes[0..-2].pack('c*') : data_bytes.pack('c*') end
terminate_bytes()
click to toggle source
# File lib/sftui/frame.rb, line 71 def terminate_bytes Array.new(TERMINATE_LENGTH, TERMINATE_CHAR) end
Private Instance Methods
addr_offset()
click to toggle source
# File lib/sftui/frame.rb, line 93 def addr_offset LENGTH_LENGTH end
data_offset()
click to toggle source
# File lib/sftui/frame.rb, line 97 def data_offset LENGTH_LENGTH + ADDRESS_LENGTH end
length()
click to toggle source
# File lib/sftui/frame.rb, line 89 def length to_integer(length_bytes) end
to_bytes(integer, length)
click to toggle source
# File lib/sftui/frame.rb, line 111 def to_bytes(integer, length) bytes = Array.new(length) index = 0 while index < length bytes[-1 - index] = (integer >> (index * 8)) & BITWISE_MASK index += 1 end bytes end
to_hex(byte)
click to toggle source
# File lib/sftui/frame.rb, line 122 def to_hex(byte) "0x#{byte.to_s(16)}" end
to_integer(bytes)
click to toggle source
# File lib/sftui/frame.rb, line 101 def to_integer(bytes) sum = 0 index = 0 while index < bytes.length sum += bytes[index] << ((bytes.length - index - 1) * 8) index += 1 end sum end