class Thrift::EventMachineConnection

Constants

BUFFER_SIZE

Public Instance Methods

close() click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 112
def close
  trap do
    if @connected
      @connected = false
      close_connection(true)
    end
  end
end
connected?() click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 72
def connected?
  @connected
end
connection_completed() click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 76
def connection_completed
  @connected = true
  succeed
end
on_read_timeout() click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 99
def on_read_timeout
  trap do
    deferrable = @deferrable
    if deferrable
      @deferrable = nil
      deferrable.fail(:read)
    else
      puts "read timeout, but no deferrable was found."
      fail
    end
  end
end
post_init() click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 65
def post_init
  @connected = false
  @deferrable = nil
  @rbuf = ''
  @index = 0
end
read(size,timeout) click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 127
def read(size,timeout)
  timer = nil
  if timeout and timeout > 0
    timer = EventMachine.add_timer(timeout){
      on_read_timeout
    }
  end
  begin
    data = nil
    if can_read?(size)
      data = yank(size)
    else
      @size = size
      @deferrable ||= EventMachine::DefaultDeferrable.new
      data = EventMachine::Synchrony.sync @deferrable
      if data == :unbind
        raise TransportException.new(TransportException::ALREADY_CLOSE, "connection already closed.")
      elsif data == :read
        raise TransportException.new(TransportException::TIMED_OUT, "read timeout")
      elsif data == :readerror
        raise TransportException.new(TransportException::UNKNOWN, "read unknown error")
      end
    end
  ensure
    if timer
      EventMachine.cancel_timer(timer)
    end
  end
  data
end
receive_data(data) click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 158
def receive_data(data)
  trap do
    puts data.length
    @rbuf << data
    if @deferrable and can_read?(@size)
      data = yank(@size)
      @size = nil
      deferrable = @deferrable
      @deferrable = nil
      deferrable.succeed(data)
    end
  end
end
unbind() click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 81
def unbind
  connected = @connected
  @connected = false
  deferrable = @deferrable
  if connected
    if deferrable
      @deferrable = nil
      deferrable.fail(:unbind)
    else
      puts "connection closed by server, but no deferrable was found."
      fail
    end
  else
    puts "connection closed"
    fail
  end
end
write(buf,timeout) click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 121
def write(buf,timeout)
  callback {
    send_data(buf)
  }
end

Private Instance Methods

can_read?(size) click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 174
def can_read?(size)
  @rbuf.size >= @index + size
end
trap() { || ... } click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 189
def trap
  begin
    yield
  rescue Exception => e
    puts e.message
    puts e.backtrace.join("\n")
    # deferrable = @deferrable
    # if deferrable
    #   @deferrable = nil
    #   deferrable.fail(:readerror)
    # else
    #   puts "read data error, but no deferrable was found."
    #   fail
    # end
  end
end
yank(len) click to toggle source
# File lib/thrift_client/thrift/transport.rb, line 178
def yank(len)
  data = @rbuf.slice(@index, len)
  @index += len
  @index = @rbuf.size if @index > @rbuf.size
  if @index >= BUFFER_SIZE
    @rbuf = @rbuf.slice(@index..-1)
    @index = 0
  end
  data
end