Class: Sftui::Frame
- Inherits:
-
Object
- Object
- Sftui::Frame
- Defined in:
- lib/sftui/frame.rb
Overview
Len Address (4 bytes) Data ----
—-----
—-----
————————----
—-+ | | | | | | |0xff|0xff| ----
—-----
—-----
————————----
—-+
Constant Summary collapse
- LENGTH_LENGTH =
1
- ADDRESS_LENGTH =
4
- TERMINATE_LENGTH =
2
- TERMINATE_CHAR =
0xff
- BITWISE_MASK =
0xff
- TRANSACTION_ENDING_CHAR =
0x0d
Instance Attribute Summary collapse
-
#bytes ⇒ Object
Returns the value of attribute bytes.
Instance Method Summary collapse
-
#+(other) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #addr_bytes ⇒ Object
- #completed? ⇒ Boolean
- #data_bytes ⇒ Object
-
#display ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(bytes) ⇒ Frame
constructor
A new instance of Frame.
- #last_frame_in_transaction? ⇒ Boolean
- #length_bytes ⇒ Object
- #machine_id ⇒ Object
- #sanitized_data ⇒ Object
- #terminate_bytes ⇒ Object
Constructor Details
#initialize(bytes) ⇒ Frame
Returns a new instance of Frame.
21 22 23 |
# File 'lib/sftui/frame.rb', line 21 def initialize(bytes) @bytes = bytes end |
Instance Attribute Details
#bytes ⇒ Object
Returns the value of attribute bytes.
12 13 14 |
# File 'lib/sftui/frame.rb', line 12 def bytes @bytes end |
Instance Method Details
#+(other) ⇒ Object
rubocop:disable Metrics/AbcSize
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
67 68 69 |
# File 'lib/sftui/frame.rb', line 67 def addr_bytes bytes[addr_offset..addr_offset + ADDRESS_LENGTH - 1] end |
#completed? ⇒ Boolean
25 26 27 |
# File 'lib/sftui/frame.rb', line 25 def completed? bytes.length == length && bytes[(-1 - TERMINATE_LENGTH + 1)..-1] == terminate_bytes end |
#data_bytes ⇒ Object
53 54 55 56 57 |
# File 'lib/sftui/frame.rb', line 53 def data_bytes return [] unless completed? bytes[data_offset..(-1 - TERMINATE_LENGTH)] end |
#display ⇒ Object
rubocop:disable Metrics/AbcSize
75 76 77 78 79 80 81 82 83 84 85 |
# 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? ⇒ Boolean
59 60 61 |
# File 'lib/sftui/frame.rb', line 59 def last_frame_in_transaction? data_bytes[-1] == TRANSACTION_ENDING_CHAR end |
#length_bytes ⇒ Object
63 64 65 |
# File 'lib/sftui/frame.rb', line 63 def length_bytes bytes[0..LENGTH_LENGTH - 1] end |
#machine_id ⇒ Object
29 30 31 32 33 |
# File 'lib/sftui/frame.rb', line 29 def machine_id return unless completed? to_integer(addr_bytes) end |
#sanitized_data ⇒ Object
49 50 51 |
# 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 ⇒ Object
71 72 73 |
# File 'lib/sftui/frame.rb', line 71 def terminate_bytes Array.new(TERMINATE_LENGTH, TERMINATE_CHAR) end |