File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/Condition.pm
Criterion Covered Total %
statement 44 51 86.2
branch 24 30 80.0
condition 8 12 66.6
subroutine 10 10 100.0
pod 7 7 100.0
total 93 110 84.5


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