Class: Arachni::RPC::EM::Client

Inherits:
Object
  • Object
show all
Includes:
Arachni::RPC::Exceptions
Defined in:
lib/arachni/rpc/em/client.rb

Overview

Simple EventMachine-based RPC client.

It’s capable of:

  • performing and handling a few thousands requests per second (depending on call size, network conditions and the like)

  • TLS encryption

  • asynchronous and synchronous requests

  • handling remote asynchronous calls that require a block

@author: Tasos “Zapotek” Laskos

<[email protected]>
<[email protected]>

@version: 0.1

Defined Under Namespace

Classes: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Starts EventMachine and connects to the remote server.

opts example:

{
    :host  => 'localhost',
    :port  => 7331,

    # optional authentication token, if it doesn't match the one
    # set on the server-side you'll be getting exceptions.
    :token => 'superdupersecret',

    # optional serializer (defaults to YAML)
    # see the 'serializer' method at:
    # http://eventmachine.rubyforge.org/EventMachine/Protocols/ObjectProtocol.html#M000369
    :serializer => Marshal,

    #
    # In order to enable peer verification one must first provide
    # the following:
    #
    # SSL CA certificate
    :ssl_ca     => cwd + '/../spec/pems/cacert.pem',
    # SSL private key
    :ssl_pkey   => cwd + '/../spec/pems/client/key.pem',
    # SSL certificate
    :ssl_cert   => cwd + '/../spec/pems/client/cert.pem'
}

Parameters:

  • opts (Hash)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/arachni/rpc/em/client.rb', line 166

def initialize( opts )

    begin
        @opts  = opts.merge( :role => :client )
        @token = @opts[:token]

        @host, @port = @opts[:host], @opts[:port]

        Arachni::RPC::EM.ensure_em_running!
    rescue EventMachine::ConnectionError => e
        exc = ConnectionError.new( e.to_s + " for '#{@k}'." )
        exc.set_backtrace( e.backtrace )
        raise exc
    end
end

Instance Attribute Details

#optsHash (readonly)

Options hash

Returns:

  • (Hash)


132
133
134
# File 'lib/arachni/rpc/em/client.rb', line 132

def opts
  @opts
end

Instance Method Details

#call(msg, *args, &block) ⇒ Object

Calls a remote method and grabs the result.

There are 2 ways to perform a call, async (non-blocking) and sync (blocking).

To perform an async call you need to provide a block which will be passed the return value once the method has finished executing.

server.call( 'handler.method', arg1, arg2 ){
    |res|
    do_stuff( res )
}

To perform a sync (blocking) call do not pass a block, the value will be returned as usual.

res = server.call( 'handler.method', arg1, arg2 )

Parameters:

  • msg (String)

    in the form of handler.method

  • args (Array)

    collection of argumenta to be passed to the method

  • &block (Proc)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/arachni/rpc/em/client.rb', line 205

def call( msg, *args, &block )

    req = Request.new(
        :message  => msg,
        :args     => args,
        :callback => block,
        :token    => @token
    )

    if block_given?
        call_async( req )
    else
        return call_sync( req )
    end
end