| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package DR::Tarantool::MsgPack::LLClient; | ||
| 6 | use Carp; | ||
| 7 | use base qw(DR::Tarantool::LLClient); | ||
| 8 | use DR::Tarantool::MsgPack::Proto; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | sub connect { | ||
| 12 | |||
| 13 | my $class = shift; | ||
| 14 | my (%opts, $cb); | ||
| 15 | |||
| 16 | if (@_ % 2) { | ||
| 17 | $cb = pop; | ||
| 18 | %opts = @_; | ||
| 19 | } else { | ||
| 20 | %opts = @_; | ||
| 21 | $cb = delete $opts{cb}; | ||
| 22 | } | ||
| 23 | $cb ||= sub { }; | ||
| 24 | |||
| 25 | $class->_check_cb( $cb ); | ||
| 26 | |||
| 27 | my $user = delete $opts{user}; | ||
| 28 | my $password = delete $opts{password}; | ||
| 29 | |||
| 30 | $class->SUPER::connect( | ||
| 31 | %opts, | ||
| 32 | |||
| 33 | cb => sub { | ||
| 34 | my ($tnt) = @_; | ||
| 35 | if (ref $tnt) { | ||
| 36 | if (defined $user and defined $password) { | ||
| 37 | $tnt->{user} = $user; | ||
| 38 | $tnt->{password} = $password; | ||
| 39 | } | ||
| 40 | $tnt->{_connect_cb} = $cb; | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | $cb->( $tnt ); | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub _reconnected { | ||
| 51 | my ($self) = @_; | ||
| 52 | $self->{handshake} = 1; | ||
| 53 | delete $self->{tnt_salt}; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _check_rbuf { | ||
| 57 | my ($self) = @_; | ||
| 58 | if ($self->{handshake}) { | ||
| 59 | return unless length $self->{rbuf} >= 128; | ||
| 60 | my $handshake = substr $self->{rbuf}, 0, 128, ''; | ||
| 61 | |||
| 62 | eval { | ||
| 63 | ($self->{tnt_version}, $self->{tnt_salt}) = | ||
| 64 | DR::Tarantool::MsgPack::Proto::handshake($handshake) | ||
| 65 | }; | ||
| 66 | if ($@) { | ||
| 67 | if (my $cb = delete $self->{_connect_cb}) { | ||
| 68 | $cb->('Broken handshake'); | ||
| 69 | } else { | ||
| 70 | $self->_fatal_error('Broken handshake'); | ||
| 71 | } | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | $self->{handshake} = 0; | ||
| 75 | |||
| 76 | if (my $cb = delete $self->{_connect_cb}) {{ | ||
| 77 | unless (defined $self->{user} and defined $self->{password}) { | ||
| 78 | $cb->($self); | ||
| 79 | last; | ||
| 80 | } | ||
| 81 | $self->auth(sub { | ||
| 82 | my ($r) = @_; | ||
| 83 | if ('HASH' eq ref $r) { | ||
| 84 | warn $r->{ERROR} unless $r->{CODE} == 0; | ||
| 85 | $cb->($self); | ||
| 86 | } else { | ||
| 87 | $cb->($r); | ||
| 88 | } | ||
| 89 | }); | ||
| 90 | }} | ||
| 91 | } | ||
| 92 | |||
| 93 | # usual receive | ||
| 94 | while(1) { | ||
| 95 | my ($resp, $tail) = eval { | ||
| 96 | DR::Tarantool::MsgPack::Proto::response $self->{rbuf}; | ||
| 97 | |||
| 98 | }; | ||
| 99 | if ($@) { | ||
| 100 | $self->_fatal_error('Broken response'); | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | return unless $resp; | ||
| 105 | $self->{rbuf} = $tail; | ||
| 106 | |||
| 107 | $self->{last_code} = $resp->{CODE}; | ||
| 108 | $self->{last_error_string} = $resp->{ERROR}; | ||
| 109 | |||
| 110 | my $id = $resp->{SYNC}; | ||
| 111 | my $cb = delete $self->{ wait }{ $id }; | ||
| 112 | if ('CODE' eq ref $cb) { | ||
| 113 | $cb->( $resp ); | ||
| 114 | } else { | ||
| 115 | warn "Unexpected reply from tarantool with id = $id"; | ||
| 116 | } | ||
| 117 | |||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | sub _fatal_error { | ||
| 123 | my ($self, $msg, $raw) = @_; | ||
| 124 | $self->{last_code} ||= -1; | ||
| 125 | $self->{last_error_string} ||= $msg; | ||
| 126 | |||
| 127 | my $wait = delete $self->{wait}; | ||
| 128 | $self->{wait} = {}; | ||
| 129 | for (keys %$wait) { | ||
| 130 | my $cb = delete $wait->{$_}; | ||
| 131 | $cb->({ status => 'fatal', ERROR => $msg, SYNC => $_ }, $raw); | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->set_error($msg) if $self->state ne 'error'; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub ping { | ||
| 138 | my ($self, $cb) = @_; | ||
| 139 | $self->_check_cb( $cb ); | ||
| 140 | |||
| 141 | my $id = $self->_req_id; | ||
| 142 | my $pkt = DR::Tarantool::MsgPack::Proto::ping($id); | ||
| 143 | |||
| 144 | $self->_request($id, $pkt, $cb); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub call_lua { | ||
| 150 | my $self = shift; | ||
| 151 | my $proc = shift; | ||
| 152 | $self->_check_cb( my $cb = pop ); | ||
| 153 | my $tuple; | ||
| 154 | if (@_) { | ||
| 155 | $self->_check_tuple($tuple = shift); | ||
| 156 | } else { | ||
| 157 | $tuple = []; | ||
| 158 | } | ||
| 159 | |||
| 160 | my $id = $self->_req_id; | ||
| 161 | my $pkt = DR::Tarantool::MsgPack::Proto::call_lua($id, $proc, $tuple); | ||
| 162 | $self->_request($id, $pkt, $cb); | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub auth { | ||
| 168 | my $self = shift; | ||
| 169 | my $cb = pop; | ||
| 170 | ($self->{user}, $self->{password}) = @_ if @_ == 2; | ||
| 171 | $self->_check_cb($cb); | ||
| 172 | |||
| 173 | croak "user and password must be defined" | ||
| 174 | unless defined $self->{user} and defined $self->{password}; | ||
| 175 | |||
| 176 | croak "salt is not received yet" unless $self->{tnt_salt}; | ||
| 177 | |||
| 178 | my $id = $self->_req_id; | ||
| 179 | my $pkt = DR::Tarantool::MsgPack::Proto::auth( | ||
| 180 | $id, $self->{user}, $self->{password}, $self->{tnt_salt}); | ||
| 181 | $self->_request($id, $pkt, $cb); | ||
| 182 | return; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | sub _request { | ||
| 187 | my ($self, $id, $pkt, $cb) = @_; | ||
| 188 | return $self->SUPER::_request($id, $pkt, sub { | ||
| 189 | unless (exists $_[0]{status}) { | ||
| 190 | if ($_[0]{CODE} == 0) { | ||
| 191 | $_[0]{status} = 'ok'; | ||
| 192 | } else { | ||
| 193 | $_[0]{status} = 'error'; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | &$cb; | ||
| 197 | }); | ||
| 198 | } | ||
| 199 | |||
| 200 | sub select { | ||
| 201 | my $self = shift; | ||
| 202 | |||
| 203 | my $space = shift; | ||
| 204 | my $index = shift; | ||
| 205 | my $key = shift; | ||
| 206 | |||
| 207 | my $cb = pop; | ||
| 208 | |||
| 209 | my $limit = shift; | ||
| 210 | my $offset = shift; | ||
| 211 | my $iterator = shift; | ||
| 212 | |||
| 213 | $self->_check_cb( $cb ); | ||
| 214 | |||
| 215 | my $id = $self->_req_id; | ||
| 216 | my $pkt = DR::Tarantool::MsgPack::Proto::select( | ||
| 217 | $id, $space, $index, $key, $limit, $offset, $iterator); | ||
| 218 | |||
| 219 | $self->_request($id, $pkt, $cb); | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | sub insert { | ||
| 225 | my $self = shift; | ||
| 226 | |||
| 227 | my $space = shift; | ||
| 228 | my $tuple = shift; | ||
| 229 | my $cb = pop; | ||
| 230 | $self->_check_tuple( $tuple ); | ||
| 231 | $self->_check_cb( $cb ); | ||
| 232 | |||
| 233 | my $id = $self->_req_id; | ||
| 234 | my $pkt = DR::Tarantool::MsgPack::Proto::insert($id, $space, $tuple); | ||
| 235 | |||
| 236 | $self->_request($id, $pkt, $cb); | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub replace { | ||
| 241 | my $self = shift; | ||
| 242 | |||
| 243 | my $space = shift; | ||
| 244 | my $tuple = shift; | ||
| 245 | my $cb = pop; | ||
| 246 | $self->_check_tuple( $tuple ); | ||
| 247 | $self->_check_cb( $cb ); | ||
| 248 | |||
| 249 | my $id = $self->_req_id; | ||
| 250 | my $pkt = DR::Tarantool::MsgPack::Proto::replace($id, $space, $tuple); | ||
| 251 | |||
| 252 | $self->_request($id, $pkt, $cb); | ||
| 253 | return; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub delete:method { | ||
| 257 | my $self = shift; | ||
| 258 | my $cb = pop; | ||
| 259 | $self->_check_cb($cb); | ||
| 260 | my $space = shift; | ||
| 261 | my $key = shift; | ||
| 262 | |||
| 263 | my $id = $self->_req_id; | ||
| 264 | my $pkt = DR::Tarantool::MsgPack::Proto::del($id, $space, $key); | ||
| 265 | $self->_request($id, $pkt, $cb); | ||
| 266 | return; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub update { | ||
| 270 | my $self = shift; | ||
| 271 | my $cb = pop; | ||
| 272 | $self->_check_cb($cb); | ||
| 273 | |||
| 274 | my $space = shift; | ||
| 275 | my $key = shift; | ||
| 276 | my $ops = shift; | ||
| 277 | |||
| 278 | |||
| 279 | my $id = $self->_req_id; | ||
| 280 | my $pkt = DR::Tarantool::MsgPack::Proto::update($id, $space, $key, $ops); | ||
| 281 | $self->_request($id, $pkt, $cb); | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | 1; |