Sha256: 4aa6cd3736ea6fa6bfbd9a2d4e8afa5b93176bf74adfe484b6b1e38a08319827

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

module PxnxJruby
  # This class managers the concurrency calls for pxGrid and Nexpose.
  class ConnectionManager
    require 'singleton'
    include Singleton
    require 'rufus-scheduler'
    require 'thread_safe'
    require 'java'
    require 'jruby/core_ext'
    java_import org.slf4j.Logger
    java_import org.slf4j.LoggerFactory
    java_import java.util.concurrent.Executors

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

    # TODO: This method ABC is too high (http://c2.com/cgi/wiki?AbcMetric)
    def setup(config_options = {})
      # 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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nexpose_pxgrid-0.1.4-java lib/pxnx_jruby/connection_manager.rb
nexpose_pxgrid-0.1.2-java lib/pxnx_jruby/connection_manager.rb