Class: Px4LogReader::LogBufferArray
- Inherits:
-
Object
- Object
- Px4LogReader::LogBufferArray
- Defined in:
- lib/px4_log_reader/log_buffer.rb
Instance Attribute Summary collapse
-
#buffers ⇒ Object
readonly
Returns the value of attribute buffers.
-
#current_buffer_index ⇒ Object
readonly
Returns the value of attribute current_buffer_index.
Instance Method Summary collapse
-
#initialize ⇒ LogBufferArray
constructor
A new instance of LogBufferArray.
- #load_buffers ⇒ Object
- #load_empty_buffers ⇒ Object
- #read(num_bytes) ⇒ Object
- #set_file(file, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ LogBufferArray
Returns a new instance of LogBufferArray.
74 75 76 77 78 |
# File 'lib/px4_log_reader/log_buffer.rb', line 74 def initialize @buffers = [] @active_file = nil @current_buffer_index = 0 end |
Instance Attribute Details
#buffers ⇒ Object (readonly)
Returns the value of attribute buffers.
71 72 73 |
# File 'lib/px4_log_reader/log_buffer.rb', line 71 def buffers @buffers end |
#current_buffer_index ⇒ Object (readonly)
Returns the value of attribute current_buffer_index.
72 73 74 |
# File 'lib/px4_log_reader/log_buffer.rb', line 72 def current_buffer_index @current_buffer_index end |
Instance Method Details
#load_buffers ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/px4_log_reader/log_buffer.rb', line 95 def load_buffers @buffers = [] @buffer_count.times do @buffers << LogBuffer.new( @buffer_size ) end @buffers.each do |buffer| buffer.write( @active_file ) end end |
#load_empty_buffers ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/px4_log_reader/log_buffer.rb', line 124 def load_empty_buffers inactive_buffer_index = ( @current_buffer_index + 1 ) % @buffer_count while ( inactive_buffer_index != @current_buffer_index ) do buffer = buffers[ inactive_buffer_index ] if buffer.empty? buffer.reset buffer.write( @active_file ) end inactive_buffer_index = ( inactive_buffer_index + 1 ) % @buffer_count end end |
#read(num_bytes) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/px4_log_reader/log_buffer.rb', line 107 def read( num_bytes ) data = '' while ( data.size < num_bytes ) do data << active_buffer.read( num_bytes ) if data.length < num_bytes increment_buffer break if active_buffer.empty? end end return data end |
#set_file(file, options = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/px4_log_reader/log_buffer.rb', line 80 def set_file( file, = {} ) opts = { buffer_count: 2, load_buffers: true, buffer_size: 1024 }.merge( ) @active_file = file @buffer_count = opts[:buffer_count] @buffer_size = opts[:buffer_size] / opts[:buffer_count] load_buffers if opts[:load_buffers] end |