--- cvsroot/Net-FCP/FCP.pm 2003/09/07 23:23:56 1.4 +++ cvsroot/Net-FCP/FCP.pm 2003/09/09 06:22:58 1.10 @@ -19,8 +19,23 @@ =head1 WARNING This module is alpha. While it probably won't destroy (much :) of your -data, it currently works only with the Event module (alkthough the event -mechanism is fully pluggable). +data, it currently falls short of what it should provide (intelligent uri +following, splitfile downloads, healing...) + +=head2 IMPORT TAGS + +Nothing much can be "imported" from this module right now. There are, +however, certain "import tags" that can be used to select the event model +to be used. + +Event models are implemented as modules under the C +class, where C is the event model to use. The default is C (or +later C). + +The import tag to use is named C, e.g. C, +C etc. + +You should specify the event module to use only in the main program. =head2 THE Net::FCP CLASS @@ -33,34 +48,24 @@ use Carp; use IO::Socket::INET; -$VERSION = 0.02; +$VERSION = 0.05; -sub event_reg_cb { - my ($obj) = @_; - require Event; +no warnings; - $obj->{eventdata} = Event->io ( - fd => $obj->{fh}, - poll => 'r', - cb => sub { - $obj->fh_ready; - }, - ); -} +our $EVENT = Net::FCP::Event::Auto::; +$EVENT = Net::FCP::Event::Event;#d# -sub event_unreg_cb { - $_[0]{eventdata} - and (delete $_[0]{eventdata})->cancel; -} +sub import { + shift; -sub event_wait_cb { - Event::one_event(); + for (@_) { + if (/^event=(\w+)$/) { + $EVENT = "Net::FCP::Event::$1"; + } + } + eval "require $EVENT"; } -$regcb = \&event_reg_cb; -$unregcb = \&event_unreg_cb; -$waitcb = \&event_wait_cb; - sub touc($) { local $_ = shift; 1 while s/((?:^|_)(?:svk|chk|uri)(?:_|$))/\U$1/; @@ -74,10 +79,73 @@ lc $_; } +=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). + +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: + + ( + version => { revision => 1 }, + document => [ + { + "info.format" => "image/jpeg", + name => "background.jpg", + "redirect.target" => "freenet:CHK\@ZcagI,ra726bSw" + }, + { + "info.format" => "text/html", + name => ".next", + "redirect.target" => "freenet:SSK\@ilUPAgM/TFEE/3" + }, + { + "info.format" => "text/html", + "redirect.target" => "freenet:CHK\@8M8Po8ucwI,8xA" + } + ] + ) + +=cut + +sub parse_metadata { + my $meta; + + my $data = shift; + if ($data =~ /^Version\015?\012/gc) { + my $hdr = $meta->{version} = {}; + + for (;;) { + while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { + my ($k, $v) = ($1, $2); + $hdr->{tolc $k} = $v; + } + + if ($data =~ /\GEndPart\015?\012/gc) { + } elsif ($data =~ /\GEnd\015?\012/gc) { + last; + } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) { + push @{$meta->{tolc $1}}, $hdr = {}; + } elsif ($data =~ /\G(.*)/gcs) { + die "metadata format error ($1)"; + } + } + } + + #$meta->{tail} = substr $data, pos $data; + + $meta; +} + =item $fcp = new Net::FCP [host => $host][, port => $port] Create a new virtual FCP connection to the given host and port (default -127.0.0.1:8481). +127.0.0.1:8481, or the environment variables C and C). Connections are virtual because no persistent physical connection is established. However, the existance of the node is checked by executing a @@ -89,15 +157,20 @@ my $class = shift; my $self = bless { @_ }, $class; - $self->{host} ||= "127.0.0.1"; - $self->{port} ||= 8481; + $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; + $self->{port} ||= $ENV{FREDPORt} || 8481; - $self->{nodehello} = $self->txn("ClientHello")->result + $self->{nodehello} = $self->client_hello or croak "unable to get nodehello from node\n"; $self; } +sub progress { + my ($self, $txn, $type, $attr) = @_; + warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; +} + =item $txn = $fcp->txn(type => attr => val,...) The low-level interface to transactions. Don't use it. @@ -189,7 +262,7 @@ _txn generate_chk => sub { my ($self, $metadata, $data) = @_; - $self->txn (generate_chk => data => "$data$metadata", meta_data_length => length $metadata); + $self->txn (generate_chk => data => "$data$metadata", metadata_length => length $metadata); }; =item $txn = $fcp->txn_generate_svk_pair @@ -248,7 +321,31 @@ $self->txn (get_size => URI => $uri); }; -=item MISSING: ClientGet, ClientPut +=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) file from +freenet. C<$meta> is the metadata (as returned by C or +C). + +Due to the overhead, a better method to download big files should be used. + + my ($meta, $data) = @{ + $fcp->client_get ( + "freenet:CHK@hdXaxkwZ9rA8-SidT0AN-bniQlgPAwI,XdCDmBuGsd-ulqbLnZ8v~w" + ) + }; + +=cut + +_txn client_get => sub { + my ($self, $uri, $htl, $removelocal) = @_; + + $self->txn (client_get => URI => $uri, hops_to_live => ($htl || 15), remove_local => $removelocal*1); +}; + +=item MISSING: ClientPut =back @@ -317,11 +414,23 @@ $self->{fh} = $fh; - $Net::FCP::regcb->($self); + $EVENT->reg_r_cb ($self); $self; } +=item $userdata = $txn->userdata ([$userdata]) + +Get and/or set user-specific data. This is useful in progress callbacks. + +=cut + +sub userdata($;$) { + my ($self, $data) = @_; + $self->{userdata} = $data if @_ >= 2; + $self->{userdata}; +} + sub fh_ready { my ($self) = @_; @@ -329,13 +438,13 @@ for (;;) { if ($self->{datalen}) { if (length $self->{buf} >= $self->{datalen}) { - $self->recv_data (substr $self->{buf}, 0, $self->{datalen}, ""); + $self->rcv_data (substr $self->{buf}, 0, $self->{datalen}, ""); } else { last; } - } elsif ($self->{buf} =~ s/^DataChunk\015?\012Length=(\d+)\015?\012Data\015?\012//) { - $self->{datalen} = $1; - } elsif ($self->{buf} =~ s/^([a-zA-Z]+)\015?\012(.*?)\015?\012EndMessage\015?\012//s) { + } elsif ($self->{buf} =~ s/^DataChunk\015?\012Length=([0-9a-fA-F]+)\015?\012Data\015?\012//) { + $self->{datalen} = hex $1; + } 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 @@ -345,7 +454,7 @@ } } } else { - $Net::FCP::unregcb->($self); + $EVENT->unreg_r_cb ($self); delete $self->{fh}; $self->eof; } @@ -353,6 +462,10 @@ sub rcv_data { my ($self, $chunk) = @_; + + $self->{data} .= $chunk; + + $self->progress ("data", { chunk => length $chunk, total => length $self->{data}, end => $self->{datalength} }); } sub rcv { @@ -360,25 +473,43 @@ $type = Net::FCP::tolc $type; + #use PApp::Util; warn PApp::Util::dumpval [$type, $attr]; + if (my $method = $self->can("rcv_$type")) { $method->($self, $attr, $type); } else { warn "received unexpected reply type '$type' for '$self->{type}', ignoring\n"; - $self->eof; } } -sub eof { +sub throw { + my ($self, $exc) = @_; + + $self->{exception} = $exc; + $self->set_result (1); +} + +sub set_result { my ($self, $result) = @_; $self->{result} = $result unless exists $self->{result}; } +sub eof { + my ($self) = @_; + $self->set_result; +} + +sub progress { + my ($self, $type, $attr) = @_; + $self->{fcp}->progress ($self, $type, $attr); +} + =item $result = $txn->result Waits until a result is available and then returns it. -This waiting is (depending on your event modul) not very efficient, as it +This waiting is (depending on your event model) not very efficient, as it is done outside the "mainloop". =cut @@ -386,13 +517,16 @@ sub result { my ($self) = @_; - $Net::FCP::waitcb->() while !exists $self->{result}; + $EVENT->wait_event while !exists $self->{result}; + + die $self->{exception} if $self->{exception}; return $self->{result}; } sub DESTROY { - $Net::FCP::unregcb->($_[0]); + $EVENT->unreg_r_cb ($_[0]); + #$EVENT->unreg_w_cb ($_[0]); } package Net::FCP::Txn::ClientHello; @@ -402,7 +536,7 @@ sub rcv_node_hello { my ($self, $attr) = @_; - $self->eof ($attr); + $self->set_result ($attr); } package Net::FCP::Txn::ClientInfo; @@ -412,7 +546,7 @@ sub rcv_node_info { my ($self, $attr) = @_; - $self->eof ($attr); + $self->set_result ($attr); } package Net::FCP::Txn::GenerateCHK; @@ -422,7 +556,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->eof ($attr); + $self->set_result ($attr); } package Net::FCP::Txn::GenerateSVKPair; @@ -432,7 +566,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->eof ([$attr->{PublicKey}, $attr->{PrivateKey}]); + $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); } package Net::FCP::Txn::InvertPrivateKey; @@ -442,7 +576,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->eof ($attr->{PublicKey}); + $self->set_result ($attr->{PublicKey}); } package Net::FCP::Txn::GetSize; @@ -452,7 +586,65 @@ sub rcv_success { my ($self, $attr) = @_; - $self->eof ($attr->{Length}); + $self->set_result ($attr->{Length}); +} + +package Net::FCP::Txn::ClientGet; + +use base Net::FCP::Txn; + +sub rcv_data_found { + my ($self, $attr, $type) = @_; + + $self->progress ($type, $attr); + + $self->{datalength} = hex $attr->{data_length}; + $self->{metalength} = hex $attr->{metadata_length}; +} + +sub rcv_route_not_found { + my ($self, $attr, $type) = @_; + + $self->throw (new Net::FCP::Exception $type, $attr); +} + +sub rcv_data_not_found { + my ($self, $attr, $type) = @_; + + $self->throw (new Net::FCP::Exception $type, $attr); +} + +sub rcv_format_error { + my ($self, $attr, $type) = @_; + + $self->throw (new Net::FCP::Exception $type, $attr); +} + +sub rcv_restarted { + my ($self, $attr, $type) = @_; + $self->progress ($type, $attr); +} + +sub eof { + my ($self) = @_; + + my $data = delete $self->{data}; + my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; + + $self->set_result ([$meta, $data]); +} + +package Net::FCP::Exception; + +use overload + '""' => sub { + "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; + }; + +sub new { + my ($class, $type, $attr) = @_; + + bless [$type, { %$attr }], $class; } =back