… | |
… | |
18 | =head1 DESCRIPTION |
18 | =head1 DESCRIPTION |
19 | |
19 | |
20 | This module implements the freenet client protocol version 2.0, as used by |
20 | This module implements the freenet client protocol version 2.0, as used by |
21 | freenet 0.7. See L<Net::FCP> for the earlier freenet 0.5 version. |
21 | freenet 0.7. See L<Net::FCP> for the earlier freenet 0.5 version. |
22 | |
22 | |
23 | See L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0> for a |
23 | See L<https://wiki.freenetproject.org/FCP> for a description of what the |
24 | description of what the messages do. |
24 | messages do. |
25 | |
25 | |
26 | The module uses L<AnyEvent> to find a suitable event module. |
26 | The module uses L<AnyEvent> to find a suitable event module. |
27 | |
27 | |
28 | Only very little is implemented, ask if you need more, and look at the |
28 | Only very little is implemented, ask if you need more, and look at the |
29 | example program later in this section. |
29 | example program later in this section. |
… | |
… | |
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, $args, $backtrace, $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 | |
… | |
… | |
414 | |
425 | |
415 | Also comes in this underscore variant: |
426 | Also 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 | |
419 | You can thinbk of the underscore as a kind of continuation indicator - the |
430 | You can think of the underscore as a kind of continuation indicator - the |
420 | normal function waits and returns with the data, the C<_> indicates that |
431 | normal function waits and returns with the data, the C<_> indicates that |
421 | you pass the continuation yourself, and the continuation will be invoked |
432 | you pass the continuation yourself, and the continuation will be invoked |
422 | with the results. |
433 | with the results. |
423 | |
434 | |
424 | This callback/continuation argument (C<$cb>) can come in three forms itself: |
435 | This callback/continuation argument (C<$cb>) can come in three forms itself: |
… | |
… | |
433 | call site. |
444 | call site. |
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 | In the failure case, if an C<on_failure> hook exists, it will be invoked |
439 | a (textual) backtrace as generated by C<Carp::longmess>, and arrayref |
450 | with the FCP object, the request type (the name of the method, an arrayref |
440 | containing the arguments from the original request invocation and the |
451 | containing the arguments from the original request invocation, a (textual) |
441 | error object from the server, in this order, e.g.: |
452 | backtrace as generated by C<Carp::longmess>, and the error object from the |
|
|
453 | server, 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 | |
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 | } |
… | |
… | |
723 | }); |
737 | }); |
724 | }; |
738 | }; |
725 | |
739 | |
726 | =item $status = $fcp->remove_request ($identifier[, $global]) |
740 | =item $status = $fcp->remove_request ($identifier[, $global]) |
727 | |
741 | |
728 | Remove the request with the given isdentifier. Returns true if successful, |
742 | Remove the request with the given identifier. Returns true if successful, |
729 | false on error. |
743 | false on error. |
730 | |
744 | |
731 | =cut |
745 | =cut |
732 | |
746 | |
733 | _txn remove_request => sub { |
747 | _txn remove_request => sub { |
… | |
… | |
778 | |
792 | |
779 | C<$want_read> and C<$want_write> should be set to a true value when you |
793 | C<$want_read> and C<$want_write> should be set to a true value when you |
780 | want to read (get) files or write (put) files, respectively. |
794 | want to read (get) files or write (put) files, respectively. |
781 | |
795 | |
782 | On error, an exception is thrown. Otherwise, C<$can_read> and |
796 | On error, an exception is thrown. Otherwise, C<$can_read> and |
783 | C<$can_write> indicate whether you can reaqd or write to freenet via the |
797 | C<$can_write> indicate whether you can read or write to freenet via the |
784 | directory. |
798 | directory. |
785 | |
799 | |
786 | =cut |
800 | =cut |
787 | |
801 | |
788 | _txn test_dda => sub { |
802 | _txn test_dda => sub { |
… | |
… | |
909 | on every change, which will be called as C<< $cb->($fcp, $kv, $type) >>, where C<$type> |
923 | on every change, which will be called as C<< $cb->($fcp, $kv, $type) >>, where C<$type> |
910 | is the type of the original message triggering the change, |
924 | is the type of the original message triggering the change, |
911 | |
925 | |
912 | To fill this cache with the global queue and keep it updated, |
926 | To fill this cache with the global queue and keep it updated, |
913 | call C<watch_global> to subscribe to updates, followed by |
927 | call C<watch_global> to subscribe to updates, followed by |
914 | C<list_persistent_requests_sync>. |
928 | C<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 | |
919 | To get a better idea of what is stored in the cache, here is an example of |
933 | To get a better idea of what is stored in the cache, here is an example of |
920 | what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>: |
934 | what 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 | |
1039 | L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. |
1053 | L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. |
1040 | |
1054 | |