Parent

Arachni::RPC::EM::Client::Handler

Transmits `Arachni::RPC::Request` objects and calls callbacks once an `Arachni::RPC::Response` is received.

@author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>

Constants

DEFAULT_TRIES

Default amount of tries for failed requests.

Attributes

status[R]

@return [Symbol] Status of the connection, can be:

  • `:idle` -- Just initialized.

  • `:ready` -- A connection has been established.

  • `:pending` -- Sending request and awaiting response.

  • `:done` -- Response received and callback invoked -- ready to be reused.

  • `:closed` -- Connection closed.

Public Class Methods

new( opts ) click to toggle source

Prepares an RPC connection and sets {status} to `:idle`.

@param [Hash] opts @option opts [Integer] :max_retries (9)

Default amount of tries for failed requests.

@option opts [Client] :base

Client instance (needed to {Client#push_connection push} ourselves
back to its connection pool once we're done and we're ready to be reused.)
# File lib/arachni/rpc/em/client/handler.rb, line 44
def initialize( opts )
    @opts = opts.dup

    @max_retries = @opts[:max_retries] || DEFAULT_TRIES

    @client = @opts[:client]

    @opts[:tries] ||= 0
    @tries ||= @opts[:tries]

    @status = :idle

    @request = nil
    assume_client_role!
end

Public Instance Methods

close_without_retry() click to toggle source

Closes the connection without triggering a retry operation and sets {status} to `:closed`.

# File lib/arachni/rpc/em/client/handler.rb, line 142
def close_without_retry
    @request = nil
    @status  = :closed
    close_connection
end
closed?() click to toggle source

@return [Boolean]

`true` when the connection has been closed, `false` otherwise.
# File lib/arachni/rpc/em/client/handler.rb, line 128
def closed?
    @status == :closed
end
done?() click to toggle source

@note If `true`, the connection can be re-used.

@return [Boolean]

`true` when the connection is done, `false` otherwise.
# File lib/arachni/rpc/em/client/handler.rb, line 136
def done?
    @status == :done
end
post_init() click to toggle source

Initializes an SSL session once the connection has been established and sets {status} # to `:ready`.

@private

# File lib/arachni/rpc/em/client/handler.rb, line 94
def post_init
    @status = :ready
    start_ssl
end
receive_response( res ) click to toggle source

@note Pushes itself to the client's connection pool to be re-used.

Handles responses to RPC requests, calls its callback and sets {status} to `:done`.

@param [Arachni::RPC::Response] res

# File lib/arachni/rpc/em/client/handler.rb, line 77
def receive_response( res )
    if exception?( res )
        res.obj = RPC::Exceptions.from_response( res )
    end

    @request.callback.call( res.obj ) if @request.callback
ensure
    @request = nil # Help the GC out.
    @status  = :done
    @opts[:tries] = @tries = 0
    @client.push_connection self
end
send_request( req ) click to toggle source

Sends an RPC request (i.e. performs an RPC call) and sets {status} to `:pending`.

@param [Arachni::RPC::Request] req

# File lib/arachni/rpc/em/client/handler.rb, line 64
def send_request( req )
    @request = req
    @status  = :pending
    super( req )
end
unbind( reason ) click to toggle source

Handles closed connections, cleans up the SSL session, retries (if necessary) and sets {status} to `:closed`.

@private

# File lib/arachni/rpc/em/client/handler.rb, line 103
def unbind( reason )
    end_ssl

    # If there is a request and a callback and the callback hasn't yet be
    # called (i.e. not done) then we got here by error so retry.
    if @request && @request.callback && !done?
        if retry? #&& reason == Errno::ECONNREFUSED
            #p 'RETRY'
            #p @client.connection_count
            retry_request
        else
            #p 'FAIL'
            #p @client.connection_count
            e = RPC::Exceptions::ConnectionError.new( "Connection closed [#{reason}]" )
            @request.callback.call( e )
            @client.connection_failed self
        end
        return
    end

    close_without_retry
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.