ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/samples/EVQ.pm
Revision: 1.1
Committed: Sun Jul 8 20:56:01 2007 UTC (18 years, 11 months ago) by elmex
Branch: MAIN
Log Message:
added some more examples  ... err.. utilities

File Contents

# User Rev Content
1 elmex 1.1 package EVQ;
2     use strict;
3     use AnyEvent;
4    
5     my $J;
6    
7     our %reqh;
8     our $id = 0;
9     my @req;
10    
11     sub schedule {
12     my $reqcnt = scalar (keys %reqh);
13     if ($reqcnt == 0 && !@req) {
14     print "no more jobs, finishing...\n";
15     $J->broadcast;
16     }
17     while ($reqcnt < 20) {
18     my $r = pop @req;
19     return unless defined $r;
20     $r->[0]->(addreq ($r->[1]));
21     $reqcnt = scalar (keys %reqh);
22     }
23     }
24    
25     sub addreq { my $k = $id . "_" . $_[0]; $reqh{$k} = 1; $id++; $k }
26     sub finreq { delete $reqh{$_[0]}; }
27    
28     sub push_request {
29     my ($s, $cb) = @_;
30     push @req, [$cb, $s];
31     schedule;
32     }
33    
34     our $t;
35     sub timer {
36     $t = AnyEvent->timer (after => 2, cb => sub {
37     schedule;
38     my $reqcnt = scalar (keys %reqh);
39     $reqcnt += @req;
40     print "$reqcnt outstanding requests\n";
41     timer ();
42     });
43     }
44    
45     sub start {
46     $J = AnyEvent->condvar;
47     timer;
48     }
49     sub wait {
50     $J->wait;
51     }
52    
53     1