File Coverage

blib/lib/POEIKC/Client.pm
Criterion Covered Total %
statement 28 30 93.3
branch n/a
condition n/a
subroutine 10 10 100.0
pod n/a
total 38 40 95.0


line stmt bran cond sub pod time code
1             package POEIKC::Client;
2              
3 1     1   6 use strict;
  1         3  
  1         40  
4 1     1   24 use 5.008_001;
  1         3  
  1         124  
5              
6 1     1   6 use warnings;
  1         2  
  1         31  
7 1     1   1350 use Getopt::Long;
  1         16764  
  1         5  
8 1     1   3647 use Pod::Usage;
  1         127602  
  1         161  
9 1     1   13 use Data::Dumper;
  1         2  
  1         46  
10 1     1   5 use Sys::Hostname ();
  1         3  
  1         16  
11 1     1   5 use UNIVERSAL::require;
  1         2  
  1         13  
12 1     1   1037 use Best [ [ qw/YAML::XS YAML::Syck YAML/ ], qw/Dump/ ];
  1         2592  
  1         11  
13 1     1   14418 use POE::Component::IKC::ClientLite;
  0            
  0            
14              
15             our $DEBUG;
16              
17             sub DEBUG {
18             my $self = shift;
19             $DEBUG = shift if @_;;
20             }
21              
22             sub new {
23             my $class = shift ;
24             my $self = {
25             @_
26             };
27             $class = ref $class if ref $class;
28             bless $self,$class ;
29             return $self ;
30             }
31              
32             sub ikc_client_format {
33             my $self = shift;
34             my ($options, @argv) = @_;
35              
36             my $args = \@argv;
37             if (exists $options->{debug}) {
38             $DEBUG = 1;
39             _DEBUG_log($options);
40             _DEBUG_log($args);
41             }
42             $options->{help} and return;
43             $options->{alias} ||= 'POEIKCd';
44             $options->{port} ||= 47225;
45            
46             ### state_name vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
47             $options->{state_name} ||= '';
48              
49             if (exists $options->{Utility}) {
50             my $commoand = $options->{Utility};
51             $commoand = $options->{Utility};
52             $options->{state_name} = 'method_respond';
53             @{$args} = ('POEIKC::Daemon::Utility', $commoand, @{$args});
54             _DEBUG_log($args);
55             }
56              
57             if (exists $options->{INC}) {
58             my @inc;
59             @inc =
60             map {split(/:/=>$_)}
61             map {ref $_ ? @{$_} : $_}
62             ($options->{INC});
63             $options->{state_name} = 'method_respond';
64             @{$args} = (qw(POEIKC::Daemon::Utility unshift_INC), @inc);
65             $options->{output} ||= 'd';
66             _DEBUG_log($args);
67             }
68              
69             if (exists $options->{inc_}) {
70             my $commoand = $options->{inc_};
71             $commoand =
72             $commoand =~ /^del$|^delete$|^delete_INC$/ ? 'delete_INC' :
73             $commoand =~ /^reset$|^reset_INC$/ ? 'reset_INC' : $commoand;
74             $options->{state_name} = 'method_respond';
75             @{$args} = ('POEIKC::Daemon::Utility', $commoand, @{$args});
76             $options->{output} ||= 'd';
77             _DEBUG_log($args);
78             }
79              
80             $options->{state_name} =
81             $options->{state_name} =~ /^method|^m$/ ? 'method_respond' :
82             $options->{state_name} =~ /^function|^f$/ ? 'function_respond' :
83             $options->{state_name} =~ /^event|^e$/ ? 'event_respond' :
84             $options->{state_name};
85              
86             if ( grep {/^shutdown$/i} @{$args}) {
87             $options->{state_name} = 'method_respond';
88             @{$args} = ('POEIKC::Daemon::Utility', 'shutdown');
89             };
90              
91              
92             if ($args and @{$args} and not $options->{state_name}) {
93             $options->{state_name} ||= 'something_respond';
94             }
95              
96             $options->{state_name} or return;
97              
98             ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99              
100             $options->{HOST} ||= '127.0.0.1';
101              
102             # if( Proc::ProcessTable->use ){
103             # for my $ps( @{Proc::ProcessTable->new->table} ) {
104             # if ($ps->{fname} eq 'poikc'){
105             # $ps->{cmndline} =~ /poikc/;
106             # $0 = $ps->{fname}. $';
107             # }
108             # }
109             # }
110              
111             if (exists $options->{debug}) {
112             _DEBUG_log($options);
113             _DEBUG_log($options->{HOST});
114             _DEBUG_log($options->{port});
115             _DEBUG_log($args);
116             }
117              
118             my $state_name = $options->{alias}.'/'.$options->{state_name};
119              
120             $DEBUG and _DEBUG_log($state_name, $args);
121            
122             return ($state_name => $args);
123             }
124              
125              
126             sub post_respond {
127             my $self = shift;
128             my ($options, $state_name, $args) = @_;
129              
130             my ($name) = join('_'=>Sys::Hostname::hostname, ($0 =~ /(\w+)/g), $$);
131             my $ikc = $self->{ikc} ||= create_ikc_client(
132             ip => $options->{HOST},
133             port => $options->{port},
134             name => $name,
135             );
136             $ikc or do{
137             return sprintf "%s\n\n",$POE::Component::IKC::ClientLite::error;
138             };
139              
140             my $ret = $ikc->post_respond($state_name => $args);
141             $ikc->error and undef $self->{ikc}, return ($ikc->error), ;
142             no warnings;
143             if (my $r = ref $ret) {
144             $DEBUG and _DEBUG_log($r);
145             if ( $options->{output} and $options->{output} =~ /^H[YD]$/i and $r eq 'HASH'){
146             $DEBUG and _DEBUG_log($ret);
147             $options->{output} =~ s/^H//i;
148             my %ret = %{$ret};
149             my $max = 0;
150             for(sort keys %ret){length($_) > $max and $max = length($_);}
151             my $format = "%-${max}s= %s";
152             for(sort keys %ret){printf $format, $_, output($options->{output}, $ret{$_})}
153             print "\n";
154             }elsif ($options->{output}) {
155             $DEBUG and _DEBUG_log($ret);
156             return (output($options->{output},$ret));
157             }elsif (ref $ret) {
158             $DEBUG and _DEBUG_log($ret);
159              
160             local $Data::Dumper::Terse = 1;
161             local $Data::Dumper::Sortkeys = 1;
162             local $Data::Dumper::Indent = 1;
163              
164             return(Dumper($ret));
165             }else{
166             $DEBUG and _DEBUG_log($ret);
167             return $ret;
168             }
169             }else{
170             $DEBUG and _DEBUG_log($ret);
171             return output($options->{output}, $ret);
172             }
173             }
174              
175             sub output {
176             my $output_flag = shift;
177             $DEBUG and _DEBUG_log(join "\t"=> grep {defined $_} caller(1));
178             return unless @_;
179              
180             local $Data::Dumper::Terse = 1;
181             local $Data::Dumper::Sortkeys = 1;
182             local $Data::Dumper::Indent = 1;
183              
184             for ($output_flag || ()) {
185             /^D$|^Dumper$/i and return Dumper(@_);
186             /^Y$|^YAML$/i and return Dump(@_);
187             }
188             return @_;
189             }
190              
191             sub _DEBUG_log {
192             $DEBUG or return;
193             Date::Calc->use or return;
194             #YAML->use or return;
195             my ($pack, $file, $line, $subroutine) = caller(0);
196             my $levels_up = 0 ;
197             ($pack, $file, $line, ) = caller($levels_up);
198             $levels_up++;
199             (undef, undef, undef, $subroutine, ) = caller($levels_up);
200             {
201             (undef, undef, undef, $subroutine, ) = caller($levels_up);
202             if(defined $subroutine and $subroutine eq "(eval)") {
203             $levels_up++;
204             redo;
205             }
206             $subroutine = "main::" unless $subroutine;
207             }
208             my $log_header = sprintf "[DEBUG %04d/%02d/%02d %02d:%02d:%02d %s %d %s %d %s] - ",
209             Date::Calc::Today_and_Now() , $ENV{HOSTNAME}, $$, $file, $line, $subroutine;
210             my @data = @_;
211             print(
212             $log_header, (join "\t" => map {
213             ref($_) ? Dumper($_) :
214             defined $_ ? $_ : "`'" ;
215             } @data ),"\n"
216             );
217             }
218              
219             1;
220             __END__