--- cvsroot/Net-FCP/FCP.pm 2005/01/12 20:37:33 1.34 +++ cvsroot/Net-FCP/FCP.pm 2008/05/01 15:30:15 1.41 @@ -13,32 +13,18 @@ =head1 DESCRIPTION -See L for a description -of what the messages do. I am too lazy to document all this here. +This module implements the first version of the freenet client protocol, +for use with freenet versions 0.5. For freenet protocol version 2.0 +support (as used by freenet 0.7), see the L module. -=head1 WARNING +See L for a description +of what the messages do. -This module is alpha. While it probably won't destroy (much :) of your -data, it currently falls short of what it should provide (intelligent uri -following, splitfile downloads, healing...) +The module uses L to find a suitable Event module. =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. - -If no event model has been specified, FCP tries to autodetect it on first -use (e.g. first transaction), in this order: Coro, Event, Glib, Tk. +Nothing much can be "imported" from this module right now. =head2 FREENET BASICS @@ -74,27 +60,15 @@ use Carp; -$VERSION = 0.7; +$VERSION = '1.2'; no warnings; +use AnyEvent; + use Net::FCP::Metadata; use Net::FCP::Util qw(tolc touc xeh); -our $EVENT = Net::FCP::Event::Auto::; - -sub import { - shift; - - for (@_) { - if (/^event=(\w+)$/) { - $EVENT = "Net::FCP::Event::$1"; - eval "require $EVENT"; - } - } - die $@ if $@; -} - =item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] Create a new virtual FCP connection to the given host and port (default @@ -408,7 +382,7 @@ my $class = shift; my $self = bless { @_ }, $class; - $self->{signal} = $EVENT->new_signal; + $self->{signal} = AnyEvent->condvar; $self->{fcp}{txn}{$self} = $self; @@ -430,10 +404,8 @@ 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 !$!{EWOULDBLOCK} - and !$!{EINPROGRESS} - and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; + 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" @@ -444,9 +416,7 @@ $self->{fh} = $fh; - $self->{w} = $EVENT->new_from_fh ($fh) - ->cb (sub { $self->fh_ready_w }) - ->poll (0, 1, 1); + $self->{w} = AnyEvent->io (fh => $fh, poll => 'w', cb => sub { $self->fh_ready_w }); $self; } @@ -515,7 +485,7 @@ 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); + $self->{w} = AnyEvent->io (fh => $self->{fh}, poll => 'r', cb => sub { $self->fh_ready_r }); } } elsif (defined $len) { $self->throw (Net::FCP::Exception->new (network_error => { reason => "unexpected end of file while writing" })); @@ -527,7 +497,7 @@ sub fh_ready_r { my ($self) = @_; - if (sysread $self->{fh}, $self->{buf}, 65536, length $self->{buf}) { + 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# @@ -590,7 +560,7 @@ unless (exists $self->{result}) { $self->{result} = $result; $self->{cb}->($self) if exists $self->{cb}; - $self->{signal}->send; + $self->{signal}->broadcast; } } @@ -840,42 +810,10 @@ =head1 AUTHOR - Marc Lehmann + Marc Lehmann http://home.schmorp.de/ =cut -package Net::FCP::Event::Auto; - -my @models = ( - [Coro => Coro::Event::], - [Event => Event::], - [Glib => Glib::], - [Tk => Tk::], -); - -sub AUTOLOAD { - $AUTOLOAD =~ s/.*://; - - for (@models) { - my ($model, $package) = @$_; - if (defined ${"$package\::VERSION"}) { - $EVENT = "Net::FCP::Event::$model"; - eval "require $EVENT"; die if $@; - goto &{"$EVENT\::$AUTOLOAD"}; - } - } - - for (@models) { - my ($model, $package) = @$_; - $EVENT = "Net::FCP::Event::$model"; - if (eval "require $EVENT") { - goto &{"$EVENT\::$AUTOLOAD"}; - } - } - - die "No event module selected for Net::FCP and autodetect failed. Install any of these: Coro, Event, Glib or Tk."; -} - -1; +1