… | |
… | |
72 | |
72 | |
73 | package Net::FCP; |
73 | package Net::FCP; |
74 | |
74 | |
75 | use Carp; |
75 | use Carp; |
76 | |
76 | |
77 | $VERSION = 0.08; |
77 | $VERSION = 0.6; |
78 | |
78 | |
79 | no warnings; |
79 | no warnings; |
80 | |
80 | |
81 | our $EVENT = Net::FCP::Event::Auto::; |
81 | our $EVENT = Net::FCP::Event::Auto::; |
82 | |
82 | |
… | |
… | |
99 | $_; |
99 | $_; |
100 | } |
100 | } |
101 | |
101 | |
102 | sub tolc($) { |
102 | sub tolc($) { |
103 | local $_ = shift; |
103 | local $_ = shift; |
|
|
104 | 1 while s/(SVK|CHK|URI)([^_])/$1\_$2/i; |
|
|
105 | 1 while s/([^_])(SVK|CHK|URI)/$1\_$2/i; |
104 | s/(?<=[a-z])(?=[A-Z])/_/g; |
106 | s/(?<=[a-z])(?=[A-Z])/_/g; |
105 | lc $_; |
107 | lc $_; |
|
|
108 | } |
|
|
109 | |
|
|
110 | # the opposite of hex |
|
|
111 | sub xeh($) { |
|
|
112 | sprintf "%x", $_[0]; |
106 | } |
113 | } |
107 | |
114 | |
108 | =item $meta = Net::FCP::parse_metadata $string |
115 | =item $meta = Net::FCP::parse_metadata $string |
109 | |
116 | |
110 | Parse a metadata string and return it. |
117 | Parse a metadata string and return it. |
… | |
… | |
175 | #$meta->{tail} = substr $data, pos $data; |
182 | #$meta->{tail} = substr $data, pos $data; |
176 | |
183 | |
177 | $meta; |
184 | $meta; |
178 | } |
185 | } |
179 | |
186 | |
180 | =item $fcp = new Net::FCP [host => $host][, port => $port] |
187 | =item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] |
181 | |
188 | |
182 | Create a new virtual FCP connection to the given host and port (default |
189 | Create a new virtual FCP connection to the given host and port (default |
183 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
190 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
184 | |
191 | |
185 | Connections are virtual because no persistent physical connection is |
192 | Connections are virtual because no persistent physical connection is |
186 | established. |
193 | established. |
|
|
194 | |
|
|
195 | You can install a progress callback that is being called with the Net::FCP |
|
|
196 | object, a txn object, the type of the transaction and the attributes. Use |
|
|
197 | it like this: |
|
|
198 | |
|
|
199 | sub progress_cb { |
|
|
200 | my ($self, $txn, $type, $attr) = @_; |
|
|
201 | |
|
|
202 | warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
|
|
203 | } |
187 | |
204 | |
188 | =begin comment |
205 | =begin comment |
189 | |
206 | |
190 | However, the existance of the node is checked by executing a |
207 | However, the existance of the node is checked by executing a |
191 | C<ClientHello> transaction. |
208 | C<ClientHello> transaction. |
… | |
… | |
207 | $self; |
224 | $self; |
208 | } |
225 | } |
209 | |
226 | |
210 | sub progress { |
227 | sub progress { |
211 | my ($self, $txn, $type, $attr) = @_; |
228 | my ($self, $txn, $type, $attr) = @_; |
212 | #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
229 | |
|
|
230 | $self->{progress}->($self, $txn, $type, $attr) |
|
|
231 | if $self->{progress}; |
213 | } |
232 | } |
214 | |
233 | |
215 | =item $txn = $fcp->txn(type => attr => val,...) |
234 | =item $txn = $fcp->txn(type => attr => val,...) |
216 | |
235 | |
217 | The low-level interface to transactions. Don't use it. |
236 | The low-level interface to transactions. Don't use it. |
… | |
… | |
318 | |
337 | |
319 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
338 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
320 | |
339 | |
321 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
340 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
322 | |
341 | |
323 | Calculcates a CHK, given the metadata and data. C<$cipher> is either |
342 | Calculates a CHK, given the metadata and data. C<$cipher> is either |
324 | C<Rijndael> or C<Twofish>, with the latter being the default. |
343 | C<Rijndael> or C<Twofish>, with the latter being the default. |
325 | |
344 | |
326 | =cut |
345 | =cut |
327 | |
346 | |
328 | $txn->(generate_chk => sub { |
347 | $txn->(generate_chk => sub { |
329 | my ($self, $metadata, $data, $cipher) = @_; |
348 | my ($self, $metadata, $data, $cipher) = @_; |
330 | |
349 | |
331 | $self->txn (generate_chk => |
350 | $self->txn (generate_chk => |
332 | data => "$metadata$data", |
351 | data => "$metadata$data", |
333 | metadata_length => length $metadata, |
352 | metadata_length => xeh length $metadata, |
334 | cipher => $cipher || "Twofish"); |
353 | cipher => $cipher || "Twofish"); |
335 | }); |
354 | }); |
336 | |
355 | |
337 | =item $txn = $fcp->txn_generate_svk_pair |
356 | =item $txn = $fcp->txn_generate_svk_pair |
338 | |
357 | |
… | |
… | |
395 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
414 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
396 | |
415 | |
397 | Fetches a (small, as it should fit into memory) file from |
416 | Fetches a (small, as it should fit into memory) file from |
398 | freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or |
417 | freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or |
399 | C<undef>). |
418 | C<undef>). |
|
|
419 | |
|
|
420 | The C<$uri> should begin with C<freenet:>, but the scheme is currently |
|
|
421 | added, if missing. |
400 | |
422 | |
401 | Due to the overhead, a better method to download big files should be used. |
423 | Due to the overhead, a better method to download big files should be used. |
402 | |
424 | |
403 | my ($meta, $data) = @{ |
425 | my ($meta, $data) = @{ |
404 | $fcp->client_get ( |
426 | $fcp->client_get ( |
… | |
… | |
409 | =cut |
431 | =cut |
410 | |
432 | |
411 | $txn->(client_get => sub { |
433 | $txn->(client_get => sub { |
412 | my ($self, $uri, $htl, $removelocal) = @_; |
434 | my ($self, $uri, $htl, $removelocal) = @_; |
413 | |
435 | |
|
|
436 | $uri =~ s/^freenet://; |
|
|
437 | $uri = "freenet:$uri"; |
|
|
438 | |
414 | $self->txn (client_get => URI => $uri, hops_to_live => (defined $htl ? $htl :15), |
439 | $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), |
415 | remove_local_key => $removelocal ? "true" : "false"); |
440 | remove_local_key => $removelocal ? "true" : "false"); |
416 | }); |
441 | }); |
417 | |
442 | |
418 | =item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) |
443 | =item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) |
419 | |
444 | |
… | |
… | |
430 | =cut |
455 | =cut |
431 | |
456 | |
432 | $txn->(client_put => sub { |
457 | $txn->(client_put => sub { |
433 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
458 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
434 | |
459 | |
435 | $self->txn (client_put => URI => $uri, hops_to_live => (defined $htl ? $htl :15), |
460 | $self->txn (client_put => URI => $uri, xeh (defined $htl ? $htl : 15), |
436 | remove_local_key => $removelocal ? "true" : "false", |
461 | remove_local_key => $removelocal ? "true" : "false", |
437 | data => "$meta$data", metadata_length => length $meta); |
462 | data => "$meta$data", metadata_length => xeh length $meta); |
438 | }); |
463 | }); |
439 | |
464 | |
440 | } # transactions |
465 | } # transactions |
441 | |
466 | |
442 | =item MISSING: (ClientPut), InsretKey |
467 | =item MISSING: (ClientPut), InsertKey |
443 | |
468 | |
444 | =back |
469 | =back |
445 | |
470 | |
446 | =head2 THE Net::FCP::Txn CLASS |
471 | =head2 THE Net::FCP::Txn CLASS |
447 | |
472 | |
448 | All requests (or transactions) are executed in a asynchroneous way (LIE: |
473 | All requests (or transactions) are executed in a asynchronous way. For |
449 | uploads are blocking). For each request, a C<Net::FCP::Txn> object is |
474 | each request, a C<Net::FCP::Txn> object is created (worse: a tcp |
450 | created (worse: a tcp connection is created, too). |
475 | connection is created, too). |
451 | |
476 | |
452 | For each request there is actually a different subclass (and it's possible |
477 | For each request there is actually a different subclass (and it's possible |
453 | to subclass these, although of course not documented). |
478 | to subclass these, although of course not documented). |
454 | |
479 | |
455 | The most interesting method is C<result>. |
480 | The most interesting method is C<result>. |
… | |
… | |
672 | } |
697 | } |
673 | } |
698 | } |
674 | |
699 | |
675 | sub progress { |
700 | sub progress { |
676 | my ($self, $type, $attr) = @_; |
701 | my ($self, $type, $attr) = @_; |
|
|
702 | |
677 | $self->{fcp}->progress ($self, $type, $attr); |
703 | $self->{fcp}->progress ($self, $type, $attr); |
678 | } |
704 | } |
679 | |
705 | |
680 | =item $result = $txn->result |
706 | =item $result = $txn->result |
681 | |
707 | |
682 | Waits until a result is available and then returns it. |
708 | Waits until a result is available and then returns it. |
683 | |
709 | |
684 | This waiting is (depending on your event model) not very efficient, as it |
710 | This waiting is (depending on your event model) not very efficient, as it |
685 | is done outside the "mainloop". |
711 | is done outside the "mainloop". The biggest problem, however, is that it's |
|
|
712 | blocking one thread of execution. Try to use the callback mechanism, if |
|
|
713 | possible, and call result from within the callback (or after is has been |
|
|
714 | run), as then no waiting is necessary. |
686 | |
715 | |
687 | =cut |
716 | =cut |
688 | |
717 | |
689 | sub result { |
718 | sub result { |
690 | my ($self) = @_; |
719 | my ($self) = @_; |
… | |
… | |
748 | |
777 | |
749 | use base Net::FCP::Txn; |
778 | use base Net::FCP::Txn; |
750 | |
779 | |
751 | sub rcv_success { |
780 | sub rcv_success { |
752 | my ($self, $attr) = @_; |
781 | my ($self, $attr) = @_; |
753 | $self->set_result ($attr->{Length}); |
782 | $self->set_result (hex $attr->{Length}); |
754 | } |
783 | } |
755 | |
784 | |
756 | package Net::FCP::Txn::GetPut; |
785 | package Net::FCP::Txn::GetPut; |
757 | |
786 | |
758 | # base class for get and put |
787 | # base class for get and put |
759 | |
788 | |
760 | use base Net::FCP::Txn; |
789 | use base Net::FCP::Txn; |
761 | |
790 | |
762 | *rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; |
791 | *rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; |
763 | *rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
792 | *rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
764 | |
793 | |
765 | sub rcv_restarted { |
794 | sub rcv_restarted { |
766 | my ($self, $attr, $type) = @_; |
795 | my ($self, $attr, $type) = @_; |
767 | |
796 | |
768 | delete $self->{datalength}; |
797 | delete $self->{datalength}; |
… | |
… | |
788 | if ($self->{datalength} == length $self->{data}) { |
817 | if ($self->{datalength} == length $self->{data}) { |
789 | my $data = delete $self->{data}; |
818 | my $data = delete $self->{data}; |
790 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
819 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
791 | |
820 | |
792 | $self->set_result ([$meta, $data]); |
821 | $self->set_result ([$meta, $data]); |
|
|
822 | $self->eof; |
793 | } |
823 | } |
794 | } |
824 | } |
795 | |
825 | |
796 | sub rcv_data_found { |
826 | sub rcv_data_found { |
797 | my ($self, $attr, $type) = @_; |
827 | my ($self, $attr, $type) = @_; |
… | |
… | |
835 | |
865 | |
836 | package Net::FCP::Exception; |
866 | package Net::FCP::Exception; |
837 | |
867 | |
838 | use overload |
868 | use overload |
839 | '""' => sub { |
869 | '""' => sub { |
840 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; |
870 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>"; |
841 | }; |
871 | }; |
842 | |
872 | |
843 | =item $exc = new Net::FCP::Exception $type, \%attr |
873 | =item $exc = new Net::FCP::Exception $type, \%attr |
844 | |
874 | |
845 | Create a new exception object of the given type (a string like |
875 | Create a new exception object of the given type (a string like |
… | |
… | |
900 | =cut |
930 | =cut |
901 | |
931 | |
902 | package Net::FCP::Event::Auto; |
932 | package Net::FCP::Event::Auto; |
903 | |
933 | |
904 | my @models = ( |
934 | my @models = ( |
905 | [Coro => Coro::Event:: ], |
935 | [Coro => Coro::Event::], |
906 | [Event => Event::], |
936 | [Event => Event::], |
907 | [Glib => Glib:: ], |
937 | [Glib => Glib::], |
908 | [Tk => Tk::], |
938 | [Tk => Tk::], |
909 | ); |
939 | ); |
910 | |
940 | |
911 | sub AUTOLOAD { |
941 | sub AUTOLOAD { |
912 | $AUTOLOAD =~ s/.*://; |
942 | $AUTOLOAD =~ s/.*://; |