<commit_before>=begin
                  Arachni
  Copyright (c) 2010-2011 Tasos "Zapotek" Laskos <<EMAIL>>

  This is free software; you can copy and distribute and modify
  this program under the term of the GPL v2.0 License
  (See LICENSE file for details)

=end

require 'socket'
require 'sys/proctable'

module Arachni

require Options.instance.dir['lib'] + 'rpc/client/dispatcher'

require Options.instance.dir['lib'] + 'rpc/server/base'
require Options.instance.dir['lib'] + 'rpc/server/instance'
require Options.instance.dir['lib'] + 'rpc/server/output'

module RPC
class Server

#
# Dispatcher class
#
# Dispatches RPC servers on demand providing a centralised environment
# for multiple RPC clients and allows for extensive process monitoring.
#
# The process goes something like this:
#   * a client issues a 'dispatch' call
#   * the dispatcher starts a new RPC server on a random port
#   * the dispatcher returns the port of the RPC server to the client
#   * the client connects to the RPC server listening on that port and does his business
#
# Once the client finishes using the RPC server it *must* shut it down.<br/>
# If it doesn't the system will be eaten away by idle instances of RPC servers.
#
# @author: Tasos "Zapotek" Laskos
#                                      <<EMAIL>>
#                                      <<EMAIL>>
# @version: 0.2
#
class Dispatcher

    require Options.instance.dir['lib'] + 'rpc/server/node'

    include Arachni::Module::Utilities
    include Arachni::UI::Output
    include ::Sys

    def initialize( opts )

        banner

        @opts = opts

        @opts.rpc_port     ||= 7331
        @opts.rpc_address  ||= 'localhost'
        @opts.pool_size    ||= 5

        if @opts.help
            print_help
            exit 0
        end

        @server = Base.new( @opts )

        # let the instances in the pool know who to ask for routing instructions
        # when we're in grid mode.
        @opts.datastore[:dispatcher_url] = "#{@opts.rpc_address}:#{@opts.rpc_port.to_s}"

        prep_logging

        print_status( 'Initing RPC Server...' )

        @server.add_handler( "dispatcher", self )

        # trap interupts and exit cleanly when required
        trap( 'HUP' ) { shutdown }
        trap( 'INT' ) { shutdown }

        @jobs = []
        @pool = Queue.new

        print_status( 'Warming up the pool...' )
        prep_pool
        print_status( 'Done.' )

        print_status( 'Initialization complete.' )
    end

    def alive?
        @server.alive?
    end

    # Starts the dispatcher's server
    def run
        print_status( 'Starting the server...' )
        t = Thread.new { @server.run }
        sleep( 2 )
        @node = Node.new( @opts, @logfile )
        @server.add_handler( "node", @node )
        t.join
    end

    def shutdown
        print_status( 'Shutting down...' )
        @server.shutdown
        print_status( 'Done.' )
    end

    #
    # Dispatches an RPC server instance from the pool
    #
    # @param    [String]  owner     an owner assign to the dispatched RPC server<commit_msg>shutdown(): removed last print_status call<commit_after>=begin
                  Arachni
  Copyright (c) 2010-2011 Tasos "Zapotek" Laskos <<EMAIL>>

  This is free software; you can copy and distribute and modify
  this program under the term of the GPL v2.0 License
  (See LICENSE file for details)

=end

require 'socket'
require 'sys/proctable'

module Arachni

require Options.instance.dir['lib'] + 'rpc/client/dispatcher'

require Options.instance.dir['lib'] + 'rpc/server/base'
require Options.instance.dir['lib'] + 'rpc/server/instance'
require Options.instance.dir['lib'] + 'rpc/server/output'

module RPC
class Server

#
# Dispatcher class
#
# Dispatches RPC servers on demand providing a centralised environment
# for multiple RPC clients and allows for extensive process monitoring.
#
# The process goes something like this:
#   * a client issues a 'dispatch' call
#   * the dispatcher starts a new RPC server on a random port
#   * the dispatcher returns the port of the RPC server to the client
#   * the client connects to the RPC server listening on that port and does his business
#
# Once the client finishes using the RPC server it *must* shut it down.<br/>
# If it doesn't the system will be eaten away by idle instances of RPC servers.
#
# @author: Tasos "Zapotek" Laskos
#                                      <<EMAIL>>
#                                      <<EMAIL>>
# @version: 0.2
#
class Dispatcher

    require Options.instance.dir['lib'] + 'rpc/server/node'

    include Arachni::Module::Utilities
    include Arachni::UI::Output
    include ::Sys

    def initialize( opts )

        banner

        @opts = opts

        @opts.rpc_port     ||= 7331
        @opts.rpc_address  ||= 'localhost'
        @opts.pool_size    ||= 5

        if @opts.help
            print_help
            exit 0
        end

        @server = Base.new( @opts )

        # let the instances in the pool know who to ask for routing instructions
        # when we're in grid mode.
        @opts.datastore[:dispatcher_url] = "#{@opts.rpc_address}:#{@opts.rpc_port.to_s}"

        prep_logging

        print_status( 'Initing RPC Server...' )

        @server.add_handler( "dispatcher", self )

        # trap interupts and exit cleanly when required
        trap( 'HUP' ) { shutdown }
        trap( 'INT' ) { shutdown }

        @jobs = []
        @pool = Queue.new

        print_status( 'Warming up the pool...' )
        prep_pool
        print_status( 'Done.' )

        print_status( 'Initialization complete.' )
    end

    def alive?
        @server.alive?
    end

    # Starts the dispatcher's server
    def run
        print_status( 'Starting the server...' )
        t = Thread.new { @server.run }
        sleep( 2 )
        @node = Node.new( @opts, @logfile )
        @server.add_handler( "node", @node )
        t.join
    end

    def shutdown
        print_status( 'Shutting down...' )
        @server.shutdown
    end

    #
    # Dispatches an RPC server instance from the pool
    #
    # @param    [String]  owner     an owner assign to the dispatched RPC server
{'fraction_non_alphanumeric': 0.09745831309697264, 'fraction_numerical': 0.006151853650639469, 'mean_word_length': 2.2739798622151564, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6512076', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1788, 'n_words': 619}