ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-FCP/FCP.pm
(Generate patch)

Comparing cvsroot/Net-FCP/FCP.pm (file contents):
Revision 1.34 by root, Wed Jan 12 20:37:33 2005 UTC vs.
Revision 1.41 by root, Thu May 1 15:30:15 2008 UTC

11 my $ni = $fcp->txn_node_info->result; 11 my $ni = $fcp->txn_node_info->result;
12 my $ni = $fcp->node_info; 12 my $ni = $fcp->node_info;
13 13
14=head1 DESCRIPTION 14=head1 DESCRIPTION
15 15
16This module implements the first version of the freenet client protocol,
17for use with freenet versions 0.5. For freenet protocol version 2.0
18support (as used by freenet 0.7), see the L<AnyEvent::FCP> module.
19
16See L<http://freenet.sourceforge.net/index.php?page=fcp> for a description 20See L<http://freenet.sourceforge.net/index.php?page=fcp> for a description
17of what the messages do. I am too lazy to document all this here. 21of what the messages do.
18 22
19=head1 WARNING 23The module uses L<AnyEvent> to find a suitable Event module.
20
21This module is alpha. While it probably won't destroy (much :) of your
22data, it currently falls short of what it should provide (intelligent uri
23following, splitfile downloads, healing...)
24 24
25=head2 IMPORT TAGS 25=head2 IMPORT TAGS
26 26
27Nothing much can be "imported" from this module right now. There are, 27Nothing much can be "imported" from this module right now.
28however, certain "import tags" that can be used to select the event model
29to be used.
30
31Event models are implemented as modules under the C<Net::FCP::Event::xyz>
32class, where C<xyz> is the event model to use. The default is C<Event> (or
33later C<Auto>).
34
35The import tag to use is named C<event=xyz>, e.g. C<event=Event>,
36C<event=Glib> etc.
37
38You should specify the event module to use only in the main program.
39
40If no event model has been specified, FCP tries to autodetect it on first
41use (e.g. first transaction), in this order: Coro, Event, Glib, Tk.
42 28
43=head2 FREENET BASICS 29=head2 FREENET BASICS
44 30
45Ok, this section will not explain any freenet basics to you, just some 31Ok, this section will not explain any freenet basics to you, just some
46problems I found that you might want to avoid: 32problems I found that you might want to avoid:
72 58
73package Net::FCP; 59package Net::FCP;
74 60
75use Carp; 61use Carp;
76 62
77$VERSION = 0.7; 63$VERSION = '1.2';
78 64
79no warnings; 65no warnings;
66
67use AnyEvent;
80 68
81use Net::FCP::Metadata; 69use Net::FCP::Metadata;
82use Net::FCP::Util qw(tolc touc xeh); 70use Net::FCP::Util qw(tolc touc xeh);
83
84our $EVENT = Net::FCP::Event::Auto::;
85
86sub import {
87 shift;
88
89 for (@_) {
90 if (/^event=(\w+)$/) {
91 $EVENT = "Net::FCP::Event::$1";
92 eval "require $EVENT";
93 }
94 }
95 die $@ if $@;
96}
97 71
98=item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] 72=item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb]
99 73
100Create a new virtual FCP connection to the given host and port (default 74Create a new virtual FCP connection to the given host and port (default
101127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). 75127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>).
406 380
407sub new { 381sub new {
408 my $class = shift; 382 my $class = shift;
409 my $self = bless { @_ }, $class; 383 my $self = bless { @_ }, $class;
410 384
411 $self->{signal} = $EVENT->new_signal; 385 $self->{signal} = AnyEvent->condvar;
412 386
413 $self->{fcp}{txn}{$self} = $self; 387 $self->{fcp}{txn}{$self} = $self;
414 388
415 my $attr = ""; 389 my $attr = "";
416 my $data = delete $self->{attr}{data}; 390 my $data = delete $self->{attr}{data};
428 402
429 socket my $fh, PF_INET, SOCK_STREAM, 0 403 socket my $fh, PF_INET, SOCK_STREAM, 0
430 or Carp::croak "unable to create new tcp socket: $!"; 404 or Carp::croak "unable to create new tcp socket: $!";
431 binmode $fh, ":raw"; 405 binmode $fh, ":raw";
432 fcntl $fh, F_SETFL, O_NONBLOCK; 406 fcntl $fh, F_SETFL, O_NONBLOCK;
433 connect $fh, (sockaddr_in $self->{fcp}{port}, inet_aton $self->{fcp}{host}) 407 connect $fh, (sockaddr_in $self->{fcp}{port}, inet_aton $self->{fcp}{host});
434 and !$!{EWOULDBLOCK}
435 and !$!{EINPROGRESS}
436 and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; 408# and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n";
437 409
438 $self->{sbuf} = 410 $self->{sbuf} =
439 "\x00\x00\x00\x02" 411 "\x00\x00\x00\x02"
440 . (Net::FCP::touc $self->{type}) 412 . (Net::FCP::touc $self->{type})
441 . "\012$attr$data"; 413 . "\012$attr$data";
442 414
443 #shutdown $fh, 1; # freenet buggy?, well, it's java... 415 #shutdown $fh, 1; # freenet buggy?, well, it's java...
444 416
445 $self->{fh} = $fh; 417 $self->{fh} = $fh;
446 418
447 $self->{w} = $EVENT->new_from_fh ($fh) 419 $self->{w} = AnyEvent->io (fh => $fh, poll => 'w', cb => sub { $self->fh_ready_w });
448 ->cb (sub { $self->fh_ready_w })
449 ->poll (0, 1, 1);
450 420
451 $self; 421 $self;
452} 422}
453 423
454=item $txn = $txn->cb ($coderef) 424=item $txn = $txn->cb ($coderef)
513 483
514 if ($len > 0) { 484 if ($len > 0) {
515 substr $self->{sbuf}, 0, $len, ""; 485 substr $self->{sbuf}, 0, $len, "";
516 unless (length $self->{sbuf}) { 486 unless (length $self->{sbuf}) {
517 fcntl $self->{fh}, F_SETFL, 0; 487 fcntl $self->{fh}, F_SETFL, 0;
518 $self->{w}->cb(sub { $self->fh_ready_r })->poll (1, 0, 1); 488 $self->{w} = AnyEvent->io (fh => $self->{fh}, poll => 'r', cb => sub { $self->fh_ready_r });
519 } 489 }
520 } elsif (defined $len) { 490 } elsif (defined $len) {
521 $self->throw (Net::FCP::Exception->new (network_error => { reason => "unexpected end of file while writing" })); 491 $self->throw (Net::FCP::Exception->new (network_error => { reason => "unexpected end of file while writing" }));
522 } else { 492 } else {
523 $self->throw (Net::FCP::Exception->new (network_error => { reason => "$!" })); 493 $self->throw (Net::FCP::Exception->new (network_error => { reason => "$!" }));
525} 495}
526 496
527sub fh_ready_r { 497sub fh_ready_r {
528 my ($self) = @_; 498 my ($self) = @_;
529 499
530 if (sysread $self->{fh}, $self->{buf}, 65536, length $self->{buf}) { 500 if (sysread $self->{fh}, $self->{buf}, 16384 + 1024, length $self->{buf}) {
531 for (;;) { 501 for (;;) {
532 if ($self->{datalen}) { 502 if ($self->{datalen}) {
533 #warn "expecting new datachunk $self->{datalen}, got ".(length $self->{buf})."\n";#d# 503 #warn "expecting new datachunk $self->{datalen}, got ".(length $self->{buf})."\n";#d#
534 if (length $self->{buf} >= $self->{datalen}) { 504 if (length $self->{buf} >= $self->{datalen}) {
535 $self->rcv_data (substr $self->{buf}, 0, delete $self->{datalen}, ""); 505 $self->rcv_data (substr $self->{buf}, 0, delete $self->{datalen}, "");
588 my ($self, $result) = @_; 558 my ($self, $result) = @_;
589 559
590 unless (exists $self->{result}) { 560 unless (exists $self->{result}) {
591 $self->{result} = $result; 561 $self->{result} = $result;
592 $self->{cb}->($self) if exists $self->{cb}; 562 $self->{cb}->($self) if exists $self->{cb};
593 $self->{signal}->send; 563 $self->{signal}->broadcast;
594 } 564 }
595} 565}
596 566
597sub eof { 567sub eof {
598 my ($self) = @_; 568 my ($self) = @_;
838 808
839=head1 BUGS 809=head1 BUGS
840 810
841=head1 AUTHOR 811=head1 AUTHOR
842 812
843 Marc Lehmann <pcg@goof.com> 813 Marc Lehmann <schmorp@schmorp.de>
844 http://home.schmorp.de/ 814 http://home.schmorp.de/
845 815
846=cut 816=cut
847 817
848package Net::FCP::Event::Auto; 8181
849 819
850my @models = (
851 [Coro => Coro::Event::],
852 [Event => Event::],
853 [Glib => Glib::],
854 [Tk => Tk::],
855);
856
857sub AUTOLOAD {
858 $AUTOLOAD =~ s/.*://;
859
860 for (@models) {
861 my ($model, $package) = @$_;
862 if (defined ${"$package\::VERSION"}) {
863 $EVENT = "Net::FCP::Event::$model";
864 eval "require $EVENT"; die if $@;
865 goto &{"$EVENT\::$AUTOLOAD"};
866 }
867 }
868
869 for (@models) {
870 my ($model, $package) = @$_;
871 $EVENT = "Net::FCP::Event::$model";
872 if (eval "require $EVENT") {
873 goto &{"$EVENT\::$AUTOLOAD"};
874 }
875 }
876
877 die "No event module selected for Net::FCP and autodetect failed. Install any of these: Coro, Event, Glib or Tk.";
878}
879
8801;
881

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines