lib/arachni/rpc/em/server.rb in arachni-rpc-em-0.1.2 vs lib/arachni/rpc/em/server.rb in arachni-rpc-em-0.1.3dev1

- old
+ new

@@ -80,77 +80,67 @@ def receive_request( req ) @request = req # the method call may block a little so tell EventMachine to # stick it in its own thread. - # ::EM.defer( proc { - res = Response.new - peer = peer_ip_addr + res = Response.new + peer = peer_ip_addr - begin - # token-based authentication - authenticate! + begin + # token-based authentication + authenticate! - # grab the result of the method call - res.merge!( @server.call( self ) ) + # grab the result of the method call + res.merge!( @server.call( self ) ) - # handle exceptions and convert them to a simple hash, - # ready to be passed to the client. - rescue Exception => e + # handle exceptions and convert them to a simple hash, + # ready to be passed to the client. + rescue Exception => e - type = '' + type = '' - # if it's an RPC exception pass the type along as is - if e.rpc_exception? - type = e.class.name.split( ':' )[-1] - - # otherwise set it to a RemoteExeption - else - type = 'RemoteException' - end - - res.obj = { - 'exception' => e.to_s, - 'backtrace' => e.backtrace, - 'type' => type - } - - msg = "#{e.to_s}\n#{e.backtrace.join( "\n" )}" - @server.logger.error( 'Exception' ){ msg + " [on behalf of #{peer}]" } + # if it's an RPC exception pass the type along as is + if e.rpc_exception? + type = e.class.name.split( ':' )[-1] + # otherwise set it to a RemoteExeption + else + type = 'RemoteException' end - # res - # }, proc { - # |res| + res.obj = { + 'exception' => e.to_s, + 'backtrace' => e.backtrace, + 'type' => type + } - # - # pass the result of the RPC call back to the client - # along with the callback ID but *only* if it wan't async - # because server.call() will have already taken care of it - # - send_response( res ) if !res.async? - # }) + msg = "#{e.to_s}\n#{e.backtrace.join( "\n" )}" + @server.logger.error( 'Exception' ){ msg + " [on behalf of #{peer}]" } + end + + # + # pass the result of the RPC call back to the client + # along with the callback ID but *only* if it wan't async + # because server.call() will have already taken care of it + # + send_response( res ) if !res.async? end # # Authenticates the client based on the token in the request. # # It will raise an exception if the token doesn't check-out. # - # @param [String] peer IP address of the client - # @param [Hash] req request - # def authenticate! if !valid_token?( @request.token ) msg = 'Token missing or invalid while calling: ' + @request.message @server.logger.error( 'Authenticator' ){ msg + " [on behalf of #{peer_ip_addr}]" } - raise InvalidToken.new( msg ) + fail InvalidToken.new( msg ) end end # # Compares the authentication token in the param with the one of the server. @@ -185,10 +175,13 @@ # # optional serializer (defaults to YAML) # # see the 'serializer' method at: # # http://eventmachine.rubyforge.org/EventMachine/Protocols/ObjectProtocol.html#M000369 # :serializer => Marshal, # + # # serializer to use if the first choice fails + # :fallback_serializer => YAML, + # # # # # In order to enable peer verification one must first provide # # the following: # # # # SSL CA certificate @@ -230,19 +223,18 @@ # # So no need to change your coding conventions to fit the RPC stuff, # you can just decide dynamically based on the plethora of data which Ruby provides # by its 'Method' class. # - # server.add_async_check { - # |method| + # server.add_async_check do |method| # # # # Must return 'true' for async and 'false' for sync. # # # # Very simple check here... # # # 'async' == method.name.to_s.split( '_' )[0] - # } + # end # # @param [Proc] &block # def add_async_check( &block ) @async_checks << block @@ -308,17 +300,17 @@ log_call( peer_ip_addr, expr, *args ) if !object_exist?( obj_name ) msg = "Trying to access non-existent object '#{obj_name}'." @logger.error( 'Call' ){ msg + " [on behalf of #{peer_ip_addr}]" } - raise( InvalidObject.new( msg ) ) + raise InvalidObject.new( msg ) end if !public_method?( obj_name, meth_name ) msg = "Trying to access non-public method '#{meth_name}'." @logger.error( 'Call' ){ msg + " [on behalf of #{peer_ip_addr}]" } - raise( InvalidMethod.new( msg ) ) + raise InvalidMethod.new( msg ) end # the proxy needs to know whether this is an async call because if it # is we'll have already send the response. res = Response.new @@ -350,10 +342,10 @@ wait_for = 2 @logger.info( 'System' ){ "Shutting down in #{wait_for} seconds..." } # don't die before returning - EventMachine::add_timer( wait_for ) { ::EM.stop } + ::EM.add_timer( wait_for ) { ::EM.stop } true end private