… | |
… | |
61 | |
61 | |
62 | use common::sense; |
62 | use common::sense; |
63 | |
63 | |
64 | use Carp; |
64 | use Carp; |
65 | |
65 | |
66 | our $VERSION = 0.4; |
66 | our $VERSION = 0.5; |
67 | |
67 | |
68 | use Scalar::Util (); |
68 | use Scalar::Util (); |
69 | |
69 | |
70 | use AnyEvent; |
70 | use AnyEvent; |
71 | use AnyEvent::Handle; |
71 | use AnyEvent::Handle; |
… | |
… | |
126 | |
126 | |
127 | These keepalive messages are useful both to detect that a connection is |
127 | These keepalive messages are useful both to detect that a connection is |
128 | no longer working and to keep any (home) routers from expiring their |
128 | no longer working and to keep any (home) routers from expiring their |
129 | masquerading entry. |
129 | masquerading entry. |
130 | |
130 | |
|
|
131 | =item on_eof => $callback->($fcp) |
|
|
132 | |
|
|
133 | Invoked when the underlying L<AnyEvent::Handle> signals EOF, currently |
|
|
134 | regardless of whether the EOF was expected or not. |
|
|
135 | |
131 | =item on_error => $callback->($fcp, $message) |
136 | =item on_error => $callback->($fcp, $message) |
132 | |
137 | |
133 | Invoked on any (fatal) errors, such as unexpected connection close. The |
138 | Invoked on any (fatal) errors, such as unexpected connection close. The |
134 | callback receives the FCP object and a textual error message. |
139 | callback receives the FCP object and a textual error message. |
135 | |
140 | |
136 | =item on_failure => $callback->($fcp, $backtrace, $args, $error) |
141 | =item on_failure => $callback->($fcp, $type, $backtrace, $args, $error) |
137 | |
142 | |
138 | Invoked when an FCP request fails that didn't have a failure callback. See |
143 | Invoked when an FCP request fails that didn't have a failure callback. See |
139 | L<FCP REQUESTS> for details. |
144 | L<FCP REQUESTS> for details. |
140 | |
145 | |
141 | =back |
146 | =back |
… | |
… | |
234 | |
239 | |
235 | $self->{hdl} = new AnyEvent::Handle |
240 | $self->{hdl} = new AnyEvent::Handle |
236 | connect => [$self->{host} => $self->{port}], |
241 | connect => [$self->{host} => $self->{port}], |
237 | timeout => $self->{timeout}, |
242 | timeout => $self->{timeout}, |
238 | on_read => $on_read, |
243 | on_read => $on_read, |
239 | on_eof => $self->{on_eof}, |
244 | on_eof => sub { |
|
|
245 | if ($self->{on_eof}) { |
|
|
246 | $self->{on_eof}($self); |
|
|
247 | } else { |
|
|
248 | $self->fatal ("EOF"); |
|
|
249 | } |
|
|
250 | }, |
240 | on_error => sub { |
251 | on_error => sub { |
241 | $self->fatal ($_[2]); |
252 | $self->fatal ($_[2]); |
242 | }, |
253 | }, |
243 | ; |
254 | ; |
244 | |
255 | |
… | |
… | |
434 | |
445 | |
435 | This is a popular choice, but it makes handling errors hard - make sure |
446 | This is a popular choice, but it makes handling errors hard - make sure |
436 | you never generate protocol errors! |
447 | you never generate protocol errors! |
437 | |
448 | |
438 | If an C<on_failure> hook exists, it will be invoked with the FCP object, |
449 | If an C<on_failure> hook exists, it will be invoked with the FCP object, |
439 | a (textual) backtrace as generated by C<Carp::longmess>, and arrayref |
450 | the request type (the name of the method), a (textual) backtrace as |
440 | containing the arguments from the original request invocation and the |
451 | generated by C<Carp::longmess>, and arrayref containing the arguments from |
441 | error object from the server, in this order, e.g.: |
452 | the original request invocation and the error object from the server, in |
|
|
453 | this order, e.g.: |
442 | |
454 | |
443 | on_failure => sub { |
455 | on_failure => sub { |
444 | my ($fcp, $backtrace, $orig_args, $error_object) = @_; |
456 | my ($fcp, $request_type, $backtrace, $orig_args, $error_object) = @_; |
445 | ... |
457 | |
|
|
458 | warn "FCP failure ($type), $error_object->{code_description} ($error_object->{extra_description})$backtrace"; |
|
|
459 | exit 1; |
446 | }, |
460 | }, |
447 | |
461 | |
448 | =item A condvar (as returned by e.g. C<< AnyEvent->condvar >>) |
462 | =item A condvar (as returned by e.g. C<< AnyEvent->condvar >>) |
449 | |
463 | |
450 | When a condvar is passed, it is sent (C<< $cv->send ($results) >>) the |
464 | When a condvar is passed, it is sent (C<< $cv->send ($results) >>) the |
… | |
… | |
498 | my $bt = Carp::longmess "AnyEvent::FCP request $name"; |
512 | my $bt = Carp::longmess "AnyEvent::FCP request $name"; |
499 | Scalar::Util::weaken (my $self = $_[0]); |
513 | Scalar::Util::weaken (my $self = $_[0]); |
500 | my $args = [@_]; shift @$args; |
514 | my $args = [@_]; shift @$args; |
501 | $err = sub { |
515 | $err = sub { |
502 | if ($self->{on_failure}) { |
516 | if ($self->{on_failure}) { |
503 | $self->{on_failure}($self, $args, $bt, $_[0]); |
517 | $self->{on_failure}($self, $name, $args, $bt, $_[0]); |
504 | } else { |
518 | } else { |
505 | die "$_[0]{code_description} ($_[0]{extra_description})$bt"; |
519 | die "$_[0]{code_description} ($_[0]{extra_description})$bt"; |
506 | } |
520 | } |
507 | }; |
521 | }; |
508 | } |
522 | } |