home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / CGI / Push.pm < prev    next >
Text File  |  1997-08-16  |  7KB  |  241 lines

  1. package CGI::Push;
  2.  
  3. # See the bottom of this file for the POD documentation.  Search for the
  4. # string '=head'.
  5.  
  6. # You can run this file through either pod2man or pod2html to produce pretty
  7. # documentation in manual or html file format (these utilities are part of the
  8. # Perl 5 distribution).
  9.  
  10. # Copyright 1995,1996, Lincoln D. Stein.  All rights reserved.
  11. # It may be used and modified freely, but I do request that this copyright
  12. # notice remain attached to the file.  You may modify this module as you 
  13. # wish, but if you redistribute a modified version, please attach a note
  14. # listing the modifications you have made.
  15.  
  16. # The most recent version and complete docs are available at:
  17. #   http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html
  18. #   ftp://ftp-genome.wi.mit.edu/pub/software/WWW/
  19.  
  20. $CGI::Push::VERSION='1.00';
  21. use CGI;
  22. @ISA = ('CGI');
  23.  
  24. # add do_push() to exported tags
  25. push(@{$CGI::EXPORT_TAGS{':standard'}},'do_push');
  26.  
  27. sub do_push {
  28.     my ($self,@p) = CGI::self_or_CGI(@_);
  29.  
  30.     # unbuffer output
  31.     $| = 1;
  32.     srand;
  33.     my ($random) = rand()*1E16;
  34.     my ($boundary) = "----------------------------------$random";
  35.  
  36.     my (@header);
  37.     my ($type,$callback,$delay,$last_page,$cookie,$target,$expires,@other) =
  38.     $self->rearrange([TYPE,NEXT_PAGE,DELAY,LAST_PAGE,[COOKIE,COOKIES],TARGET,EXPIRES],@p);
  39.     $type = 'text/html' unless $type;
  40.     $callback = \&simple_counter unless $callback && ref($callback) eq 'CODE';
  41.     $delay = 1 unless defined($delay);
  42.  
  43.     my(@o);
  44.     foreach (@other) { push(@o,split("=")); }
  45.     push(@o,'-Target'=>$target) if defined($target);
  46.     push(@o,'-Cookie'=>$cookie) if defined($cookie);
  47.     push(@o,'-Type'=>"multipart/x-mixed-replace; boundary=$boundary");
  48.     push(@o,'-Server'=>"CGI.pm Push Module");
  49.     push(@o,'-Status'=>'200 OK');
  50.     push(@o,'-nph'=>1);
  51.     print $self->header(@o);
  52.     print "${boundary}$CGI::CRLF";
  53.     
  54.     # now we enter a little loop
  55.     my @contents;
  56.     while (1) {
  57.     last unless (@contents = &$callback($self,++$COUNTER)) && defined($contents[0]);
  58.     print "Content-type: ${type}$CGI::CRLF$CGI::CRLF";
  59.     print @contents,"$CGI::CRLF";
  60.     print "${boundary}$CGI::CRLF";
  61.     $_ = <STDIN> if ($^O eq 'MacOS');    # MacOS Push hack
  62.     do_sleep($delay) if $delay;
  63.     }
  64.     print "Content-type: ${type}$CGI::CRLF$CGI::CRLF",
  65.           &$last_page($self,++$COUNTER),
  66.           "$CGI::CRLF${boundary}$CGI::CRLF"
  67.           if $last_page && ref($last_page) eq 'CODE';
  68. }
  69.  
  70. sub simple_counter {
  71.     my ($self,$count) = @_;
  72.     return (
  73.         CGI->start_html("CGI::Push Default Counter"),
  74.         CGI->h1("CGI::Push Default Counter"),
  75.         "This page has been updated ",CGI->strong($count)," times.",
  76.         CGI->hr(),
  77.         CGI->a({'-href'=>'http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html'},'CGI.pm home page'),
  78.         CGI->end_html
  79.         );
  80. }
  81.  
  82. sub do_sleep {
  83.     my $delay = shift;
  84.     if ( ($delay >= 1) && ($delay!~/\./) ){
  85.     sleep($delay);
  86.     } else {
  87.     select(undef,undef,undef,$delay);
  88.     }
  89. }
  90.  
  91. 1;
  92.  
  93. =head1 NAME
  94.  
  95. CGI::Push - Simple Interface to Server Push
  96.  
  97. =head1 SYNOPSIS
  98.  
  99.     use CGI::Push qw(:standard);
  100.  
  101.     do_push(-next_page=>\&next_page,
  102.             -last_page=>\&last_page,
  103.             -delay=>0.5);
  104.  
  105.     sub next_page {
  106.         my($q,$counter) = @_;
  107.         return undef if $counter >= 10;
  108.         return start_html('Test'),
  109.              h1('Visible'),"\n",
  110.                "This page has been called ", strong($counter)," times",
  111.                end_html();
  112.       }
  113.  
  114.      sub last_page {
  115.      my($q,$counter) = @_;
  116.          return start_html('Done'),
  117.                 h1('Finished'),
  118.                 strong($counter),' iterations.',
  119.                 end_html;
  120.      }
  121.  
  122. =head1 DESCRIPTION
  123.  
  124. CGI::Push is a subclass of the CGI object created by CGI.pm.  It is
  125. specialized for server push operations, which allow you to create
  126. animated pages whose content changes at regular intervals.
  127.  
  128. You provide CGI::Push with a pointer to a subroutine that will draw
  129. one page.  Every time your subroutine is called, it generates a new
  130. page.  The contents of the page will be transmitted to the browser
  131. in such a way that it will replace what was there beforehand.  The
  132. technique will work with HTML pages as well as with graphics files, 
  133. allowing you to create animated GIFs.
  134.  
  135. =head1 USING CGI::Push
  136.  
  137. CGI::Push adds one new method to the standard CGI suite, do_push().
  138. When you call this method, you pass it a reference to a subroutine
  139. that is responsible for drawing each new page, an interval delay, and
  140. an optional subroutine for drawing the last page.  Other optional
  141. parameters include most of those recognized by the CGI header()
  142. method.
  143.  
  144. You may call do_push() in the object oriented manner or not, as you
  145. prefer:
  146.  
  147.     use CGI::Push;
  148.     $q = new CGI::Push;
  149.     $q->do_push(-next_page=>\&draw_a_page);
  150.  
  151.         -or-
  152.  
  153.     use CGI::Push qw(:standard);
  154.     do_push(-next_page=>\&draw_a_page);
  155.  
  156. Parameters are as follows:
  157.  
  158. =over 4
  159.     
  160. =item -next_page
  161.  
  162.     do_push(-next_page=>\&my_draw_routine);
  163.  
  164. This required parameter points to a reference to a subroutine responsible for
  165. drawing each new page.  The subroutine should expect two parameters
  166. consisting of the CGI object and a counter indicating the number
  167. of times the subroutine has been called.  It should return the
  168. contents of the page as an B<array> of one or more items to print.  
  169. It can return a false value (or an empty array) in order to abort the
  170. redrawing loop and print out the final page (if any)
  171.  
  172.     sub my_draw_routine {
  173.         my($q,$counter) = @_;
  174.         return undef if $counter > 100;
  175.         return start_html('testing'),
  176.                h1('testing'),
  177.            "This page called $counter times";
  178.     }
  179.  
  180. =item -last_page
  181.  
  182. This optional parameter points to a reference to the subroutine
  183. responsible for drawing the last page of the series.  It is called
  184. after the -next_page routine returns a false value.  The subroutine
  185. itself should have exactly the same calling conventions as the
  186. -next_page routine.
  187.  
  188. =item -type
  189.  
  190. This optional parameter indicates the content type of each page.  It
  191. defaults to "text/html".  Currently, server push of heterogeneous
  192. document types is not supported.
  193.  
  194. =item -delay
  195.  
  196. This indicates the delay, in seconds, between frames.  Smaller delays
  197. refresh the page faster.  Fractional values are allowed.
  198.  
  199. B<If not specified, -delay will default to 1 second>
  200.  
  201. =item -cookie, -target, -expires
  202.  
  203. These have the same meaning as the like-named parameters in
  204. CGI::header().
  205.  
  206. =back
  207.  
  208. =head1 INSTALLING CGI::Push SCRIPTS
  209.  
  210. Server push scripts B<must> be installed as no-parsed-header (NPH)
  211. scripts in order to work correctly.  On Unix systems, this is most
  212. often accomplished by prefixing the script's name with "nph-".  
  213. Recognition of NPH scripts happens automatically with WebSTAR and 
  214. Microsoft IIS.  Users of other servers should see their documentation
  215. for help.
  216.  
  217. =head1 CAVEATS
  218.  
  219. This is a new module.  It hasn't been extensively tested.
  220.  
  221. =head1 AUTHOR INFORMATION
  222.  
  223. be used and modified freely, but I do request that this copyright
  224. notice remain attached to the file.  You may modify this module as you
  225. wish, but if you redistribute a modified version, please attach a note
  226. listing the modifications you have made.
  227.  
  228. Address bug reports and comments to:
  229. lstein@genome.wi.mit.edu
  230.  
  231. =head1 BUGS
  232.  
  233. This section intentionally left blank.
  234.  
  235. =head1 SEE ALSO
  236.  
  237. L<CGI::Carp>, L<CGI>
  238.  
  239. =cut
  240.  
  241.