=head1 NAME AnyEvent::FCP - freenet client protocol 2.0 =head1 SYNOPSIS use AnyEvent::FCP; my $fcp = new AnyEvent::FCP; my $ni = $fcp->txn_node_info->result; my $ni = $fcp->node_info; =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. The module uses L to find a suitable event module. =head2 IMPORT TAGS Nothing much can be "imported" from this module right now. =head2 FREENET BASICS Ok, this section will not explain any freenet basics to you, just some problems I found that you might want to avoid: =over 4 =item freenet URIs are _NOT_ URIs 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. =back =head2 THE AnyEvent::FCP CLASS =over 4 =cut package AnyEvent::FCP; use common::sense; use Carp; our $VERSION = '0.1'; use Scalar::Util (); use AnyEvent; use AnyEvent::Handle; sub touc($) { local $_ = shift; 1 while s/((?:^|_)(?:svk|chk|uri|fcp)(?:_|$))/\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; s/(?<=[a-z])(?=[A-Z])/_/g; lc } =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: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. #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"; # } =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->{id} = "a0"; { Scalar::Util::weaken (my $self = $self); $self->{hdl} = new AnyEvent::Handle connect => [$self->{host} => $self->{port}], timeout => $self->{timeout}, on_error => sub { warn "<@_>\n"; exit 1; }, on_read => sub { $self->on_read (@_) }, on_eof => $self->{on_eof} || sub { }; Scalar::Util::weaken ($self->{hdl}{fcp} = $self); } $self->send_msg ( client_hello => name => $self->{name}, expected_version => "2.0", ); $self } sub progress { my ($self, $txn, $type, $attr) = @_; $self->{progress}->($self, $txn, $type, $attr) if $self->{progress}; } sub send_msg { my ($self, $type, %kv) = @_; my $data = delete $kv{data}; if (exists $kv{id_cb}) { my $id = $kv{identifier} || ++$self->{id}; $self->{id}{$id} = delete $kv{id_cb}; $kv{identifier} = $id; } my $msg = (touc $type) . "\012" . join "", map +(touc $_) . "=$kv{$_}\012", keys %kv; sub id { my ($self) = @_; } if (defined $data) { $msg .= "DataLength=" . (length $data) . "\012" . "Data\012$data"; } else { $msg .= "EndMessage\012"; } $self->{hdl}->push_write ($msg); } sub on_read { my ($self) = @_; my $type; my %kv; my $rdata; my $done_cb = sub { $kv{pkt_type} = $type; if (my $cb = $self->{queue}[0]) { $cb->($self, $type, \%kv, $rdata) and shift @{ $self->{queue} }; } else { $self->default_recv ($type, \%kv, $rdata); } }; 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 { $ro = \$$ro->{$k}; } } $$ro = $v; $_[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# } }; $self->{hdl}->push_read (line => sub { $type = tolc $_[1]; $_[0]->push_read (line => $hdr_cb); }); } 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 { # on_warn #warn "protocol warning (unexpected $type message)\n"; } } sub _txn { my ($name, $sub) = @_; *{$name} = sub { splice @_, 1, 0, (my $cv = AnyEvent->condvar); &$sub; $cv }; *{"$name\_sync"} = sub { splice @_, 1, 0, (my $cv = AnyEvent->condvar); &$sub; $cv->recv }; } _txn list_peers => sub { my ($self, $cv, $with_metadata, $with_volatile) = @_; my @res; $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) = @_; if ($type eq "end_list_peers") { $cv->(\@res); 1 } else { push @res, $kv; 0 } }, ); }; _txn list_peer_notes => sub { my ($self, $cv, $node_identifier) = @_; $self->send_msg (list_peer_notes => node_identifier => $node_identifier, id_cb => sub { my ($self, $type, $kv, $rdata) = @_; $cv->($kv); 1 }, ); }; _txn watch_global => sub { my ($self, $cv, $enabled, $verbosity_mask) = @_; $self->send_msg (watch_global => enabled => $enabled ? "true" : "false", defined $verbosity_mask ? (verbosity_mask => $verbosity_mask+0) : (), ); $cv->(); }; _txn list_persistent_requests => sub { my ($self, $cv) = @_; my %res; $self->send_msg ("list_persistent_requests"); push @{ $self->{queue} }, sub { my ($self, $type, $kv, $rdata) = @_; if ($type eq "end_list_persistent_requests") { $cv->(\%res); 1 } else { my $id = $kv->{identifier}; 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; } 0 } }; }; _txn remove_request => sub { my ($self, $cv, $global, $identifier) = @_; $self->send_msg (remove_request => global => $global ? "true" : "false", identifier => $identifier, id_cb => sub { my ($self, $type, $kv, $rdata) = @_; $cv->($kv); 1 }, ); $cv->(); }; _txn modify_persistent_request => sub { my ($self, $cv, $global, $identifier, $client_token, $priority_class) = @_; $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) : (), id_cb => sub { my ($self, $type, $kv, $rdata) = @_; $cv->($kv); 1 }, ); $cv->(); }; _txn get_plugin_info => sub { my ($self, $cv, $name, $detailed) = @_; $self->send_msg (get_plugin_info => plugin_name => $name, detailed => $detailed ? "true" : "false", id_cb => sub { my ($self, $type, $kv, $rdata) = @_; $cv->($kv); 1 }, ); $cv->(); }; =back =head1 SEE ALSO L, L. =head1 BUGS =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ =cut 1