ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FCP/FCP.pm
(Generate patch)

Comparing AnyEvent-FCP/FCP.pm (file contents):
Revision 1.20 by root, Sun Jun 12 01:32:37 2016 UTC vs.
Revision 1.26 by root, Wed Jun 15 09:21:14 2016 UTC

61 61
62use common::sense; 62use common::sense;
63 63
64use Carp; 64use Carp;
65 65
66our $VERSION = 0.4; 66our $VERSION = 0.5;
67 67
68use Scalar::Util (); 68use Scalar::Util ();
69 69
70use AnyEvent; 70use AnyEvent;
71use AnyEvent::Handle; 71use AnyEvent::Handle;
126 126
127These keepalive messages are useful both to detect that a connection is 127These keepalive messages are useful both to detect that a connection is
128no longer working and to keep any (home) routers from expiring their 128no longer working and to keep any (home) routers from expiring their
129masquerading entry. 129masquerading entry.
130 130
131=item on_eof => $callback->($fcp)
132
133Invoked when the underlying L<AnyEvent::Handle> signals EOF, currently
134regardless 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
133Invoked on any (fatal) errors, such as unexpected connection close. The 138Invoked on any (fatal) errors, such as unexpected connection close. The
134callback receives the FCP object and a textual error message. 139callback 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, $args, $backtrace, $error)
137 142
138Invoked when an FCP request fails that didn't have a failure callback. See 143Invoked when an FCP request fails that didn't have a failure callback. See
139L<FCP REQUESTS> for details. 144L<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
414 425
415Also comes in this underscore variant: 426Also comes in this underscore variant:
416 427
417 $fcp->get_plugin_info_ ($name, $detailed, $cb); 428 $fcp->get_plugin_info_ ($name, $detailed, $cb);
418 429
419You can thinbk of the underscore as a kind of continuation indicator - the 430You can think of the underscore as a kind of continuation indicator - the
420normal function waits and returns with the data, the C<_> indicates that 431normal function waits and returns with the data, the C<_> indicates that
421you pass the continuation yourself, and the continuation will be invoked 432you pass the continuation yourself, and the continuation will be invoked
422with the results. 433with the results.
423 434
424This callback/continuation argument (C<$cb>) can come in three forms itself: 435This callback/continuation argument (C<$cb>) can come in three forms itself:
433call site. 444call site.
434 445
435This is a popular choice, but it makes handling errors hard - make sure 446This is a popular choice, but it makes handling errors hard - make sure
436you never generate protocol errors! 447you never generate protocol errors!
437 448
438If an C<on_failure> hook exists, it will be invoked with the FCP object, 449In the failure case, if an C<on_failure> hook exists, it will be invoked
439a (textual) backtrace as generated by C<Carp::longmess>, and arrayref 450with the FCP object, the request type (the name of the method, an arrayref
440containing the arguments from the original request invocation and the 451containing the arguments from the original request invocation, a (textual)
441error object from the server, in this order, e.g.: 452backtrace as generated by C<Carp::longmess>, and the error object from the
453server, in 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, $orig_args, $backtrace, $error_object) = @_;
445 ... 457
458 warn "FCP failure ($type @$args), $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
450When a condvar is passed, it is sent (C<< $cv->send ($results) >>) the 464When 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 }
723 }); 737 });
724}; 738};
725 739
726=item $status = $fcp->remove_request ($identifier[, $global]) 740=item $status = $fcp->remove_request ($identifier[, $global])
727 741
728Remove the request with the given isdentifier. Returns true if successful, 742Remove the request with the given identifier. Returns true if successful,
729false on error. 743false on error.
730 744
731=cut 745=cut
732 746
733_txn remove_request => sub { 747_txn remove_request => sub {
778 792
779C<$want_read> and C<$want_write> should be set to a true value when you 793C<$want_read> and C<$want_write> should be set to a true value when you
780want to read (get) files or write (put) files, respectively. 794want to read (get) files or write (put) files, respectively.
781 795
782On error, an exception is thrown. Otherwise, C<$can_read> and 796On error, an exception is thrown. Otherwise, C<$can_read> and
783C<$can_write> indicate whether you can reaqd or write to freenet via the 797C<$can_write> indicate whether you can read or write to freenet via the
784directory. 798directory.
785 799
786=cut 800=cut
787 801
788_txn test_dda => sub { 802_txn test_dda => sub {
909on every change, which will be called as C<< $cb->($fcp, $kv, $type) >>, where C<$type> 923on every change, which will be called as C<< $cb->($fcp, $kv, $type) >>, where C<$type>
910is the type of the original message triggering the change, 924is the type of the original message triggering the change,
911 925
912To fill this cache with the global queue and keep it updated, 926To fill this cache with the global queue and keep it updated,
913call C<watch_global> to subscribe to updates, followed by 927call C<watch_global> to subscribe to updates, followed by
914C<list_persistent_requests_sync>. 928C<list_persistent_requests>.
915 929
916 $fcp->watch_global_sync_; # do not wait 930 $fcp->watch_global_; # do not wait
917 $fcp->list_persistent_requests; # wait 931 $fcp->list_persistent_requests; # wait
918 932
919To get a better idea of what is stored in the cache, here is an example of 933To get a better idea of what is stored in the cache, here is an example of
920what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>: 934what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>:
921 935
1030 if 0.1 > rand; 1044 if 0.1 > rand;
1031 } 1045 }
1032 } 1046 }
1033 1047
1034 # see if the dummy plugin is loaded, to ensure all previous requests have finished. 1048 # see if the dummy plugin is loaded, to ensure all previous requests have finished.
1035 $fcp->get_plugin_info_sync ("dummy"); 1049 $fcp->get_plugin_info ("dummy");
1036 1050
1037=head1 SEE ALSO 1051=head1 SEE ALSO
1038 1052
1039L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. 1053L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>.
1040 1054

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines