File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/Condition.pm
Criterion Covered Total %
statement 40 44 90.9
branch 20 24 83.3
condition 8 12 66.6
subroutine 10 10 100.0
pod 7 7 100.0
total 85 97 87.6


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::Daemon::Condition;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::Daemon::Condition - object that checks which conditions should keep a Siebel::Srvrmgr::Daemon running
8              
9             =head1 SYNOPSIS
10              
11             my $condition = Siebel::Srvrmgr::Daemon::Condition->new(
12             {
13             total_commands => 10
14             }
15             );
16              
17             =cut
18              
19 4     4   2864 use Moose 2.0401;
  4         77  
  4         37  
20 4     4   34098 use namespace::autoclean 0.13;
  4         103  
  4         37  
21             our $VERSION = '0.29'; # VERSION
22              
23             =pod
24              
25             =head1 DESCRIPTION
26              
27             Siebel::Srvrmgr::Daemon::Condition class has one function: define if the L<Siebel::Srvrmgr::Daemon> object instance should continue it's loop execution or stop.
28              
29             There are several checkings that are carried (for example, if the loop must be infinite or if the stack of commands is finished) and the object of this class
30             will return true (1) or false (0) depending on the context.
31              
32             Since this class was used exclusively to control de loop of execution, it does not make much sense to use it outside of L<Siebel::Srvrmgr::Daemon> class.
33              
34             There are more status that this class will keep, please check the attributes and methods.
35              
36             =head1 ATTRIBUTES
37              
38             =head2 max_cmd_idx
39              
40             Maximum command index. This is an integer attribute that identifies the last command from the commands stack (of L<Siebel::Srvrmgr::Daemon>).
41              
42             It's automatically set to C<total_commands> - 1 right after object creation.
43              
44             =cut
45              
46             has max_cmd_idx =>
47             ( isa => 'Int', is => 'ro', required => 0, builder => '_set_max', lazy => 1 );
48              
49             =pod
50              
51             =head2 total_commands
52              
53             This is an integer that tells the total amount of commands available for execution. This class will keep track of the executed commands and the result
54             of it will be part of the definition of the result returned from the method C<check> and restart, if necessary, the stack of commands to be executed.
55              
56             This attribute is required during object creation.
57              
58             =cut
59              
60             has total_commands =>
61             ( isa => 'Int', is => 'ro', required => 1, writer => '_set_total_commands' );
62              
63             =pod
64              
65             =head2 cmd_counter
66              
67             An integer that keeps tracking of the current command being executed, always starting from zero.
68              
69             This attribute has a default value of zero.
70              
71             =cut
72              
73             has cmd_counter => (
74             isa => 'Int',
75             is => 'ro',
76             required => 1,
77             writer => '_set_cmd_counter',
78             reader => 'get_cmd_counter',
79             default => 0
80             );
81              
82             =pod
83              
84             =head2 output_used
85              
86             An boolean that identifies if the last executed command output was used or not.
87              
88             =cut
89              
90             has output_used => (
91             isa => 'Bool',
92             is => 'rw',
93             default => 0,
94             reader => 'is_output_used',
95             writer => '_set_output_used'
96             );
97              
98             =pod
99              
100             =head2 cmd_sent
101              
102             An boolean that identifies if the current command to be executed was truly submitted to C<srvrmgr> program or not.
103              
104             =cut
105              
106             has cmd_sent => (
107             isa => 'Bool',
108             is => 'rw',
109             default => 0,
110             reader => 'is_cmd_sent',
111             writer => 'set_cmd_sent'
112             );
113              
114             =pod
115              
116             =head1 METHODS
117              
118             =head2 get_cmd_counter
119              
120             Returns the content of the attribute C<cmd_counter> as an integer.
121              
122             =head2 is_output_used
123              
124             Returns a true or false based on the content of C<output_used> attribute.
125              
126             =head2 set_output_used
127              
128             Sets the C<output_used> attribute. Expects a 1 (true) or 0 (false) value as parameter.
129              
130             If the parameter is true then the attribute C<cmd_sent> will be set to false (a new command will need to be submitted).
131              
132             =cut
133              
134             sub set_output_used {
135              
136 54     54 1 196 my $self = shift;
137 54         138 my $value = shift;
138              
139 54         2867 $self->_set_output_used($value);
140              
141 54 100       378 if ($value) {
142              
143             # if there is a single command to execute, there is no reason to reset the cmd_sent attribute
144 25 100       1403 $self->set_cmd_sent(0) unless ( $self->max_cmd_idx() == 0 );
145              
146             }
147              
148             }
149              
150             =pod
151              
152             =head2 is_cmd_sent
153              
154             Returns a true or false based on the content of C<cmd_sent> attribute.
155              
156             =head2 set_cmd_sent
157              
158             Sets the attribute C<cmd_sent>. Expects true (1) or false (0) as value.
159              
160             =head2 max_cmd_idx
161              
162             Returns an integer based on the content of C<max_cmd_idx> attribute.
163              
164             =head2 reduce_total_cmd
165              
166             This method subtracts one from the C<total_cmd> attribute.
167              
168             =cut
169              
170             sub reduce_total_cmd {
171              
172 14     14 1 43 my $self = shift;
173              
174 14         595 $self->_set_total_commands( $self->total_commands() - 1 );
175              
176             }
177              
178             sub _set_max {
179              
180 19     19   60 my $self = shift;
181              
182 19 50       1259 if ( $self->total_commands() >= 1 ) {
183              
184 19         783 $self->{max_cmd_idx} = $self->total_commands() - 1;
185              
186             }
187             else {
188              
189 0         0 warn "total_commands has zero commands?";
190              
191             }
192              
193             }
194              
195             =pod
196              
197             =head2 check
198              
199             This method will check various conditions and depending on them will return true (1) or false (0).
200              
201             The conditions that are taken in consideration:
202              
203             =over 3
204              
205             =item *
206              
207             The execution loop is infinite or not.
208              
209             =item *
210              
211             There is more items to execute from the commands/actions stack.
212              
213             =item *
214              
215             The output from a previous executed command was used or not.
216              
217             =back
218              
219             =cut
220              
221             sub check {
222              
223 44     44 1 3615 my $self = shift;
224              
225 44 100 100     2129 if ( ( $self->get_cmd_counter() == $self->max_cmd_idx() )
    100          
226             and $self->is_output_used() )
227             {
228              
229 16         79 return 0;
230              
231             }
232             elsif ( $self->total_commands() > 0 ) {
233              
234             # if at least one command to execute
235 27 100 33     1309 if ( $self->total_commands() == 1 ) {
    50          
236              
237 14         79 $self->reduce_total_cmd();
238 14         74 return 1;
239              
240             # or there are more commands to execute
241             }
242             elsif ( ( $self->total_commands() > 1 )
243             and ( $self->get_cmd_counter() <= ( $self->max_cmd_idx() ) ) )
244             {
245              
246 13         75 return 1;
247              
248             }
249             else {
250              
251 0         0 return 0;
252              
253             }
254              
255             }
256             else {
257              
258 1         4 return 0;
259              
260             }
261              
262             }
263              
264             =pod
265              
266             =head2 add_cmd_counter
267              
268             Increments by one the C<cmd_counter> attribute, if possible.
269              
270             It will check if the C<cmd_counter> after incrementing will not pass the C<max_cmd_idx> attribute. In this case, the method will
271             not change C<cmd_counter> value and will raise an exception.
272              
273             =cut
274              
275             sub add_cmd_counter {
276              
277 17     17 1 2060 my $self = shift;
278              
279 17 50       1136 if ( ( $self->get_cmd_counter() + 1 ) <= ( $self->max_cmd_idx() ) ) {
280              
281 17         867 $self->_set_cmd_counter( $self->get_cmd_counter() + 1 );
282              
283             }
284             else {
285              
286 0         0 die "Can't increment counter because maximum index of command is "
287             . $self->max_cmd_idx() . "\n";
288              
289             }
290              
291             }
292              
293             =pod
294              
295             =head2 can_increment
296              
297             This method checks if the C<cmd_counter> can be increment or not, returning true (1) or false (0) depending
298             on the conditions evaluated.
299              
300             =cut
301              
302             sub can_increment {
303              
304 28     28 1 94 my $self = shift;
305              
306 28 100 100     1472 if ( ( $self->is_output_used() )
307             and ( ( $self->get_cmd_counter() + 1 ) <= $self->max_cmd_idx() ) )
308             {
309              
310 10         107 return 1;
311              
312             }
313             else {
314              
315 18         111 return 0;
316              
317             }
318              
319             }
320              
321             =pod
322              
323             =head2 is_last_cmd
324              
325             This method returns true (1) if the C<cmd_counter> holds the last command index from the command stack, otherwise it
326             returns false.
327              
328             =cut
329              
330             sub is_last_cmd {
331              
332 30     30 1 118 my $self = shift;
333              
334 30 100       1793 if ( $self->get_cmd_counter() == $self->max_cmd_idx() ) {
335              
336 18 50 33     855 if ( ( $self->max_cmd_idx() == 1 ) and ( not( $self->is_cmd_sent() ) ) )
    100          
337             {
338              
339 0         0 return 1;
340              
341             }
342             elsif ( $self->is_output_used() ) {
343              
344 4         35 return 1;
345              
346             }
347             else {
348              
349 14         117 return 0;
350              
351             }
352              
353             }
354             else {
355              
356 12         83 return 0;
357              
358             }
359              
360             }
361              
362             =pod
363              
364             =head2 reset_cmd_counter
365              
366             Resets the C<cmd_counter> attributing setting it to zero. This is useful specially if the loop of execution is infinite (thus the command stack must be restarted).
367              
368             =cut
369              
370             sub reset_cmd_counter {
371              
372 1     1 1 4 my $self = shift;
373              
374 1         62 $self->_set_cmd_counter(0);
375              
376 1         8 return 1;
377              
378             }
379              
380             =pod
381              
382             =head1 CAVEATS
383              
384             This class is becoming more and more complex due the several conditions that need to be evaluated for defining if the L<Siebel::Srvrmgr::Daemon> should still
385             execute the C<run> method or not. This probably should be replaced by a state machine.
386              
387             =head1 SEE ALSO
388              
389             =over
390              
391             =item *
392              
393             L<Moose>
394              
395             =item *
396              
397             L<Siebel::Srvrmgr::Daemon>
398              
399             =back
400              
401             =head1 AUTHOR
402              
403             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
404              
405             =head1 COPYRIGHT AND LICENSE
406              
407             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
408              
409             This file is part of Siebel Monitoring Tools.
410              
411             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
412             it under the terms of the GNU General Public License as published by
413             the Free Software Foundation, either version 3 of the License, or
414             (at your option) any later version.
415              
416             Siebel Monitoring Tools is distributed in the hope that it will be useful,
417             but WITHOUT ANY WARRANTY; without even the implied warranty of
418             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
419             GNU General Public License for more details.
420              
421             You should have received a copy of the GNU General Public License
422             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
423              
424             =cut
425              
426             __PACKAGE__->meta->make_immutable;