--- cvsroot/Net-FCP/FCP.pm 2003/09/16 07:00:59 1.21 +++ cvsroot/Net-FCP/FCP.pm 2004/05/13 21:43:16 1.29 @@ -74,7 +74,7 @@ use Carp; -$VERSION = 0.08; +$VERSION = 0.6; no warnings; @@ -101,10 +101,17 @@ sub tolc($) { local $_ = shift; + 1 while s/(SVK|CHK|URI)([^_])/$1\_$2/i; + 1 while s/([^_])(SVK|CHK|URI)/$1\_$2/i; s/(?<=[a-z])(?=[A-Z])/_/g; lc $_; } +# the opposite of hex +sub xeh($) { + sprintf "%x", $_[0]; +} + =item $meta = Net::FCP::parse_metadata $string Parse a metadata string and return it. @@ -177,7 +184,54 @@ $meta; } -=item $fcp = new Net::FCP [host => $host][, port => $port] +=item $string = Net::FCP::build_metadata $meta + +Takes a hash reference as returned by C and +returns the corresponding string form. If a string is given, it's returned +as is. + +=cut + +sub build_metadata_subhash($$$) { + my ($prefix, $level, $hash) = @_; + + join "", + map + ref $hash->{$_} ? build_metadata_subhash ($prefix . (Net::FCP::touc $_) . ".", $level + 1, $hash->{$_}) + : $prefix . ($level > 1 ? $_ : Net::FCP::touc $_) . "=" . $hash->{$_} . "\n", + keys %$hash; +} + +sub build_metadata_hash($$) { + my ($header, $hash) = @_; + + if (ref $hash eq ARRAY::) { + join "", map build_metadata_hash ($header, $_), @$hash + } else { + (Net::FCP::touc $header) . "\n" + . (build_metadata_subhash "", 0, $hash) + . "EndPart\n"; + } +} + +sub build_metadata($) { + my ($meta) = @_; + + return $meta unless ref $meta; + + $meta = { %$meta }; + + delete $meta->{raw}; + + my $res = + (build_metadata_hash version => delete $meta->{version}) + . (join "", map +(build_metadata_hash $_, $meta->{$_}), keys %$meta); + + substr $res, 0, -5; # get rid of "Part". Broken Syntax.... +} + + +=item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] Create a new virtual FCP connection to the given host and port (default 127.0.0.1:8481, or the environment variables C and C). @@ -185,6 +239,16 @@ Connections are virtual because no persistent physical connection is established. +You can install a progress callback that is being called with the Net::FCP +object, a txn object, the type of the transaction and the attributes. Use +it like this: + + sub progress_cb { + my ($self, $txn, $type, $attr) = @_; + + warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; + } + =begin comment However, the existance of the node is checked by executing a @@ -209,7 +273,9 @@ sub progress { my ($self, $txn, $type, $attr) = @_; - #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; + + $self->{progress}->($self, $txn, $type, $attr) + if $self->{progress}; } =item $txn = $fcp->txn(type => attr => val,...) @@ -245,7 +311,7 @@ $type = touc $type; - my $txn = "Net::FCP::Txn::$type"->new(fcp => $self, type => tolc $type, attr => \%attr); + my $txn = "Net::FCP::Txn::$type"->new (fcp => $self, type => tolc $type, attr => \%attr); $txn; } @@ -320,7 +386,7 @@ =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) -Calculcates a CHK, given the metadata and data. C<$cipher> is either +Calculates a CHK, given the metadata and data. C<$cipher> is either C or C, with the latter being the default. =cut @@ -330,7 +396,7 @@ $self->txn (generate_chk => data => "$metadata$data", - metadata_length => length $metadata, + metadata_length => xeh length $metadata, cipher => $cipher || "Twofish"); }); @@ -338,13 +404,25 @@ =item ($public, $private) = @{ $fcp->generate_svk_pair } -Creates a new SVK pair. Returns an arrayref. +Creates a new SVK pair. Returns an arrayref with the public key, the +private key and a crypto key, which is just additional entropy. [ - "hKs0-WDQA4pVZyMPKNFsK1zapWY", - "ZnmvMITaTXBMFGl4~jrjuyWxOWg" + "acLx4dux9fvvABH15Gk6~d3I-yw", + "cPoDkDMXDGSMM32plaPZDhJDxSs", + "BH7LXCov0w51-y9i~BoB3g", ] +A private key (for inserting) can be constructed like this: + + SSK@,/ + +It can be used to insert data. The corresponding public key looks like this: + + SSK@PAgM,/ + +Watch out for the C-part! + =cut $txn->(generate_svk_pair => sub { @@ -353,21 +431,19 @@ $self->txn ("generate_svk_pair"); }); -=item $txn = $fcp->txn_insert_private_key ($private) +=item $txn = $fcp->txn_invert_private_key ($private) -=item $public = $fcp->insert_private_key ($private) +=item $public = $fcp->invert_private_key ($private) -Inserts a private key. $private can be either an insert URI (must start -with C) or a raw private key (i.e. the private value you get -back from C). +Inverts a private key (returns the public key). C<$private> can be either +an insert URI (must start with C) or a raw private key (i.e. +the private value you get back from C). Returns the public key. -UNTESTED. - =cut -$txn->(insert_private_key => sub { +$txn->(invert_private_key => sub { my ($self, $privkey) = @_; $self->txn (invert_private_key => private => $privkey); @@ -380,8 +456,6 @@ Finds and returns the size (rounded up to the nearest power of two) of the given document. -UNTESTED. - =cut $txn->(get_size => sub { @@ -398,6 +472,9 @@ freenet. C<$meta> is the metadata (as returned by C or C). +The C<$uri> should begin with C, but the scheme is currently +added, if missing. + Due to the overhead, a better method to download big files should be used. my ($meta, $data) = @{ @@ -411,7 +488,10 @@ $txn->(client_get => sub { my ($self, $uri, $htl, $removelocal) = @_; - $self->txn (client_get => URI => $uri, hops_to_live => (defined $htl ? $htl :15), + $uri =~ s/^freenet://; + $uri = "freenet:$uri"; + + $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), remove_local_key => $removelocal ? "true" : "false"); }); @@ -421,33 +501,36 @@ Insert a new key. If the client is inserting a CHK, the URI may be abbreviated as just CHK@. In this case, the node will calculate the -CHK. +CHK. If the key is a private SSK key, the node will calculcate the public +key and the resulting public URI. -C<$meta> can be a reference or a string (ONLY THE STRING CASE IS IMPLEMENTED!). +C<$meta> can be a hash reference (same format as returned by +C) or a string. -THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. +The result is an arrayref with the keys C, C and C. =cut $txn->(client_put => sub { my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; - $self->txn (client_put => URI => $uri, hops_to_live => (defined $htl ? $htl :15), + $meta = build_metadata $meta; + + $self->txn (client_put => URI => $uri, + hops_to_live => 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 - =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). @@ -674,6 +757,7 @@ sub progress { my ($self, $type, $attr) = @_; + $self->{fcp}->progress ($self, $type, $attr); } @@ -682,7 +766,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 @@ -732,16 +819,16 @@ sub rcv_success { my ($self, $attr) = @_; - $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); + $self->set_result ([$attr->{public_key}, $attr->{private_key}, $attr->{crypto_key}]); } -package Net::FCP::Txn::InsertPrivateKey; +package Net::FCP::Txn::InvertPrivateKey; use base Net::FCP::Txn; sub rcv_success { my ($self, $attr) = @_; - $self->set_result ($attr->{PublicKey}); + $self->set_result ($attr->{public_key}); } package Net::FCP::Txn::GetSize; @@ -750,7 +837,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->set_result ($attr->{Length}); + $self->set_result (hex $attr->{length}); } package Net::FCP::Txn::GetPut; @@ -759,8 +846,8 @@ use base Net::FCP::Txn; -*rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; -*rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; +*rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; +*rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; sub rcv_restarted { my ($self, $attr, $type) = @_; @@ -790,6 +877,7 @@ my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; $self->set_result ([$meta, $data]); + $self->eof; } } @@ -837,7 +925,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 @@ -902,9 +990,9 @@ package Net::FCP::Event::Auto; my @models = ( - [Coro => Coro::Event:: ], + [Coro => Coro::Event::], [Event => Event::], - [Glib => Glib:: ], + [Glib => Glib::], [Tk => Tk::], );