| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package DR::Tarantool::MsgPack::CoroClient; | ||
| 6 | use base qw(DR::Tarantool::MsgPack::AsyncClient); | ||
| 7 | use AnyEvent; | ||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Coro; | ||
| 11 | |||
| 12 | sub connect { | ||
| 13 | my ($class, %opts) = @_; | ||
| 14 | my $cv = AE::cv; | ||
| 15 | |||
| 16 | $opts{raise_error} = 1 unless exists $opts{raise_error}; | ||
| 17 | |||
| 18 | $class->SUPER::connect(%opts, sub { $cv->send(@_) }); | ||
| 19 | my ($self) = $cv->recv; | ||
| 20 | croak $self unless ref $self; | ||
| 21 | |||
| 22 | $self->{raise_error} = $opts{raise_error}; | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub ping { | ||
| 27 | my ($self) = @_; | ||
| 28 | $self->SUPER::ping( Coro::rouse_cb ); | ||
| 29 | my ($status, $tuple, $code) = Coro::rouse_wait; | ||
| 30 | return 1 if $status and $status eq 'ok'; | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | for my $method (qw(insert replace select update delete call_lua)) { | ||
| 36 | no strict 'refs'; | ||
| 37 | *{ __PACKAGE__ . "::$method" } = sub { | ||
| 38 | my ($self, @args) = @_; | ||
| 39 | my $m = "SUPER::$method"; | ||
| 40 | $self->$m(@args, Coro::rouse_cb); | ||
| 41 | my @res = Coro::rouse_wait; | ||
| 42 | |||
| 43 | return $res[1] if $res[0] eq 'ok'; | ||
| 44 | |||
| 45 | return undef unless $self->{raise_error}; | ||
| 46 | croak sprintf "%s: %s", | ||
| 47 | defined($res[1])? $res[1] : 'unknown', | ||
| 48 | $res[2] | ||
| 49 | ; | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | 1; |