Class: TacScribe::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/tac_scribe/daemon.rb

Overview

Main entry-point into Tac Scribe. Instantiate this class to start processing events.

Instance Method Summary collapse

Constructor Details

#initialize(db_host:, db_port:, db_name:, db_user:, db_password:, tacview_host:, tacview_port:, tacview_password:, tacview_client_name:, verbose_logging:, thread_count:, populate_airfields:, whitelist: nil) ⇒ Daemon

Returns a new instance of Daemon.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tac_scribe/daemon.rb', line 14

def initialize(db_host:, db_port:, db_name:, db_user:, db_password:,
               tacview_host:, tacview_port:, tacview_password:,
               tacview_client_name:, verbose_logging:, thread_count:,
               populate_airfields:, whitelist: nil)
  Datastore.instance.configure do |config|
    config.host = db_host
    config.port = db_port
    config.database = db_name
    config.username = db_user
    config.password = db_password
  end
  Datastore.instance.connect

  @event_queue = EventQueue.new verbose_logging: verbose_logging

  @populate_airfields = populate_airfields
  @thread_count = thread_count
  @processing_threads = []
  @whitelist = Set.new(IO.read(whitelist).split) if whitelist

  @client = TacviewClient::Client.new(
    host: tacview_host,
    port: tacview_port,
    password: tacview_password,
    processor: @event_queue,
    client_name: tacview_client_name
  )
end

Instance Method Details

#kill_processing_threadsObject



68
69
70
71
72
73
# File 'lib/tac_scribe/daemon.rb', line 68

def kill_processing_threads
  @processing_threads.each do |thr|
    thr.kill
    thr.join
  end
end

#populate_airfieldsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tac_scribe/daemon.rb', line 85

def populate_airfields
  json = File.read(File.join(File.dirname(__FILE__), '../../data/airfields.json'))
  airfields = JSON.parse(json)
  airfields.each_with_index do |airfield, i|
    @event_queue.update_object(
      object_id: (45_000_000 + i).to_s,
      latitude: BigDecimal(airfield['lat'].to_s),
      longitude: BigDecimal(airfield['lon'].to_s),
      altitude: BigDecimal(airfield['alt'].to_s),
      type: 'Ground+Static+Aerodrome',
      name: airfield['name'],
      coalition: 2
    )
  end
end

#start_processingObject

Starts processing and reconnects if the client was disconnected. Because connecting to Tacview always gives an initial unit dump we truncate the table each time we reconnect. This will make sure there are no ghost units hanging around after server restart for example



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tac_scribe/daemon.rb', line 48

def start_processing
  loop do
    kill_processing_threads
    @event_queue.clear
    Datastore.instance.truncate_table
    start_processing_threads
    populate_airfields if @populate_airfields
    @client.connect
  # Rescuing reliably from Net::HTTP is a complete bear so rescue
  # StandardError. It ain't pretty but it is reliable. We will puts
  # the exception just in case
  # https://stackoverflow.com/questions/5370697/what-s-the-best-way-to-handle-exceptions-from-nethttp
  rescue StandardError => e
    puts e.message
    puts e.backtrace
    sleep 30
    next
  end
end

#start_processing_threadsObject



75
76
77
78
79
80
81
82
83
# File 'lib/tac_scribe/daemon.rb', line 75

def start_processing_threads
  @thread_count.times do
    @processing_threads << Thread.new do
      EventProcessor.new(datastore: Datastore.instance,
                         event_queue: @event_queue,
                         whitelist: @whitelist).start
    end
  end
end