Completion of Initial Major Setup System and Program Efficiency Enhancements

This commit marks a significant milestone in the NETRAVE project. The first major system has been successfully completed, laying a solid foundation for the subsequent development phases.

Key changes include:

1. Completion of the Initial Setup Process: The initial setup process has been successfully implemented. This process gathers necessary information from the user and sets up the basic environment for the application to run.

2. Removal of Redundant Thread in Alert System: An unnecessary thread in the alert system was identified and removed. This change simplifies the alert system's architecture and reduces the potential for concurrency-related issues.

3. Program Efficiency Improvements: Several optimizations have been made to enhance the efficiency of the program. These include changes to the way alerts are handled and displayed, as well as improvements to the handling of the alert queue.

These changes have been verified and tested to ensure they work as expected and contribute to the overall functionality and performance of the NETRAVE project.
This commit is contained in:
VetheonGames 2023-07-05 16:25:26 -06:00
parent e4df29b0c1
commit 5b76cd117e
5 changed files with 49 additions and 31 deletions

View File

@ -87,4 +87,4 @@ end
# End of the program # End of the program
# wait for the alert_queue_manager to block before we exit. # wait for the alert_queue_manager to block before we exit.
@alert_queue_manager.join_worker @alert_queue_manager.shutdown

View File

@ -31,12 +31,12 @@ gem 'securerandom', '~> 0.2.2'
gem 'dotenv', '~> 2.8' gem 'dotenv', '~> 2.8'
gem "tracer", "~> 0.2.2" gem 'tracer', '~> 0.2.2'
gem "flay", "~> 2.13" gem 'flay', '~> 2.13'
gem "pcaprub", "~> 0.13.1" gem 'pcaprub', '~> 0.13.1'
gem "packetfu", "~> 2.0" gem 'packetfu', '~> 2.0'
gem "sudo", "~> 0.2.0" gem 'sudo', '~> 0.2.0'

View File

@ -3,7 +3,7 @@
# Class for creating and displaying alerts in the Curses TUI. This class also manages a little bit of concurrency # Class for creating and displaying alerts in the Curses TUI. This class also manages a little bit of concurrency
# We use mutex for sync so we don't break Curses, as Curses isn't thread safe # We use mutex for sync so we don't break Curses, as Curses isn't thread safe
class Alert class Alert
attr_reader :message, :severity attr_reader :message, :severity, :alert_window
def initialize(message, severity) def initialize(message, severity)
@message = message @message = message
@ -11,7 +11,7 @@ class Alert
@curses_mutex = Mutex.new @curses_mutex = Mutex.new
end end
def display def display # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@curses_mutex.synchronize do @curses_mutex.synchronize do
# Initialize color pairs # Initialize color pairs
Curses.start_color Curses.start_color
@ -20,33 +20,30 @@ class Alert
Curses.init_pair(3, Curses::COLOR_YELLOW, Curses::COLOR_BLACK) # Warning Curses.init_pair(3, Curses::COLOR_YELLOW, Curses::COLOR_BLACK) # Warning
# Create a new window for the alert at the bottom of the screen # Create a new window for the alert at the bottom of the screen
alert_window = Curses::Window.new(1, Curses.cols, Curses.lines - 1, 0) @alert_window = Curses::Window.new(1, Curses.cols, Curses.lines - 1, 0)
# Set the color attribute based on the severity of the alert # Set the color attribute based on the severity of the alert
case @severity case @severity
when :info when :info
alert_window.attron(Curses.color_pair(1) | Curses::A_NORMAL) # Blue color @alert_window.attron(Curses.color_pair(1) | Curses::A_NORMAL) # Blue color
when :warning when :warning
alert_window.attron(Curses.color_pair(3) | Curses::A_NORMAL) # Yellow color @alert_window.attron(Curses.color_pair(3) | Curses::A_NORMAL) # Yellow color
when :error when :error
alert_window.attron(Curses.color_pair(2) | Curses::A_NORMAL) # Red color @alert_window.attron(Curses.color_pair(2) | Curses::A_NORMAL) # Red color
end end
# Add the message to the window and refresh it to display the message # Add the message to the window and refresh it to display the message
alert_window.addstr(@message) @alert_window.addstr(@message)
alert_window.refresh @alert_window.refresh
end
end
# Create a new thread to handle the delay and clearing of the alert def clear
# This is done in a separate thread to prevent the entire program from @curses_mutex.synchronize do
# pausing while the alert is displayed # Clear the alert
Thread.new do @alert_window.clear
sleep(5) # Pause for 5 seconds @alert_window.refresh
@alert_window.close
# Clear the alert
alert_window.clear
alert_window.refresh
alert_window.close
end
end end
end end
end end

View File

@ -3,24 +3,43 @@
require_relative 'ring_buffer' require_relative 'ring_buffer'
# Class for managing the queues for alerts # Class for managing the queues for alerts
class AlertQueueManager class AlertQueueManager
def initialize(logger, size = 2 * 1024 * 1024) SHUTDOWN_SIGNAL = 'shutdown'
def initialize(logger, size = 2 * 1024 * 1024) # rubocop:disable Metrics/MethodLength
@loggman = logger @loggman = logger
@queue = RingBuffer.new(@loggman, size) @queue = RingBuffer.new(@loggman, size)
@shutdown = false
# Start a thread that continuously checks the queue and displays alerts # Start a thread that continuously checks the queue and displays alerts
@worker_thread = Thread.new do @worker_thread = Thread.new do
loop do loop do
break if @shutdown && @queue.empty?
if @queue.empty?
sleep(0.1) # Sleep for 100 milliseconds
next
end
alert = @queue.pop # This will block until there's an alert in the queue alert = @queue.pop # This will block until there's an alert in the queue
alert&.display next if alert.nil?
break if alert.message == SHUTDOWN_SIGNAL
alert.display
sleep(4.5)
alert.clear
end end
end end
end end
def enqueue_alert(alert) def enqueue_alert(alert)
return if @shutdown
@queue.push(alert) @queue.push(alert)
end end
def join_worker def shutdown
@worker_thread.join @shutdown = true
enqueue_alert(Alert.new(SHUTDOWN_SIGNAL, :info))
end end
end end

View File

@ -13,10 +13,12 @@ class FirstRunInit
include Utilities include Utilities
include Curses include Curses
def initialize(loggman, alert_queue_manager, db_manager = nil) def initialize(logger, alert_queue_manager, db_manager = nil)
@loggman = loggman @loggman = logger
@db_manager = db_manager @db_manager = db_manager
@alert_queue_manager = alert_queue_manager @alert_queue_manager = alert_queue_manager
@info_gatherer = SystemInformationGather.new(@db_manager, @loggman)
Dotenv.load
end end
def run def run