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