Included is a patch to get the current number of requests a user agent
is processing.

Only marginally tested, and I'm not sure the name is good or that the
calling convention is (but the interface works for what I needed it for).

 - ask

ps. is this in a public CVS somewhere?  Rocco, if not and if you would
like it to be then we can host it at cvs.perl.org. (Or in Subversion
if you'd prefer that).


diff -ur POE-Component-Client-HTTP-0.54/HTTP.pm POE-Component-Client-HTTP/HTTP.pm
--- POE-Component-Client-HTTP-0.54/HTTP.pm      Sun Jun 29 20:06:40 2003
+++ POE-Component-Client-HTTP/HTTP.pm   Wed Jul 23 16:35:27 2003
@@ -177,6 +177,7 @@
 
         # Public interface.
         request => \&poco_weeble_request,
+       pending_requests_count => \&poco_weeble_pending_requests_count,
 
         # Net::DNS interface.
         got_dns_response  => \&poco_weeble_dns_answer,
@@ -246,6 +247,12 @@
   DEBUG and warn "weeble stopped.\n";
 }
 
+sub poco_weeble_pending_requests_count {
+  my ($heap) = $_[HEAP];
+  my $r = $heap->{request} || {};
+  return keys %$r;
+}
+
 #------------------------------------------------------------------------------
 
 sub poco_weeble_request {
@@ -1219,6 +1226,11 @@
 
 Please see the HTTP::Request and HTTP::Response manpages for more
 information.
+
+There's also a pending_requests_count state that returns the number of
+requests currently being processed.
+
+  my $count = $kernel->call('ua' => 'pending_requests_count');
 
 The example progress handler shows how to calculate a percentage of
 download completion.
diff -ur POE-Component-Client-HTTP-0.54/t/01_request.t 
POE-Component-Client-HTTP/t/01_request.t
--- POE-Component-Client-HTTP-0.54/t/01_request.t       Fri Mar  7 20:14:55 2003
+++ POE-Component-Client-HTTP/t/01_request.t    Wed Jul 23 16:32:25 2003
@@ -15,10 +15,10 @@
 sub MAX_STREAM_CHUNK_SIZE () { 1024 }  # Needed for agreement with test CGI.
 
 $| = 1;
-print "1..8\n";
+print "1..9\n";
 
 my @test_results = ( 'not ok 1', 'not ok 2', 'not ok 3', 'not ok 4',
-                     'ok 5', 'not ok 6', 'not ok 7', 'not ok 8',
+                     'ok 5', 'not ok 6', 'not ok 7', 'not ok 8', 'not ok 9',
                    );
 
 BEGIN {
@@ -79,6 +79,17 @@
   $kernel->post( streamer => request => got_stream_response =>
                  GET 'http://poe.perl.org/misc/stream-test.cgi'
                );
+
+  $kernel->yield('check_counts', 9, (HAS_SSL ? 7 : 6));
+
+}
+
+sub client_check_counts {
+  my ($kernel, $test_number, $expected_count) = @_[KERNEL, ARG0, ARG1];
+  # a better test would be to also keep track of the responses we are
+  # receiving and checking that pending_requests_count decrements properly.
+  my $count = $kernel->call( weeble => 'pending_requests_count' );
+  $test_results[$test_number-1] = "ok $test_number" if $expected_count == $count;
 }
 
 sub client_stop {
@@ -89,7 +100,7 @@
 }
 
 sub client_got_response {
-  my ($heap, $request_packet, $response_packet) = @_[HEAP, ARG0, ARG1];
+  my ($heap, $kernel, $request_packet, $response_packet) = @_[HEAP, KERNEL, ARG0, 
ARG1];
   my $http_request  = $request_packet->[0];
   my $http_response = $response_packet->[0];
 
@@ -220,6 +231,7 @@
       got_response        => \&client_got_response,
       got_big_response    => \&client_got_big_response,
       got_stream_response => \&client_got_stream_response,
+      check_counts        => \&client_check_counts,
     }
   );
 


-- 
ask bjoern hansen, http://www.askbjoernhansen.com/ !try; do();

Reply via email to