--- cvsroot/Net-FCP/FCP.pm 2003/09/08 00:36:44 1.6 +++ cvsroot/Net-FCP/FCP.pm 2003/09/10 05:14:03 1.15 @@ -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 @@ -31,35 +46,25 @@ package Net::FCP; use Carp; -use IO::Socket::INET; -$VERSION = 0.03; +$VERSION = 0.06; -sub event_reg_cb { - my ($obj) = @_; - require Event; - - $obj->{eventdata} = Event->io ( - fd => $obj->{fh}, - poll => 'r', - cb => sub { - $obj->fh_ready; - }, - ); -} +no warnings; -sub event_unreg_cb { - $_[0]{eventdata} - and (delete $_[0]{eventdata})->cancel; -} +our $EVENT = Net::FCP::Event::Auto::; +$EVENT = Net::FCP::Event::Event;#d# -sub event_wait_cb { - Event::one_event(); -} +sub import { + shift; -$regcb = \&event_reg_cb; -$unregcb = \&event_unreg_cb; -$waitcb = \&event_wait_cb; + for (@_) { + if (/^event=(\w+)$/) { + $EVENT = "Net::FCP::Event::$1"; + } + } + eval "require $EVENT"; + die $@ if $@; +} sub touc($) { local $_ = shift; @@ -74,6 +79,75 @@ 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); + my @p = split /\./, tolc $k, 3; + + $hdr->{$p[0]} = $v if @p == 1; # lamest code I ever wrote + $hdr->{$p[0]}{$p[1]} = $v if @p == 2; + $hdr->{$p[0]}{$p[1]}{$p[2]} = $v if @p == 3; + die "FATAL: 4+ dot metadata" if @p >= 4; + } + + if ($data =~ /\GEndPart\015?\012/gc) { + # nop + } 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 @@ -90,18 +164,45 @@ my $self = bless { @_ }, $class; $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; - $self->{port} ||= $ENV{FREDPORt} || 8481; + $self->{port} ||= $ENV{FREDPORT} || 8481; - $self->{nodehello} = $self->client_hello - or croak "unable to get nodehello from node\n"; + #$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. +Here are some examples of using transactions: + +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: + + my $nodehello = $fcp->txn_client_hello->result; + +Setting callbacks: + + $fcp->txn_client_hello->cb( + sub { my $nodehello => $_[0]->result } + ); + =cut sub txn { @@ -189,7 +290,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 @@ -250,13 +351,15 @@ =item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) -=item ($data, $metadata) = @{ $fcp->client_get ($uri, $htl, $removelocal) +=item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) -Fetches a (small, as it should fit into memory) file from freenet. +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 fiels should be used. +Due to the overhead, a better method to download big files should be used. - my ($data, $meta) = @{ + my ($meta, $data) = @{ $fcp->client_get ( "freenet:CHK@hdXaxkwZ9rA8-SidT0AN-bniQlgPAwI,XdCDmBuGsd-ulqbLnZ8v~w" ) @@ -267,7 +370,7 @@ _txn client_get => sub { my ($self, $uri, $htl, $removelocal) = @_; - $self->txn (client_get => URI => $uri, hops_to_live => ($htl || 15), remove_local => $removelocal*1); + $self->txn (client_get => URI => $uri, hops_to_live => ($htl || 15), remove_local_key => $removelocal ? "true" : "false"); }; =item MISSING: ClientPut @@ -291,6 +394,9 @@ package Net::FCP::Txn; +use Fcntl; +use Socket; + =item new arg => val,... Creates a new C object. Not normally used. @@ -301,6 +407,10 @@ my $class = shift; my $self = bless { @_ }, $class; + $self->{signal} = $EVENT->new_signal; + + $self->{fcp}{txn}{$self} = $self; + my $attr = ""; my $data = delete $self->{attr}{data}; @@ -315,49 +425,102 @@ $data = "EndMessage\012"; } - my $fh = new IO::Socket::INET - PeerHost => $self->{fcp}{host}, - PeerPort => $self->{fcp}{port} - or Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; - + socket my $fh, PF_INET, SOCK_STREAM, 0 + or Carp::croak "unable to create new tcp socket: $!"; binmode $fh, ":raw"; - - if (0) { - print - Net::FCP::touc $self->{type}, "\012", - $attr, - $data, "\012"; - } - - print $fh - "\x00\x00", "\x00\x02", # SESSID, PRESID - Net::FCP::touc $self->{type}, "\012", - $attr, - $data; + fcntl $fh, F_SETFL, O_NONBLOCK; + connect $fh, (sockaddr_in $self->{fcp}{port}, inet_aton $self->{fcp}{host}) + and !$!{EWOULDBLOCK} + and !$!{EINPROGRESS} + 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"; #$fh->shutdown (1); # freenet buggy?, well, it's java... $self->{fh} = $fh; - $Net::FCP::regcb->($self); + $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); $self; } -sub fh_ready { +=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($$) { + 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} >>. + +Returns the txn object, useful for chaining. + +=cut + +sub userdata($$) { + my ($self, $data) = @_; + $self->{userdata} = $data; + $self; +} + +sub fh_ready_w { + 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}->cb(sub { $self->fh_ready_r })->poll (1, 0, 1); + } + } 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 (sysread $self->{fh}, $self->{buf}, 65536, 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, $self->{datalen}, ""); + $self->rcv_data (substr $self->{buf}, 0, delete $self->{datalen}, ""); } else { last; } } 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?\012EndMessage\015?\012//s) { + #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 @@ -367,8 +530,6 @@ } } } else { - $Net::FCP::unregcb->($self); - delete $self->{fh}; $self->eof; } } @@ -377,6 +538,8 @@ my ($self, $chunk) = @_; $self->{data} .= $chunk; + + $self->progress ("data", { chunk => length $chunk, total => length $self->{data}, end => $self->{datalength} }); } sub rcv { @@ -393,15 +556,47 @@ } } +# 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 (1); + $self->eof; # must be last to avoid loops +} + sub set_result { my ($self, $result) = @_; - $self->{result} = $result unless exists $self->{result}; + unless (exists $self->{result}) { + $self->{result} = $result; + $self->{cb}->($self) if exists $self->{cb}; + $self->{signal}->send; + } } sub eof { my ($self) = @_; - $self->set_result; + + delete $self->{w}; + delete $self->{fh}; + + delete $self->{fcp}{txn}{$self}; + + $self->set_result; # just in case +} + +sub progress { + my ($self, $type, $attr) = @_; + $self->{fcp}->progress ($self, $type, $attr); } =item $result = $txn->result @@ -416,13 +611,11 @@ sub result { my ($self) = @_; - $Net::FCP::waitcb->() while !exists $self->{result}; + $self->{signal}->wait while !exists $self->{result}; - return $self->{result}; -} + die $self->{exception} if $self->{exception}; -sub DESTROY { - $Net::FCP::unregcb->($_[0]); + return $self->{result}; } package Net::FCP::Txn::ClientHello; @@ -485,25 +678,85 @@ $self->set_result ($attr->{Length}); } -package Net::FCP::Txn::ClientGet; +package Net::FCP::Txn::GetPut; + +# base class for get and put use base Net::FCP::Txn; +*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) = @_; + + delete $self->{datalength}; + delete $self->{metalength}; + delete $self->{data}; + + $self->progress ($type, $attr); +} + +package Net::FCP::Txn::ClientGet; + +use base Net::FCP::Txn::GetPut; + +*rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; + sub rcv_data_found { - my ($self, $attr) = @_; + my ($self, $attr, $type) = @_; + + $self->progress ($type, $attr); $self->{datalength} = hex $attr->{data_length}; - $self->{metalength} = hex $attr->{meta_data_length}; + $self->{metalength} = hex $attr->{metadata_length}; } sub eof { my ($self) = @_; - #use PApp::Util; warn PApp::Util::dumpval $self; - my $data = delete $self->{data}; - $self->set_result ([ - (substr $data, 0, $self->{datalength}-$self->{metalength}), - (substr $data, $self->{datalength}-$self->{metalength}), - ]); + + if ($self->{datalength} == length $self->{data}) { + my $data = delete $self->{data}; + my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; + + $self->set_result ([$meta, $data]); + } elsif (!exists $self->{result}) { + $self->throw (Net::FCP::Exception->new (short_data => { + reason => "unexpected eof or internal node error", + received => length $self->{data}, + expected => $self->{datalength}, + })); + } +} + +package Net::FCP::Txn::ClientPut; + +use base Net::FCP::Txn::GetPut; + +*rcv_size_error = \&Net::FCP::Txn::rcv_throw_exception; +*rcv_key_collision = \&Net::FCP::Txn::rcv_throw_exception; + +sub rcv_pending { + my ($self, $attr, $type) = @_; + $self->progress ($type, $attr); +} + +sub rcv_success { + my ($self, $attr, $type) = @_; + $self->set_result ($attr); +} + +package Net::FCP::Exception; + +use overload + '""' => sub { + "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; + }; + +sub new { + my ($class, $type, $attr) = @_; + + bless [Net::FCP::tolc $type, { %$attr }], $class; } =back