Class: Px4LogReader::LogBufferArray

Inherits:
Object
  • Object
show all
Defined in:
lib/px4_log_reader/log_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogBufferArray

Returns a new instance of LogBufferArray.



106
107
108
109
110
# File 'lib/px4_log_reader/log_buffer.rb', line 106

def initialize
  @buffers = []
  @active_file = nil
  @current_buffer_index = 0
end

Instance Attribute Details

#buffersObject (readonly)

Returns the value of attribute buffers.



103
104
105
# File 'lib/px4_log_reader/log_buffer.rb', line 103

def buffers
  @buffers
end

#current_buffer_indexObject (readonly)

Returns the value of attribute current_buffer_index.



104
105
106
# File 'lib/px4_log_reader/log_buffer.rb', line 104

def current_buffer_index
  @current_buffer_index
end

Instance Method Details

#load_buffersObject



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/px4_log_reader/log_buffer.rb', line 127

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_buffersObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/px4_log_reader/log_buffer.rb', line 156

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/px4_log_reader/log_buffer.rb', line 139

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/px4_log_reader/log_buffer.rb', line 112

def set_file( file, options = {} )

  opts = {
    buffer_count: 2,
    load_buffers: true,
    buffer_size: 1024
  }.merge( options )

  @active_file = file
  @buffer_count = opts[:buffer_count]
  @buffer_size = opts[:buffer_size] / opts[:buffer_count]

  load_buffers if opts[:load_buffers]
end