ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FCP/eg/f7progress
Revision: 1.1
Committed: Sat Jun 5 14:49:25 2010 UTC (14 years ago) by root
Branch: MAIN
CVS Tags: rel-0_3
Log Message:
0.3

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3     # displays, via curses, progress messages by downloaded file
4    
5     use common::sense;
6     use POSIX ();
7     use Curses;
8     use AnyEvent;
9     use AnyEvent::FCP;
10    
11     my $hst = "10.0.0.17";
12     my $cnt = 20;
13     my $log = 15;
14    
15     initscr;
16     curs_set 0;
17    
18     addstr 0, 0, "waiting for progress messages...";
19     refresh;
20    
21     my %ID;
22     my $updater;
23     my @log;
24    
25     sub updater {
26     undef $updater;
27    
28     erase;
29    
30     my @id = sort { $b->[1] <=> $a->[1] } values %ID;
31    
32     delete $ID{(pop @id)->[0]}
33     while @id > $cnt;
34    
35     my $y = 0;
36     for (@id) {
37     addstr $y++, 0, sprintf "%s %5d", $_->[2], AE::now - $_->[1];
38     }
39    
40     $y++;
41    
42     shift @log
43     while @log > $log;
44    
45     addstr $y++, 0, $_
46     for @log;
47    
48     refresh;
49     }
50    
51     my $w = AE::timer 10, 10, \&updater;
52    
53     my $fcp = new AnyEvent::FCP host => $hst, progress => sub {
54     my ($fcp, $type, $kv, $rdata) = @_;
55    
56     delete $kv->{pkt_type};
57     (my $id = delete $kv->{identifier}) =~ s/^FProxy://;
58    
59     if ($type eq "simple_progress") {
60     my $progress = sprintf "%5d / %5d, %3d%%", $kv->{succeeded}, $kv->{required}, 100 * $kv->{succeeded} / $kv->{required};
61     $progress = $kv->{finalized_total} eq "true" ? " $progress " : "($progress)";
62     my $progress = sprintf "%-60.60s %s", $id, $progress;
63     $ID{$id} = [$id, AE::now, $progress];
64    
65     $updater ||= AE::idle \&updater;
66     } elsif ($type !~ /^(?:sending_to_network|persistent_get|persistent_request_modified)$/) {
67     my $line = sprintf "%s %-25.25s %-40.40s %-80.80s",
68     (POSIX::strftime "%H:%M:%S", localtime AE::now),
69     $type, $id, join " ", %$kv;
70     push @log, $line;
71     $updater ||= AE::idle \&updater;
72     }
73     };
74    
75     $fcp->watch_global (1, 0);
76    
77     AE::cv->recv;