--- AnyEvent-FCP/FCP.pm 2009/07/18 05:57:59 1.1 +++ AnyEvent-FCP/FCP.pm 2015/08/04 00:50:25 1.10 @@ -4,44 +4,51 @@ =head1 SYNOPSIS - use AnyEvent::FCP; + use AnyEvent::FCP; - my $fcp = new AnyEvent::FCP; + my $fcp = new AnyEvent::FCP; - my $ni = $fcp->txn_node_info->result; - my $ni = $fcp->node_info; + # transactions return condvars + my $lp_cv = $fcp->list_peers; + my $pr_cv = $fcp->list_persistent_requests; + + my $peers = $lp_cv->recv; + my $reqs = $pr_cv->recv; =head1 DESCRIPTION This module implements the freenet client protocol version 2.0, as used by freenet 0.7. See L for the earlier freenet 0.5 version. -See L for a description -of what the messages do. +See L for a +description of what the messages do. The module uses L to find a suitable event module. -=head2 IMPORT TAGS +Only very little is implemented, ask if you need more, and look at the +example program later in this section. -Nothing much can be "imported" from this module right now. +=head2 EXAMPLE -=head2 FREENET BASICS +This example fetches the download list and sets the priority of all files +with "a" in their name to "emergency": -Ok, this section will not explain any freenet basics to you, just some -problems I found that you might want to avoid: + use AnyEvent::FCP; -=over 4 + my $fcp = new AnyEvent::FCP; -=item freenet URIs are _NOT_ URIs + $fcp->watch_global_sync (1, 0); + my $req = $fcp->list_persistent_requests_sync; -Whenever a "uri" is required by the protocol, freenet expects a kind of -URI prefixed with the "freenet:" scheme, e.g. "freenet:CHK...". However, -these are not URIs, as freeent fails to parse them correctly, that is, you -must unescape an escaped characters ("%2c" => ",") yourself. Maybe in the -future this library will do it for you, so watch out for this incompatible -change. + for my $req (values %$req) { + if ($req->{filename} =~ /a/) { + $fcp->modify_persistent_request_sync (1, $req->{identifier}, undef, 0); + } + } -=back +=head2 IMPORT TAGS + +Nothing much can be "imported" from this module right now. =head2 THE AnyEvent::FCP CLASS @@ -51,26 +58,29 @@ package AnyEvent::FCP; +use common::sense; + use Carp; -$VERSION = '0.1'; +our $VERSION = '0.3'; -no warnings; +use Scalar::Util (); use AnyEvent; -use AnyEvent::Socket; +use AnyEvent::Handle; +use AnyEvent::Util (); sub touc($) { local $_ = shift; - 1 while s/((?:^|_)(?:svk|chk|uri)(?:_|$))/\U$1/; + 1 while s/((?:^|_)(?:svk|chk|uri|fcp|ds|mime|dda)(?:_|$))/\U$1/; s/(?:^|_)(.)/\U$1/g; $_ } sub tolc($) { local $_ = shift; - 1 while s/(SVK|CHK|URI)([^_])/$1\_$2/i; - 1 while s/([^_])(SVK|CHK|URI)/$1\_$2/i; + 1 while s/(SVK|CHK|URI|FCP|DS|MIME|DDA)([^_])/$1\_$2/i; + 1 while s/([^_])(SVK|CHK|URI|FCP|DS|MIME|DDA)/$1\_$2/i; s/(?<=[a-z])(?=[A-Z])/_/g; lc } @@ -78,21 +88,24 @@ =item $fcp = new AnyEvent::FCP [host => $host][, port => $port][, progress => \&cb][, name => $name] Create a new FCP connection to the given host and port (default -127.0.0.1:8481, or the environment variables C and C). +127.0.0.1:9481, or the environment variables C and C). + +If no C was specified, then AnyEvent::FCP will generate a +(hopefully) unique client name for you. -If no C was specified, then AnyEvent::FCP will generate a (hopefully) -unique client name for you. +You can install a progress callback that is being called with the AnyEvent::FCP +object, the type, a hashref with key-value pairs and a reference to any received data, +for all unsolicited messages. -#TODO -#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"; -# } +Example: + + sub progress_cb { + my ($self, $type, $kv, $rdata) = @_; + + if ($type eq "simple_progress") { + warn "$kv->{identifier} $kv->{succeeded}/$kv->{required}\n"; + } + } =cut @@ -100,723 +113,531 @@ my $class = shift; my $self = bless { @_ }, $class; - $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; - $self->{port} ||= $ENV{FREDPORT} || 9481; - $self->{name} ||= time.rand.rand.rand; # lame - - $self->{conn} = new AnyEvent::Socket - PeerAddr => "$self->{host}:$self->{port}", - on_eof => $self->{on_eof} || sub { }, - - $self -} - -sub progress { - my ($self, $txn, $type, $attr) = @_; - - $self->{progress}->($self, $txn, $type, $attr) - if $self->{progress}; -} - -=item $txn = $fcp->txn (type => attr => val,...) + $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; + $self->{port} ||= $ENV{FREDPORT} || 9481; + $self->{name} ||= time.rand.rand.rand; # lame + $self->{timeout} ||= 3600*2; + $self->{progress} ||= sub { }; -The low-level interface to transactions. Don't use it unless you have -"special needs". Instead, use predefiend transactions like this: + $self->{id} = "a0"; -The blocking case, no (visible) transactions involved: - - my $nodehello = $fcp->client_hello; - -A transaction used in a blocking fashion: - - my $txn = $fcp->txn_client_hello; - ... - my $nodehello = $txn->result; - -Or shorter: + { + Scalar::Util::weaken (my $self = $self); - my $nodehello = $fcp->txn_client_hello->result; + $self->{hdl} = new AnyEvent::Handle + connect => [$self->{host} => $self->{port}], + timeout => $self->{timeout}, + on_error => sub { + warn "@_\n";#d# + exit 1; + }, + on_read => sub { $self->on_read (@_) }, + on_eof => $self->{on_eof} || sub { }; -Setting callbacks: + Scalar::Util::weaken ($self->{hdl}{fcp} = $self); + } - $fcp->txn_client_hello->cb( - sub { my $nodehello => $_[0]->result } + $self->send_msg ( + client_hello => + name => $self->{name}, + expected_version => "2.0", ); -=cut - -sub txn { - my ($self, $type, %attr) = @_; - - $type = touc $type; - - my $txn = "Net::FCP::Txn::$type"->new (fcp => $self, type => tolc $type, attr => \%attr); - - $txn; + $self } -{ # transactions - -my $txn = sub { - my ($name, $sub) = @_; - *{"txn_$name"} = $sub; - *{$name} = sub { $sub->(@_)->result }; -}; - -=item $txn = $fcp->txn_client_hello - -=item $nodehello = $fcp->client_hello - -Executes a ClientHello request and returns it's results. - - { - max_file_size => "5f5e100", - node => "Fred,0.6,1.46,7050" - protocol => "1.2", - } - -=cut - -$txn->(client_hello => sub { - my ($self) = @_; - - $self->txn ("client_hello"); -}); - -=item $txn = $fcp->txn_client_info +sub send_msg { + my ($self, $type, %kv) = @_; -=item $nodeinfo = $fcp->client_info + my $data = delete $kv{data}; -Executes a ClientInfo request and returns it's results. - - { - active_jobs => "1f", - allocated_memory => "bde0000", - architecture => "i386", - available_threads => 17, - datastore_free => "5ce03400", - datastore_max => "2540be400", - datastore_used => "1f72bb000", - estimated_load => 52, - free_memory => "5cc0148", - is_transient => "false", - java_name => "Java HotSpot(_T_M) Server VM", - java_vendor => "http://www.blackdown.org/", - java_version => "Blackdown-1.4.1-01", - least_recent_timestamp => "f41538b878", - max_file_size => "5f5e100", - most_recent_timestamp => "f77e2cc520" - node_address => "1.2.3.4", - node_port => 369, - operating_system => "Linux", - operating_system_version => "2.4.20", - routing_time => "a5", + if (exists $kv{id_cb}) { + my $id = $kv{identifier} ||= ++$self->{id}; + $self->{id}{$id} = delete $kv{id_cb}; } -=cut - -$txn->(client_info => sub { - my ($self) = @_; - - $self->txn ("client_info"); -}); - -=item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) - -=item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) - -Calculates 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, $cipher) = @_; - - $metadata = Net::FCP::Metadata::build_metadata $metadata; - - $self->txn (generate_chk => - data => "$metadata$data", - metadata_length => xeh length $metadata, - cipher => $cipher || "Twofish"); -}); + my $msg = (touc $type) . "\012" + . join "", map +(touc $_) . "=$kv{$_}\012", keys %kv; -=item $txn = $fcp->txn_generate_svk_pair + sub id { + my ($self) = @_; -=item ($public, $private, $crypto) = @{ $fcp->generate_svk_pair } - -Creates a new SVK pair. Returns an arrayref with the public key, the -private key and a crypto key, which is just additional entropy. - - [ - "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 { - my ($self) = @_; - - $self->txn ("generate_svk_pair"); -}); - -=item $txn = $fcp->txn_invert_private_key ($private) - -=item $public = $fcp->invert_private_key ($private) - -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. - -=cut - -$txn->(invert_private_key => sub { - my ($self, $privkey) = @_; - - $self->txn (invert_private_key => private => $privkey); -}); - -=item $txn = $fcp->txn_get_size ($uri) - -=item $length = $fcp->get_size ($uri) - -Finds and returns the size (rounded up to the nearest power of two) of the -given document. - -=cut - -$txn->(get_size => sub { - my ($self, $uri) = @_; - - $self->txn (get_size => URI => $uri); -}); - -=item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) - -=item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) - -Fetches a (small, as it should fit into memory) key content block from -freenet. C<$meta> is a C object or C). - -The C<$uri> should begin with C, but the scheme is currently -added, if missing. - - my ($meta, $data) = @{ - $fcp->client_get ( - "freenet:CHK@hdXaxkwZ9rA8-SidT0AN-bniQlgPAwI,XdCDmBuGsd-ulqbLnZ8v~w" - ) - }; - -=cut - -$txn->(client_get => sub { - my ($self, $uri, $htl, $removelocal) = @_; - - $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"); -}); - -=item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) - -=item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); - -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. 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 hash reference (same format as returned by -C) or a string. - -The result is an arrayref with the keys C, C and C. - -=cut - -$txn->(client_put => sub { - my ($self, $uri, $metadata, $data, $htl, $removelocal) = @_; - - $metadata = Net::FCP::Metadata::build_metadata $metadata; - $uri =~ s/^freenet://; $uri = "freenet:$uri"; - - $self->txn (client_put => URI => $uri, - hops_to_live => xeh (defined $htl ? $htl : 15), - remove_local_key => $removelocal ? "true" : "false", - data => "$metadata$data", metadata_length => xeh length $metadata); -}); - -} # transactions - -=back - -=head2 THE Net::FCP::Txn CLASS - -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). - -The most interesting method is C. - -=over 4 - -=cut - -package Net::FCP::Txn; - -use Fcntl; -use Socket; - -=item new arg => val,... - -Creates a new C object. Not normally used. - -=cut -sub new { - my $class = shift; - my $self = bless { @_ }, $class; - - $self->{signal} = AnyEvent->condvar; - - $self->{fcp}{txn}{$self} = $self; - - my $attr = ""; - my $data = delete $self->{attr}{data}; - - while (my ($k, $v) = each %{$self->{attr}}) { - $attr .= (Net::FCP::touc $k) . "=$v\012" - } + } if (defined $data) { - $attr .= sprintf "DataLength=%x\012", length $data; - $data = "Data\012$data"; + $msg .= "DataLength=" . (length $data) . "\012" + . "Data\012$data"; } else { - $data = "EndMessage\012"; + $msg .= "EndMessage\012"; } - socket my $fh, PF_INET, SOCK_STREAM, 0 - or Carp::croak "unable to create new tcp socket: $!"; - binmode $fh, ":raw"; - fcntl $fh, F_SETFL, O_NONBLOCK; - connect $fh, (sockaddr_in $self->{fcp}{port}, inet_aton $self->{fcp}{host}); -# and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; - - $self->{sbuf} = - "\x00\x00\x00\x02" - . (Net::FCP::touc $self->{type}) - . "\012$attr$data"; - - #shutdown $fh, 1; # freenet buggy?, well, it's java... - - $self->{fh} = $fh; - - $self->{w} = AnyEvent->io (fh => $fh, poll => 'w', cb => sub { $self->fh_ready_w }); - - $self; + $self->{hdl}->push_write ($msg); } -=item $txn = $txn->cb ($coderef) - -Sets a callback to be called when the request is finished. The coderef -will be called with the txn as it's sole argument, so it has to call -C itself. - -Returns the txn object, useful for chaining. - -Example: - - $fcp->txn_client_get ("freenet:CHK....") - ->userdata ("ehrm") - ->cb(sub { - my $data = shift->result; - }); - -=cut - -sub cb($$) { +sub on { my ($self, $cb) = @_; - $self->{cb} = $cb; - $self; -} - -=item $txn = $txn->userdata ([$userdata]) -Set user-specific data. This is useful in progress callbacks. The data can be accessed -using C<< $txn->{userdata} >>. + # cb return undef - message eaten, remove cb + # cb return 0 - message eaten + # cb return 1 - pass to next -Returns the txn object, useful for chaining. - -=cut - -sub userdata($$) { - my ($self, $data) = @_; - $self->{userdata} = $data; - $self; + push @{ $self->{on} }, $cb; } -=item $txn->cancel (%attr) - -Cancels the operation with a C exception and the given attributes -(consider at least giving the attribute C). +sub _push_queue { + my ($self, $queue) = @_; -UNTESTED. + shift @$queue; + $queue->[0]($self, AnyEvent::Util::guard { $self->_push_queue ($queue) }) + if @$queue; +} -=cut +# lock so only one $type (arbitrary string) is in flight, +# to work around horribly misdesigned protocol. +sub serialise { + my ($self, $type, $cb) = @_; -sub cancel { - my ($self, %attr) = @_; - $self->throw (Net::FCP::Exception->new (cancel => { %attr })); - $self->set_result; - $self->eof; + my $queue = $self->{serialise}{$type} ||= []; + push @$queue, $cb; + $cb->($self, AnyEvent::Util::guard { $self->_push_queue ($queue) }) + unless $#$queue; } -sub fh_ready_w { +sub on_read { my ($self) = @_; - my $len = syswrite $self->{fh}, $self->{sbuf}; - - if ($len > 0) { - substr $self->{sbuf}, 0, $len, ""; - unless (length $self->{sbuf}) { - fcntl $self->{fh}, F_SETFL, 0; - $self->{w} = AnyEvent->io (fh => $self->{fh}, poll => 'r', cb => sub { $self->fh_ready_r }); + my $type; + my %kv; + my $rdata; + + my $done_cb = sub { + $kv{pkt_type} = $type; + + my $on = $self->{on}; + for (0 .. $#$on) { + unless (my $res = $on->[$_]($self, $type, \%kv, $rdata)) { + splice @$on, $_, 1 unless defined $res; + return; + } } - } elsif (defined $len) { - $self->throw (Net::FCP::Exception->new (network_error => { reason => "unexpected end of file while writing" })); - } else { - $self->throw (Net::FCP::Exception->new (network_error => { reason => "$!" })); - } -} -sub fh_ready_r { - my ($self) = @_; + if (my $cb = $self->{queue}[0]) { + $cb->($self, $type, \%kv, $rdata) + and shift @{ $self->{queue} }; + } else { + $self->default_recv ($type, \%kv, $rdata); + } + }; - if (sysread $self->{fh}, $self->{buf}, 16384 + 1024, length $self->{buf}) { - for (;;) { - if ($self->{datalen}) { - #warn "expecting new datachunk $self->{datalen}, got ".(length $self->{buf})."\n";#d# - if (length $self->{buf} >= $self->{datalen}) { - $self->rcv_data (substr $self->{buf}, 0, delete $self->{datalen}, ""); + my $hdr_cb; $hdr_cb = sub { + if ($_[1] =~ /^([^=]+)=(.*)$/) { + my ($k, $v) = ($1, $2); + my @k = split /\./, tolc $k; + my $ro = \\%kv; + + while (@k) { + my $k = shift @k; + if ($k =~ /^\d+$/) { + $ro = \$$ro->[$k]; } else { - last; + $ro = \$$ro->{$k}; } - } elsif ($self->{buf} =~ s/^DataChunk\015?\012Length=([0-9a-fA-F]+)\015?\012Data\015?\012//) { - $self->{datalen} = hex $1; - #warn "expecting new datachunk $self->{datalen}\n";#d# - } elsif ($self->{buf} =~ s/^([a-zA-Z]+)\015?\012(?:(.+?)\015?\012)?EndMessage\015?\012//s) { - $self->rcv ($1, { - map { my ($a, $b) = split /=/, $_, 2; ((Net::FCP::tolc $a), $b) } - split /\015?\012/, $2 - }); - } else { - last; } - } - } else { - $self->eof; - } -} -sub rcv { - my ($self, $type, $attr) = @_; + $$ro = $v; - $type = Net::FCP::tolc $type; + $_[0]->push_read (line => $hdr_cb); + } elsif ($_[1] eq "Data") { + $_[0]->push_read (chunk => delete $kv{data_length}, sub { + $rdata = \$_[1]; + $done_cb->(); + }); + } elsif ($_[1] eq "EndMessage") { + $done_cb->(); + } else { + die "protocol error, expected message end, got $_[1]\n";#d# + } + }; - #use PApp::Util; warn PApp::Util::dumpval [$type, $attr]; + $self->{hdl}->push_read (line => sub { + $type = tolc $_[1]; + $_[0]->push_read (line => $hdr_cb); + }); +} - if (my $method = $self->can("rcv_$type")) { - $method->($self, $attr, $type); +sub default_recv { + my ($self, $type, $kv, $rdata) = @_; + + if ($type eq "node_hello") { + $self->{node_hello} = $kv; + } elsif (exists $self->{id}{$kv->{identifier}}) { + $self->{id}{$kv->{identifier}}($self, $type, $kv, $rdata) + and delete $self->{id}{$kv->{identifier}}; } else { - warn "received unexpected reply type '$type' for '$self->{type}', ignoring\n"; + &{ $self->{progress} }; } } -# used as a default exception thrower -sub rcv_throw_exception { - my ($self, $attr, $type) = @_; - $self->throw (Net::FCP::Exception->new ($type, $attr)); -} - -*rcv_failed = \&Net::FCP::Txn::rcv_throw_exception; -*rcv_format_error = \&Net::FCP::Txn::rcv_throw_exception; - -sub throw { - my ($self, $exc) = @_; - - $self->{exception} = $exc; - $self->set_result; - $self->eof; # must be last to avoid loops -} +sub _txn { + my ($name, $sub) = @_; -sub set_result { - my ($self, $result) = @_; + *{$name} = sub { + splice @_, 1, 0, (my $cv = AnyEvent->condvar); + &$sub; + $cv + }; - unless (exists $self->{result}) { - $self->{result} = $result; - $self->{cb}->($self) if exists $self->{cb}; - $self->{signal}->broadcast; - } + *{"$name\_sync"} = sub { + splice @_, 1, 0, (my $cv = AnyEvent->condvar); + &$sub; + $cv->recv + }; } -sub eof { - my ($self) = @_; +=item $cv = $fcp->list_peers ([$with_metdata[, $with_volatile]]) - delete $self->{w}; - delete $self->{fh}; +=item $peers = $fcp->list_peers_sync ([$with_metdata[, $with_volatile]]) - delete $self->{fcp}{txn}{$self}; +=cut - unless (exists $self->{result}) { - $self->throw (Net::FCP::Exception->new (short_data => { - reason => "unexpected eof or internal node error", - })); - } -} +_txn list_peers => sub { + my ($self, $cv, $with_metadata, $with_volatile) = @_; -sub progress { - my ($self, $type, $attr) = @_; + my @res; - $self->{fcp}->progress ($self, $type, $attr); -} + $self->send_msg (list_peers => + with_metadata => $with_metadata ? "true" : "false", + with_volatile => $with_volatile ? "true" : "false", + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; -=item $result = $txn->result + if ($type eq "end_list_peers") { + $cv->(\@res); + 1 + } else { + push @res, $kv; + 0 + } + }, + ); +}; -Waits until a result is available and then returns it. +=item $cv = $fcp->list_peer_notes ($node_identifier) -This waiting is (depending on your event model) not very efficient, as it -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. +=item $notes = $fcp->list_peer_notes_sync ($node_identifier) =cut -sub result { - my ($self) = @_; - - $self->{signal}->wait while !exists $self->{result}; +_txn list_peer_notes => sub { + my ($self, $cv, $node_identifier) = @_; - die $self->{exception} if $self->{exception}; + $self->send_msg (list_peer_notes => + node_identifier => $node_identifier, + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; - return $self->{result}; -} - -package Net::FCP::Txn::ClientHello; - -use base Net::FCP::Txn; - -sub rcv_node_hello { - my ($self, $attr) = @_; + $cv->($kv); + 1 + }, + ); +}; - $self->set_result ($attr); -} +=item $cv = $fcp->watch_global ($enabled[, $verbosity_mask]) -package Net::FCP::Txn::ClientInfo; +=item $fcp->watch_global_sync ($enabled[, $verbosity_mask]) -use base Net::FCP::Txn; +=cut -sub rcv_node_info { - my ($self, $attr) = @_; +_txn watch_global => sub { + my ($self, $cv, $enabled, $verbosity_mask) = @_; - $self->set_result ($attr); -} + $self->send_msg (watch_global => + enabled => $enabled ? "true" : "false", + defined $verbosity_mask ? (verbosity_mask => $verbosity_mask+0) : (), + ); -package Net::FCP::Txn::GenerateCHK; + $cv->(); +}; -use base Net::FCP::Txn; +=item $cv = $fcp->list_persistent_requests -sub rcv_success { - my ($self, $attr) = @_; +=item $reqs = $fcp->list_persistent_requests_sync - $self->set_result ($attr->{uri}); -} +=cut -package Net::FCP::Txn::GenerateSVKPair; +_txn list_persistent_requests => sub { + my ($self, $cv) = @_; -use base Net::FCP::Txn; + $self->serialise (list_persistent_requests => sub { + my ($self, $guard) = @_; -sub rcv_success { - my ($self, $attr) = @_; - $self->set_result ([$attr->{public_key}, $attr->{private_key}, $attr->{crypto_key}]); -} + my %res; -package Net::FCP::Txn::InvertPrivateKey; + $self->send_msg ("list_persistent_requests"); -use base Net::FCP::Txn; + $self->on (sub { + my ($self, $type, $kv, $rdata) = @_; -sub rcv_success { - my ($self, $attr) = @_; - $self->set_result ($attr->{public_key}); -} + $guard if 0; -package Net::FCP::Txn::GetSize; + if ($type eq "end_list_persistent_requests") { + $cv->(\%res); + return; + } else { + my $id = $kv->{identifier}; -use base Net::FCP::Txn; + if ($type =~ /^persistent_(get|put|put_dir)$/) { + $res{$id} = { + type => $1, + %{ $res{$id} }, + %$kv, + }; + } elsif ($type eq "simple_progress") { + delete $kv->{pkt_type}; # save memory + push @{ $res{delete $kv->{identifier}}{simple_progress} }, $kv; + } else { + $res{delete $kv->{identifier}}{delete $kv->{pkt_type}} = $kv; + } + } -sub rcv_success { - my ($self, $attr) = @_; - $self->set_result (hex $attr->{length}); -} + 1 + }); + }); +}; -package Net::FCP::Txn::GetPut; +=item $cv = $fcp->remove_request ($global, $identifier) -# base class for get and put +=item $status = $fcp->remove_request_sync ($global, $identifier) -use base Net::FCP::Txn; +=cut -*rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; -*rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; +_txn remove_request => sub { + my ($self, $cv, $global, $identifier) = @_; -sub rcv_restarted { - my ($self, $attr, $type) = @_; + $self->send_msg (remove_request => + global => $global ? "true" : "false", + identifier => $identifier, + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; - delete $self->{datalength}; - delete $self->{metalength}; - delete $self->{data}; + $cv->($kv); + 1 + }, + ); +}; - $self->progress ($type, $attr); -} +=item $cv = $fcp->modify_persistent_request ($global, $identifier[, $client_token[, $priority_class]]) -package Net::FCP::Txn::ClientGet; +=item $sync = $fcp->modify_persistent_request_sync ($global, $identifier[, $client_token[, $priority_class]]) -use base Net::FCP::Txn::GetPut; +=cut -*rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; +_txn modify_persistent_request => sub { + my ($self, $cv, $global, $identifier, $client_token, $priority_class) = @_; -sub rcv_data { - my ($self, $chunk) = @_; + $self->send_msg (modify_persistent_request => + global => $global ? "true" : "false", + defined $client_token ? (client_token => $client_token ) : (), + defined $priority_class ? (priority_class => $priority_class) : (), + identifier => $identifier, + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; - $self->{data} .= $chunk; + $cv->($kv); + 1 + }, + ); +}; - $self->progress ("data", { chunk => length $chunk, received => length $self->{data}, total => $self->{datalength} }); +=item $cv = $fcp->get_plugin_info ($name, $detailed) - if ($self->{datalength} == length $self->{data}) { - my $data = delete $self->{data}; - my $meta = new Net::FCP::Metadata (substr $data, 0, $self->{metalength}, ""); +=item $info = $fcp->get_plugin_info_sync ($name, $detailed) - $self->set_result ([$meta, $data]); - $self->eof; - } -} +=cut -sub rcv_data_found { - my ($self, $attr, $type) = @_; +_txn get_plugin_info => sub { + my ($self, $cv, $name, $detailed) = @_; - $self->progress ($type, $attr); + $self->send_msg (get_plugin_info => + plugin_name => $name, + detailed => $detailed ? "true" : "false", + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; - $self->{datalength} = hex $attr->{data_length}; - $self->{metalength} = hex $attr->{metadata_length}; -} + $cv->($kv); + 1 + }, + ); +}; -package Net::FCP::Txn::ClientPut; +=item $cv = $fcp->client_get ($uri, $identifier, %kv) -use base Net::FCP::Txn::GetPut; +=item $status = $fcp->client_get_sync ($uri, $identifier, %kv) -*rcv_size_error = \&Net::FCP::Txn::rcv_throw_exception; +%kv can contain (L). -sub rcv_pending { - my ($self, $attr, $type) = @_; - $self->progress ($type, $attr); -} +ignore_ds, ds_only, verbosity, max_size, max_temp_size, max_retries, +priority_class, persistence, client_token, global, return_type, +binary_blob, allowed_mime_types, filename, temp_filename -sub rcv_success { - my ($self, $attr, $type) = @_; - $self->set_result ($attr); -} +=cut -sub rcv_key_collision { - my ($self, $attr, $type) = @_; - $self->set_result ({ key_collision => 1, %$attr }); -} +_txn client_get => sub { + my ($self, $cv, $uri, $identifier, %kv) = @_; -=back + $self->send_msg (client_get => + %kv, + uri => $uri, + identifier => $identifier, + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; -=head2 The Net::FCP::Exception CLASS + $cv->($kv); + 1 + }, + ); +}; -Any unexpected (non-standard) responses that make it impossible to return -the advertised result will result in an exception being thrown when the -C method is called. +=item $cv = $fcp->test_dda_sync ($local_directory, $remote_directory, $want_read, $want_write) -These exceptions are represented by objects of this class. +=item ($can_read, $can_write) = $fcp->test_dda_sync ($local_directory, $remote_directory, $want_read, $want_write)) -=over 4 +The DDA test in FCP is probably the single most broken protocol - only +one directory test can be outstanding at any time, and some guessing and +heuristics are involved in mangling the paths. + +This function combines C and C in one +request, handling file reading and writing as well. =cut -package Net::FCP::Exception; +_txn test_dda => sub { + my ($self, $cv, $local, $remote, $want_read, $want_write) = @_; + + $self->serialise (test_dda => sub { + my ($self, $guard) = @_; + + $self->send_msg (test_dda_request => + directory => $remote, + want_read_directory => $want_read ? "true" : "false", + want_write_directory => $want_write ? "true" : "false", + ); + $self->on (sub { + my ($self, $type, $kv) = @_; + + if ($type eq "test_dda_reply") { + # the filenames are all relative to the server-side directory, + # which might or might not match $remote anymore, so we + # need to rewrite the paths to be relative to $local + for my $k (qw(read_filename write_filename)) { + my $f = $kv->{$k}; + for my $dir ($kv->{directory}, $remote) { + if ($dir eq substr $f, 0, length $dir) { + substr $f, 0, 1 + length $dir, ""; + $kv->{$k} = $f; + last; + } + } + } -use overload - '""' => sub { - "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>"; - }; + my %response = (directory => $remote); -=item $exc = new Net::FCP::Exception $type, \%attr + if (length $kv->{read_filename}) { + warn "$local/$kv->{read_filename}";#d# + if (open my $fh, "<:raw", "$local/$kv->{read_filename}") { + sysread $fh, my $buf, -s $fh; + $response{read_content} = $buf; + } + } -Create a new exception object of the given type (a string like -C), and a hashref containing additional attributes -(usually the attributes of the message causing the exception). + if (length $kv->{write_filename}) { + if (open my $fh, ">:raw", "$local/$kv->{write_filename}") { + syswrite $fh, $kv->{content_to_write}; + } + } -=cut + $self->send_msg (test_dda_response => %response); -sub new { - my ($class, $type, $attr) = @_; + $self->on (sub { + my ($self, $type, $kv) = @_; - bless [Net::FCP::tolc $type, { %$attr }], $class; -} + $guard if 0; # reference -=item $exc->type([$type]) + if ($type eq "test_dda_complete") { + $cv->( + $kv->{read_directory_allowed} eq "true", + $kv->{write_directory_allowed} eq "true", + ); + } elsif ($type eq "protocol_error" && $kv->{identifier} eq $remote) { + $cv->croak ($kv->{extra_description}); + return; + } -With no arguments, returns the exception type. Otherwise a boolean -indicating wether the exception is of the given type is returned. + 1 + }); -=cut + return; + } elsif ($type eq "protocol_error" && $kv->{identifier} eq $remote) { + $cv->croak ($kv->{extra_description}); + return; + } -sub type { - my ($self, $type) = @_; + 1 + }); + }); +}; - @_ >= 2 - ? $self->[0] eq $type - : $self->[0]; -} +=back -=item $exc->attr([$attr]) +=head1 EXAMPLE PROGRAM -With no arguments, returns the attributes. Otherwise the named attribute -value is returned. + use AnyEvent::FCP; -=cut + my $fcp = new AnyEvent::FCP; -sub attr { - my ($self, $attr) = @_; + # let us look at the global request list + $fcp->watch_global (1, 0); - @_ >= 2 - ? $self->[1]{$attr} - : $self->[1]; -} + # list them, synchronously + my $req = $fcp->list_persistent_requests_sync; + + # go through all requests + for my $req (values %$req) { + # skip jobs not directly-to-disk + next unless $req->{return_type} eq "disk"; + # skip jobs not issued by FProxy + next unless $req->{identifier} =~ /^FProxy:/; + + if ($req->{data_found}) { + # file has been successfully downloaded + + ... move the file away + (left as exercise) + + # remove the request + + $fcp->remove_request (1, $req->{identifier}); + } elsif ($req->{get_failed}) { + # request has failed + if ($req->{get_failed}{code} == 11) { + # too many path components, should restart + } else { + # other failure + } + } else { + # modify priorities randomly, to improve download rates + $fcp->modify_persistent_request (1, $req->{identifier}, undef, int 6 - 5 * (rand) ** 1.7) + if 0.1 > rand; + } + } -=back + # see if the dummy plugin is loaded, to ensure all previous requests have finished. + $fcp->get_plugin_info_sync ("dummy"); =head1 SEE ALSO -L. +L, L. =head1 BUGS