class TacviewClient::Reader
Reads events from an input source, parses them using the {Parser} and calls the appropriate event processor method
Constants
- GLOBAL_PROPERTY_MARKER
The format that matches the beginning of an ACMI Property line
- OBJECT_DELETION_MARKER
The format that matches the beginning of an ACMI delete line
- OBJECT_UPDATE_MARKER
The format that matches the beginning of an ACMI update line
- TIME_UPDATE_MARKER
The format that matches the beginning of an ACMI time update line
Public Class Methods
new(input_source:, processor:)
click to toggle source
@param input_source [IO, gets] An {IO} object (or object that implements
the {IO#gets} method. Typically this is a Socket or File.
@param processor [BaseProcessor] The object that processes the events
emitted by the {Reader}. Must implement the methods defined by the {BaseProcessor} and can optionally inherit from it.
# File lib/tacview_client/reader.rb, line 27 def initialize(input_source:, processor:) raise ArgumentError, 'input_source cannot be nil' if input_source.nil? raise ArgumentError, 'processor cannot be nil' if processor.nil? @input_source = input_source @processor = processor end
Public Instance Methods
start_reading()
click to toggle source
# File lib/tacview_client/reader.rb, line 35 def start_reading while (line = @input_source.gets) route_line(line.chomp) end true rescue SignalException exit ensure @input_source.close end
Private Instance Methods
global_property(line)
click to toggle source
# File lib/tacview_client/reader.rb, line 73 def global_property(line) key, value = line[2..-1].split('=') if value.end_with?('\\') value = [value] + read_multiline value = value.inject([]) do |arr, array_line| arr << array_line.delete('\\').strip end.join("\n") end @processor.set_property(property: key, value: value.strip) end
object_deletion(line)
click to toggle source
# File lib/tacview_client/reader.rb, line 65 def object_deletion(line) @processor.delete_object line[1..-1] end
object_update(line)
click to toggle source
# File lib/tacview_client/reader.rb, line 60 def object_update(line) result = Parser.new.parse_object_update(line) @processor.update_object(result) if result end
read_multiline()
click to toggle source
# File lib/tacview_client/reader.rb, line 86 def read_multiline array_lines = [] while (line = @input_source.readline) array_lines << line break unless line.end_with?('\\') end array_lines end
route_line(line)
click to toggle source
# File lib/tacview_client/reader.rb, line 48 def route_line(line) if line.match?(OBJECT_UPDATE_MARKER) object_update(line) elsif line[0] == TIME_UPDATE_MARKER time_update(line) elsif line[0] == OBJECT_DELETION_MARKER object_deletion(line) elsif line[0..1] == GLOBAL_PROPERTY_MARKER global_property(line) end end
time_update(line)
click to toggle source
# File lib/tacview_client/reader.rb, line 69 def time_update(line) @processor.update_time BigDecimal(line[1..-1]) end