class Okuyama::FastClient

Attributes

base64_encode_flag[RW]
host[R]
parse_flag[RW]
port[R]
protocol[R]
retry_max[R]
timeout[R]
to_i_flag[RW]

Public Class Methods

new(options) click to toggle source
# File lib/okuyama/fast_client.rb, line 7
def initialize(options)
  @host = options[:host] || 'localhost'
  @port = options[:port] || 8888
  @timeout = options[:timeout] || 10
  @retry_max = options[:retry_max] || 3
  @to_i_flag = options[:to_i_flag]
  @to_i_flag = true if @to_i_flag.nil?
  @parse_flag = options[:parse_flag]
  @parse_flag = true if @parse_flag.nil?
  protocol_version = options[:protocol_version] || '1.0.0'
  protocol_version = '1.0.0'
  case protocol_version
  when '1.0.0'        
    @protocol = Okuyama::Protocol::Version1.new(:base64_encode_flag=>options[:base64_encode_flag])
  else
    raise OkuyamaError, "protocol version #{protocol_version.inspect} is invalid"
  end
end

Public Instance Methods

close(raise_exception=nil) click to toggle source
# File lib/okuyama/fast_client.rb, line 35
def close(raise_exception=nil)
  if @socket then
    begin
      @socket.close
    rescue Exception=>e
      @socket = nil
      raise e if raise_exception
      Okuyama.logger.error "ERROR: #{e.message}"
    end
  end
  @socket = nil
end
debug=(d) click to toggle source
# File lib/okuyama/fast_client.rb, line 26
def debug=(d)
  @debug = d
  @protocol.debug = d
end
protocol_version() click to toggle source
# File lib/okuyama/fast_client.rb, line 31
def protocol_version
  @protocol.version
end
recv_decr_value()
Alias for: recvs
recv_get_multi_value(&block)
Alias for: recv_lines
recv_get_tag_keys()
Alias for: recvs
recv_get_tag_values(&block)
Alias for: recv_lines
recv_get_value()
Alias for: recvs
recv_get_value_version_check()
Alias for: recvs
recv_incr_value()
Alias for: recvs
recv_init_count()
Alias for: recvs
recv_lines(&block) click to toggle source
# File lib/okuyama/fast_client.rb, line 57
def recv_lines(&block)
  if block_given? then
    return self.each(&block)
  else
    return self.readlines
  end
end
recv_remove_tag_from_key()
Alias for: recvs
recv_remove_value()
Alias for: recvs
recv_search_value()
Alias for: recvs
recv_set_new_value()
Alias for: recvs
recv_set_value()
Alias for: recvs
recv_set_value_and_create_index()
Alias for: recvs
recv_set_value_version_check()
Alias for: recvs
recvs() click to toggle source
# File lib/okuyama/fast_client.rb, line 48
def recvs
  line = self.socket.gets
  line.chomp!
  # Disable debug message for better performance
  Okuyama.logger.debug "recv: #{line.inspect}" if @debug
  return self.parse_result(line) if @parse_flag
  return line
end

Protected Instance Methods

each() { |result| ... } click to toggle source
# File lib/okuyama/fast_client.rb, line 98
def each(&block)
  while result = socket.gets do
    result.chomp!
    if result == "END" then
      break
    else
      result = self.parse_result(result) if @parse_flag
      yield(result)
    end
  end
end
parse_result(result) click to toggle source
# File lib/okuyama/fast_client.rb, line 155
def parse_result(result)
  begin
    result = @protocol.parse_line_result(result, @to_i_flag)
  rescue Okuyama::ServerError => e
    self.close
    raise Okuyama::ServerError, "#{e.message}, message = #{result.inspect}"
  end
  return result
end
readlines() click to toggle source
# File lib/okuyama/fast_client.rb, line 110
def readlines
  ret = []
  while result = socket.gets do
    result.chomp!
    if result == "END" then
      break
    else
      result = self.parse_result(result) if @parse_flag
      ret.push result
    end
  end
  return ret
end
socket() click to toggle source
# File lib/okuyama/fast_client.rb, line 124
def socket
  if @socket.nil? then
    retry_count = 0
    begin
      @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
      sockaddr = Socket.sockaddr_in(@port, @host)
      if @timeout then
        secs = Integer(@timeout)
        usecs = Integer((@timeout - secs) * 1_000_000)
        optval = [secs, usecs].pack("l_2")
        @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
        @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
      end
      @socket.connect(sockaddr)
    rescue Exception => e
      if retry_count < @retry_max then
        Okuyama.logger.error "ERROR: #{e.message}"
        @socket.close if @socket != nil
        @socket = nil
        retry_count += 1
        retry
      else
        raise e
      end
      @socket = nil
      raise e
    end
  end
  return @socket
end