Class: PxnxJruby::ConnectionManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pxnx_jruby/connection_manager.rb

Overview

This class managers the concurrency calls for pxGrid and Nexpose.

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

Returns a new instance of ConnectionManager.



14
15
16
17
18
19
# File 'lib/pxnx_jruby/connection_manager.rb', line 14

def initialize
  @log = LoggerFactory.getLogger(ConnectionManager.become_java!)
  @join_list = ThreadSafe::Array.new
  @connection_pool = nil
  @scheduler = nil
end

Instance Method Details

#new_connection(ip) ⇒ Object

Adds a new IP to the list.



44
45
46
47
# File 'lib/pxnx_jruby/connection_manager.rb', line 44

def new_connection(ip)
  fail 'The IP address for a new connection cannot be empty!' if ip.nil? || ip.empty?
  @join_list << ip
end

#setup(config_options = {}) ⇒ Object

TODO: This method ABC is too high (c2.com/cgi/wiki?AbcMetric)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pxnx_jruby/connection_manager.rb', line 22

def setup(config_options = {})
  # Fail early if scan template id is invalid
  unless PxnxJruby::NexposeConnection.is_valid_scan_template(config_options)
    @log.error("#{config_options[:nexpose_data][:scan_template_id]} is not a valid scan template ID -- aborting application.")
    abort('Scan template ID not valid, please update pxnx.config with proper setting')
  end
  # We can only have a certain number of connections to Nexpose. Generate a pool of connections for realtime or batched scans.
  @connection_pool = Executors.newFixedThreadPool(config_options[:nexpose_connection_max])
  # Schedule our "realtime" or batched tasks
  @scheduler = Rufus::Scheduler.new
  @scheduler.every config_options[:batch_mode_delay] do
    begin
      @log.debug("Scheduler executed. Number of queued connections for scanning is <#{@join_list.size}>.")
      @connection_pool.submit(PxnxJruby::NexposeConnection.new(@join_list.clone, config_options)) unless @join_list.empty?
      @join_list.clear
    rescue Exception => e
      @log.error("Error when executing the scheduler! The error was <#{e.message}> and backtrace was <#{e.backtrace.join("\n")}>.")
    end
  end
end