examples/client.rb in arachni-rpc-pure-0.1.2 vs examples/client.rb in arachni-rpc-pure-0.2

- old
+ new

@@ -1,63 +1,51 @@ -require File.join( File.expand_path( File.dirname( __FILE__ ) ), '../lib/arachni/rpc/', 'pure' ) +require_relative '../lib/arachni/rpc/pure' -# require 'arachni/rpc/pure' -require 'pp' +dispatcher = Arachni::RPC::Pure::Client.new( + host: '127.0.0.1', + port: 7331 +) -client = Arachni::RPC::Pure::Client.new( - :host => 'localhost', - :port => 7332, - :token => 'superdupersecret', - :serializer => Marshal +instance_info = dispatcher.call( 'dispatcher.dispatch', 'The Dude' ) +# => { +# "token" => "1ac10910f41ce86e23ab954033b40a83", +# "pid" => 27031, +# "port" => 23496, +# "url" => "127.0.0.1:23496", +# "owner" => "The Dude", +# "birthdate" => "2014-08-05 19:54:07 +0300", +# "starttime" => "2014-08-05 19:54:58 +0300", +# "helpers" => {} +# } + +instance = Arachni::RPC::Pure::Client.new( + host: '127.0.0.1', + port: instance_info['port'], + token: instance_info['token'] ) +p instance.call( 'service.alive?' ) +# => true + begin - # 'bench2' doesn't exist... - client.call( 'bench2.foo', 1 ) -rescue Arachni::RPC::Exceptions::InvalidObject => e - pp e - # => #<Arachni::RPC::Exceptions::InvalidObject: Trying to access non-existent object 'bench2'.> + instance.call( 'service.scan' ) +rescue => e + # => #<RuntimeError: RemoteException: Option 'url' is mandatory.> end -# This is just an echo method so it will return: 1 -pp client.call( 'bench.foo', 1 ) -# => 1 - -# make access more natural, map (proxy actually) the remote object to a local one -bench = Arachni::RPC::RemoteObjectMapper.new( client, 'bench' ) - -pp bench.foo( 2 ) -# => 2 - begin - # doesn't exist so we'll get an exception, - # won't reach the remote object - # - bench.does_not_exist -rescue Arachni::RPC::Exceptions::InvalidMethod => e - pp e - # => #<Arachni::RPC::Exceptions::InvalidMethod: Trying to access non-public method 'does_not_exist'.> + instance.call( 'service.error_test' ) +rescue => e + # => #<RuntimeError: RemoteException: wrong number of arguments (0 for 1)> end begin - # foo() expects an argument but arity is not checked so the call will be - # forwarded to the remote object which will throw a remote exception - bench.foo -rescue Arachni::RPC::Exceptions::RemoteException => e - pp e - # => #<Arachni::RPC::Exceptions::RemoteException: wrong number of arguments (0 for 1)> + instance.call( 'service.blahblah' ) +rescue => e + # => #<RuntimeError: InvalidMethod: Trying to access non-public method 'blahblah'.> end -client = Arachni::RPC::Pure::Client.new( - :host => 'localhost', - :port => 7332, - :token => 'invalidtoken', - :serializer => Marshal -) - begin - # the token is not valid so the remote server won't let us through - client.call( 'bench.foo', 1 ) -rescue Arachni::RPC::Exceptions::InvalidToken => e - pp e - # => #<Arachni::RPC::Exceptions::InvalidToken: Token missing or invalid while calling: bench.foo> + instance.call( 'blahblah.stuff' ) +rescue => e + # => #<RuntimeError: InvalidObject: Trying to access non-existent object 'blahblah'.> end