Lightspeed/Introspection.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 150 CC 21 SCAR 47.3
CC
21
Cov
0%
list_states
182.0
list_sessions
90.0
list_kernels
2.0
CRAP 113.9
1 # Declare our package
2 package POE::Component::Lightspeed::Introspection;
3  
4 # Standard stuff to catch errors
5 use strict qw(subs vars refs); # Make sure we can't mess up
6 use warnings FATAL => 'all'; # Enable warnings to catch errors
7  
8 # Initialize our version
9 our $VERSION = '1.' . sprintf( "%04d", (qw($Revision: 1082 $))[1] );
10  
11 # Load the constants
12 use POE::Component::Lightspeed::Constants qw( ACTION_INTROSPECTION FROM_KERNEL FROM_SESSION FROM_STATE FROM_FILE FROM_LINE DEST_KERNEL DEST_SESSION );
13  
14 # Make sure the router instance is loaded no matter what
15 use POE::Component::Lightspeed::Router;
16  
17 # Spawn the Router session to make sure :)
18 POE::Component::Lightspeed::Router->spawn();
19  
20 # We export some stuff
21 require Exporter;
22 our @ISA = qw( Exporter );
23 our @EXPORT_OK = qw( list_kernels list_sessions list_states );
24  
25 # Lists all kernels
26 sub list_kernels {
27 # Get the heap from the router session
28 my $heap = $POE::Kernel::poe_kernel->_resolve_session( $POE::Component::Lightspeed::Router::SES_ALIAS )->get_heap();
29  
30 # Get the list of kernels
31 my @kernels = $heap->{'GRAPH'}->vertices();
32  
33 # Return it!
34 return \@kernels;
35 }
36  
37 # Lists all sessions in kernel X
38 sub list_sessions {
39 my( $dest, $rsvp ) = @_;
40  
41 # Validate the kernel
42 if ( ! defined $dest ) {
43 return undef;
44 } elsif ( ref( $dest ) ) {
45 if ( ref( $dest ) eq 'POE::Component::Lightspeed::Hack::Session' ) {
46 # Cool! Convert it :)
47 $dest = $dest->remote_kernel();
48 } elsif ( ref( $dest ) ne 'ARRAY' ) {
49 return undef;
50 }
51 }
52  
53 # Validate the RSVP
54 $rsvp = POE::Component::Lightspeed::Router::ValidateDestination( $rsvp );
55 if ( ! defined $rsvp ) {
56 return undef;
57 }
58  
59 # Get the heap from the router session
60 my $heap = $POE::Kernel::poe_kernel->_resolve_session( $POE::Component::Lightspeed::Router::SES_ALIAS )->get_heap();
61  
62 # Get the session alias
63 my @list = $POE::Kernel::poe_kernel->alias_list( $POE::Kernel::poe_kernel->get_active_session() );
64 if ( ! defined $list[0] ) {
65 $list[0] = $POE::Kernel::poe_kernel->get_active_session()->ID;
66 }
67  
68 # Construct the from array
69 my $from = [];
70 $from->[ FROM_KERNEL ] = $heap->{'MYKERNEL'};
71  
72 # Add the session
73 $from->[ FROM_SESSION ] = $list[0];
74  
75 # Add the state
76 $from->[ FROM_STATE ] = $POE::Kernel::poe_kernel->get_active_event();
77  
78 # Add the file/line
79 push( @$from, (caller)[1,2] );
80  
81 # Is it a local introspection request?
82 if ( $dest eq $heap->{'MYKERNEL'} or $dest eq '*' ) {
83 # Send it to the router to emulate a "packet"
84 $POE::Kernel::poe_kernel->post(
85 $POE::Component::Lightspeed::Router::SES_ALIAS,
86 'ACTION_' . ACTION_INTROSPECTION,
87 [
88 $heap->{'MYKERNEL'}, # MSG_TO
89 $heap->{'MYKERNEL'}, # MSG_FROM
90 ACTION_INTROSPECTION, # MSG_ACTION
91 [ # MSG_DATA
92 'SESSION', # INTROSPECTION_WHAT
93 $from, # INTROSPECTION_FROM
94 $rsvp, # INTROSPECTION_RSVP
95 ],
96 ],
97 );
98 }
99  
100 # Send it off!
101 if ( $dest ne $heap->{'MYKERNEL'} ) {
102 POE::Component::Lightspeed::Router::SendMessage( $heap, [
103 $dest, # MSG_TO
104 undef, # MSG_FROM
105 ACTION_INTROSPECTION, # MSG_ACTION
106 [ # MSG_DATA
107 'SESSION', # INTROSPECTION_WHAT
108 $from, # INTROSPECTION_FROM
109 $rsvp, # INTROSPECTION_RSVP
110 ],
111 ] );
112 }
113  
114 # All done!
115 return 1;
116 }
117  
118 # Lists all states in session X in kernel Y
119 sub list_states {
120 my( $dest, $rsvp ) = @_;
121  
122 # Validate the destination
123 if ( ! defined $dest ) {
124 return undef;
125 } elsif ( ! ref( $dest ) ) {
126 # Okay, before we send it to ValidateDestination, we have to make sure there's a '/' at the end to make it happy
127 if ( $dest =~ m|^poe://(?:[^/]+)/(?:[^/]+)$| ) {
128 $dest .= '/';
129 }
130  
131 # If it's terribly corrupt, who cares, ValidateDestination will catch it for us :)
132 $dest = POE::Component::Lightspeed::Router::ValidateDestination( $dest );
133 if ( ! defined $dest ) {
134 return undef;
135 }
136 } elsif ( ref( $dest ) eq 'ARRAY' ) {
137 # Must have 2 parts
138 if ( scalar( @$dest ) != 2 ) {
139 return undef;
140 }
141 } elsif ( ref( $dest ) eq 'HASH' ) {
142 # Must have 2 parts
143 if ( ! exists $dest->{'KERNEL'} or ! exists $dest->{'SESSION'} ) {
144 return undef;
145 } else {
146 # Change it into an array
147 $dest = [ $dest->{'KERNEL'}, $dest->{'SESSION'} ];
148 }
149 } elsif ( ref( $dest ) eq 'POE::Component::Lightspeed::Hack::Session' ) {
150 # Cool! Convert it :)
151 $dest = [ $dest->remote_kernel(), $dest->remote_session() ];
152 } else {
153 # What the hell is it?
154 return undef;
155 }
156  
157 # Validate the rsvp
158 $rsvp = POE::Component::Lightspeed::Router::ValidateDestination( $rsvp );
159 if ( ! defined $rsvp ) {
160 return undef;
161 }
162  
163 # Get the heap from the router session
164 my $heap = $POE::Kernel::poe_kernel->_resolve_session( $POE::Component::Lightspeed::Router::SES_ALIAS )->get_heap();
165  
166 # Construct the from array
167 my $from = [];
168 $from->[ FROM_KERNEL ] = $heap->{'MYKERNEL'};
169  
170 # Add the session
171 my @list = $POE::Kernel::poe_kernel->alias_list( $POE::Kernel::poe_kernel->get_active_session() );
172 if ( ! defined $list[0] ) {
173 $list[0] = $POE::Kernel::poe_kernel->get_active_session()->ID;
174 }
175 $from->[ FROM_SESSION ] = $list[0];
176  
177 # Add the state
178 $from->[ FROM_STATE ] = $POE::Kernel::poe_kernel->get_active_event();
179  
180 # Add the file/line
181 push( @$from, (caller)[1,2] );
182  
183 # Is it a local introspection request?
184 if ( POE::Component::Lightspeed::Router::FindOurself( $heap->{'MYKERNEL'}, $dest->[ DEST_KERNEL ] ) ) {
185 # Send it to the router to emulate a "packet"
186 $POE::Kernel::poe_kernel->post(
187 $POE::Component::Lightspeed::Router::SES_ALIAS,
188 'ACTION_' . ACTION_INTROSPECTION,
189 [
190 $heap->{'MYKERNEL'}, # MSG_TO
191 $heap->{'MYKERNEL'}, # MSG_FROM
192 ACTION_INTROSPECTION, # MSG_ACTION
193 [ # MSG_DATA
194 'STATE', # INTROSPECTION_WHAT
195 $from, # INTROSPECTION_FROM
196 $rsvp, # INTROSPECTION_RSVP
197 $dest->[ DEST_SESSION ], # INTROSPECTION_ARGS
198 ],
199 ],
200 );
201 }
202  
203 # Send it off!
204 POE::Component::Lightspeed::Router::SendMessage( $heap, [
205 $dest->[ DEST_KERNEL ], # MSG_TO
206 undef, # MSG_FROM
207 ACTION_INTROSPECTION, # MSG_ACTION
208 [ # MSG_DATA
209 'STATE', # INTROSPECTION_WHAT
210 $from, # INTROSPECTION_FROM
211 $rsvp, # INTROSPECTION_RSVP
212 $dest->[ DEST_SESSION ], # INTROSPECTION_ARGS
213 ],
214 ] );
215  
216 # All done!
217 return 1;
218 }
219  
220 # End of module
221 1;
222 __END__