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]>

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,

    # serializer to use if the first choice fails
    :fallback_serializer => YAML,

    :max_retries => 0,

    #
    # 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)


183
184
185
186
187
188
189
190
# File 'lib/arachni/rpc/em/client.rb', line 183

def initialize( opts )
    @opts  = opts.merge( role: :client )
    @token = @opts[:token]

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

    Arachni::RPC::EM.ensure_em_running
end

Instance Attribute Details

#optsHash (readonly)

Options hash

Returns:

  • (Hash)


144
145
146
# File 'lib/arachni/rpc/em/client.rb', line 144

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 ) do |res|
    do_stuff( res )
end

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 arguments to be passed to the method

  • &block (Proc)


214
215
216
217
218
219
220
221
222
223
# File 'lib/arachni/rpc/em/client.rb', line 214

def call( msg, *args, &block )
    req = Request.new(
        message:  msg,
        args:     args,
        callback: block,
        token:    @token
    )

    block_given? ? call_async( req ) : call_sync( req )
end