diff options
| author | Andreas Brachold <vdr07@deltab.de> | 2007-08-13 18:41:27 +0000 |
|---|---|---|
| committer | Andreas Brachold <vdr07@deltab.de> | 2007-08-13 18:41:27 +0000 |
| commit | bcbf441e09fb502cf64924ff2530fa144bdf52c5 (patch) | |
| tree | f377707a2dac078db8cd0c7d7abfe69ac1006d71 /lib/XXV/OUTPUT/HTML | |
| download | xxv-bcbf441e09fb502cf64924ff2530fa144bdf52c5.tar.gz xxv-bcbf441e09fb502cf64924ff2530fa144bdf52c5.tar.bz2 | |
* Move files to trunk
Diffstat (limited to 'lib/XXV/OUTPUT/HTML')
| -rw-r--r-- | lib/XXV/OUTPUT/HTML/PUSH.pm | 95 | ||||
| -rw-r--r-- | lib/XXV/OUTPUT/HTML/WAIT.pm | 169 |
2 files changed, 264 insertions, 0 deletions
diff --git a/lib/XXV/OUTPUT/HTML/PUSH.pm b/lib/XXV/OUTPUT/HTML/PUSH.pm new file mode 100644 index 0000000..7c7d7df --- /dev/null +++ b/lib/XXV/OUTPUT/HTML/PUSH.pm @@ -0,0 +1,95 @@ +package XXV::OUTPUT::HTML::PUSH; + +use strict; + +use Tools; + +$| = 1; + +=head1 NAME + +XXV::OUTPUT::HTML::PUSH - A Push for http system + +=head1 SYNOPSIS + + use XXV::OUTPUT::HTML::PUSH; + + my $pusher = XXV::OUTPUT::HTML::PUSH->new( + -cgi => $obj->{cgi}, # The CGI Object from Lincoln Stein + -handle => $obj->{handle}, # The handle to printout the http Stuff + ); + + $pusher->start(); # Start the Push Process + + while($c > 10) { + $pusher->print($c++); # Print out the message + } + + $pusher->stop(); # Stop the Push + + +=cut + +# ------------------ +sub new { +# ------------------ + my($class, %attr) = @_; + my $self = {}; + bless($self, $class); + + $self->{handle} = $attr{'-handle'} + || return error('No handle defined!'); + + $self->{cgi} = $attr{'-cgi'} + || return error('No CGI Object defined!'); + + return $self; +} + +# ------------------ +sub start { +# ------------------ + my $obj = shift || return error ('No Object!' ); + my $out = shift || 0; + $obj->{handle}->print($obj->{cgi}->multipart_init(-boundary=>'----here we go!')); + $obj->print($out) if($out); +} + +# ------------------ +sub print { +# ------------------ + my $obj = shift || return error ('No Object!' ); + my $msg = shift || return; + my $type = shift || 'text/html'; + + $obj->{handle}->print($obj->{cgi}->multipart_start(-type=>$type)); + $obj->{handle}->print($msg."\n"); + $obj->{handle}->print($obj->{cgi}->multipart_end); +} + +# ------------------ +sub follow_print { +# ------------------ + my $obj = shift || return error ('No Object!' ); + my $msg = shift || return; + my $type = shift || 'text/html'; + + unless($obj->{header}) { + $obj->{handle}->print($obj->{cgi}->multipart_start(-type=>$type)); + $obj->{header} = 1; + } + $obj->{handle}->print($msg."\n"); +} + +# ------------------ +sub stop { +# ------------------ + my $obj = shift || return error ('No Object!' ); + $obj->{handle}->print($obj->{cgi}->multipart_end); + $obj->{handle}->print($obj->{cgi}->header( + -type => 'text/html', + -status => "200 OK", + )); +} + +1; diff --git a/lib/XXV/OUTPUT/HTML/WAIT.pm b/lib/XXV/OUTPUT/HTML/WAIT.pm new file mode 100644 index 0000000..8ebd430 --- /dev/null +++ b/lib/XXV/OUTPUT/HTML/WAIT.pm @@ -0,0 +1,169 @@ +package XXV::OUTPUT::HTML::WAIT; + +use strict; + +use Tools; +use XXV::OUTPUT::HTML::PUSH; + +=head1 NAME + +XXV::OUTPUT::HTML::WAIT - A Processbar for XXV system + +=head1 SYNOPSIS + + use XXV::OUTPUT::HTML::WAIT; + + my $waiter = XXV::OUTPUT::HTML::WAIT->new( + -cgi => $obj->{cgi}, # The CGI Object from Lincoln Stein + -handle => $obj->{handle}, # The handle to printout the http Stuff + -callback => sub{ # Callback for html output. + # In this case parse the html template wait.tmpl + my ($min, $max, $cur, $steps) = @_; + my $out = $obj->parseTemplate( + 'wait', + { + msg => $msg, + minimum => $min, + current => $cur, + maximum => $max, + steps => $steps + }, + ); + return $out; + }, + ); + + $waiter->min(0); # Min Value for process Bar + $waiter->max(10); # Max Value for process Bar + $waiter->screen('yes'); # Every call of next will redraw the process bar + + while($c > 10) { + $waiter->next($c++); # Next Event with current value + } + $waiter->end; + +=cut + +# ------------------ +sub new { +# ------------------ + my($class, %attr) = @_; + my $self = {}; + bless($self, $class); + + $self->{handle} = $attr{'-handle'} + || return error('No handle defined!'); + + $self->{cgi} = $attr{'-cgi'} + || return error('No CGI Object defined!'); + + $self->{callback} = $attr{'-callback'} + || return error('No Callback to print out!'); + + $self->{steps} = $attr{'-steps'} || 10; + + $self->{pusher} = XXV::OUTPUT::HTML::PUSH->new( + -cgi => $self->{cgi}, # The CGI Object from Lincoln Stein + -handle => $self->{handle}, # The handle to printout the http Stuff + ); + + $self->init(); + + return $self; +} + +# ------------------ +sub init { +# ------------------ + my $obj = shift || return error ('No Object!' ); + + $obj->{STEP} = 0; + $obj->{pusher}->start(); + undef $obj->{FirstRefresh}; +} + +# ------------------ +sub next { +# ------------------ + my $obj = shift || return error ('No Object!' ); + my $cur = shift || $obj->{MAX}; + my $end = shift || 0; + my $msg = shift || 0; + + + # Don't show really every step, even is screen defined, + # avoid high traffic and long duration of waiter progress + my $t = time; + return + if(defined $obj->{SCREEN} && $obj->{SCREEN} eq 'no' + && $end == 0 + && $obj->{LastRefreshTime} && $obj->{LastRefreshTime} > ($t - 1)); + + # remember time from from first call + $obj->{FirstRefresh} = $t + if(not $obj->{FirstRefresh}); + + # calc end time of execution + my $rest = $end ? 0 : $obj->{MAX} - $cur; + my $deltaT = $t - $obj->{FirstRefresh}; + my $etaT = ($cur > 0) ? ($deltaT / $cur * $rest) : 0; + # Format end time of execution from seconds to human readable format + my $eta = sprintf("%02d:%02d:%02d",$etaT / 3600 % 24 ,($etaT / 60) % 60, $etaT % 60 ); + + $obj->{LastRefreshTime} = $t; + + + # 2.2 = 22 / 10 + my $step = $obj->{MAX} / $obj->{steps}; + $obj->{STEP} += $step; + + if ($end or $cur > $obj->{MAX}) { + $obj->{pusher}->stop(); + my $out = $obj->{endcallback}($obj->{MIN}, $obj->{MAX}, $cur, $obj->{steps}, $msg, $eta) + if(ref $obj->{endcallback} eq 'CODE'); + } else { + my $out = $obj->{callback}($obj->{MIN}, $obj->{MAX}, $cur, $obj->{steps}, $msg, $eta); + $obj->{pusher}->print($out); + } +} + +# ------------------ +sub end { +# ------------------ + my $obj = shift || return error ('No Object!' ); + my $msg = shift || 0; + + return $obj->next(undef, $obj->{MAX}, 1, $msg); +} + + +# ------------------ +sub endcallback { +# ------------------ + my $obj = shift || return error ('No Object!' ); + $obj->{endcallback} = shift || return $obj->{endcallback}; +} + +# ------------------ +sub max { +# ------------------ + my $obj = shift || return error ('No Object!' ); + $obj->{MAX} = shift || return $obj->{MAX}; +} + +# ------------------ +sub min { +# ------------------ + my $obj = shift || return error ('No Object!' ); + $obj->{MIN} = shift || return $obj->{MIN}; +} + +# ------------------ +sub screen { +# ------------------ + my $obj = shift || return error ('No Object!' ); + $obj->{SCREEN} = shift || return $obj->{SCREEN}; +} + + +1; |
