Apache-Queue
view release on metacpan or search on metacpan
# them to the queue.
if($#sends < ($max_sends - 1) && $#queue < 0) {
# There is an open send slot, add the visitor, and let him go.
push @sends, "$now|0|$host";
show_template("queue_send.html", { 'uri' => $r->uri });
$status = OK;
} else {
# All send slots are full, attempt to queue the visitor.
$found = $x = $pos = 0;
foreach(@queue) {
if(m/^\d+\|$host$/) {
$found = 1;
$pos = $x;
last;
}
$x++;
}
if($found) {
my $open = $max_sends - ($#sends + 1);
if($pos < $open) {
push @sends, "$now|0|$host";
@queue = splice(@queue, $pos, 1);
show_template("queue_send.html", { 'uri' => $r->uri });
$status = OK;
} else {
$queue[$pos] = "$now|$host";
show_template("queue_position.html", { 'url' => $r->uri, 'position' => ($pos + 1), 'queue_size' => ($#queue + 1) });
$status = OK;
}
} else {
if(($#queue + 1) < $queue_size) {
$pos = push @queue, "$now|$host";
show_template("queue_position.html", { 'url' => $r->uri, 'position' => $pos, 'queue_size' => $#queue + 1 });
$status = OK;
} else {
show_template("queue_full.html", { 'queue_size' => $queue_size });
$status = OK;
}
}
}
}
untie @sends;
untie @queue;
return $status;
}
sub send_file {
my $r = shift;
my $sub = $r->lookup_uri($r->uri);
return $sub->run();
}
sub show_template {
my ($name, $vars) = @_;
my $file = Template::Provider->new(ABSOLUTE => '1');
my $hash = Queue::Template::Default->new();
$template = Template->new( {
OUTPUT => $r,
LOAD_TEMPLATES => [ $file, $hash ],
PREFIX_MAP => {
file => '0',
hash => '1',
default => '1',
},
});
$r->content_type("text/html");
my $path = $r->dir_config("TemplatePath");
if($path ne '') {
$template->process("file:$path/$name", $vars) || warn $template->error();
} else {
$template->process("hash:$name", $vars) || warn $template->error();
}
}
package Queue::Template::Default;
@Queue::Template::Default::ISA = qw(Template::Provider);
my %Defaults = (
'queue_send.html' => <<EOQSH
<html>
<head>
<meta HTTP-EQUIV="refresh" content="1; URL=[% uri %]">
</head>
<body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
<table height=100% width=100%>
<tr><td valign=center align=center>
<font face="arial" size=2>
Click <a href="[% uri %]">here</a> if your download does not start
</font>
</td></tr></table>
</body>
</html>
EOQSH
,
'queue_full.html' => <<EOQFH
<html>
<head>
<meta HTTP-EQUIV="refresh" content="300; URL=[% uri %]">
</head>
<body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
<table height=100% width=100%>
<tr><td valign=center align=center>
<font face="arial" size=2>
Sorry, the queue is full<BR>Keep this window open to keep trying
</font>
</td></tr></table>
</body>
</html>
EOQFH
,
'queue_position.html' => <<EOQPH
<html>
<head>
<meta HTTP-EQUIV="refresh" content="60; URL=[% uri %]">
</head>
<body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
( run in 0.719 second using v1.01-cache-2.11-cpan-e1769b4cff6 )