--- cvsroot/Net-FCP/FCP.pm 2003/09/15 00:05:32 1.20 +++ cvsroot/Net-FCP/FCP.pm 2003/09/17 08:57:32 1.23 @@ -74,7 +74,7 @@ use Carp; -$VERSION = 0.08; +$VERSION = 0.5; no warnings; @@ -105,19 +105,26 @@ lc $_; } +# the opposite of hex +sub xeh($) { + sprintf "%x", $_[0]; +} + =item $meta = Net::FCP::parse_metadata $string Parse a metadata string and return it. -The metadata will be a hashref with key C (containing -the mandatory version header entries). +The metadata will be a hashref with key C (containing the +mandatory version header entries) and key C containing the original +metadata string. All other headers are represented by arrayrefs (they can be repeated). -Since this is confusing, here is a rather verbose example of a parsed -manifest: +Since this description is confusing, here is a rather verbose example of a +parsed manifest: ( + raw => "Version...", version => { revision => 1 }, document => [ { @@ -140,9 +147,9 @@ =cut sub parse_metadata { - my $meta; - my $data = shift; + my $meta = { raw => $data }; + if ($data =~ /^Version\015?\012/gc) { my $hdr = $meta->{version} = {}; @@ -314,18 +321,22 @@ $self->txn ("client_info"); }); -=item $txn = $fcp->txn_generate_chk ($metadata, $data) +=item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) -=item $uri = $fcp->generate_chk ($metadata, $data) +=item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) -Creates a new CHK, given the metadata and data. UNTESTED. +Calculcates a CHK, given the metadata and data. C<$cipher> is either +C or C, with the latter being the default. =cut $txn->(generate_chk => sub { - my ($self, $metadata, $data) = @_; + my ($self, $metadata, $data, $cipher) = @_; - $self->txn (generate_chk => data => "$metadata$data", metadata_length => length $metadata); + $self->txn (generate_chk => + data => "$metadata$data", + metadata_length => xeh length $metadata, + cipher => $cipher || "Twofish"); }); =item $txn = $fcp->txn_generate_svk_pair @@ -405,7 +416,7 @@ $txn->(client_get => sub { my ($self, $uri, $htl, $removelocal) = @_; - $self->txn (client_get => URI => $uri, hops_to_live => (defined $htl ? $htl :15), + $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), remove_local_key => $removelocal ? "true" : "false"); }); @@ -426,22 +437,22 @@ $txn->(client_put => sub { my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; - $self->txn (client_put => URI => $uri, hops_to_live => (defined $htl ? $htl :15), + $self->txn (client_put => URI => $uri, xeh (defined $htl ? $htl : 15), remove_local_key => $removelocal ? "true" : "false", - data => "$meta$data", metadata_length => length $meta); + data => "$meta$data", metadata_length => xeh length $meta); }); } # transactions -=item MISSING: (ClientPut), InsretKey +=item MISSING: (ClientPut), InsertKey =back =head2 THE Net::FCP::Txn CLASS -All requests (or transactions) are executed in a asynchroneous way (LIE: -uploads are blocking). For each request, a C object is -created (worse: a tcp connection is created, too). +All requests (or transactions) are executed in a asynchronous way. For +each request, a C object is created (worse: a tcp +connection is created, too). For each request there is actually a different subclass (and it's possible to subclass these, although of course not documented). @@ -479,7 +490,7 @@ } if (defined $data) { - $attr .= "DataLength=" . (length $data) . "\012"; + $attr .= sprintf "DataLength=%x\012", length $data; $data = "Data\012$data"; } else { $data = "EndMessage\012"; @@ -496,10 +507,10 @@ $self->{sbuf} = "\x00\x00\x00\x02" - . Net::FCP::touc $self->{type} + . (Net::FCP::touc $self->{type}) . "\012$attr$data"; - #$fh->shutdown (1); # freenet buggy?, well, it's java... + #shutdown $fh, 1; # freenet buggy?, well, it's java... $self->{fh} = $fh; @@ -676,7 +687,10 @@ Waits until a result is available and then returns it. This waiting is (depending on your event model) not very efficient, as it -is done outside the "mainloop". +is done outside the "mainloop". The biggest problem, however, is that it's +blocking one thread of execution. Try to use the callback mechanism, if +possible, and call result from within the callback (or after is has been +run), as then no waiting is necessary. =cut @@ -717,7 +731,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->set_result ($attr); + $self->set_result ($attr->{uri}); } package Net::FCP::Txn::GenerateSVKPair; @@ -744,7 +758,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->set_result ($attr->{Length}); + $self->set_result (hex $attr->{Length}); } package Net::FCP::Txn::GetPut; @@ -784,6 +798,7 @@ my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; $self->set_result ([$meta, $data]); + $self->eof; } } @@ -831,7 +846,7 @@ use overload '""' => sub { - "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; + "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>"; }; =item $exc = new Net::FCP::Exception $type, \%attr