--- AnyEvent-FCP/FCP.pm 2009/07/28 02:20:51 1.3 +++ AnyEvent-FCP/FCP.pm 2010/06/05 14:49:25 1.7 @@ -8,7 +8,7 @@ my $fcp = new AnyEvent::FCP; -# transactions return condvars + # transactions return condvars my $lp_cv = $fcp->list_peers; my $pr_cv = $fcp->list_persistent_requests; @@ -28,6 +28,24 @@ Only very little is implemented, ask if you need more, and look at the example program later in this section. +=head2 EXAMPLE + +This example fetches the download list and sets the priority of all files +with "a" in their name to "emergency": + + use AnyEvent::FCP; + + my $fcp = new AnyEvent::FCP; + + $fcp->watch_global_sync (1, 0); + my $req = $fcp->list_persistent_requests_sync; + + for my $req (values %$req) { + if ($req->{filename} =~ /a/) { + $fcp->modify_persistent_request_sync (1, $req->{identifier}, undef, 0); + } + } + =head2 IMPORT TAGS Nothing much can be "imported" from this module right now. @@ -44,7 +62,7 @@ use Carp; -our $VERSION = '0.2'; +our $VERSION = '0.3'; use Scalar::Util (); @@ -53,15 +71,15 @@ sub touc($) { local $_ = shift; - 1 while s/((?:^|_)(?:svk|chk|uri|fcp)(?:_|$))/\U$1/; + 1 while s/((?:^|_)(?:svk|chk|uri|fcp|ds|mime)(?:_|$))/\U$1/; s/(?:^|_)(.)/\U$1/g; $_ } sub tolc($) { local $_ = shift; - 1 while s/(SVK|CHK|URI|FCP)([^_])/$1\_$2/i; - 1 while s/([^_])(SVK|CHK|URI|FCP)/$1\_$2/i; + 1 while s/(SVK|CHK|URI|FCP|DS|MIME)([^_])/$1\_$2/i; + 1 while s/([^_])(SVK|CHK|URI|FCP|DS|MIME)/$1\_$2/i; s/(?<=[a-z])(?=[A-Z])/_/g; lc } @@ -74,27 +92,31 @@ If no C was specified, then AnyEvent::FCP will generate a (hopefully) unique client name for you. -=cut +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 AnyEvent::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 sub new { 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->{timeout} ||= 600; + $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; + $self->{port} ||= $ENV{FREDPORT} || 9481; + $self->{name} ||= time.rand.rand.rand; # lame + $self->{timeout} ||= 600; + $self->{progress} ||= sub { }; $self->{id} = "a0"; @@ -123,13 +145,6 @@ $self } -#sub progress { -# my ($self, $txn, $type, $attr) = @_; -# -# $self->{progress}->($self, $txn, $type, $attr) -# if $self->{progress}; -#} - sub send_msg { my ($self, $type, %kv) = @_; @@ -223,8 +238,7 @@ $self->{id}{$kv->{identifier}}($self, $type, $kv, $rdata) and delete $self->{id}{$kv->{identifier}}; } else { - # on_warn - #warn "protocol warning (unexpected $type message)\n"; + &{ $self->{progress} }; } } @@ -367,13 +381,11 @@ 1 }, ); - - $cv->(); }; =item $cv = $fcp->modify_persistent_request ($global, $identifier[, $client_token[, $priority_class]]) -=item $status = $fcp->modify_persistent_request_sync ($global, $identifier[, $client_token[, $priority_class]]) +=item $sync = $fcp->modify_persistent_request_sync ($global, $identifier[, $client_token[, $priority_class]]) =cut @@ -382,9 +394,9 @@ $self->send_msg (modify_persistent_request => global => $global ? "true" : "false", - identifier => $identifier, defined $client_token ? (client_token => $client_token ) : (), defined $priority_class ? (priority_class => $priority_class) : (), + identifier => $identifier, id_cb => sub { my ($self, $type, $kv, $rdata) = @_; @@ -392,8 +404,6 @@ 1 }, ); - - $cv->(); }; =item $cv = $fcp->get_plugin_info ($name, $detailed) @@ -415,8 +425,34 @@ 1 }, ); +}; - $cv->(); +=item $cv = $fcp->client_get ($uri, $identifier, %kv) + +=item $status = $fcp->client_get_sync ($uri, $identifier, %kv) + +%kv can contain (L). + +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 + +=cut + +_txn client_get => sub { + my ($self, $cv, $uri, $identifier, %kv) = @_; + + $self->send_msg (client_get => + %kv, + uri => $uri, + identifier => $identifier, + id_cb => sub { + my ($self, $type, $kv, $rdata) = @_; + + $cv->($kv); + 1 + }, + ); }; =back