Module: Tarantool16::Connection::Common

Included in:
Dumb
Defined in:
lib/tarantool16/connection/common.rb

Constant Summary collapse

DEFAULT_RECONNECT =
0.2
CLOCK_KIND =
::Process::CLOCK_REALTIME
REQ_EMPTY =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



18
19
20
# File 'lib/tarantool16/connection/common.rb', line 18

def host
  @host
end

Instance Method Details

#_call(name, args, cb) ⇒ Object



184
185
186
187
188
# File 'lib/tarantool16/connection/common.rb', line 184

def _call(name, args, cb)
  req = {IPROTO_FUNCTION_NAME => name,
         IPROTO_TUPLE => args}
  send_request(REQUEST_TYPE_CALL, req, cb)
end

#_delete(space_no, index_no, key, cb) ⇒ Object



155
156
157
158
159
160
# File 'lib/tarantool16/connection/common.rb', line 155

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

#_init_common(host, opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tarantool16/connection/common.rb', line 19

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
  @p = MessagePack::Packer.new
  @u = MessagePack::Unpacker.new
  @s = 0
end

#_insert(space_no, tuple, cb) ⇒ Object



143
144
145
146
147
# File 'lib/tarantool16/connection/common.rb', line 143

def _insert(space_no, tuple, cb)
  req = {IPROTO_SPACE_ID => space_no,
         IPROTO_TUPLE => tuple}
  send_request(REQUEST_TYPE_INSERT, req, cb)
end

#_ping(cb) ⇒ Object



191
192
193
# File 'lib/tarantool16/connection/common.rb', line 191

def _ping(cb)
  send_request(REQUEST_TYPE_PING, REQ_EMPTY, cb)
end

#_replace(space_no, tuple, cb) ⇒ Object



149
150
151
152
153
# File 'lib/tarantool16/connection/common.rb', line 149

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) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/tarantool16/connection/common.rb', line 162

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

#_update(space_no, index_no, key, ops, cb) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/tarantool16/connection/common.rb', line 176

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

#format_authenticate(user, pass1, salt) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tarantool16/connection/common.rb', line 79

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] }
  format_request(REQUEST_TYPE_AUTHENTICATE, next_sync, {
    IPROTO_USER_NAME => user,
    IPROTO_TUPLE => [ 'chap-sha1', sints.pack('L*') ]
  })
end

#format_request(code, sync, body) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tarantool16/connection/common.rb', line 62

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
end

#host_portObject



138
139
140
141
# File 'lib/tarantool16/connection/common.rb', line 138

def host_port
  h, p = @host.split(':')
  [h, p.to_i]
end

#next_syncObject



58
59
60
# File 'lib/tarantool16/connection/common.rb', line 58

def next_sync
  @s = @s % 0x3fffffff + 1
end

#now_fObject



49
50
51
# File 'lib/tarantool16/connection/common.rb', line 49

def now_f
  ::Process.clock_gettime(CLOCK_KIND)
end

#parse_greeting(greeting) ⇒ Object



91
92
93
94
# File 'lib/tarantool16/connection/common.rb', line 91

def parse_greeting(greeting)
  @greeting = greeting[0, 64]
  @salt = greeting[64..-1].unpack('m')[0][0,20]
end

#parse_response(str) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/tarantool16/connection/common.rb', line 107

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
  unless @u.buffer.empty?
    bmap = @u.read
    body = bmap[IPROTO_DATA] || bmap[IPROTO_ERROR]
  else
    body = nil
  end
  Option.ok(sync, code, body)
rescue ::MessagePack::UnpackError, ::MessagePack::TypeError => e
  Option.error(sync, e, nil)
end

#parse_size(str) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/tarantool16/connection/common.rb', line 96

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
  e
end