… | |
… | |
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. |
… | |
… | |
136 | =item on_error => $callback->($fcp, $message) |
136 | =item on_error => $callback->($fcp, $message) |
137 | |
137 | |
138 | Invoked on any (fatal) errors, such as unexpected connection close. The |
138 | Invoked on any (fatal) errors, such as unexpected connection close. The |
139 | callback receives the FCP object and a textual error message. |
139 | callback receives the FCP object and a textual error message. |
140 | |
140 | |
141 | =item on_failure => $callback->($fcp, $type, $backtrace, $args, $error) |
141 | =item on_failure => $callback->($fcp, $type, $args, $backtrace, $error) |
142 | |
142 | |
143 | 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 |
144 | L<FCP REQUESTS> for details. |
144 | L<FCP REQUESTS> for details. |
145 | |
145 | |
146 | =back |
146 | =back |
… | |
… | |
265 | } |
265 | } |
266 | |
266 | |
267 | sub fatal { |
267 | sub fatal { |
268 | my ($self, $msg) = @_; |
268 | my ($self, $msg) = @_; |
269 | |
269 | |
270 | $self->{hdl}->shutdown; |
270 | $self->{hdl}->push_shutdown if $self->{hdl}; |
271 | delete $self->{kw}; |
271 | delete $self->{kw}; |
272 | |
272 | |
273 | if ($self->{on_error}) { |
273 | if ($self->{on_error}) { |
274 | $self->{on_error}->($self, $msg); |
274 | $self->{on_error}->($self, $msg); |
275 | } else { |
275 | } else { |
276 | die $msg; |
276 | die "AnyEvent::FCP($self->{host}:$self->{port}): $msg"; |
277 | } |
277 | } |
278 | } |
278 | } |
279 | |
279 | |
280 | sub identifier { |
280 | sub identifier { |
281 | $_[0]{prefix} . ++$_[0]{idseq} |
281 | $_[0]{prefix} . ++$_[0]{idseq} |
… | |
… | |
291 | $self->{id}{$id} = delete $kv{id_cb}; |
291 | $self->{id}{$id} = delete $kv{id_cb}; |
292 | } |
292 | } |
293 | |
293 | |
294 | my $msg = (touc $type) . "\012" |
294 | my $msg = (touc $type) . "\012" |
295 | . join "", map +(touc $_) . "=$kv{$_}\012", keys %kv; |
295 | . join "", map +(touc $_) . "=$kv{$_}\012", keys %kv; |
296 | |
|
|
297 | sub id { |
|
|
298 | my ($self) = @_; |
|
|
299 | |
|
|
300 | |
|
|
301 | } |
|
|
302 | |
296 | |
303 | if (defined $data) { |
297 | if (defined $data) { |
304 | $msg .= "DataLength=" . (length $data) . "\012" |
298 | $msg .= "DataLength=" . (length $data) . "\012" |
305 | . "Data\012$data"; |
299 | . "Data\012$data"; |
306 | } else { |
300 | } else { |
… | |
… | |
425 | |
419 | |
426 | Also comes in this underscore variant: |
420 | Also comes in this underscore variant: |
427 | |
421 | |
428 | $fcp->get_plugin_info_ ($name, $detailed, $cb); |
422 | $fcp->get_plugin_info_ ($name, $detailed, $cb); |
429 | |
423 | |
430 | You can thinbk of the underscore as a kind of continuation indicator - the |
424 | You can think of the underscore as a kind of continuation indicator - the |
431 | normal function waits and returns with the data, the C<_> indicates that |
425 | normal function waits and returns with the data, the C<_> indicates that |
432 | you pass the continuation yourself, and the continuation will be invoked |
426 | you pass the continuation yourself, and the continuation will be invoked |
433 | with the results. |
427 | with the results. |
434 | |
428 | |
435 | This callback/continuation argument (C<$cb>) can come in three forms itself: |
429 | This callback/continuation argument (C<$cb>) can come in three forms itself: |
… | |
… | |
444 | call site. |
438 | call site. |
445 | |
439 | |
446 | This is a popular choice, but it makes handling errors hard - make sure |
440 | This is a popular choice, but it makes handling errors hard - make sure |
447 | you never generate protocol errors! |
441 | you never generate protocol errors! |
448 | |
442 | |
449 | If an C<on_failure> hook exists, it will be invoked with the FCP object, |
443 | In the failure case, if an C<on_failure> hook exists, it will be invoked |
450 | the request type (the name of the method), a (textual) backtrace as |
444 | with the FCP object, the request type (the name of the method, an arrayref |
451 | generated by C<Carp::longmess>, and arrayref containing the arguments from |
445 | containing the arguments from the original request invocation, a (textual) |
452 | the original request invocation and the error object from the server, in |
446 | backtrace as generated by C<Carp::longmess>, and the error object from the |
453 | this order, e.g.: |
447 | server, in this order, e.g.: |
454 | |
448 | |
455 | on_failure => sub { |
449 | on_failure => sub { |
456 | my ($fcp, $request_type, $backtrace, $orig_args, $error_object) = @_; |
450 | my ($fcp, $request_type, $orig_args, $backtrace, $error_object) = @_; |
457 | |
451 | |
458 | warn "FCP failure ($type), $error_object->{code_description} ($error_object->{extra_description})$backtrace"; |
452 | warn "FCP failure ($type @$args), $error_object->{code_description} ($error_object->{extra_description})$backtrace"; |
459 | exit 1; |
453 | exit 1; |
460 | }, |
454 | }, |
461 | |
455 | |
462 | =item A condvar (as returned by e.g. C<< AnyEvent->condvar >>) |
456 | =item A condvar (as returned by e.g. C<< AnyEvent->condvar >>) |
463 | |
457 | |
… | |
… | |
737 | }); |
731 | }); |
738 | }; |
732 | }; |
739 | |
733 | |
740 | =item $status = $fcp->remove_request ($identifier[, $global]) |
734 | =item $status = $fcp->remove_request ($identifier[, $global]) |
741 | |
735 | |
742 | Remove the request with the given isdentifier. Returns true if successful, |
736 | Remove the request with the given identifier. Returns true if successful, |
743 | false on error. |
737 | false on error. |
744 | |
738 | |
745 | =cut |
739 | =cut |
746 | |
740 | |
747 | _txn remove_request => sub { |
741 | _txn remove_request => sub { |
… | |
… | |
792 | |
786 | |
793 | C<$want_read> and C<$want_write> should be set to a true value when you |
787 | C<$want_read> and C<$want_write> should be set to a true value when you |
794 | want to read (get) files or write (put) files, respectively. |
788 | want to read (get) files or write (put) files, respectively. |
795 | |
789 | |
796 | On error, an exception is thrown. Otherwise, C<$can_read> and |
790 | On error, an exception is thrown. Otherwise, C<$can_read> and |
797 | C<$can_write> indicate whether you can reaqd or write to freenet via the |
791 | C<$can_write> indicate whether you can read or write to freenet via the |
798 | directory. |
792 | directory. |
799 | |
793 | |
800 | =cut |
794 | =cut |
801 | |
795 | |
802 | _txn test_dda => sub { |
796 | _txn test_dda => sub { |
… | |
… | |
923 | on every change, which will be called as C<< $cb->($fcp, $kv, $type) >>, where C<$type> |
917 | on every change, which will be called as C<< $cb->($fcp, $kv, $type) >>, where C<$type> |
924 | is the type of the original message triggering the change, |
918 | is the type of the original message triggering the change, |
925 | |
919 | |
926 | To fill this cache with the global queue and keep it updated, |
920 | To fill this cache with the global queue and keep it updated, |
927 | call C<watch_global> to subscribe to updates, followed by |
921 | call C<watch_global> to subscribe to updates, followed by |
928 | C<list_persistent_requests_sync>. |
922 | C<list_persistent_requests>. |
929 | |
923 | |
930 | $fcp->watch_global_sync_; # do not wait |
924 | $fcp->watch_global_; # do not wait |
931 | $fcp->list_persistent_requests; # wait |
925 | $fcp->list_persistent_requests; # wait |
932 | |
926 | |
933 | To get a better idea of what is stored in the cache, here is an example of |
927 | To get a better idea of what is stored in the cache, here is an example of |
934 | what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>: |
928 | what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>: |
935 | |
929 | |
… | |
… | |
1044 | if 0.1 > rand; |
1038 | if 0.1 > rand; |
1045 | } |
1039 | } |
1046 | } |
1040 | } |
1047 | |
1041 | |
1048 | # see if the dummy plugin is loaded, to ensure all previous requests have finished. |
1042 | # see if the dummy plugin is loaded, to ensure all previous requests have finished. |
1049 | $fcp->get_plugin_info_sync ("dummy"); |
1043 | $fcp->get_plugin_info ("dummy"); |
1050 | |
1044 | |
1051 | =head1 SEE ALSO |
1045 | =head1 SEE ALSO |
1052 | |
1046 | |
1053 | L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. |
1047 | L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. |
1054 | |
1048 | |