« lib/POE/Component/MessageQueue/Storage/Default.pm
↑ Summary
lib/POE/Component/MessageQueue/Statistics/Publish.pm »
| 1 | # | ||||||||||||
| 2 | # Copyright 2007, 2008 Paul Driver <frodwith@gmail.com> | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # This program is free software: you can redistribute it and/or modify | ||||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||||
| 6 | # the Free Software Foundation, either version 2 of the License, or | ||||||||||||
| 7 | # (at your option) any later version. | ||||||||||||
| 8 | # | ||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||||
| 13 | # | ||||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||||
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||
| 16 | # | ||||||||||||
| 17 | |||||||||||||
| 18 | package POE::Component::MessageQueue::IDGenerator::SimpleInt; | ||||||||||||
| 19 | 1 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | with qw(POE::Component::MessageQueue::IDGenerator); | |||||||||||
| 21 | |||||||||||||
| 22 | 1 | has 'filename' => ( | |||||||||||
| 23 | is => 'ro', | ||||||||||||
| 24 | isa => 'Str', | ||||||||||||
| 25 | required => 1, | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | 1 | has 'last_id' => ( | |||||||||||
| 29 | is => 'rw', | ||||||||||||
| 30 | isa => 'Num', | ||||||||||||
| 31 | default => 0, | ||||||||||||
| 32 | traits => ['Counter'], | ||||||||||||
| 33 | handles => { | ||||||||||||
| 34 | 'inc_last_id' => 'inc', | ||||||||||||
| 35 | 'dec_last_id' => 'dec', | ||||||||||||
| 36 | 'reset_last_id' => 'reset', | ||||||||||||
| 37 | } | ||||||||||||
| 38 | ); | ||||||||||||
| 39 | |||||||||||||
| 40 | sub BUILD | ||||||||||||
| 41 | { | ||||||||||||
| 42 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
BUILD: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 43 | 0 | my $filename = $self->filename; | |||||||||||
| 44 | |||||||||||||
| 45 | 0 | ▶ | if (-e $filename) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | { | ||||||||||||
| 47 | 0 | ▶ | open my $in, '<', $filename || | ||||||||||
|
Condition0%0/2
$filename || die("Couldn't open $filename for reading: $!")
MC/DC0%0/1
$filename || die("Couldn't open $filename for reading: $!")
$filename
Truth table0%0/2
A
A= $filename
| |||||||||||||
| 48 | die "Couldn't open $filename for reading: $!"; | ||||||||||||
| 49 | 0 | my $line = <$in>; | |||||||||||
| 50 | 0 | close $in; | |||||||||||
| 51 | 0 | chomp $line; | |||||||||||
| 52 | 0 | ▶ | die "$filename didn't contain a number." unless ($line =~ /^\d+$/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | $self->last_id(0 + $line); | |||||||||||
| 54 | } | ||||||||||||
| 55 | else | ||||||||||||
| 56 | { | ||||||||||||
| 57 | 0 | ▶ | open my $out, '>', $filename || | ||||||||||
|
Condition0%0/2
$filename || die("Couldn't touch ${filename}: $!")
MC/DC0%0/1
$filename || die("Couldn't touch ${filename}: $!")
$filename
Truth table0%0/2
A
A= $filename
| |||||||||||||
| 58 | die "Couldn't touch $filename: $!"; | ||||||||||||
| 59 | 0 | close $out; | |||||||||||
| 60 | 0 | $self->reset_last_id(); | |||||||||||
| 61 | } | ||||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | sub generate | ||||||||||||
| 65 | { | ||||||||||||
| 66 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
generate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 67 | 0 | $self->inc_last_id(); | |||||||||||
| 68 | 0 | my $id = $self->last_id; | |||||||||||
| 69 | 0 | return "$id"; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | sub DESTROY | ||||||||||||
| 73 | { | ||||||||||||
| 74 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||
| 75 | 0 | my $fn = $self->filename; | |||||||||||
| 76 | 0 | ▶ | open my $out, '>', $fn || | ||||||||||
|
Condition0%0/2
$fn || die("Couldn't reopen $fn to write last ID!")
MC/DC0%0/1
$fn || die("Couldn't reopen $fn to write last ID!")
$fn
Truth table0%0/2
A
A= $fn
| |||||||||||||
| 77 | die "Couldn't reopen $fn to write last ID!"; | ||||||||||||
| 78 | 0 | my $id = $self->last_id; | |||||||||||
| 79 | 0 | print $out "$id\n"; | |||||||||||
| 80 | 0 | close $out; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 1 | 1; | |||||||||||
| 84 | |||||||||||||
| 85 | =head1 NAME | ||||||||||||
| 86 | |||||||||||||
| 87 | POE::Component::MessageQueue::IDGenerator::SimpleInt - Simple integer IDs. | ||||||||||||
| 88 | |||||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||||
| 90 | |||||||||||||
| 91 | This is a concrete implementation of the Generator interface for creating | ||||||||||||
| 92 | message IDs. It simply increments an integer, and makes some attempt to | ||||||||||||
| 93 | remember what the last one it used was across runs. | ||||||||||||
| 94 | |||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||
| 96 | |||||||||||||
| 97 | Paul Driver <frodwith@gmail.com> | ||||||||||||