Lightspeed/Router.pm untested

filter: Statementstmt 0.0% 0 / 516 Branchbran 0.0% 0 / 258 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 875 CC 130 SCAR 52.0
CC
130
Cov
0%
ValidateDestination
650.0
A_INTROSPECTION
272.0
call
210.0
CRAP 182.1
1 # Declare our package
2 package POE::Component::Lightspeed::Router;
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 # Set some constants
12 BEGIN {
13 # Debug fun!
14 if ( ! defined &DEBUG ) {
15 eval "sub DEBUG () { 0 }";
16 }
17 }
18  
19 # Import what we need
20 use Carp qw( croak );
21 use POE;
22 use Graph::Undirected;
23  
24 # Time to hack POE!
25 use POE::Component::Lightspeed::Hack::Session;
26 use POE::Component::Lightspeed::Hack::Kernel;
27 use POE::Component::Lightspeed::Hack::Events;
28  
29 # Import the message constants
30 use POE::Component::Lightspeed::Constants qw( :ALL );
31  
32 # Our global session alias
33 our $SES_ALIAS = undef;
34  
35 # Spawns an instance of the Router
36 sub spawn {
37 # Are we already loaded?
38 if ( defined $SES_ALIAS ) {
39 # Debugging info
40 if ( DEBUG ) {
41 warn 'Tried to start a second instance of the Router, ignoring it...';
42 }
43 return undef;
44 }
45  
46 # Get the OOP's type
47 my $type = shift;
48  
49 # Get our lone option
50 $SES_ALIAS = shift;
51  
52 # Get the session alias
53 if ( ! defined $SES_ALIAS or ! length( $SES_ALIAS ) ) {
54 # Debugging info...
55 if ( DEBUG ) {
56 warn 'Using default ALIAS = lightspeed_router';
57 }
58  
59 # Set the default
60 $SES_ALIAS = 'lightspeed_router';
61 }
62  
63 # Create the graph object
64 my $graph = Graph::Undirected->new;
65  
66 # Create the POE Session!
67 POE::Session->create(
68 'inline_states' => {
69 # Generic stuff
70 '_start' => sub { $_[KERNEL]->alias_set( $SES_ALIAS ); },
71 '_stop' => sub {},
72 '_default' => sub {
73 warn "SES: ", Data::Dumper::Dumper( $_[SESSION] );
74 return 0;
75 },
76  
77 # Post/Call stuff
78 'post' => \&post,
79 'call' => \&call,
80  
81 # Events from client/server
82 'killserver' => \&Server_Shutdown,
83  
84 # Link status
85 'link_down' => \&Link_Down,
86 'link_up' => \&Link_Up,
87  
88 # Packet Action mapping
89 'ACTION_' . ACTION_HELLO . '' => \&A_HELLO,
90 'ACTION_' . ACTION_ROUTEDEL . '' => \&A_ROUTE_DEL,
91 'ACTION_' . ACTION_ROUTENEW . '' => \&A_ROUTE_NEW,
92 'ACTION_' . ACTION_POST . '' => \&A_POST,
93 'ACTION_' . ACTION_CALL . '' => \&A_CALL,
94 'ACTION_' . ACTION_CALLREPLY . '' => \&A_POST,
95 'ACTION_' . ACTION_INTROSPECTION . '' => \&A_INTROSPECTION,
96 },
97  
98 # Our own heap
99 'heap' => {
100 # Stores the sessions associated with specific links
101 'SID_WID' => {},
102 'WID_SID' => {},
103  
104 # Which wheels are part of servers or clients?
105 'WID_TYPE' => {},
106  
107 # Keep track of the kernels we know of
108 # KERNEL => WID
109 'KERNEL_WID' => {},
110 'WID_KERNEL' => {},
111 'MYKERNEL' => undef,
112  
113 # Our graph object
114 'GRAPH' => $graph,
115 'APSP' => $graph->APSP_Floyd_Warshall,
116  
117 # Authentication stuff
118 'AUTH' => {},
119  
120 # Monitor stuff
121 'MONITOR' => {},
122 },
123 ) or die 'Unable to create a new session!';
124  
125 # Return success
126 return 1;
127 }
128  
129 # A server session is going down, along with it's links
130 sub Server_Shutdown {
131 # ARG0 = arrayref of wheel ID's
132  
133 # Get the real kernel names
134 my @edges = ();
135 foreach my $w ( @{ $_[ARG0] } ) {
136 push( @edges, [ $_[HEAP]->{'MYKERNEL'}, $_[HEAP]->{'WID_KERNEL'}->{ $w } ] );
137 }
138  
139 # Send it off!
140 SendMessage( $_[HEAP], [
141 undef, # MSG_TO
142 undef, # MSG_FROM
143 ACTION_ROUTEDEL, # MSG_ACTION
144 \@edges, # MSG_DATA
145 ] );
146  
147 # All done!
148 return 1;
149 }
150  
151 # The kernel is telling us to post a message somewhere
152 sub post {
153 # ARG0 = destination, ARG1: args
154 my( $sender, $state, $file, $line, $dest, $args ) = @_[ SENDER, CALLER_STATE, CALLER_FILE, CALLER_LINE, ARG0, ARG1 ];
155  
156 # Assemble the message
157 my @aliases = $_[KERNEL]->alias_list( $sender );
158  
159 # Does it have any alias?
160 if ( ! defined $aliases[0] ) {
161 $aliases[0] = $sender->ID;
162 }
163  
164 # Which kernels?
165 my @kernels = ();
166  
167 # Is it the broadcast kernel ( * )?
168 if ( ref( $dest->[ DEST_KERNEL ] ) ) {
169 if ( $dest->[ DEST_KERNEL ]->[0] eq '*' ) {
170 @kernels = [ $_[HEAP]->{'GRAPH'}->vertices() ];
171 } else {
172 @kernels = @{ $dest->[ DEST_KERNEL ] };
173 }
174 } else {
175 push( @kernels, $dest->[ DEST_KERNEL ] );
176 }
177  
178 # Process each destination
179 my @finalkern = ();
180 foreach my $k ( @kernels ) {
181 # Make sure it exists
182 if ( ! $_[HEAP]->{'GRAPH'}->has_vertex( $k ) ) {
183 if ( DEBUG ) {
184 warn "Kernel $k does not exist!";
185 }
186 next;
187 }
188  
189 # Are we sending to ourself?
190 if ( $k eq $_[HEAP]->{'MYKERNEL'} ) {
191 post_self(
192 $_[HEAP],
193 POE::Component::Lightspeed::Hack::Session->new(
194 $_[HEAP]->{'MYKERNEL'},
195 $aliases[0],
196 $state,
197 $file,
198 $line,
199 ),
200 $dest,
201 $args,
202 );
203 } else {
204 # Add it to the kernel list
205 push( @finalkern, $k );
206 }
207 }
208  
209 # Send the message!
210 SendMessage( $_[HEAP], [
211 \@finalkern, # MSG_TO
212 undef, # MSG_FROM
213 ACTION_POST, # MSG_ACTION
214 [ # MSG_DATA
215 $dest, # POST_TO
216 [ # POST_FROM
217 $_[HEAP]->{'MYKERNEL'}, # DEST_KERNEL
218 $aliases[0], # DEST_SESSION
219 $state, # DEST_STATE
220 $file, # DEST_FILE
221 $line, # DEST_LINE
222 ],
223 $args, # POST_ARGS
224 ],
225 ] );
226  
227 # All done!
228 return 1;
229 }
230  
231 # The kernel is telling us to call a message somewhere
232 sub call {
233 # ARG0 = destination, ARG1 = rsvp, ARG3 = args
234 my( $sender, $state, $file, $line, $dest, $rsvp, $args ) = @_[ SENDER, CALLER_STATE, CALLER_FILE, CALLER_LINE, ARG0 .. ARG2 ];
235  
236 # Assemble the message
237 my @aliases = $_[KERNEL]->alias_list( $sender );
238  
239 # Does it have any alias?
240 if ( ! defined $aliases[0] ) {
241 $aliases[0] = $sender->ID;
242 }
243  
244 # Which kernels?
245 my @kernels = ();
246  
247 # Is it the broadcast kernel ( * )?
248 if ( ref( $dest->[ DEST_KERNEL ] ) ) {
249 if ( $dest->[ DEST_KERNEL ]->[0] eq '*' ) {
250 @kernels = [ $_[HEAP]->{'GRAPH'}->vertices() ];
251 } else {
252 @kernels = @{ $dest->[ DEST_KERNEL ] };
253 }
254 } else {
255 push( @kernels, $dest->[ DEST_KERNEL ] );
256 }
257  
258 # Process each destination
259 my @finalkern = ();
260 foreach my $k ( @kernels ) {
261 # Make sure it exists
262 if ( ! $_[HEAP]->{'GRAPH'}->has_vertex( $k ) ) {
263 if ( DEBUG ) {
264 warn "Kernel $k does not exist!";
265 }
266 next;
267 }
268  
269 # Are we sending to ourself?
270 if ( $k eq $_[HEAP]->{'MYKERNEL'} ) {
271 # Create the fake session
272 my $fake_session = POE::Component::Lightspeed::Hack::Session->new(
273 $k,
274 $aliases[0],
275 $state,
276 $file,
277 $line,
278 );
279  
280 # Is it the broadcast session ( * )?
281 my @sessions = ();
282 if ( ref( $dest->[ DEST_SESSION ] ) ) {
283 if ( $dest->[ DEST_SESSION ]->[0] eq '*' ) {
284 @sessions = GetSessions();
285 } else {
286 @sessions = @{ $dest->[ DEST_SESSION ] };
287 }
288 } else {
289 push( @sessions, $dest->[ DEST_SESSION ] );
290 }
291  
292 # Fire off each session!
293 foreach my $s ( @sessions ) {
294 # Check if it's a multiple-alias session
295 if ( ref( $s ) ) {
296 $s = $s->[0];
297 }
298  
299 # Resolve it into a session ref
300 $s = $_[KERNEL]->_resolve_session( $s );
301  
302 # Sanity check
303 if ( ! defined $s ) {
304 return undef;
305 }
306  
307 # Route it through POE :)
308 my @result = $_[KERNEL]->lightspeed_fake_call(
309 $fake_session,
310 $s,
311 $dest->[ DEST_STATE ],
312 $args,
313 );
314  
315 # Where should the RSVP go?
316 my @rsvpfinal = ();
317 foreach my $r ( @{ $rsvp->[ DEST_KERNEL ] } ) {
318 # Make sure it exists
319 if ( ! $_[HEAP]->{'GRAPH'}->has_vertex( $r ) ) {
320 if ( DEBUG ) {
321 warn "Kernel $k does not exist!";
322 }
323 next;
324 }
325  
326 # Are we sending to ourself?
327 if ( $r eq $_[HEAP]->{'MYKERNEL'} ) {
328 post_self(
329 $_[HEAP],
330 POE::Component::Lightspeed::Hack::Session->new(
331 $_[HEAP]->{'MYKERNEL'},
332 $aliases[0],
333 $state,
334 'Lightspeed.pm',
335 325,
336 ),
337 $rsvp,
338 \@result,
339 );
340 } else {
341 # Add it to the kernel list
342 push( @rsvpfinal, $r );
343 }
344 }
345  
346 # Send the message!
347 SendMessage( $_[HEAP], [
348 \@rsvpfinal, # MSG_TO
349 undef, # MSG_FROM
350 ACTION_CALLREPLY, # MSG_ACTION
351 [ # MSG_DATA
352 $rsvp, # CALLREPLY_TO
353 [ # CALLREPLY_FROM
354 $_[HEAP]->{'MYKERNEL'}, # FROM_KERNEL
355 $aliases[0], # FROM_SESSION
356 $state, # FROM_STATE
357 'Lightspeed.pm', # FROM_FILE
358 348, # FROM_LINE
359 ],
360 \@result, # CALLREPLY_ARGS
361 ],
362 ] );
363 }
364 } else {
365 # Add it to the kernel list
366 push( @finalkern, $k );
367 }
368 }
369  
370 # Send the message!
371 SendMessage( $_[HEAP], [
372 \@finalkern, # MSG_TO
373 undef, # MSG_FROM
374 ACTION_CALL, # MSG_ACTION
375 [ # MSG_DATA
376 $dest, # CALL_TO
377 [ # CALL_FROM
378 $_[HEAP]->{'MYKERNEL'}, # FROM_KERNEL
379 $aliases[0], # FROM_SESSION
380 $state, # FROM_STATE
381 $file, # FROM_FILE
382 $line, # FROM_LINE
383 ],
384 $rsvp, # CALL_RSVP
385 $args, # CALL_ARGS
386 ],
387 ] );
388  
389 # All done!
390 return 1;
391 }
392  
393 # Helper to post messages to ourself
394 sub post_self {
395 my( $heap, $fake_session, $dest, $args ) = @_;
396  
397 # Is it the broadcast session ( * )?
398 my @sessions = ();
399 if ( ref( $dest->[ DEST_SESSION ] ) ) {
400 if ( $dest->[ DEST_SESSION ]->[0] eq '*' ) {
401 @sessions = GetSessions();
402 } else {
403 @sessions = @{ $dest->[ DEST_SESSION ] };
404 }
405 } else {
406 push( @sessions, $dest->[ DEST_SESSION ] );
407 }
408  
409 # Fire off each session!
410 foreach my $s ( @sessions ) {
411 # Check if it's a multiple-alias session
412 if ( ref( $s ) ) {
413 $s = $s->[0];
414 }
415  
416 # Resolve it into a session ref
417 $s = $POE::Kernel::poe_kernel->_resolve_session( $s );
418  
419 # Sanity check
420 if ( ! defined $s ) {
421 next;
422 }
423  
424 # Route it through POE :)
425 $POE::Kernel::poe_kernel->lightspeed_fake_post(
426 $fake_session,
427 $s,
428 $dest->[ DEST_STATE ],
429 $args,
430 );
431 }
432  
433 # All done!
434 return 1;
435 }
436  
437 # A link is up!
438 sub Link_Up {
439 # ARG0 = wheel id, ARG1 = our kernel name, ARG2 = remote name, ARG3 = Server/Client
440 my( $ses, $id, $kernel, $remotekernel, $type ) = @_[ SENDER, ARG0 .. ARG3 ];
441 $ses = $ses->ID;
442  
443 # The kernel name cannot include * or / or ,
444 if ( $kernel =~ tr|*/,|| ) {
445 die "The kernel name cannot include '*' or '/' or ','";
446 }
447  
448 # Ah, now we know our name :)
449 if ( ! defined $_[HEAP]->{'MYKERNEL'} ) {
450 $_[HEAP]->{'MYKERNEL'} = $kernel;
451 } else {
452 # Sanity check...
453 if ( $_[HEAP]->{'MYKERNEL'} ne $kernel ) {
454 die 'Using different kernel names in the same process is not supported!';
455 }
456 }
457  
458 # We do some sanity checks here
459 if ( $_[HEAP]->{'GRAPH'}->has_vertex( $remotekernel ) ) {
460 # A new node wants to join, but we already have the same kernel name!
461 warn "Detected same kernel name in use - $remotekernel";
462 $_[KERNEL]->call( $ses, 'killclient', $id );
463 return undef;
464 }
465  
466 # Store it!
467 push( @{ $_[HEAP]->{'SID_WID'}->{ $ses } }, $id );
468 $_[HEAP]->{'WID_SID'}->{ $id } = $ses;
469 $_[HEAP]->{'WID_TYPE'}->{ $id } = $type;
470 $_[HEAP]->{'KERNEL_WID'}->{ $remotekernel } = $id;
471 $_[HEAP]->{'WID_KERNEL'}->{ $id } = $remotekernel;
472  
473 # Get the edges
474 my @edges = $_[HEAP]->{'GRAPH'}->edges();
475 if ( scalar( @edges ) == 1 ) {
476 @edges = ( $edges[0]->[0], $edges[0]->[1] );
477 }
478  
479 # Send our kernel info
480 $_[KERNEL]->post( $ses, 'send', $id, [
481 $remotekernel, # MSG_TO
482 $kernel, # MSG_FROM
483 ACTION_HELLO, # MSG_ACTION
484 \@edges, # MSG_DATA
485 ] );
486 }
487  
488 # A link is down!
489 sub Link_Down {
490 # ARG0 = wheel id
491 my( $ses, $id ) = @_[ SENDER, ARG0 ];
492 $ses = $ses->ID;
493 my $kernel = $_[HEAP]->{'WID_KERNEL'}->{ $id };
494  
495 # Remove it from that session
496 $_[HEAP]->{'SID_WID'}->{ $ses } = [ grep { $_ ne $id } @{ $_[HEAP]->{'SID_WID'}->{ $ses } } ];
497 delete $_[HEAP]->{'WID_SID'}->{ $id };
498 delete $_[HEAP]->{'SID_WID'}->{ $ses } if scalar( @{ $_[HEAP]->{'SID_WID'}->{ $ses } } ) == 0;
499  
500 # Delete this edge from our graph
501 $_[HEAP]->{'GRAPH'}->delete_edge( $_[HEAP]->{'MYKERNEL'}, $kernel );
502  
503 # Update the APSP
504 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
505  
506 # Are any kernels isolated now ( no way to reach them )
507 foreach my $node ( $_[HEAP]->{'GRAPH'}->vertices ) {
508 if ( $node eq $_[HEAP]->{'MYKERNEL'} ) {
509 next;
510 }
511  
512 if ( ! $_[HEAP]->{'APSP'}->is_reachable( $_[HEAP]->{'MYKERNEL'}, $node ) ) {
513 # This kernel is isolated, delete it
514 $_[HEAP]->{'GRAPH'}->delete_vertex( $node );
515  
516 # Update the APSP
517 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
518 }
519 }
520  
521 # Remove it from the wheel mapping
522 delete $_[HEAP]->{'KERNEL_WID'}->{ $kernel };
523 delete $_[HEAP]->{'WID_KERNEL'}->{ $id };
524  
525 # Are we responsible?
526 if ( $_[HEAP]->{'WID_TYPE'}->{ $id } eq 'Server' or ! $_[HEAP]->{'GRAPH'}->has_vertex( $kernel ) ) {
527 # Let all kernels know
528 SendMessage( $_[HEAP], [
529 undef, # MSG_TO
530 undef, # MSG_FROM
531 ACTION_ROUTEDEL, # MSG_ACTION
532 [ $kernel, $_[HEAP]->{'MYKERNEL'} ], # MSG_DATA
533 ] );
534 }
535  
536 # One last piece of deletion
537 delete $_[HEAP]->{'WID_TYPE'}->{ $id };
538  
539 # All done!
540 return 1;
541 }
542  
543 # Processes a HELLO message
544 sub A_HELLO {
545 # ARG0 = data packet, ARG1 = wheel id
546 my $msg = $_[ARG0];
547  
548 # Add it to our graph
549 $_[HEAP]->{'GRAPH'}->add_weighted_edge( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_FROM ], 1 );
550  
551 # Update the APSP
552 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
553  
554 # Store the new edges
555 my @edges = ();
556 if ( scalar( @{ $msg->[ MSG_DATA ] } ) > 0 ) {
557 if ( ! ref( $msg->[ MSG_DATA ]->[0] ) ) {
558 # Single edge
559 if ( ! $_[HEAP]->{'GRAPH'}->has_edge( $msg->[ MSG_DATA ]->[0], $msg->[ MSG_DATA ]->[1] ) ) {
560 push( @edges, [ $msg->[ MSG_DATA ]->[0], $msg->[ MSG_DATA ]->[1] ] );
561 }
562 } else {
563 foreach my $e ( @{ $msg->[ MSG_DATA ] } ) {
564 # Do we know about this already?
565 if ( ! $_[HEAP]->{'GRAPH'}->has_edge( $e->[0], $e->[1] ) ) {
566 # This is new to us
567 push( @edges, $e );
568 }
569 }
570 }
571 }
572  
573 # This sucks.
574 my @targets = $_[HEAP]->{'GRAPH'}->vertices;
575 @targets = grep { $_ ne $msg->[ MSG_FROM ] } @targets;
576  
577 # Broadcast this new info to our network
578 if ( scalar( @edges ) > 0 ) {
579 # Add them all to the graph
580 foreach my $e ( @edges ) {
581 $_[HEAP]->{'GRAPH'}->add_weighted_edge( $e->[0], $e->[1], 1 );
582 }
583  
584 # Update the APSP
585 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
586  
587 # Add our link to the edges
588 push( @edges, [ $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_FROM ] ] );
589 } else {
590 # Add our link to the edges
591 @edges = ( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_FROM ] );
592 }
593  
594 # Inform the network about our new link!
595 SendMessage( $_[HEAP], [
596 \@targets, # MSG_TO
597 undef, # MSG_FROM
598 ACTION_ROUTENEW, # MSG_ACTION
599 \@edges, # MSG_DATA
600 ] );
601  
602 # All done!
603 return 1;
604 }
605  
606 # Processes ROUTE_NEW messages
607 sub A_ROUTE_NEW {
608 # ARG0 = data packet, ARG1 = wheel id
609 my $msg = $_[ARG0];
610  
611 # Is it for us?
612 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_TO ] ) ) {
613 # Process each edge
614 my $edges = [];
615 if ( ! ref( $msg->[ MSG_DATA ]->[0] ) ) {
616 push( @$edges, [ $msg->[ MSG_DATA ]->[0], $msg->[ MSG_DATA ]->[1] ] );
617 } else {
618 $edges = $msg->[ MSG_DATA ];
619 }
620  
621 # Do it!
622 foreach my $e ( @$edges ) {
623 # Do we know about this already?
624 if ( ! $_[HEAP]->{'GRAPH'}->has_edge( $e->[0], $e->[1] ) ) {
625 # Add it!
626 $_[HEAP]->{'GRAPH'}->add_weighted_edge( $e->[0], $e->[1], 1 );
627 } else {
628 if ( DEBUG ) {
629 warn "ROUTE_NEW -> discarding known edge: $e->[0] <=> $e->[1]";
630 }
631 }
632 }
633  
634 # Update the APSP
635 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
636 }
637  
638 # # Find LINK-MISS hits
639 # foreach my $link ( $_[HEAP]->{'GRAPH'}->vertices() ) {
640 # # Skip the sender and ourself
641 # if ( $link eq $_[HEAP]->{'MYKERNEL'} or $link eq $msg->[ MSG_FROM ] ) { next }
642 #
643 # # Find it in the REALTO
644 # if ( ( ! ref( $msg->[ MSG_REALTO ] ) and $msg->[ MSG_REALTO ] ne $link ) or ! Array_Search( $msg->[ MSG_REALTO ], $link ) ) {
645 # # Add this link to the message
646 # if ( ! ref( $msg->[ MSG_TO ] ) ) {
647 # $msg->[ MSG_TO ] = [ $msg->[ MSG_TO ], $link ];
648 # } else {
649 # push( @{ $msg->[ MSG_TO ] }, $link );
650 # }
651 #
652 # # Add it to the REALTO too
653 # if ( ! ref( $msg->[ MSG_REALTO ] ) ) {
654 # $msg->[ MSG_REALTO ] = [ $msg->[ MSG_REALTO ], $link ];
655 # } else {
656 # push( @{ $msg->[ MSG_REALTO ] }, $link );
657 # }
658 # }
659 # }
660  
661 # Do whatever is necessary with this packet
662 SendMessage( $_[HEAP], $msg );
663  
664 # All done!
665 return 1;
666 }
667  
668 # Processes a ROUTE_DEL message
669 sub A_ROUTE_DEL {
670 # ARG0 = data packet, ARG1 = wheel id
671 my $msg = $_[ARG0];
672  
673 # Is it for us?
674 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_TO ] ) ) {
675 # Process each edge
676 my @edges = ();
677 if ( ! ref( $msg->[ MSG_DATA ]->[0] ) ) {
678 push( @edges, [ $msg->[ MSG_DATA ]->[0], $msg->[ MSG_DATA ]->[1] ] );
679 } else {
680 @edges = @{ $msg->[ MSG_DATA ] };
681 }
682  
683 # Do it!
684 foreach my $e ( @edges ) {
685 # Delete this edge from our graph
686 $_[HEAP]->{'GRAPH'}->delete_edge( $e->[0], $e->[1] );
687  
688 # Update the APSP
689 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
690  
691 # Are any kernels isolated now ( no way to reach them )
692 foreach my $kernel ( $_[HEAP]->{'GRAPH'}->vertices ) {
693 if ( $kernel eq $_[HEAP]->{'MYKERNEL'} ) {
694 next;
695 }
696  
697 if ( ! $_[HEAP]->{'APSP'}->is_reachable( $_[HEAP]->{'MYKERNEL'}, $kernel ) ) {
698 # This kernel is isolated, delete it
699 $_[HEAP]->{'GRAPH'}->delete_vertex( $kernel );
700  
701 # Update the APSP
702 $_[HEAP]->{'APSP'} = $_[HEAP]->{'GRAPH'}->APSP_Floyd_Warshall;
703 }
704 }
705 }
706 }
707  
708 # # Find LINK-MISS hits
709 # foreach my $link ( $_[HEAP]->{'GRAPH'}->vertices() ) {
710 # # Skip the sender and ourself
711 # if ( $link eq $_[HEAP]->{'MYKERNEL'} or $link eq $msg->[ MSG_FROM ] ) { next }
712 #
713 # # Find it in the REALTO
714 # if ( ( ! ref( $msg->[ MSG_REALTO ] ) and $msg->[ MSG_REALTO ] ne $link ) or ! Array_Search( $msg->[ MSG_REALTO ], $link ) ) {
715 # # Add this link to the message
716 # if ( ! ref( $msg->[ MSG_TO ] ) ) {
717 # $msg->[ MSG_TO ] = [ $msg->[ MSG_TO ], $link ];
718 # } else {
719 # push( @{ $msg->[ MSG_TO ] }, $link );
720 # }
721 #
722 # # Add it to the REALTO too
723 # if ( ! ref( $msg->[ MSG_REALTO ] ) ) {
724 # $msg->[ MSG_REALTO ] = [ $msg->[ MSG_REALTO ], $link ];
725 # } else {
726 # push( @{ $msg->[ MSG_REALTO ] }, $link );
727 # }
728 # }
729 # }
730  
731 # Do whatever is necessary with this packet
732 SendMessage( $_[HEAP], $msg );
733  
734 # All done!
735 return 1;
736 }
737  
738 # Processes POST messages
739 sub A_POST {
740 # ARG0 = data packet, ARG1 = wheel id
741 my $msg = $_[ARG0];
742  
743 # Is it for us?
744 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_TO ] ) ) {
745 # Create the fake session
746 my $fake_session = POE::Component::Lightspeed::Hack::Session->new( @{ $msg->[ MSG_DATA ]->[ POST_FROM ] } );
747  
748 # Determine the type
749 my $type;
750 if ( $_[STATE] eq 'ACTION_0' ) {
751 $type = 'post';
752 } else {
753 $type = 'callreply';
754 }
755  
756 # Short-circuit all our AUTH crap
757 if ( ! exists $_[HEAP]->{'AUTH'}->{ $type } ) {
758 # Okay, send it!
759 post_self(
760 $_[HEAP],
761 $fake_session,
762 $msg->[ MSG_DATA ]->[ POST_TO ],
763 $msg->[ MSG_DATA ]->[ POST_ARGS ],
764 );
765 } else {
766 # Find out the sessions
767 my @sessions = ();
768 if ( ! ref( $msg->[ MSG_DATA ]->[ POST_TO ]->[ DEST_SESSION ] ) ) {
769 push( @sessions, $msg->[ MSG_DATA ]->[ POST_TO ]->[ DEST_SESSION ] );
770 } else {
771 push( @sessions, @{ $msg->[ MSG_DATA ]->[ POST_TO ]->[ DEST_SESSION ] } );
772 }
773  
774 # Loop over the sessions
775 foreach my $ses ( @sessions ) {
776 # Authenticate it!
777 if ( Authenticate( $_[HEAP], $type, $ses, $msg->[ MSG_DATA ]->[ POST_TO ]->[ DEST_STATE ], undef, $fake_session ) ) {
778 # Okay, send it!
779 post_self(
780 $_[HEAP],
781 $fake_session,
782 [ $_[HEAP]->{'MYKERNEL'}, $ses, $msg->[ MSG_DATA ]->[ POST_TO ]->[ DEST_STATE ] ],
783 $msg->[ MSG_DATA ]->[ POST_ARGS ],
784 );
785 } else {
786 # Hook returned false, drop it
787 if ( DEBUG ) {
788 warn "Authentication hook returned false, not sending to session $ses";
789 }
790 }
791 }
792 }
793 }
794  
795 # Do whatever is necessary with this packet
796 SendMessage( $_[HEAP], $msg );
797  
798 # All done!
799 return 1;
800 }
801  
802 # Processes CALL messages
803 sub A_CALL {
804 # ARG0 = data packet, ARG1 = wheel id
805 my $msg = $_[ARG0];
806  
807 # Is it for us?
808 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_TO ] ) ) {
809 # Create the fake session
810 my $fake_session = POE::Component::Lightspeed::Hack::Session->new( @{ $msg->[ MSG_DATA ]->[ CALL_FROM ] } );
811  
812 # Which session are we sending this to?
813 my @sessions = ();
814 if ( ! ref( $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_SESSION ] ) ) {
815 # Are we doing all sessions?
816 if ( $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_SESSION ] eq '*' ) {
817 @sessions = GetSessions();
818 } else {
819 $sessions[0] = $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_SESSION ];
820 }
821 } else {
822 @sessions = @{ $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_SESSION ] };
823 }
824  
825 # Do our job!
826 foreach my $ses ( @sessions ) {
827 # Check if it's a multiple-alias session
828 if ( ref( $ses ) ) {
829 $ses = $ses->[0];
830 }
831  
832 # Okay, check if the session exists
833 my $sesreal = $_[KERNEL]->_resolve_session( $ses );
834 if ( ! defined $sesreal ) {
835 # Error...
836 if ( DEBUG ) {
837 warn "Unknown session: $ses\n" . Data::Dumper::Dumper( $msg );
838 }
839 next;
840 }
841  
842 # Authenticate or move on
843 if ( ! exists $_[HEAP]->{'AUTH'}->{'call'} or Authenticate( $_[HEAP], 'call', $ses, $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_STATE ], $msg->[ MSG_DATA ]->[ CALL_RSVP ], $fake_session ) ) {
844 # Call it!
845 my $args = $_[KERNEL]->lightspeed_fake_call(
846 $fake_session,
847 $sesreal,
848 $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_STATE ],
849 $msg->[ MSG_DATA ]->[ CALL_ARGS ],
850 );
851  
852 # Return it back to the RSVP
853 SendMessage( $_[HEAP], [
854 $msg->[ MSG_DATA ]->[ CALL_RSVP ]->[ DEST_KERNEL ], # MSG_TO
855 undef, # MSG_FROM
856 ACTION_CALLREPLY, # MSG_ACTION
857 [ # MSG_DATA
858 $msg->[ MSG_DATA ]->[ CALL_RSVP ], # CALLREPLY_TO
859 [ # CALLREPLY_FROM
860 $_[HEAP]->{'MYKERNEL'}, # FROM_KERNEL
861 $ses, # FROM_SESSION
862 $msg->[ MSG_DATA ]->[ CALL_TO ]->[ DEST_STATE ],# FROM_STATE
863 'Lightspeed.pm', # FROM_FILE
864 1, # FROM_LINE
865 ],
866 $args, # CALLREPLY_ARGS
867 ],
868 ] );
869 } else {
870 # Failed authentication
871 if ( DEBUG ) {
872 warn "Authentication hook returned false, not sending to session $ses";
873 }
874 }
875 }
876 }
877  
878 # Do whatever is necessary with this packet
879 SendMessage( $_[HEAP], $msg );
880  
881 # All done!
882 return 1;
883 }
884  
885 # processes INTROSPECTION messages
886 sub A_INTROSPECTION {
887 # ARG0 = data packet, ARG1 = wheel id
888 my $msg = $_[ARG0];
889  
890 # Is it for us?
891 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_TO ] ) ) {
892 # Create the fake session
893 my $fake_session = POE::Component::Lightspeed::Hack::Session->new( @{ $msg->[ MSG_DATA ]->[ INTROSPECTION_FROM ] } );
894  
895 # Okay, is it a SESSION or STATE request?
896 if ( $msg->[ MSG_DATA ]->[ INTROSPECTION_WHAT ] eq 'SESSION' ) {
897 # Authenticate or move on
898 if ( ! exists $_[HEAP]->{'AUTH'}->{'introspection'} or Authenticate( $_[HEAP], 'introspection', 'session', undef, $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ], $fake_session ) ) {
899 # Get the list of sessions and send it back!
900 my @sessions = GetSessions();
901  
902 # Now, is the rsvp towards ourself?
903 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ]->[ DEST_KERNEL ] ) ) {
904 # Send it to ourself...
905 post_self(
906 $_[HEAP],
907 $fake_session,
908 $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ],
909 [ $_[HEAP]->{'MYKERNEL'}, \@sessions ],
910 );
911 }
912  
913 # Send it back to the RSVP!
914 SendMessage( $_[HEAP], [
915 $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ]->[ DEST_KERNEL ], # MSG_TO
916 undef, # MSG_FROM
917 ACTION_POST, # MSG_ACTION
918 [ # MSG_DATA
919 $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ], # POST_TO
920 $msg->[ MSG_DATA ]->[ INTROSPECTION_FROM ], # POST_FROM
921 [ $_[HEAP]->{'MYKERNEL'}, \@sessions ], # POST_ARGS
922 ],
923 ] );
924 } else {
925 # Failed Authentication
926 if ( DEBUG ) {
927 warn "Authentication hook returned false, not doing introspection of sessions";
928 }
929 }
930 } elsif ( $msg->[ MSG_DATA ]->[ INTROSPECTION_WHAT ] eq 'STATE' ) {
931 # Authenticate or move on
932 if ( ! exists $_[HEAP]->{'AUTH'}->{'introspection'} or Authenticate( $_[HEAP], 'introspection', 'state', $msg->[ MSG_DATA ]->[ INTROSPECTION_ARGS ], $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ], $fake_session ) ) {
933 # Loop through the sessions
934 my %data = ();
935 my @sessions = ();
936  
937 # Is it the special '*' session flag?
938 if ( ! ref( $msg->[ MSG_DATA ]->[ INTROSPECTION_ARGS ] ) ) {
939 if ( $msg->[ MSG_DATA ]->[ INTROSPECTION_ARGS ] eq '*' ) {
940 @sessions = GetSessions();
941 } else {
942 $sessions[0] = $msg->[ MSG_DATA ]->[ INTROSPECTION_ARGS ];
943 }
944 } else {
945 @sessions = @{ $msg->[ MSG_DATA ]->[ INTROSPECTION_ARGS ] };
946 }
947  
948 foreach my $ses ( @sessions ) {
949 # Check if it's a multiple-alias session
950 if ( ref( $ses ) ) {
951 $ses = $ses->[0];
952 }
953  
954 # Is it a valid session?
955 my $sesreal = $_[KERNEL]->_resolve_session( $ses );
956 if ( ! defined $sesreal ) {
957 # Error...
958 if ( DEBUG ) {
959 warn "Unknown session: $ses";
960 }
961 next;
962 }
963  
964 # Okay, we have the session reference, HAX0R TIME!
965 $data{ $ses } = [ keys %{ $sesreal->[ POE::Session::SE_STATES ] } ];
966 }
967  
968 # Now, is the rsvp towards ourself?
969 if ( FindOurself( $_[HEAP]->{'MYKERNEL'}, $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ]->[ DEST_KERNEL ] ) ) {
970 # Send it to ourself...
971 post_self(
972 $_[HEAP],
973 $fake_session,
974 $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ],
975 [ $_[HEAP]->{'MYKERNEL'}, \%data ],
976 );
977 }
978  
979 # Send it back to the RSVP!
980 SendMessage( $_[HEAP], [
981 $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ]->[ DEST_KERNEL ], # MSG_TO
982 undef, # MSG_FROM
983 ACTION_POST, # MSG_ACTION
984 [ # MSG_DATA
985 $msg->[ MSG_DATA ]->[ INTROSPECTION_RSVP ], # POST_TO
986 $msg->[ MSG_DATA ]->[ INTROSPECTION_FROM ], # POST_FROM
987 [ $_[HEAP]->{'MYKERNEL'}, \%data ], # POST_ARGS
988 ],
989 ] );
990 } else {
991 # Failed Authentication
992 if ( DEBUG ) {
993 warn "Authentication hook returned false, not doing introspection of states";
994 }
995 }
996 } else {
997 # Unknown request!
998 if ( DEBUG ) {
999 warn "Unknown INTROSPECTION_WHAT -> " . Data::Dumper::Dumper( $msg );
1000 }
1001 }
1002 }
1003  
1004 # Do whatever is necessary with this packet
1005 SendMessage( $_[HEAP], $msg );
1006  
1007 # All done!
1008 return 1;
1009 }
1010  
1011 # Subroutine to authenticate whatever we can
1012 sub Authenticate {
1013 my( $heap, $type, $ses, $arg, $rsvp, $fake_session ) = @_;
1014  
1015 # Check if there's a hook
1016 if ( exists $heap->{'AUTH'}->{ $type } ) {
1017 # Check again
1018 if ( exists $heap->{'AUTH'}->{ $type }->{ $ses } ) {
1019 # post/callreply messages do not have RSVP
1020 if ( $type eq 'post' or $type eq 'callreply' ) {
1021 if ( $heap->{'AUTH'}->{ $type }->{ $ses }->( $type, $ses, $arg, $fake_session ) ) {
1022 return 1;
1023 } else {
1024 return undef;
1025 }
1026 } else {
1027 if ( $heap->{'AUTH'}->{ $type }->{ $ses }->( $type, $ses, $arg, $rsvp, $fake_session ) ) {
1028 return 1;
1029 } else {
1030 return undef;
1031 }
1032 }
1033 } else {
1034 # Try the catch-all hook
1035 if ( exists $heap->{'AUTH'}->{ $type }->{'*'} ) {
1036 # post/callreply messages do not have RSVP
1037 if ( $type eq 'post' or $type eq 'callreply' ) {
1038 if ( $heap->{'AUTH'}->{ $type }->{'*'}->( $type, $ses, $arg, $fake_session ) ) {
1039 return 1;
1040 } else {
1041 return undef;
1042 }
1043 } else {
1044 if ( $heap->{'AUTH'}->{ $type }->{'*'}->( $type, $ses, $arg, $rsvp, $fake_session ) ) {
1045 return 1;
1046 } else {
1047 return undef;
1048 }
1049 }
1050 } else {
1051 # No hook available
1052 return 1;
1053 }
1054 }
1055 } else {
1056 # No hook available
1057 return 1;
1058 }
1059 }
1060  
1061 # Helper sub to get the list of sessions with aliases
1062 sub GetSessions {
1063 # Big thanks to POE::API::Peek for this code
1064  
1065 my @sessions = ();
1066 my $kr_sessions = $poe_kernel->[ POE::Kernel::KR_SESSIONS ];
1067 foreach my $key ( keys %$kr_sessions ) {
1068 # Skip over the Kernel itself
1069 next if $key =~ /POE::Kernel/;
1070  
1071 # Get the alias
1072 my @list = $poe_kernel->alias_list( $kr_sessions->{ $key }->[ POE::Kernel::SS_SESSION ] );
1073 if ( defined $list[0] ) {
1074 push( @sessions, [ @list ] );
1075 }
1076 }
1077  
1078 # All done!
1079 return @sessions;
1080 }
1081  
1082 # Helper to find if we are the target of a destination
1083 sub FindOurself {
1084 my( $our, $dest ) = @_;
1085  
1086 # If destination is unspecified, then obviously not us
1087 if ( ! defined $dest ) {
1088 if ( DEBUG ) {
1089 Carp::confess( "Undefined destination!" );
1090 }
1091 return 0;
1092 }
1093  
1094 # Is it an arrayref?
1095 if ( ref( $dest ) ) {
1096 # Find ourself in it...
1097 foreach my $t ( @$dest ) {
1098 if ( $our eq $t ) {
1099 return 1;
1100 }
1101 }
1102  
1103 # Got here, didn't find it
1104 return 0;
1105 } else {
1106 # Is it *
1107 if ( $dest eq '*' ) {
1108 return 1;
1109 } elsif ( $dest eq $our ) {
1110 return 1;
1111 } else {
1112 return 0;
1113 }
1114 }
1115 }
1116  
1117 # Helper subroutine to parse destination scalars
1118 sub ValidateDestination {
1119 my $dest = shift;
1120  
1121 # Simple checks
1122 if ( ! defined $dest ) {
1123 return undef;
1124 }
1125  
1126 # Okay, if it is a simple scalar we pull the values out
1127 if ( ! ref( $dest ) ) {
1128 if( $dest =~ m|^poe://(.+)/(.+)/(.+)$|i ) {
1129 # Set the stuff
1130 $dest = [ $1, $2, $3 ];
1131 } elsif ( $dest =~ m|^poe://(.+)/(.+)/$|i ) {
1132 # Set the stuff
1133 $dest = [ $1, $2 ];
1134 } else {
1135 if ( DEBUG ) {
1136 warn "The string is malformed: $dest";
1137 }
1138 return undef;
1139 }
1140 } else {
1141 if ( ref( $dest ) eq 'HASH' ) {
1142 # Make sure we have all 3 parts
1143 unless ( exists $dest->{'KERNEL'} and exists $dest->{'SESSION'} and exists $dest->{'STATE'} ) {
1144 if ( DEBUG ) {
1145 warn "Destination does not have all 3 parts -" . Data::Dumper::Dumper( $dest );
1146 }
1147 return undef;
1148 } else {
1149 # Convert it into an array
1150 $dest = [
1151 $dest->{'KERNEL'},
1152 $dest->{'SESSION'},
1153 $dest->{'STATE'}
1154 ];
1155 }
1156 } elsif ( ref( $dest ) eq 'ARRAY' ) {
1157 # Make sure we have all 3 parts
1158 if ( scalar( @$dest ) != 3 ) {
1159 if ( DEBUG ) {
1160 warn "Destination does not have all 3 parts -" . Data::Dumper::Dumper( $dest );
1161 }
1162 return undef;
1163 }
1164 } else {
1165 # Unknown reference here...
1166 if ( DEBUG ) {
1167 warn "Destination is a unknown reference -" . Data::Dumper::Dumper( $dest );
1168 }
1169 return undef;
1170 }
1171 }
1172  
1173 # Convert multiple kernel arguments
1174 if ( ! ref( $dest->[0] ) ) {
1175 # If there's a comma, then we have a multi-kernel message
1176 if ( $dest->[0] =~ tr/,/,/ ) {
1177 # Split on the comma
1178 $dest->[0] = [ split( ',', $dest->[0] ) ];
1179 }
1180 } else {
1181 # Make sure it's an arrayref full of scalars
1182 if ( ref( $dest->[0] ) eq 'ARRAY' ) {
1183 foreach my $d ( @{ $dest->[0] } ) {
1184 if ( ref( $d ) ) {
1185 if ( DEBUG ) {
1186 warn "Destination kernel has some non-scalars in it -" . Data::Dumper::Dumper( $dest );
1187 }
1188 return undef;
1189 }
1190 }
1191 } else {
1192 if ( DEBUG ) {
1193 warn "Destination kernel is not an arrayref -" . Data::Dumper::Dumper( $dest );
1194 }
1195 return undef;
1196 }
1197 }
1198  
1199 # TODO Remove duplicate kernels
1200  
1201 # Convert multiple session arguments
1202 if ( ! ref( $dest->[1] ) ) {
1203 # If there's a comma, then we have a multi-session message
1204 if ( $dest->[1] =~ tr/,/,/ ) {
1205 # Split on the comma
1206 $dest->[1] = [ split( ',', $dest->[1] ) ];
1207 }
1208 } else {
1209 # Make sure it's an arrayref full of scalars
1210 if ( ref( $dest->[1] ) eq 'ARRAY' ) {
1211 foreach my $d ( @{ $dest->[1] } ) {
1212 if ( ref( $d ) ) {
1213 if ( DEBUG ) {
1214 warn "Destination session has some non-scalars in it -" . Data::Dumper::Dumper( $dest );
1215 }
1216 return undef;
1217 }
1218 }
1219 } else {
1220 if ( DEBUG ) {
1221 warn "Destination session is not an arrayref -" . Data::Dumper::Dumper( $dest );
1222 }
1223 return undef;
1224 }
1225 }
1226  
1227 # TODO Remove duplicate sessions
1228  
1229 # All done!
1230 return $dest;
1231 }
1232  
1233 # Sends the message!
1234 sub SendMessage {
1235 my( $heap, $msg ) = @_;
1236  
1237 # Add the To
1238 if ( ! defined $msg->[ MSG_TO ] ) {
1239 $msg->[ MSG_TO ] = [ $heap->{'GRAPH'}->vertices ];
1240 } elsif ( ! ref( $msg->[ MSG_TO ] ) ) {
1241 # Is it the broadcast kernel?
1242 if ( $msg->[ MSG_TO ] eq '*' ) {
1243 $msg->[ MSG_TO ] = [ $heap->{'GRAPH'}->vertices ];
1244 } else {
1245 # Convert it into an array
1246 $msg->[ MSG_TO ] = [ $msg->[ MSG_TO ] ];
1247 }
1248 }
1249  
1250 # For starters, strip out our kernel if it is there + remove any invalid kernels
1251 $msg->[ MSG_TO ] = [ grep { $_ ne $heap->{'MYKERNEL'} and $heap->{'GRAPH'}->has_vertex( $_ ) } @{ $msg->[ MSG_TO ] } ];
1252  
1253 # Nothing left?
1254 if ( scalar( @{ $msg->[ MSG_TO ] } ) == 0 ) {
1255 return 1;
1256 }
1257  
1258 # Add the From
1259 if ( ! defined $msg->[ MSG_FROM ] ) {
1260 $msg->[ MSG_FROM ] = $heap->{'MYKERNEL'};
1261 }
1262  
1263 # Add the realto field
1264 if ( ! defined $msg->[ MSG_REALTO ] ) {
1265 $msg->[ MSG_REALTO ] = [ @{ $msg->[ MSG_TO ] } ];
1266 }
1267  
1268 # Short-circuit it if only one kernel to send message to
1269 if ( scalar( @{ $msg->[ MSG_TO ] } ) == 1 ) {
1270 # Munge the MSG_TO field
1271 $msg->[ MSG_TO ] = $msg->[ MSG_TO ]->[0];
1272  
1273 # The MSG_REALTO field too
1274 $msg->[ MSG_REALTO ] = $msg->[ MSG_TO ];
1275  
1276 # Find the route
1277 my @route = $heap ->{'APSP'}->path_vertices( $heap->{'MYKERNEL'}, $msg->[ MSG_TO ] );
1278  
1279 # Send it off!
1280 $POE::Kernel::poe_kernel->post(
1281 $heap->{'WID_SID'}->{ $heap->{'KERNEL_WID'}->{ $route[1] } },
1282 'send',
1283 $heap->{'KERNEL_WID'}->{ $route[1] },
1284 $msg,
1285 );
1286 } else {
1287 # Find out all the paths we need
1288 my %route = ();
1289  
1290 foreach my $k ( @{ $msg->[ MSG_TO ] } ) {
1291 $route{ $k } = [ $heap ->{'APSP'}->path_vertices( $heap->{'MYKERNEL'}, $k ) ];
1292 }
1293  
1294 # Collect the routes that are on the same link
1295 my %link = ();
1296  
1297 foreach my $k ( keys %route ) {
1298 push( @{ $link{ $route{ $k }->[1] } }, $k );
1299 }
1300  
1301 # Now, send the message to all links
1302 foreach my $l ( keys %link ) {
1303 # Make a new message
1304 my $p = [
1305 undef,
1306 $msg->[ MSG_FROM ],
1307 $msg->[ MSG_ACTION ],
1308 $msg->[ MSG_DATA ],
1309 $msg->[ MSG_REALTO ],
1310 $msg->[ MSG_TIMESTAMP ],
1311 ];
1312  
1313 # Munge the TO-field
1314 if ( scalar( @{ $link{ $l } } ) == 1 ) {
1315 $p->[ MSG_TO ] = $link{ $l }->[0];
1316 } else {
1317 $p->[ MSG_TO ] = $link{ $l };
1318 }
1319  
1320 # Send it!
1321 $POE::Kernel::poe_kernel->post(
1322 $heap->{'WID_SID'}->{ $heap->{'KERNEL_WID'}->{ $l } },
1323 'send',
1324 $heap->{'KERNEL_WID'}->{ $l },
1325 $p,
1326 );
1327 }
1328 }
1329  
1330 # All done!
1331 return 1;
1332 }
1333  
1334 # Searches for something in an array
1335 sub Array_Search {
1336 # Get the pointer to array
1337 my $ary = shift;
1338  
1339 # Get the name
1340 my $name = shift;
1341  
1342 # Iterate over the array
1343 foreach my $tmp ( @{ $ary } ) {
1344 if ( $tmp eq $name ) {
1345 return 1;
1346 }
1347 }
1348  
1349 # Failed to find a match
1350 return 0;
1351 }
1352  
1353 # End of module
1354 1;
1355 __END__