Class: AbstractThriftClient

### Home Classes Methods

### In Files

- lib/thrift_client/abstract_thrift_client.rb

### Parent

Object

### Namespace

- CLASS AbstractThriftClient::Server

### Methods

- ::new
- #add_callback
- #connect!
- #disconnect!
- #inspect

### Files

- CHANGELOG
- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- ThriftClient
- ThriftClient::NoServersAvailable
- ThriftClient::Simple
- ThriftClient::Simple::ComplexType
- ThriftClient::Simple::ComplexType::Extends
- ThriftClient::Simple::ComplexType::Includes
- ThriftClient::Simple::Field
- ThriftClient::Simple::ThriftException
- ThriftClient::Simple::ThriftService
- ThriftClient::Simple::ThriftStruct
- ThriftClient::Simple::ThriftStruct::Extend
- ThriftClient::Simple::ThriftStruct::Include
- Thrift
- Thrift::BaseTransport
- Thrift::BufferedTransport
- Thrift::Client
- Thrift::EventMachineConnection
- Thrift::EventMachineTransport
- Thrift::FramedTransport
- Connection
- Connection::Base
- Connection::Factory
- Connection::HTTP
- Connection::Socket
- AbstractThriftClient
- AbstractThriftClient::Server

No matching classes.

# AbstractThriftClient

### Constants

DEFAULTS
DEFAULT_WRAPPED_ERRORS
DISCONNECT_ERRORS

### Attributes

client[R]
client_class[R]
client_methods[R]
current_server[R]
options[R]
server_list[R]

### Public Class Methods

new(client_class, servers, options = {}) click to toggle source

    # File lib/thrift_client/abstract_thrift_client.rb, line 49
    def initialize(client_class, servers, options = {})
      @options = DEFAULTS.merge(options)
      @options[:server_retry_period] ||= 0
      @client_class = client_class
      @server_list = Array(servers).collect{|s| Server.new(s)}.sort_by { rand }
      @current_server = @server_list.first

      @callbacks = {}
      @client_methods = []
      @client_class.instance_methods.each do |method_name|
        if method_name != 'send_message' && method_name =~ /^send_(.*)$/
          instance_eval("def #{$1}(*args); handled_proxy(:'#{$1}', *args); end", __FILE__, __LINE__)
          @client_methods << $1
        end
      end
      @request_count = 0
      @options[:wrapped_exception_classes].each do |exception_klass|
        name = exception_klass.to_s.split('::').last
        begin
          @client_class.const_get(name)
        rescue NameError
          @client_class.const_set(name, Class.new(exception_klass))
        end
      end
    end

### Public Instance Methods

add_callback(callback_type, &block) click to toggle source

Adds a callback that will be invoked at a certain time. The valid callback types are:

    :post_connect  - should accept a single AbstractThriftClient argument, which is the client object to
                     which the callback was added. Called after a connection to the remote thrift server
                     is established.
    :before_method - should accept a single method name argument. Called before a method is invoked on the
                     thrift server.
    :on_exception  - should accept 2 args: an Exception instance and a method name. Called right before the
                     exception is raised.

    # File lib/thrift_client/abstract_thrift_client.rb, line 83
    def add_callback(callback_type, &block)
      case callback_type
      when :post_connect, :before_method, :on_exception
        @callbacks[callback_type] ||= []
        @callbacks[callback_type].push(block)
        # Allow chaining
        return self
      else
        return nil
      end
    end

connect!() click to toggle source

Force the client to connect to the server. Not necessary to be called as the connection will be made on the first RPC method call.

    # File lib/thrift_client/abstract_thrift_client.rb, line 102
    def connect!
      @current_server = next_live_server
      @connection = Connection::Factory.create(@options[:transport], @options[:transport_wrapper], @current_server.connection_string, @options[:connect_timeout])
      @connection.connect!
      transport = @connection.transport
      transport.timeout = @options[:timeout] if transport_can_timeout?
      @client = @client_class.new(@options[:protocol].new(transport, *@options[:protocol_extra_params]))
      do_callbacks(:post_connect, self)
    end

disconnect!() click to toggle source

    # File lib/thrift_client/abstract_thrift_client.rb, line 112
    def disconnect!
      @connection.close rescue nil #TODO
      @client = nil
      @current_server = nil
      @request_count = 0
    end

inspect() click to toggle source

    # File lib/thrift_client/abstract_thrift_client.rb, line 95
    def inspect
      "<#{self.class}(#{client_class}) @current_server=#{@current_server}>"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.