lib/POE/Component/MessageQueue/Queue.pm

filter: Statementstmt 22.4% 22 / 98 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 23.8% 5 / 21 Podpod 0.0% 0 / 9 totaltotal 15.3% 27 / 176 CC 19 SCAR 24.4
CC
19
Cov
16%
send
30.0
next_subscriber
20.0
next_ready
20.0
CRAP 11.5
« lib/POE/Component/MessageQueue/Storage/Generic/DBI.pm ↑ Summary lib/POE/Component/MessageQueue/Storage/FileSystem.pm »
1 #
2 # Copyright 2007-2010 David Snopek <dsnopek@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::Queue;
19  
20 11 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 11 with qw(POE::Component::MessageQueue::Destination);
25  
26 33 ▶sub flag { has $_[0] => (is => 'rw', default => 0) }
Subroutine flag: called CC=1 SCAR=0.0
Pod undocumented
27 11 flag 'pumping';
28 11 flag 'pump_pending';
29 11 flag 'shutting_down';
30  
31 sub stash {
32 22 ▶ my $n = $_[0];
Subroutine stash: called CC=1 SCAR=0.4
Pod undocumented
33 has $n => (
34 is => 'rw',
35 isa => 'HashRef',
36 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 22 traits => ['Hash'],
38 handles => {
39 "set_$n" => 'set',
40 "del_$n" => 'delete',
41 "${n}_keys" => 'keys',
42 }
43 );
44 }
45  
46 11 stash 'waiting';
47 11 stash 'serviced';
48  
49 sub next_subscriber
50 {
51 0 ▶ my $self = $_[0];
Subroutine next_subscriber: not called CC=4 SCAR=30.0
Pod undocumented
52 0 ▶ if (my @keys = $self->waiting_keys)
Branch: 1 branch
BranchTrueFalseTotal
if (my(@keys) = $self->waiting_keys) { } 0 0 0
53 {
54 0 my $id = pop(@keys);
55 0 my $s = $self->del_waiting($id);
56 0 ▶ if($s->client)
Branch: 1 branch
BranchTrueFalseTotal
if ($s->client) { } 0 0 0
57 {
58 0 $self->set_serviced($id, $s);
59 0 return $s;
60 }
61 else
62 {
63 0 $self->delete_subscription($id);
64 0 return $self->next_subscriber;
65 }
66 }
67 else
68 {
69 0 my $serviced = $self->serviced;
70 0 $self->serviced($self->waiting);
71 0 $self->waiting($serviced);
72 0 ▶ return (keys %$serviced) && $self->next_subscriber;
Condition0%0/3
keys %$serviced && $self->next_subscriber
!ll&&!rl&&r
000
MC/DC0%0/2
keys %$serviced && $self->next_subscriber
keys %$serviced $self->next_subscriber
Truth table0%0/3
A && B
A= keys %$servicedB= $self->next_subscriber
ABresult
0X0
100
111
73 }
74 }
75  
76 sub next_ready
77 {
78 0 ▶ my $self = $_[0];
Subroutine next_ready: not called CC=4 SCAR=30.0
Pod undocumented
79 0 my ($s, %seen);
80  
81 0 ▶ while ($s = $self->next_subscriber and !exists $seen{$s})
Branch: 1 branch
BranchTrueFalseTotal
if ($s = $self->next_subscriber and not exists $seen{$s}) 0 0 0
Condition0%0/3
$s = $self->next_subscriber and not exists $seen{$s}
!ll&&!rl&&r
000
MC/DC0%0/2
$s = $self->next_subscriber and not exists $seen{$s}
$s = $self->next_subscriber not exists $seen{$s}
Truth table0%0/3
A and not(B)
A= $s = $self->next_subscriberB= not exists $seen{$s}
ABresult
0X0
100
111
82 {
83 0 ▶ return $s if $s->ready;
Branch: 1 branch
BranchTrueFalseTotal
if $s->ready 0 0 0
84 0 $seen{$s} = 1;
85 }
86 0 return;
87 }
88  
89 11 after set_subscription => __PACKAGE__->can('set_waiting');
90 after delete_subscription => sub {
91 0 ▶ my ($self, @args) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 $self->del_waiting(@args);
93 0 $self->del_serviced(@args);
94 11 };
95  
96 11 __PACKAGE__->meta->make_immutable();
97  
98 sub BUILD
99 {
100 0 ▶ my ($self, $args) = @_;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
101 0 POE::Session->create(
102 object_states => [ $self => [qw(_start _shutdown _pump_state _pump_timer)]],
103 );
104 }
105  
106 sub _start
107 {
108 0 ▶ my ($self, $kernel) = @_[OBJECT, KERNEL];
Subroutine _start: not called CC=2 SCAR=17.9
109 0 $kernel->alias_set($self->name);
110  
111 0 ▶ $kernel->delay(_pump_timer => $self->parent->pump_frequency)
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent->pump_frequency 0 0 0
112 if ($self->parent->pump_frequency);
113 }
114  
115 sub shutdown {
116 0 ▶ my $self = $_[0];
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod undocumented
117 0 $self->shutting_down(1);
118 0 $poe_kernel->post($self->name, '_shutdown')
119 }
120  
121 sub _shutdown
122 {
123 0 ▶ my ($self, $kernel) = @_[OBJECT, KERNEL];
Subroutine _shutdown: not called CC=1 SCAR=6.9
124 0 $kernel->alias_remove($self->name);
125 0 $kernel->alarm_remove_all();
126 }
127  
128 # This is the pumping philosophy: When we receive a pump request, we will
129 # give everyone a chance to claim a message. If any pumps are asked for while
130 # this is happening, we will remember and do another pump when this one is
131 # finished (just one).
132  
133 # This means we're serializing claim and retrieve requests. More work needs
134 # to be done to determine whether this is good or necessary.
135  
136 0 ▶sub is_persistent { return 1 }
Subroutine is_persistent: not called CC=1 SCAR=6.9
Pod undocumented
137  
138 sub _pump_state
139 {
140 0 ▶ my $self = $_[OBJECT];
Subroutine _pump_state: not called CC=3 SCAR=24.8
141 0 ▶ return if $self->shutting_down;
Branch: 1 branch
BranchTrueFalseTotal
if $self->shutting_down 0 0 0
142  
143 0 ▶ if (my $s = $self->next_ready)
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $self->next_ready) { } 0 0 0
144 {
145 0 $s->ready(0);
146  
147 $self->storage->claim_and_retrieve($self->name, $s->client->id, sub {
148 0 ▶ if (my $msg = $_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $_[0]) { } 0 0 0
149 {
150 0 $self->dispatch_message($msg, $s);
151 0 $poe_kernel->post($self->name, '_pump_state');
152 }
153 else
154 {
155 0 $s->ready(1);
156 0 $self->_done_pumping();
157 }
158 0 });
159 }
160 else
161 {
162 0 $self->_done_pumping();
163 }
164 }
165  
166 sub _done_pumping
167 {
168 0 ▶ my $self = $_[0];
Subroutine _done_pumping: not called CC=2 SCAR=17.9
169 0 $self->pumping(0);
170 0 ▶ $self->pump() if $self->pump_pending;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pump_pending 0 0 0
171 }
172  
173 sub _pump_timer
174 {
175 0 ▶ my ($self, $kernel) = @_[OBJECT, KERNEL];
Subroutine _pump_timer: not called CC=2 SCAR=17.9
176 0 ▶ return if $self->shutting_down;
Branch: 1 branch
BranchTrueFalseTotal
if $self->shutting_down 0 0 0
177  
178 # pump (the 1 means that we won't set pump_pending if
179 # we are already in a pumping state).
180 0 $self->pump(1);
181  
182 0 $kernel->delay(_pump_timer => $self->parent->pump_frequency);
183 }
184  
185 sub pump
186 {
187 0 ▶ my ($self, $skip_pending) = @_;
Subroutine pump: not called CC=3 SCAR=24.8
Pod undocumented
188  
189 0 ▶ if($self->pumping and not $skip_pending)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pumping and not $skip_pending) { } 0 0 0
Condition0%0/3
$self->pumping and not $skip_pending
!ll&&!rl&&r
000
MC/DC0%0/2
$self->pumping and not $skip_pending
$self->pumping not $skip_pending
Truth table0%0/3
A and not(B)
A= $self->pumpingB= not $skip_pending
ABresult
0X0
100
111
190 {
191 0 $self->pump_pending(1);
192 }
193 else
194 {
195 0 $self->log(debug => ' -- PUMP QUEUE: '.$self->name.' -- ');
196 0 $self->notify('pump');
197 0 $self->pump_pending(0);
198 0 $self->pumping(1);
199 0 $poe_kernel->call($self->name, '_pump_state');
200 }
201 }
202  
203 sub send
204 {
205 0 ▶ my ($self, $message) = @_;
Subroutine send: not called CC=5 SCAR=34.0
Pod undocumented
206 0 ▶ return if $self->shutting_down;
Branch: 1 branch
BranchTrueFalseTotal
if $self->shutting_down 0 0 0
207  
208 # If we already have a ready subscriber, we'll dispatch before we
209 # store to give the subscriber a headstart on processing.
210 0 ▶ if (not $message->has_delay and my $s = $self->next_ready)
Branch: 1 branch
BranchTrueFalseTotal
if (not $message->has_delay and my $s = $self->next_ready) { } 0 0 0
Condition0%0/3
not $message->has_delay and my $s = $self->next_ready
!ll&&!rl&&r
000
MC/DC0%0/2
not $message->has_delay and my $s = $self->next_ready
not $message->has_delay my $s = $self->next_ready
Truth table0%0/3
not(A) and B
A= not $message->has_delayB= my $s = $self->next_ready
ABresult
0X0
100
111
211 {
212 0 my $mid = $message->id;
213 0 my $cid = $s->client->id;
214 0 ▶ if ($s->client_ack)
Branch: 1 branch
BranchTrueFalseTotal
if ($s->client_ack) { } 0 0 0
215 {
216 0 $message->claim($cid);
217 0 $self->log(info => "QUEUE: Message $mid claimed by $cid during send");
218 0 $self->storage->store($message);
219 0 $self->notify(store => $message);
220 }
221 else
222 {
223 0 $self->log(info => "QUEUE: Message $mid not stored, sent to $cid");
224 }
225 0 $self->dispatch_message($message, $s);
226 }
227 else
228 {
229 $self->storage->store($message, sub {
230 0 ▶ $self->notify(store => $message);
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 $self->pump();
232 0 });
233 }
234 }
235  
236 11 1;
237  
« lib/POE/Component/MessageQueue/Storage/Generic/DBI.pm ↑ Summary lib/POE/Component/MessageQueue/Storage/FileSystem.pm »