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

Comparing cvsroot/Net-FCP/FCP.pm (file contents):
Revision 1.21 by root, Tue Sep 16 07:00:59 2003 UTC vs.
Revision 1.28 by root, Thu May 13 16:13:42 2004 UTC

72 72
73package Net::FCP; 73package Net::FCP;
74 74
75use Carp; 75use Carp;
76 76
77$VERSION = 0.08; 77$VERSION = 0.6;
78 78
79no warnings; 79no warnings;
80 80
81our $EVENT = Net::FCP::Event::Auto::; 81our $EVENT = Net::FCP::Event::Auto::;
82 82
99 $_; 99 $_;
100} 100}
101 101
102sub tolc($) { 102sub 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
111sub 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
110Parse a metadata string and return it. 117Parse 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
182Create a new virtual FCP connection to the given host and port (default 189Create a new virtual FCP connection to the given host and port (default
183127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). 190127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>).
184 191
185Connections are virtual because no persistent physical connection is 192Connections are virtual because no persistent physical connection is
186established. 193established.
194
195You can install a progress callback that is being called with the Net::FCP
196object, a txn object, the type of the transaction and the attributes. Use
197it 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
190However, the existance of the node is checked by executing a 207However, the existance of the node is checked by executing a
191C<ClientHello> transaction. 208C<ClientHello> transaction.
207 $self; 224 $self;
208} 225}
209 226
210sub progress { 227sub 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
217The low-level interface to transactions. Don't use it. 236The 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
323Calculcates a CHK, given the metadata and data. C<$cipher> is either 342Calculates a CHK, given the metadata and data. C<$cipher> is either
324C<Rijndael> or C<Twofish>, with the latter being the default. 343C<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
397Fetches a (small, as it should fit into memory) file from 416Fetches a (small, as it should fit into memory) file from
398freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or 417freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or
399C<undef>). 418C<undef>).
419
420The C<$uri> should begin with C<freenet:>, but the scheme is currently
421added, if missing.
400 422
401Due to the overhead, a better method to download big files should be used. 423Due 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
448All requests (or transactions) are executed in a asynchroneous way (LIE: 473All requests (or transactions) are executed in a asynchronous way. For
449uploads are blocking). For each request, a C<Net::FCP::Txn> object is 474each request, a C<Net::FCP::Txn> object is created (worse: a tcp
450created (worse: a tcp connection is created, too). 475connection is created, too).
451 476
452For each request there is actually a different subclass (and it's possible 477For each request there is actually a different subclass (and it's possible
453to subclass these, although of course not documented). 478to subclass these, although of course not documented).
454 479
455The most interesting method is C<result>. 480The most interesting method is C<result>.
672 } 697 }
673} 698}
674 699
675sub progress { 700sub 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
682Waits until a result is available and then returns it. 708Waits until a result is available and then returns it.
683 709
684This waiting is (depending on your event model) not very efficient, as it 710This waiting is (depending on your event model) not very efficient, as it
685is done outside the "mainloop". 711is done outside the "mainloop". The biggest problem, however, is that it's
712blocking one thread of execution. Try to use the callback mechanism, if
713possible, and call result from within the callback (or after is has been
714run), as then no waiting is necessary.
686 715
687=cut 716=cut
688 717
689sub result { 718sub result {
690 my ($self) = @_; 719 my ($self) = @_;
748 777
749use base Net::FCP::Txn; 778use base Net::FCP::Txn;
750 779
751sub rcv_success { 780sub 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
756package Net::FCP::Txn::GetPut; 785package Net::FCP::Txn::GetPut;
757 786
758# base class for get and put 787# base class for get and put
759 788
760use base Net::FCP::Txn; 789use 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
765sub rcv_restarted { 794sub 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
796sub rcv_data_found { 826sub rcv_data_found {
797 my ($self, $attr, $type) = @_; 827 my ($self, $attr, $type) = @_;
835 865
836package Net::FCP::Exception; 866package Net::FCP::Exception;
837 867
838use overload 868use 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
845Create a new exception object of the given type (a string like 875Create a new exception object of the given type (a string like
900=cut 930=cut
901 931
902package Net::FCP::Event::Auto; 932package Net::FCP::Event::Auto;
903 933
904my @models = ( 934my @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
911sub AUTOLOAD { 941sub AUTOLOAD {
912 $AUTOLOAD =~ s/.*://; 942 $AUTOLOAD =~ s/.*://;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines