module Tarantool16::Connection::Common

Constants

CLOCK_KIND
DEFAULT_RECONNECT
REQ_EMPTY

Attributes

host[RW]

Public Instance Methods

_call(name, args, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 217
def _call(name, args, cb)
  req = {IPROTO_FUNCTION_NAME => name,
         IPROTO_TUPLE => args}
  send_request(REQUEST_TYPE_CALL17, req, cb)
end
_call16(name, args, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 223
def _call16(name, args, cb)
  req = {IPROTO_FUNCTION_NAME => name,
         IPROTO_TUPLE => args}
  send_request(REQUEST_TYPE_CALL16, req, cb)
end
_delete(space_no, index_no, key, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 180
def _delete(space_no, index_no, key, cb)
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_INDEX_ID => index_no,
         IPROTO_KEY => key}
  send_request(REQUEST_TYPE_DELETE, req, cb)
end
_eval(expr, args, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 229
def _eval(expr, args, cb)
  req = {IPROTO_EXPR => expr,
         IPROTO_TUPLE => args}
  send_request(REQUEST_TYPE_EVAL, req, cb)
end
_init_common(host, opts) click to toggle source
# File lib/tarantool16/connection/common.rb, line 20
def _init_common(host, opts)
  @host = host
  @user = opts[:user]
  if opts[:password]
    @passwd = ::OpenSSL::Digest::SHA1.digest(opts[:password])
  end
  if opts[:reconnect].nil?
    @reconnect_timeout = DEFAULT_RECONNECT
    @reconnect = true
  elsif Numeric === opts[:reconnect]
    @reconnect_timeout = opts[:reconnect]
    @reconnect = true
  else
    @reconnect = false
  end
  @timeout = opts[:timeout]
  @p = MessagePack::Packer.new
  @u = MessagePack::Unpacker.new
  @s = 0
end
_insert(space_no, tuple, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 168
def _insert(space_no, tuple, cb)
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_TUPLE => tuple}
  send_request(REQUEST_TYPE_INSERT, req, cb)
end
_ipv6?() click to toggle source
# File lib/tarantool16/connection/common.rb, line 164
def _ipv6?
  @host[1].count(':') > 1
end
_ping(cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 236
def _ping(cb)
  send_request(REQUEST_TYPE_PING, REQ_EMPTY, cb)
end
_replace(space_no, tuple, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 174
def _replace(space_no, tuple, cb)
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_TUPLE => tuple}
  send_request(REQUEST_TYPE_REPLACE, req, cb)
end
_select(space_no, index_no, key, offset, limit, iterator, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 187
def _select(space_no, index_no, key, offset, limit, iterator, cb)
  iterator ||= ::Tarantool16::ITERATOR_EQ
  unless Integer === iterator
    iterator = ::Tarantool16.iter(iterator)
  end
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_INDEX_ID => index_no,
         IPROTO_KEY => key || [],
         IPROTO_OFFSET => offset,
         IPROTO_LIMIT => limit,
         IPROTO_ITERATOR => iterator}
  send_request(REQUEST_TYPE_SELECT, req, cb)
end
_tcp?() click to toggle source
# File lib/tarantool16/connection/common.rb, line 151
def _tcp?
  @host[0] == 'tcp' || @host[0] == :tcp
end
_unix?() click to toggle source
# File lib/tarantool16/connection/common.rb, line 147
def _unix?
  @host[0] == 'unix' || @host[0] == :unix
end
_unix_sock_path() click to toggle source
# File lib/tarantool16/connection/common.rb, line 155
def _unix_sock_path
  @host[1]
end
_update(space_no, index_no, key, ops, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 201
def _update(space_no, index_no, key, ops, cb)
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_INDEX_ID => index_no,
         IPROTO_KEY => key,
         IPROTO_TUPLE => ops}
  send_request(REQUEST_TYPE_UPDATE, req, cb)
end
_upsert(space_no, index_no, tuple_key, ops, cb) click to toggle source
# File lib/tarantool16/connection/common.rb, line 209
def _upsert(space_no, index_no, tuple_key, ops, cb)
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_INDEX_ID => index_no,
         IPROTO_TUPLE => tuple_key,
         IPROTO_DEF_DUPLE => ops}
  send_request(REQUEST_TYPE_UPSERT, req, cb)
end
format_authenticate(user, pass1, salt) click to toggle source
# File lib/tarantool16/connection/common.rb, line 83
def format_authenticate(user, pass1, salt)
  pass2 = ::OpenSSL::Digest::SHA1.digest(pass1)
  scramble = ::OpenSSL::Digest::SHA1.new(salt).update(pass2).digest
  pints = pass1.unpack('L*')
  sints = scramble.unpack('L*')
  pints.size.times{|i| sints[i] ^= pints[i] }
  packed = sints.pack('L*')
  # tarantool waits packed as a string, so that force msgpack to pack as string
  packed.force_encoding('utf-8')
  format_request(REQUEST_TYPE_AUTHENTICATE, next_sync, {
    IPROTO_USER_NAME => user,
    IPROTO_TUPLE => [ 'chap-sha1', packed ]
  })
end
format_request(code, sync, body) click to toggle source
# File lib/tarantool16/connection/common.rb, line 64
def format_request(code, sync, body)
  @p.write(0x01020304).
    write_map_header(2).
    write(IPROTO_CODE).write(code).
    write(IPROTO_SYNC).write(sync).
    write(body)
  sz = @p.size - 5
  str = @p.to_s
  @p.clear
  # fix bigendian size
  str.setbyte(4, sz)
  str.setbyte(3, sz>>8)
  str.setbyte(2, sz>>16)
  str.setbyte(1, sz>>24)
  str
ensure
  @p.clear
end
host_port() click to toggle source
# File lib/tarantool16/connection/common.rb, line 159
def host_port
  @host[1] =~ /^(.*):([^:]+)$/
  [$1, $2.to_i]
end
next_sync() click to toggle source
# File lib/tarantool16/connection/common.rb, line 60
def next_sync
  @s = @s % 0x3fffffff + 1
end
now_f() click to toggle source
# File lib/tarantool16/connection/common.rb, line 51
def now_f
  ::Process.clock_gettime(CLOCK_KIND)
end
parse_greeting(greeting) click to toggle source
# File lib/tarantool16/connection/common.rb, line 98
def parse_greeting(greeting)
  @greeting = greeting[0, 64]
  @salt = greeting[64..-1].unpack('m')[0][0,20]
end
parse_response(str) click to toggle source
# File lib/tarantool16/connection/common.rb, line 115
def parse_response(str)
  sync = nil
  @u.feed(str)
  n = @u.read_map_header
  while n > 0
    cd = @u.read
    vl = @u.read
    case cd
    when IPROTO_SYNC
      sync = vl
    when IPROTO_CODE
      code = vl
    end
    n -= 1
  end
  if sync == nil
    return Option.error(nil, UnexpectedResponse, "Mailformed response: no sync")
  elsif code == nil
    return Option.error(nil, UnexpectedResponse, "Mailformed response: no code for sync=#{sync}")
  end
  begin
    bmap = @u.read
    body = bmap[IPROTO_DATA] || bmap[IPROTO_ERROR]
  rescue EOFError
    body = nil
  end
  Option.ok(sync, code, body)
rescue ::MessagePack::UnpackError, ::MessagePack::TypeError => e
  @u.reset
  Option.error(sync, e, nil)
end
parse_size(str) click to toggle source
# File lib/tarantool16/connection/common.rb, line 103
def parse_size(str)
  @u.feed(str)
  n = @u.read
  unless Integer === n
    return UnexpectedResponse.new("wanted response size, got #{n.inspect}")
  end
  n
rescue ::MessagePack::UnpackError, ::MessagePack::TypeError => e
  @u.reset
  e
end