Class: Arachni::RPC::EM::Client::Handler
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- Arachni::RPC::EM::Client::Handler
- Includes:
- Arachni::RPC::EM::ConnectionUtilities, Protocol
- Defined in:
- lib/arachni/rpc/em/client.rb
Overview
Handles EventMachine’s connection and RPC related stuff.
It’s responsible for TLS, storing and calling callbacks as well as serializing, transmitting and receiving objects.
@author: Tasos “Zapotek” Laskos <[email protected]>
Constant Summary collapse
- DEFAULT_TRIES =
9
Constants included from Protocol
Instance Method Summary collapse
- #connection_completed ⇒ Object
- #done? ⇒ Boolean
- #exception?(res) ⇒ Boolean
-
#initialize(opts) ⇒ Handler
constructor
A new instance of Handler.
- #post_init ⇒ Object
-
#receive_response(res) ⇒ Object
Used to handle responses.
- #retry? ⇒ Boolean
- #retry_request ⇒ Object
-
#send_request(req) ⇒ Object
Sends the request.
- #status ⇒ Object
- #unbind(reason) ⇒ Object
Methods included from Arachni::RPC::EM::ConnectionUtilities
Methods included from Protocol
#assume_client_role!, #assume_server_role!, #fallback_serializer, #receive_data, #receive_object, #receive_request, #send_message, #send_object, #serialize, #serializer, #unserialize
Methods included from SSL
#are_we_a_client?, #ca_store, #end_ssl, #log, #ssl_handshake_completed, #ssl_opts?, #ssl_verify_peer, #start_ssl, #verified_peer?
Constructor Details
#initialize(opts) ⇒ Handler
Returns a new instance of Handler.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/arachni/rpc/em/client.rb', line 42 def initialize( opts ) @opts = opts.dup @max_retries = @opts[:max_retries] || DEFAULT_TRIES @opts[:tries] ||= 0 @tries ||= @opts[:tries] @status = :idle @request = nil assume_client_role! end |
Instance Method Details
#connection_completed ⇒ Object
76 77 78 |
# File 'lib/arachni/rpc/em/client.rb', line 76 def connection_completed @status = :established end |
#done? ⇒ Boolean
84 85 86 |
# File 'lib/arachni/rpc/em/client.rb', line 84 def done? !!@done end |
#exception?(res) ⇒ Boolean
123 124 125 |
# File 'lib/arachni/rpc/em/client.rb', line 123 def exception?( res ) res.obj.is_a?( Hash ) && res.obj['exception'] ? true : false end |
#post_init ⇒ Object
56 57 58 59 |
# File 'lib/arachni/rpc/em/client.rb', line 56 def post_init @status = :active start_ssl end |
#receive_response(res) ⇒ Object
Used to handle responses.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/arachni/rpc/em/client.rb', line 93 def receive_response( res ) if exception?( res ) res.obj = Arachni::RPC::Exceptions.from_response( res ) end @request.callback.call( res.obj ) if @request.callback ensure @done = true @status = :done close_connection end |
#retry? ⇒ Boolean
118 119 120 |
# File 'lib/arachni/rpc/em/client.rb', line 118 def retry? @tries < @max_retries end |
#retry_request ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/arachni/rpc/em/client.rb', line 105 def retry_request opts = @opts.dup opts[:tries] += 1 @tries += 1 ::EM.next_tick { ::EM::Timer.new( 0.2 ) { ::EM.connect( opts[:host], opts[:port], self.class, opts ). send_request( @request ) } } end |
#send_request(req) ⇒ Object
Sends the request.
132 133 134 135 136 |
# File 'lib/arachni/rpc/em/client.rb', line 132 def send_request( req ) @request = req super( req ) @status = :pending end |
#status ⇒ Object
80 81 82 |
# File 'lib/arachni/rpc/em/client.rb', line 80 def status @status end |
#unbind(reason) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/arachni/rpc/em/client.rb', line 61 def unbind( reason ) end_ssl if @request && @request.callback && !done? if retry? #&& reason == Errno::ECONNREFUSED retry_request else e = Arachni::RPC::Exceptions::ConnectionError.new( "Connection closed [#{reason}]" ) @request.callback.call( e ) end end @status = :closed end |