… | |
… | |
61 | |
61 | |
62 | use common::sense; |
62 | use common::sense; |
63 | |
63 | |
64 | use Carp; |
64 | use Carp; |
65 | |
65 | |
66 | our $VERSION = '0.3'; |
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; |
… | |
… | |
86 | 1 while s/([^_])(SVK|CHK|URI|FCP|DS|MIME|DDA)/$1\_$2/; |
86 | 1 while s/([^_])(SVK|CHK|URI|FCP|DS|MIME|DDA)/$1\_$2/; |
87 | s/(?<=[a-z])(?=[A-Z])/_/g; |
87 | s/(?<=[a-z])(?=[A-Z])/_/g; |
88 | lc |
88 | lc |
89 | } |
89 | } |
90 | |
90 | |
91 | =item $fcp = new AnyEvent::FCP [host => $host][, port => $port][, name => $name] |
91 | =item $fcp = new AnyEvent::FCP key => value...; |
92 | |
92 | |
93 | Create a new FCP connection to the given host and port (default |
93 | Create a new FCP connection to the given host and port (default |
94 | 127.0.0.1:9481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
94 | 127.0.0.1:9481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
95 | |
95 | |
96 | If no C<name> was specified, then AnyEvent::FCP will generate a |
96 | If no C<name> was specified, then AnyEvent::FCP will generate a |
97 | (hopefully) unique client name for you. |
97 | (hopefully) unique client name for you. |
|
|
98 | |
|
|
99 | The following keys can be specified (they are all optional): |
|
|
100 | |
|
|
101 | =over 4 |
|
|
102 | |
|
|
103 | =item name => $string |
|
|
104 | |
|
|
105 | A unique name to identify this client. If none is specified, a randomly |
|
|
106 | generated name will be used. |
|
|
107 | |
|
|
108 | =item host => $hostname |
|
|
109 | |
|
|
110 | The hostname or IP address of the freenet node. Default is C<$ENV{FREDHOST}> |
|
|
111 | or C<127.0.0.1>. |
|
|
112 | |
|
|
113 | =item port => $portnumber |
|
|
114 | |
|
|
115 | The port number of the FCP port. Default is C<$ENV{FREDPORT}> or C<9481>. |
|
|
116 | |
|
|
117 | =item timeout => $seconds |
|
|
118 | |
|
|
119 | The timeout, in seconds, after which a connection error is assumed when |
|
|
120 | there is no activity. Default is C<7200>, i.e. two hours. |
|
|
121 | |
|
|
122 | =item keepalive => $seconds |
|
|
123 | |
|
|
124 | The interval, in seconds, at which keepalive messages will be |
|
|
125 | sent. Default is C<540>, i.e. nine minutes. |
|
|
126 | |
|
|
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 |
|
|
129 | masquerading entry. |
|
|
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 | |
|
|
136 | =item on_error => $callback->($fcp, $message) |
|
|
137 | |
|
|
138 | Invoked on any (fatal) errors, such as unexpected connection close. The |
|
|
139 | callback receives the FCP object and a textual error message. |
|
|
140 | |
|
|
141 | =item on_failure => $callback->($fcp, $type, $args, $backtrace, $error) |
|
|
142 | |
|
|
143 | Invoked when an FCP request fails that didn't have a failure callback. See |
|
|
144 | L<FCP REQUESTS> for details. |
|
|
145 | |
|
|
146 | =back |
98 | |
147 | |
99 | =cut |
148 | =cut |
100 | |
149 | |
101 | sub new { |
150 | sub new { |
102 | my $class = shift; |
151 | my $class = shift; |
… | |
… | |
105 | |
154 | |
106 | my $self = bless { |
155 | my $self = bless { |
107 | host => $ENV{FREDHOST} || "127.0.0.1", |
156 | host => $ENV{FREDHOST} || "127.0.0.1", |
108 | port => $ENV{FREDPORT} || 9481, |
157 | port => $ENV{FREDPORT} || 9481, |
109 | timeout => 3600 * 2, |
158 | timeout => 3600 * 2, |
|
|
159 | keepalive => 9 * 60, |
110 | name => time.rand.rand.rand, # lame |
160 | name => time.rand.rand.rand, # lame |
111 | @_, |
161 | @_, |
112 | queue => [], |
162 | queue => [], |
113 | req => {}, |
163 | req => {}, |
114 | prefix => "..:aefcpid:$rand:", |
164 | prefix => "..:aefcpid:$rand:", |
… | |
… | |
116 | }, $class; |
166 | }, $class; |
117 | |
167 | |
118 | { |
168 | { |
119 | Scalar::Util::weaken (my $self = $self); |
169 | Scalar::Util::weaken (my $self = $self); |
120 | |
170 | |
|
|
171 | $self->{kw} = AE::timer $self->{keepalive}, $self->{keepalive}, sub { |
|
|
172 | $self->{hdl}->push_write ("\n"); |
|
|
173 | }; |
|
|
174 | |
|
|
175 | our $ENDMESSAGE = qr<\012(EndMessage|Data)\012>; |
|
|
176 | |
|
|
177 | # these are declared here for performance reasons |
|
|
178 | my ($k, $v, $type); |
|
|
179 | my $rdata; |
|
|
180 | |
|
|
181 | my $on_read = sub { |
|
|
182 | my ($hdl) = @_; |
|
|
183 | |
|
|
184 | # we only carve out whole messages here |
|
|
185 | while ($hdl->{rbuf} =~ /\012(EndMessage|Data)\012/) { |
|
|
186 | # remember end marker |
|
|
187 | $rdata = $1 eq "Data" |
|
|
188 | or $1 eq "EndMessage" |
|
|
189 | or return $self->fatal ("protocol error, expected message end, got $1\n"); |
|
|
190 | |
|
|
191 | my @lines = split /\012/, substr $hdl->{rbuf}, 0, $-[0]; |
|
|
192 | |
|
|
193 | substr $hdl->{rbuf}, 0, $+[0], ""; # remove pkg |
|
|
194 | |
|
|
195 | $type = shift @lines; |
|
|
196 | $type = ($TOLC{$type} ||= tolc $type); |
|
|
197 | |
|
|
198 | my %kv; |
|
|
199 | |
|
|
200 | for (@lines) { |
|
|
201 | ($k, $v) = split /=/, $_, 2; |
|
|
202 | $k = ($TOLC{$k} ||= tolc $k); |
|
|
203 | |
|
|
204 | if ($k =~ /\./) { |
|
|
205 | # generic, slow case |
|
|
206 | my @k = split /\./, $k; |
|
|
207 | my $ro = \\%kv; |
|
|
208 | |
|
|
209 | while (@k) { |
|
|
210 | $k = shift @k; |
|
|
211 | if ($k =~ /^\d+$/) { |
|
|
212 | $ro = \$$ro->[$k]; |
|
|
213 | } else { |
|
|
214 | $ro = \$$ro->{$k}; |
|
|
215 | } |
|
|
216 | } |
|
|
217 | |
|
|
218 | $$ro = $v; |
|
|
219 | |
|
|
220 | next; |
|
|
221 | } |
|
|
222 | |
|
|
223 | # special comon case, for performance only |
|
|
224 | $kv{$k} = $v; |
|
|
225 | } |
|
|
226 | |
|
|
227 | if ($rdata) { |
|
|
228 | $_[0]->push_read (chunk => delete $kv{data_length}, sub { |
|
|
229 | $rdata = \$_[1]; |
|
|
230 | $self->recv ($type, \%kv, $rdata); |
|
|
231 | }); |
|
|
232 | |
|
|
233 | last; # do not tgry to parse more messages |
|
|
234 | } else { |
|
|
235 | $self->recv ($type, \%kv); |
|
|
236 | } |
|
|
237 | } |
|
|
238 | }; |
|
|
239 | |
121 | $self->{hdl} = new AnyEvent::Handle |
240 | $self->{hdl} = new AnyEvent::Handle |
122 | connect => [$self->{host} => $self->{port}], |
241 | connect => [$self->{host} => $self->{port}], |
123 | timeout => $self->{timeout}, |
242 | timeout => $self->{timeout}, |
|
|
243 | on_read => $on_read, |
|
|
244 | on_eof => sub { |
|
|
245 | if ($self->{on_eof}) { |
|
|
246 | $self->{on_eof}($self); |
|
|
247 | } else { |
|
|
248 | $self->fatal ("EOF"); |
|
|
249 | } |
|
|
250 | }, |
124 | on_error => sub { |
251 | on_error => sub { |
125 | warn "@_\n";#d# |
252 | $self->fatal ($_[2]); |
126 | exit 1; |
|
|
127 | }, |
253 | }, |
128 | on_read => sub { $self->on_read (@_) }, |
254 | ; |
129 | on_eof => $self->{on_eof} || sub { }; |
|
|
130 | |
255 | |
131 | Scalar::Util::weaken ($self->{hdl}{fcp} = $self); |
256 | Scalar::Util::weaken ($self->{hdl}{fcp} = $self); |
132 | } |
257 | } |
133 | |
258 | |
134 | $self->send_msg (client_hello => |
259 | $self->send_msg (client_hello => |
135 | name => $self->{name}, |
260 | name => $self->{name}, |
136 | expected_version => "2.0", |
261 | expected_version => "2.0", |
137 | ); |
262 | ); |
138 | |
263 | |
139 | $self |
264 | $self |
|
|
265 | } |
|
|
266 | |
|
|
267 | sub fatal { |
|
|
268 | my ($self, $msg) = @_; |
|
|
269 | |
|
|
270 | $self->{hdl}->shutdown; |
|
|
271 | delete $self->{kw}; |
|
|
272 | |
|
|
273 | if ($self->{on_error}) { |
|
|
274 | $self->{on_error}->($self, $msg); |
|
|
275 | } else { |
|
|
276 | die $msg; |
|
|
277 | } |
140 | } |
278 | } |
141 | |
279 | |
142 | sub identifier { |
280 | sub identifier { |
143 | $_[0]{prefix} . ++$_[0]{idseq} |
281 | $_[0]{prefix} . ++$_[0]{idseq} |
144 | } |
282 | } |
… | |
… | |
253 | } else { |
391 | } else { |
254 | $self->default_recv ($type, $kv, @extra); |
392 | $self->default_recv ($type, $kv, @extra); |
255 | } |
393 | } |
256 | } |
394 | } |
257 | |
395 | |
258 | sub on_read { |
|
|
259 | my ($self) = @_; |
|
|
260 | |
|
|
261 | my ($k, $v, $type); |
|
|
262 | my %kv; |
|
|
263 | my $rdata; |
|
|
264 | |
|
|
265 | my $hdr_cb; $hdr_cb = sub { |
|
|
266 | if (($v = index $_[1], "=") >= 0) { |
|
|
267 | $k = substr $_[1], 0, $v; |
|
|
268 | $v = substr $_[1], $v + 1; |
|
|
269 | $k = ($TOLC{$k} ||= tolc $k); |
|
|
270 | |
|
|
271 | if ($k !~ /\./) { |
|
|
272 | # special case common case, for performance only |
|
|
273 | $kv{$k} = $v; |
|
|
274 | } else { |
|
|
275 | my @k = split /\./, $k; |
|
|
276 | my $ro = \\%kv; |
|
|
277 | |
|
|
278 | while (@k) { |
|
|
279 | $k = shift @k; |
|
|
280 | if ($k =~ /^\d+$/) { |
|
|
281 | $ro = \$$ro->[$k]; |
|
|
282 | } else { |
|
|
283 | $ro = \$$ro->{$k}; |
|
|
284 | } |
|
|
285 | } |
|
|
286 | |
|
|
287 | $$ro = $v; |
|
|
288 | } |
|
|
289 | |
|
|
290 | $_[0]->push_read (line => $hdr_cb); |
|
|
291 | } elsif ($_[1] eq "Data") { |
|
|
292 | $_[0]->push_read (chunk => delete $kv{data_length}, sub { |
|
|
293 | $rdata = \$_[1]; |
|
|
294 | $self->recv ($type, \%kv, $rdata); |
|
|
295 | }); |
|
|
296 | } elsif ($_[1] eq "EndMessage") { |
|
|
297 | $self->recv ($type, \%kv); |
|
|
298 | } else { |
|
|
299 | die "protocol error, expected message end, got $_[1]\n";#d# |
|
|
300 | } |
|
|
301 | }; |
|
|
302 | |
|
|
303 | $self->{hdl}->push_read (line => sub { |
|
|
304 | $type = ($TOLC{$_[1]} ||= tolc $_[1]); |
|
|
305 | $_[0]->push_read (line => $hdr_cb); |
|
|
306 | }); |
|
|
307 | } |
|
|
308 | |
|
|
309 | sub default_recv { |
396 | sub default_recv { |
310 | my ($self, $type, $kv, $rdata) = @_; |
397 | my ($self, $type, $kv, $rdata) = @_; |
311 | |
398 | |
312 | if ($type eq "node_hello") { |
399 | if ($type eq "node_hello") { |
313 | $self->{node_hello} = $kv; |
400 | $self->{node_hello} = $kv; |
… | |
… | |
338 | |
425 | |
339 | Also comes in this underscore variant: |
426 | Also comes in this underscore variant: |
340 | |
427 | |
341 | $fcp->get_plugin_info_ ($name, $detailed, $cb); |
428 | $fcp->get_plugin_info_ ($name, $detailed, $cb); |
342 | |
429 | |
343 | 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 |
344 | 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 |
345 | you pass the continuation yourself, and the continuation will be invoked |
432 | you pass the continuation yourself, and the continuation will be invoked |
346 | with the results. |
433 | with the results. |
347 | |
434 | |
348 | 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: |
… | |
… | |
350 | =over 4 |
437 | =over 4 |
351 | |
438 | |
352 | =item A code reference (or rather anything not matching some other alternative) |
439 | =item A code reference (or rather anything not matching some other alternative) |
353 | |
440 | |
354 | This code reference will be invoked with the result on success. On an |
441 | This code reference will be invoked with the result on success. On an |
|
|
442 | error, it will invoke the C<on_failure> callback of the FCP object, or, |
355 | error, it will die (in the event loop) with a backtrace of the call site. |
443 | if none was defined, will die (in the event loop) with a backtrace of the |
|
|
444 | call site. |
356 | |
445 | |
357 | 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 |
358 | you never generate protocol errors! |
447 | you never generate protocol errors! |
|
|
448 | |
|
|
449 | In the failure case, if an C<on_failure> hook exists, it will be invoked |
|
|
450 | with the FCP object, the request type (the name of the method, an arrayref |
|
|
451 | containing the arguments from the original request invocation, a (textual) |
|
|
452 | backtrace as generated by C<Carp::longmess>, and the error object from the |
|
|
453 | server, in this order, e.g.: |
|
|
454 | |
|
|
455 | on_failure => sub { |
|
|
456 | my ($fcp, $request_type, $orig_args, $backtrace, $error_object) = @_; |
|
|
457 | |
|
|
458 | warn "FCP failure ($type), $error_object->{code_description} ($error_object->{extra_description})$backtrace"; |
|
|
459 | exit 1; |
|
|
460 | }, |
359 | |
461 | |
360 | =item A condvar (as returned by e.g. C<< AnyEvent->condvar >>) |
462 | =item A condvar (as returned by e.g. C<< AnyEvent->condvar >>) |
361 | |
463 | |
362 | 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 |
363 | results when the request has finished. Should an error occur, the error |
465 | results when the request has finished. Should an error occur, the error |
… | |
… | |
368 | =item An array with two callbacks C<[$success, $failure]> |
470 | =item An array with two callbacks C<[$success, $failure]> |
369 | |
471 | |
370 | The C<$success> callback will be invoked with the results, while the |
472 | The C<$success> callback will be invoked with the results, while the |
371 | C<$failure> callback will be invoked on any errors. |
473 | C<$failure> callback will be invoked on any errors. |
372 | |
474 | |
|
|
475 | The C<$failure> callback will be invoked with the error object from the |
|
|
476 | server. |
|
|
477 | |
373 | =item C<undef> |
478 | =item C<undef> |
374 | |
479 | |
375 | This is the same thing as specifying C<sub { }> as callback, i.e. on |
480 | This is the same thing as specifying C<sub { }> as callback, i.e. on |
376 | success, the results are ignored, while on failure, you the module dies |
481 | success, the results are ignored, while on failure, the C<on_failure> hook |
377 | with a backtrace. |
482 | is invoked or the module dies with a backtrace. |
378 | |
483 | |
379 | This is good for quick scripts, or when you really aren't interested in |
484 | This is good for quick scripts, or when you really aren't interested in |
380 | the results. |
485 | the results. |
381 | |
486 | |
382 | =back |
487 | =back |
… | |
… | |
402 | if (ARRAY:: eq ref $ok) { |
507 | if (ARRAY:: eq ref $ok) { |
403 | ($ok, $err) = @$ok; |
508 | ($ok, $err) = @$ok; |
404 | } elsif (UNIVERSAL::isa $ok, AnyEvent::CondVar::) { |
509 | } elsif (UNIVERSAL::isa $ok, AnyEvent::CondVar::) { |
405 | $err = sub { $ok->croak ($_[0]{extra_description}) }; |
510 | $err = sub { $ok->croak ($_[0]{extra_description}) }; |
406 | } else { |
511 | } else { |
407 | my $bt = Carp::longmess ""; |
512 | my $bt = Carp::longmess "AnyEvent::FCP request $name"; |
|
|
513 | Scalar::Util::weaken (my $self = $_[0]); |
|
|
514 | my $args = [@_]; shift @$args; |
408 | $err = sub { |
515 | $err = sub { |
|
|
516 | if ($self->{on_failure}) { |
|
|
517 | $self->{on_failure}($self, $name, $args, $bt, $_[0]); |
|
|
518 | } else { |
409 | die "$_[0]{code_description} ($_[0]{extra_description})$bt"; |
519 | die "$_[0]{code_description} ($_[0]{extra_description})$bt"; |
|
|
520 | } |
410 | }; |
521 | }; |
411 | } |
522 | } |
412 | |
523 | |
413 | $ok ||= $NOP_CB; |
524 | $ok ||= $NOP_CB; |
414 | |
525 | |
… | |
… | |
626 | }); |
737 | }); |
627 | }; |
738 | }; |
628 | |
739 | |
629 | =item $status = $fcp->remove_request ($identifier[, $global]) |
740 | =item $status = $fcp->remove_request ($identifier[, $global]) |
630 | |
741 | |
631 | Remove the request with the given isdentifier. Returns true if successful, |
742 | Remove the request with the given identifier. Returns true if successful, |
632 | false on error. |
743 | false on error. |
633 | |
744 | |
634 | =cut |
745 | =cut |
635 | |
746 | |
636 | _txn remove_request => sub { |
747 | _txn remove_request => sub { |
… | |
… | |
681 | |
792 | |
682 | 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 |
683 | want to read (get) files or write (put) files, respectively. |
794 | want to read (get) files or write (put) files, respectively. |
684 | |
795 | |
685 | On error, an exception is thrown. Otherwise, C<$can_read> and |
796 | On error, an exception is thrown. Otherwise, C<$can_read> and |
686 | 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 |
687 | directory. |
798 | directory. |
688 | |
799 | |
689 | =cut |
800 | =cut |
690 | |
801 | |
691 | _txn test_dda => sub { |
802 | _txn test_dda => sub { |
… | |
… | |
812 | 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> |
813 | is the type of the original message triggering the change, |
924 | is the type of the original message triggering the change, |
814 | |
925 | |
815 | 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, |
816 | call C<watch_global> to subscribe to updates, followed by |
927 | call C<watch_global> to subscribe to updates, followed by |
817 | C<list_persistent_requests_sync>. |
928 | C<list_persistent_requests>. |
818 | |
929 | |
819 | $fcp->watch_global_sync_; # do not wait |
930 | $fcp->watch_global_; # do not wait |
820 | $fcp->list_persistent_requests; # wait |
931 | $fcp->list_persistent_requests; # wait |
821 | |
932 | |
822 | 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 |
823 | what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>: |
934 | what might be stored in C<< $fcp->{req}{"Frost-gpl.txt"} >>: |
824 | |
935 | |
… | |
… | |
933 | if 0.1 > rand; |
1044 | if 0.1 > rand; |
934 | } |
1045 | } |
935 | } |
1046 | } |
936 | |
1047 | |
937 | # 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. |
938 | $fcp->get_plugin_info_sync ("dummy"); |
1049 | $fcp->get_plugin_info ("dummy"); |
939 | |
1050 | |
940 | =head1 SEE ALSO |
1051 | =head1 SEE ALSO |
941 | |
1052 | |
942 | L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. |
1053 | L<http://wiki.freenetproject.org/FreenetFCPSpec2Point0>, L<Net::FCP>. |
943 | |
1054 | |