2010-03-22 08:33:44 +01:00
|
|
|
# File: Timer.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Provides functionality to register and execute one or more subroutines every X seconds.
|
|
|
|
#
|
|
|
|
# Caveats: Uses ALARM signal and all its issues.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
package PBot::Timer;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use Carp ();
|
|
|
|
|
2017-05-21 11:23:15 +02:00
|
|
|
our $min_timeout = 1;
|
2010-03-22 08:33:44 +01:00
|
|
|
our $max_seconds = 1000000;
|
|
|
|
our $seconds = 0;
|
|
|
|
our @timer_funcs;
|
|
|
|
|
2019-06-26 18:34:19 +02:00
|
|
|
$SIG{ALRM} = sub {
|
|
|
|
$seconds += $min_timeout;
|
|
|
|
alarm $min_timeout;
|
|
|
|
|
|
|
|
# print "ALARM! $seconds $min_timeout\n";
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
# call timer func subroutines
|
|
|
|
foreach my $func (@timer_funcs) { &$func; }
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
# prevent $seconds over-flow
|
2019-06-26 18:34:19 +02:00
|
|
|
$seconds -= $max_seconds if $seconds > $max_seconds;
|
2010-03-22 08:33:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sub new {
|
2020-01-19 06:41:47 +01:00
|
|
|
Carp::croak("Options to Timer should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
2020-01-19 06:41:47 +01:00
|
|
|
my $timeout = $conf{timeout} // 10;
|
|
|
|
my $name = $conf{name} // "Unnamed $timeout Second Timer";
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
my $self = {
|
|
|
|
handlers => [],
|
|
|
|
name => $name,
|
|
|
|
timeout => $timeout,
|
|
|
|
enabled => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
bless $self, $class;
|
|
|
|
$min_timeout = $timeout if $timeout < $min_timeout;
|
|
|
|
|
|
|
|
# alarm signal handler (poor-man's timer)
|
|
|
|
$self->{timer_func} = sub { on_tick_handler($self) };
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{enabled} = 1;
|
|
|
|
push @timer_funcs, $self->{timer_func};
|
|
|
|
alarm $min_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub stop {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{enabled} = 0;
|
|
|
|
@timer_funcs = grep { $_ != $self->{timer_func} } @timer_funcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_tick_handler {
|
|
|
|
my $self = shift;
|
|
|
|
my $elapsed = 0;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($self->{enabled}) {
|
|
|
|
if ($#{ $self->{handlers} } > -1) {
|
2010-03-22 08:33:44 +01:00
|
|
|
# call handlers supplied via register() if timeout for each has elapsed
|
|
|
|
foreach my $func (@{ $self->{handlers} }) {
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $func->{last}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$func->{last} -= $max_seconds if $seconds < $func->{last}; # handle wrap-around of $seconds
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($seconds - $func->{last} >= $func->{timeout}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$func->{last} = $seconds;
|
|
|
|
$elapsed = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$func->{last} = $seconds;
|
|
|
|
$elapsed = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($elapsed) {
|
2010-03-22 08:33:44 +01:00
|
|
|
&{ $func->{subref} }($self);
|
|
|
|
$elapsed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# call default overridable handler if timeout has elapsed
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $self->{last}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{last} -= $max_seconds if $seconds < $self->{last}; # handle wrap-around
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($seconds - $self->{last} >= $self->{timeout}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$elapsed = 1;
|
|
|
|
$self->{last} = $seconds;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$elapsed = 1;
|
|
|
|
$self->{last} = $seconds;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($elapsed) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->on_tick();
|
|
|
|
$elapsed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# overridable method, executed whenever timeout is triggered
|
|
|
|
sub on_tick {
|
|
|
|
my $self = shift;
|
|
|
|
print "Tick! $self->{name} $self->{timeout} $self->{last} $seconds\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub register {
|
|
|
|
my $self = shift;
|
2014-05-19 12:30:25 +02:00
|
|
|
my ($ref, $timeout, $id) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-19 12:30:25 +02:00
|
|
|
Carp::croak("Must pass subroutine reference to register()") if not defined $ref;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
# TODO: Check if subref already exists in handlers?
|
|
|
|
$timeout = 300 if not defined $timeout; # set default value of 5 minutes if not defined
|
2014-05-19 12:30:25 +02:00
|
|
|
$id = 'timer' if not defined $id;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-19 12:30:25 +02:00
|
|
|
my $h = { subref => $ref, timeout => $timeout, id => $id };
|
2010-03-22 08:33:44 +01:00
|
|
|
push @{ $self->{handlers} }, $h;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($timeout < $min_timeout) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$min_timeout = $timeout;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($self->{enabled}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
alarm $min_timeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unregister {
|
2020-01-19 06:41:47 +01:00
|
|
|
my ($self, $id) = @_;
|
|
|
|
Carp::croak("Must pass timer id to unregister()") if not defined $id;
|
2016-01-29 21:59:07 +01:00
|
|
|
@{ $self->{handlers} } = grep { $_->{id} ne $id } @{ $self->{handlers} };
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2014-05-19 12:30:25 +02:00
|
|
|
sub update_interval {
|
|
|
|
my ($self, $id, $interval) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-19 12:30:25 +02:00
|
|
|
foreach my $h (@{ $self->{handlers} }) {
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($h->{id} eq $id) {
|
2014-05-19 12:30:25 +02:00
|
|
|
$h->{timeout} = $interval;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|