| 1 | package Net::Peep::Scheduler; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | require 5.00503; | |||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | # use warnings; # commented out for 5.005 compatibility | ||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Time::HiRes qw{ tv_interval gettimeofday alarm }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Net::Peep::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | require Exporter; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use vars qw{ @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 16 | 10 | %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); | |||||||||||||||||||||||||||
| 17 | 10 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 18 | 10 | @EXPORT = qw( ); | |||||||||||||||||||||||||||
| 19 | 10 | $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # structure of an event | ||||||||||||||||||||||||||||
| 22 | # $entry = { | ||||||||||||||||||||||||||||
| 23 | # 'application' => The application name | ||||||||||||||||||||||||||||
| 24 | # 'schedule_time' => the time for wakeup | ||||||||||||||||||||||||||||
| 25 | # 'type' => the type of event | ||||||||||||||||||||||||||||
| 26 | # 'data' => the data to pass to the handler | ||||||||||||||||||||||||||||
| 27 | # 'handler' => the handler to invoke | ||||||||||||||||||||||||||||
| 28 | # } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # The scheduled event queue | ||||||||||||||||||||||||||||
| 31 | 10 | ▶ | use vars qw( @scheduler_queue ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 37 | 11 | my $this = { }; | |||||||||||||||||||||||||||
| 38 | 11 | bless $this, $class; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Init the scheduler | ||||||||||||||||||||||||||||
| 41 | 11 | $this->logger()->debug(8, "Registering scheduler and scheduling alarm ..."); | |||||||||||||||||||||||||||
| 42 | 11 | ▶ | $SIG{'ALRM'} = sub { $this->schedulerWakeUp }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 11 | return $this; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | } #end sub new | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # returns a logging object | ||||||||||||||||||||||||||||
| 49 | 22 | ▶ | sub logger { | ||||||||||||||||||||||||||
|
Subroutine
logger: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | 22 | ▶ | unless ( exists $self->{'__LOGGER'} ) { $self->{'__LOGGER'} = new Net::Peep::Log } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 22 | return $self->{'__LOGGER'}; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | } #end sub logger | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | sub schedulerAddEvent { | ||||||||||||||||||||||||||
|
Subroutine
schedulerAddEvent: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | my ($self, $app, $sleepsec, $sleepusec, $type, $handler, $data, $repeated) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Do some sanity checking | ||||||||||||||||||||||||||||
| 62 | 3 | ▶ | confess "Error: No application name given to scheduler when adding event." unless $app; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | confess "Error: Wakeup given to scheduler is in the past." unless $sleepsec > 0.0 || $sleepusec > 0.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sleepsec > 0 or $sleepusec > 0
MC/DC0%0/2
$sleepsec > 0 or $sleepusec > 0
$sleepsec > 0
$sleepusec > 0
Truth table33%1/3
A or B
A= $sleepsec > 0B= $sleepusec > 0
| |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | confess "Error: No scheduled event type given to scheduler when adding event." unless $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | confess "Error: No handler given to scheduler when adding event." unless $handler; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | my ($s, $usec) = gettimeofday(); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | my $entry = { | |||||||||||||||||||||||||||
| 70 | 'application' => $app, | ||||||||||||||||||||||||||||
| 71 | 'sleepsec' => $sleepsec, | ||||||||||||||||||||||||||||
| 72 | 'sleepusec' => $sleepusec, | ||||||||||||||||||||||||||||
| 73 | 'schedule_time' => [ $s + $sleepsec, $usec + $sleepusec ], | ||||||||||||||||||||||||||||
| 74 | 'type' => $type, | ||||||||||||||||||||||||||||
| 75 | 'data' => $data, | ||||||||||||||||||||||||||||
| 76 | 'handler' => $handler, | ||||||||||||||||||||||||||||
| 77 | 'repeated' => $repeated, | ||||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Add the entry into the scheduler queue and sort by time | ||||||||||||||||||||||||||||
| 81 | 3 | push @scheduler_queue, $entry; | |||||||||||||||||||||||||||
| 82 | @scheduler_queue = sort { | ||||||||||||||||||||||||||||
| 83 | 3 | my ($asec, $ausec) = @{ $a->{'schedule_time'} }; | |||||||||||||||||||||||||||
| 84 | 4 | my ($bsec, $busec) = @{ $b->{'schedule_time'} }; | |||||||||||||||||||||||||||
| 85 | 4 | $asec + 0.000001 * $ausec <=> $bsec + 0.000001 * $busec; | |||||||||||||||||||||||||||
| 86 | } @scheduler_queue; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Now sleep for the new time | ||||||||||||||||||||||||||||
| 89 | 3 | $self->schedulerSleep; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | } #end sub schedulerAddEvent | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub schedulerRemoveEventsForApp { | ||||||||||||||||||||||||||
|
Subroutine
schedulerRemoveEventsForApp: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Removes all entries in the scheduler queue for an application | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $app = shift || die "Application name not found!"; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('Application name not found!')
MC/DC0%0/1
shift() || die('Application name not found!')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | @scheduler_queue = grep ! $_->{'app'} eq $app, @scheduler_queue; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | } # end sub schedulerRemoveEventsForApp | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | sub schedulerGetEvent { | ||||||||||||||||||||||||||
|
Subroutine
schedulerGetEvent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 3 | return (shift @scheduler_queue); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | } #end sub schedulerGetEvent | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | sub schedulerCalcSleepTime { | ||||||||||||||||||||||||||
|
Subroutine
schedulerCalcSleepTime: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 6 | my $nextent = $scheduler_queue[0]; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Check if we have an empty queue | ||||||||||||||||||||||||||||
| 118 | 6 | ▶ | unless ( $nextent ) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | my $sleeptime = tv_interval ( [ gettimeofday() ], $nextent->{'schedule_time'} ); | |||||||||||||||||||||||||||
| 121 | 5 | return $sleeptime; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | } #end sub schedulerCalcSleepTime | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 6 | ▶ | sub schedulerSleep { | ||||||||||||||||||||||||||
|
Subroutine
schedulerSleep: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 6 | my ($self, $time) = @_; | |||||||||||||||||||||||||||
| 128 | 6 | ▶ | my $sleeptime = $time || $self->schedulerCalcSleepTime; | ||||||||||||||||||||||||||
|
Condition66%2/3
$time || $self->schedulerCalcSleepTime
MC/DC50%1/2
$time || $self->schedulerCalcSleepTime
$time
$self->schedulerCalcSleepTime
Truth table66%2/3
A || B
A= $timeB= $self->schedulerCalcSleepTime
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Check if there's no such sleep time at this moment | ||||||||||||||||||||||||||||
| 131 | 6 | ▶ | unless ( $sleeptime ) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | $self->logger()->debug(8, "Scheduler will wake up in $sleeptime seconds."); | |||||||||||||||||||||||||||
| 134 | 5 | alarm ( $sleeptime ); | |||||||||||||||||||||||||||
| 135 | 5 | return $sleeptime; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | } #end sub schedulerSleep | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub schedulerExplicitWakeUp { | ||||||||||||||||||||||||||
|
Subroutine
schedulerExplicitWakeUp: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 0 | $self->logger()->debug(8, "Scheduler received explicit wake up..."); | |||||||||||||||||||||||||||
| 143 | 0 | $self->schedulerWakeUp; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } #end sub schedulerExplicitWakeUp | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 3 | ▶ | sub schedulerWakeUp { | ||||||||||||||||||||||||||
|
Subroutine
schedulerWakeUp: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 3 | $self->logger()->debug(8, "Scheduler woke up."); | |||||||||||||||||||||||||||
| 151 | 3 | my $entry = $self->schedulerGetEvent; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Doesn't apply because a schedulerExplicitWakeUp call would violate this and | ||||||||||||||||||||||||||||
| 154 | # still be valid | ||||||||||||||||||||||||||||
| 155 | # | ||||||||||||||||||||||||||||
| 156 | # # Check that the time has past | ||||||||||||||||||||||||||||
| 157 | # unless ( &Time::HiRes::tv_interval ( [ &Time::HiRes::gettimeofday() ], $entry->{'schedule_time'}) < 0.0 ) { | ||||||||||||||||||||||||||||
| 158 | # $self->logger()->debug(8, "Scheduled event was premature - returned error."); | ||||||||||||||||||||||||||||
| 159 | # return "Error: Scheduler woke up prematurely."; | ||||||||||||||||||||||||||||
| 160 | # } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Check if this is an internal housekeeping entry | ||||||||||||||||||||||||||||
| 163 | # Otherwise, pass control and data to the handler | ||||||||||||||||||||||||||||
| 164 | 3 | ▶ | if ($entry->{'application'} eq '__SCHEDULER') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | # internal processing - reserved for future use | ||||||||||||||||||||||||||||
| 166 | 0 | $self->logger()->debug(8, "Processing internal event..."); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||
| 169 | # Otherwise, call the handler with arguments of the type | ||||||||||||||||||||||||||||
| 170 | # of scheduled event and the data associated | ||||||||||||||||||||||||||||
| 171 | 3 | $self->logger()->debug(8, "Invoking event handler for ". $entry->{'application'}. " of type ". $entry->{'type'}. " ..."); | |||||||||||||||||||||||||||
| 172 | 3 | &{ $entry->{'handler'} } ( $entry->{'type'}, $entry->{'data'} ); | |||||||||||||||||||||||||||
| 173 | 3 | ▶ | if ($entry->{'repeated'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # if it's a repeated event, it should | ||||||||||||||||||||||||||||
| 176 | # reschedule itself | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # note that repeated events don't happen | ||||||||||||||||||||||||||||
| 179 | # precisely every sleepsec + 0.000001 * | ||||||||||||||||||||||||||||
| 180 | # sleepusec because of a delay every cycle | ||||||||||||||||||||||||||||
| 181 | # imposed by the execution time of the handler | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | $self->schedulerAddEvent( | ||||||||||||||||||||||||||||
| 184 | $entry->{'application'}, | ||||||||||||||||||||||||||||
| 185 | $entry->{'sleepsec'}, | ||||||||||||||||||||||||||||
| 186 | $entry->{'sleepusec'}, | ||||||||||||||||||||||||||||
| 187 | $entry->{'type'}, | ||||||||||||||||||||||||||||
| 188 | $entry->{'handler'}, | ||||||||||||||||||||||||||||
| 189 | $entry->{'data'}, | ||||||||||||||||||||||||||||
| 190 | 0 | $entry->{'repeated'} | |||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Reassign ourselves before we exit | ||||||||||||||||||||||||||||
| 196 | 3 | ▶ | $SIG{'ALRM'} = sub { $self->schedulerWakeUp }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 197 | 3 | $self->schedulerSleep; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | } #end sub schedulerWakeUp | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 10 | 1; | |||||||||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||||||||||