HOE/POE/Kernel.pm untested

filter: Statementstmt 0.0% 0 / 632 Branchbran 0.0% 0 / 260 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 102 Podpod 0.0% 0 / 86 totaltotal 0.0% 0 / 1183 CC 131 SCAR 35.1
CC
131
Cov
0%
sig
156.0
run
110.0
call
90.0
CRAP 33.3
1 package POE::Kernel;
2  
3 use strict;
4 use warnings;
5  
6 use Time::HiRes;
7 use WeakRef;
8  
9 use POE::Callstack qw(CURRENT_SESSION CURRENT_EVENT);
10 use POE::Event;
11 use POE::Event::Signal;
12 use POE::Event::Alarm;
13 use POE::Session::Dead;
14  
15 use Carp qw(cluck croak);
16  
17 use Errno qw(EPERM ESRCH EEXIST);
18  
19 use vars qw($poe_kernel);
20  
21 # This is for silently making POE::Kernel->whatever from a package to an object call...
22 # it may not even be necessary... heck, it may not even work... should test that.
23 # Followup: It is necessary.
24 sub POE::Kernel {
25 return $poe_kernel;
26 }
27  
28 BEGIN {
29 if($ENV{'HOE_DEBUG'}) {
30 eval "sub DEBUGGING () { 1 }";
31 }
32 else {
33 eval "sub DEBUGGING () { 0 }";
34 }
35  
36 unless (__PACKAGE__->can('ASSERT_USAGE')) {
37 eval "sub ASSERT_USAGE () { 0 }";
38 }
39  
40 my $debug_file;
41
42 if(my $debug_filename = $ENV{'HOE_DEBUG_FILE'}) {
43 unless (open $debug_file, '>', $debug_filename) {
44 warn "can't open debug file '$debug_filename': $!";
45 $debug_file = \*STDERR;
46 }
47 CORE::select((CORE::select($debug_file), $| = 1)[0]);
48 }
49 else {
50 $debug_file = \*STDERR;
51 }
52  
53 sub DEBUG {
54 print $debug_file @_;
55 }
56 }
57  
58 sub RUNNING_IN_HELL () { 0 }
59  
60 sub CHECKING_INTEGRITY () { 1 }
61  
62 sub new {
63 my $class = shift;
64 my $self = bless [], (ref $class || $class);
65 $self->_init();
66  
67 return $self;
68 }
69  
70 my $counter = 0;
71  
72 sub _init {
73 my $self = shift;
74  
75 ++$counter;
76
77 @$self = (
78 {}, # Aliases
79 [], # Queue
80 {}, # FH Reads
81 {}, # FH Reads (Paused)
82 {}, # FH Writes
83 {}, # FH Writes (Paused)
84 {}, # FH Expedites
85 {}, # FH Expedites (Paused spot, DANGEROUS, necessary for now)
86 {}, # Refs
87 {}, # IDS
88 {}, # Sessions
89 {}, # Signals
90 {}, # Parent Session, by child session
91 {}, # Child Sessions hashref "key"==value (weakened values), by parent session
92 "THE KERNEL $counter", # ID
93 );
94 }
95  
96 sub KR_ALIASES () { 0 }
97 sub KR_QUEUE () { 1 }
98 sub KR_FH_READS () { 2 }
99 sub KR_FH_READS_PAUSED () { 3 }
100 sub KR_FH_WRITES () { 4 }
101 sub KR_FH_WRITES_PAUSED () { 5 }
102 sub KR_FH_EXPEDITES () { 6 }
103 sub KR_FH_EXPEDITES_NASTY () { 7 } # I have to make this exist to prevent crashes until I make watcher objects.
104 sub KR_REFS () { 8 }
105 sub KR_IDS () { 9 }
106 sub KR_SESSIONS () { 10 }
107 sub KR_SIGNALS () { 11 }
108 sub KR_PARENTS () { 12 }
109 sub KR_CHILDREN () { 13 }
110 sub KR_ID () { 14 }
111  
112 sub import {
113 my $package = caller();
114 no strict 'refs';
115 my $export_kernel = $poe_kernel;
116 *{ "${package}::poe_kernel" } = \$export_kernel;
117 weaken( ${ "${package}::poe_kernel" } );
118 }
119  
120 {
121 my $next_id = 1;
122 my %hijacked_namespaces;
123
124 sub session_alloc {
125 my $self = shift;
126 my $session = shift;
127 my @args = @_;
128 my $id = $next_id++;
129 weaken($self->[KR_IDS]->{$id} = $session);
130 $self->[KR_SESSIONS]->{$session} = $id;
131  
132 my $parent = CURRENT_SESSION;
133  
134 $self->[KR_PARENTS]->{$session} = $parent;
135 weaken($self->[KR_CHILDREN]->{$parent}->{$session} = $session);
136
137 # Who is SENDER in this case?
138 my $result = $self->call( $session, '_start', @args );
139
140 # $parent could be the Kernel, \@result may not be correct, see POE::Session docs which are vague.
141 $self->call( $parent, '_child', 'create', $session, $result );
142  
143 no strict 'refs';
144 no warnings 'redefine';
145 my $package = ref($session);
146 unless (exists $hijacked_namespaces{$package}) {
147 my $old_destroy = *{"${package}::DESTROY"}{CODE};
148 *{"${package}::DESTROY"} = sub {
149 my $inner_self = shift;
150 defined( $POE::Kernel::poe_kernel ) and
151 $POE::Kernel::poe_kernel->session_dealloc( $inner_self );
152 return unless $old_destroy;
153 $old_destroy->($inner_self);
154 bless $inner_self, 'POE::Session::Dead';
155 };
156 $hijacked_namespaces{$package} = undef;
157 }
158 }
159 }
160  
161 sub session_dealloc {
162 my $self = shift;
163 my $session = shift;
164 DEBUG "[SESSION] Deallocating $session\n" if DEBUGGING;
165  
166 if (exists $self->[KR_SESSIONS]->{$session}) {
167 my @result = $self->call( $session, '_stop' );
168 my $parent = $self->[KR_PARENTS]->{$session};
169  
170 # $parent could be the Kernel, the args list is vaguely documented in POE::Session... not sure this is correct
171 $self->call( $parent, '_child', 'lose', $session, \@result );
172 }
173 $self->cleanup_session( $session );
174 }
175  
176 sub cleanup_session {
177 my $self = shift;
178 my $session = shift;
179  
180 # We could destroy the innards of the session and that may help clean things up
181  
182 if (my $id = $self->[KR_SESSIONS]->{$session} ) {
183 delete $self->[KR_SESSIONS]->{$session};
184 delete $self->[KR_IDS]->{$id};
185 }
186 if (my $parent = delete $self->[KR_PARENTS]->{$session}) {
187 delete $self->[KR_CHILDREN]->{$parent}->{$session};
188 }
189 return;
190 }
191  
192 sub ID {
193 my $self = shift;
194 return $self->[KR_ID];
195 }
196  
197 sub ID_session_to_id {
198 my $self = shift;
199 my $session = shift;
200
201 return $self->[KR_SESSIONS]->{$session};
202 }
203  
204 sub ID_id_to_session {
205 my $self = shift;
206 my $id = shift;
207  
208 return $self->[KR_IDS]->{$id};
209 }
210  
211 sub get_active_session {
212 return CURRENT_SESSION;
213 }
214  
215 sub get_active_event {
216 return CURRENT_EVENT;
217 }
218  
219 sub get_children {
220 my $self = shift;
221 my $parent = shift;
222  
223 unless (defined( $parent )) {
224 cluck( "Undefined parent to find children of\n" );
225 }
226
227 if (exists $self->[KR_CHILDREN]->{$parent}) {
228 return values %{$self->[KR_CHILDREN]->{$parent}};
229 }
230 return (); # return empty list or undef... empty list prevents recursion problems... undef seems more correct
231 }
232  
233 sub stop {
234 # If this gets called from within an event dispatched by a signal event,
235 # then it won't stop till the signal event is finished dispatching, dangerous
236 my $self = shift;
237 foreach my $child ($self->get_children($self)) {
238 $self->cleanup_session( $child );
239 }
240
241 # all kernel structures need to be purged in the same way, yucky, but very necessary
242 @{$self->[KR_QUEUE]} = ();
243 $self->_init();
244 initialize_kernel();
245 }
246  
247 sub detach_child {
248 my $self = shift;
249 my $session = shift;
250  
251 if (grep { $session == $_ } $self->get_children( CURRENT_SESSION )) {
252 return _internal_detach( $session );
253 }
254 else {
255 $! = EPERM;
256 return 0;
257 }
258 }
259  
260 sub detach_myself {
261 my $self = shift;
262 return _internal_detach( CURRENT_SESSION );
263 }
264  
265 sub _internal_detach {
266 my $self = shift;
267 my $session = shift;
268  
269 my $parents = $self->[KR_PARENTS];
270 my $children = $self->[KR_CHILDREN];
271  
272 if (exists( $parents->{$session} )) {
273 my $old_parent = $parents->{$session};
274
275 if ($old_parent == $self) {
276 $! = EPERM;
277 return 0;
278 }
279 else {
280 $self->call( $old_parent, '_child', 'lose', $session );
281 delete( $children->{$old_parent}->{$session} );
282  
283 # Prevent leaks
284 keys( %{$children->{$old_parent}} ) or delete $children->{$old_parent};
285  
286 $self->[KR_PARENTS]->{$session} = $self;
287 weaken($self->[KR_CHILDREN]->{$self}->{$session} = $session);
288  
289 $self->call( $session, '_parent', $old_parent, $self );
290 # This is always the kernel... what the heck is this event for?
291 #$self->call( $self, '_child', 'gain', $session );
292
293 return 1;
294 }
295 }
296 else {
297 $! = ESRCH;
298 return 0;
299 }
300 }
301  
302 sub post {
303 my $self = shift;
304 my ($to, $state, @etc) = @_;
305  
306 die "destination is undefined in post" unless(defined( $to ));
307 die "event is undefined in post" unless(defined( $state ));
308  
309 # Name resolution /could/ happen during dispatch instead, I think everything would stay alive just fine simply because kernel is embedded in the event, and the sessions are all held inside aliases within that.
310 my $from = CURRENT_SESSION;
311 my $queue = $self->[KR_QUEUE];
312 @$queue = sort { $a <=> $b } (@$queue, POE::Event->new( $self, time, $from, $to, $state, \@etc ));
313  
314 DEBUG "[POST] Kernel: $self From: $from To: $to State: $state Args: @etc\n" if DEBUGGING;
315 }
316  
317 sub yield {
318 my $self = shift;
319 my ($state, @etc) = @_;
320  
321 die "event name is undefined in yield" unless(defined( $state ));
322  
323 my $from = CURRENT_SESSION;
324 my $queue = $self->[KR_QUEUE];
325 @$queue = sort { $a <=> $b } (@$queue, POE::Event->new( $self, time, $from, $from, $state, \@etc ));
326
327 DEBUG "[YIELD] Kernel $self From/To: $from State: $state\n" if DEBUGGING;
328 }
329  
330 sub call {
331 my $self = shift;
332 my ($to, $state, @etc) = @_;
333  
334 croak( "destination undefined in call" ) unless(defined( $to ));
335 croak( "event undefined in call" ) unless(defined( $to ));
336  
337 DEBUG "[CALL] Kernel: $self To: $to State: $state\n" if DEBUGGING;
338 my $return;
339 my @return;
340 my $wantarray = wantarray;
341  
342 my $event = POE::Event->new( $self, undef, CURRENT_SESSION, $to, $state, \@etc );
343  
344 if (defined( $wantarray )) {
345 if ($wantarray) {
346 @return = $event->dispatch();
347 }
348 else {
349 $return = $event->dispatch();
350 }
351 }
352 else {
353 $event->dispatch();
354 }
355
356 DEBUG "[CALL] Completed\n" if DEBUGGING;
357  
358 if (defined( $wantarray )) {
359 if ($wantarray) {
360 return @return;
361 }
362 else {
363 return $return;
364 }
365 }
366 else {
367 return;
368 }
369 }
370  
371 sub resolve_session {
372 my $self = shift;
373 my $input = $_[0];
374
375 my $aliases = $self->[KR_ALIASES];
376 my $ids = $self->[KR_IDS];
377  
378 DEBUG( "[RESOLVE] Input: $input\n" ) if DEBUGGING;
379  
380 unless( $input ) {
381 cluck( "Undefined state resolution attempted\n" );
382 }
383  
384 if (ref( $input ) and $input->can('_invoke_state')) {
385 return $input;
386 }
387 elsif (exists( $aliases->{$input} )) {
388 return $aliases->{$input};
389 }
390 elsif (exists( $ids->{$input} )) {
391 return $ids->{$input};
392 }
393 else {
394 # return $input;
395 return undef; # shouldn't this be more correct?
396 }
397 }
398  
399 sub _select_any {
400 my $self = shift;
401 my $class = shift;
402 my $fh = shift;
403 my $event = shift;
404  
405 my $fd = fileno($fh);
406  
407 my $current_session = CURRENT_SESSION;
408  
409 unless (defined( $current_session )) {
410 DEBUG "[[[BAD]]] Current session undefined, global destruction?\n";
411 return;
412 }
413
414 my $main_class = $self->[$class];
415 my $paused_class = $self->[$class + 1];
416  
417 if ($event) { # Setup watcher
418 DEBUG "[WATCH] Watch Fd: $fd Fh: $fh Class: $class Event: $event\n" if DEBUGGING;
419 unless (exists $main_class->{$fd} and ref $main_class->{$fd} eq 'ARRAY') {
420 $main_class->{$fd} = [];
421 }
422  
423 unless (grep { $_->{session} == $current_session } @{$main_class->{$fd}}) {
424 push @{$main_class->{$fd}}, {
425 kernel => $self,
426 session => $current_session,
427 fd => $fd,
428 fh => $fh,
429 event => $event,
430 };
431 }
432 }
433 else { # Clear watcher
434 DEBUG "[WATCH] Stop Fd: $fd Fh: $fh Class: $class\n" if DEBUGGING;
435 @{$main_class->{$fd}} = grep {
436 defined( $_->{session} ) and $_->{session} != $current_session
437 } (@{$main_class->{$fd}});
438
439 @{$paused_class->{$fd}} = grep {
440 defined( $_->{session} ) and $_->{session} != $current_session
441 } (@{$paused_class->{$fd}});
442  
443 unless (@{$main_class->{$fd}}) {
444 delete $main_class->{$fd};
445 }
446 unless (@{$paused_class->{$fd}}) {
447 delete $paused_class->{$fd};
448 }
449 }
450 }
451  
452 sub select_read {
453 my $self = shift;
454 DEBUG "[WATCH] Read @_\n" if DEBUGGING;
455 $self->_select_any( KR_FH_READS, @_ );
456 }
457  
458 sub select_write {
459 my $self = shift;
460 DEBUG "[WATCH] Write @_\n" if DEBUGGING;
461 $self->_select_any( KR_FH_WRITES, @_ );
462 }
463  
464 sub select_expedite {
465 my $self = shift;
466 DEBUG "[WATCH] Expedite @_\n" if DEBUGGING;
467 $self->_select_any( KR_FH_EXPEDITES, @_ );
468 }
469  
470 sub select {
471 my $self = shift;
472 my $fh = shift;
473  
474 # $self->
475 # if (@_ == 3) {
476 my ($read, $write, $expedite) = @_;
477  
478 $self->select_read( $fh, $read );
479 $self->select_write( $fh, $write );
480 $self->select_expedite( $fh, $expedite );
481 # }
482 # elsif (@_ == 0) {
483 # $self->select_read( $fh );
484 # $self->select_write( $fh );
485 # $self->select_expedite( $fh );
486 # }
487 # else {
488 # die();
489 # }
490 }
491  
492 sub _select_pause_any {
493 my $self = shift;
494 my $class = shift;
495 my $fh = shift;
496 my $fd = fileno( $fh );
497  
498 my $main_class = $self->[$class];
499 my $paused_class = $self->[$class + 1];
500  
501 $paused_class->{$fd} = $main_class->{$fd};
502 delete $main_class->{$fd};
503 }
504  
505 sub select_pause_read {
506 my $self = shift;
507 DEBUG "[WATCH] Read pause: @_\n" if DEBUGGING;
508 $self->_select_pause_any( KR_FH_READS, @_ );
509 }
510  
511 sub select_pause_write {
512 my $self = shift;
513 DEBUG "[WATCH] Write pause: @_\n" if DEBUGGING;
514 $self->_select_pause_any( KR_FH_WRITES, @_ );
515 }
516  
517 sub _select_resume_any {
518 my $self = shift;
519 my $class = shift;
520 my $fh = shift;
521 my $fd = fileno( $fh );
522  
523 my $main_class = $self->[$class];
524 my $paused_class = $self->[$class + 1];
525  
526 $main_class->{$fd} = $paused_class->{$fd};
527 delete $paused_class->{$fd};
528 }
529  
530 sub select_resume_read {
531 my $self = shift;
532 DEBUG "[WATCH] Read resume: @_\n" if DEBUGGING;
533 $self->_select_resume_any( KR_FH_READS, @_ );
534 }
535  
536 sub select_resume_write {
537 my $self = shift;
538 DEBUG "[WATCH] Write resume: @_\n" if DEBUGGING;
539 $self->_select_resume_any( KR_FH_WRITES, @_ );
540 }
541  
542 sub delay {
543 my @stuff = @_;
544  
545 DEBUG( "[ALARM] delay\n" ) if DEBUGGING;
546
547 die unless $_[1];
548  
549 _internal_alarm_destroy(@stuff);
550  
551 my $result;
552  
553 if (defined( $_[2] )) {
554 $stuff[2] += time;
555 $result = _internal_alarm_add(@stuff);
556 }
557  
558 return 0 if (defined( $result ));
559 }
560  
561 sub delay_add {
562 my @stuff = @_;
563
564 DEBUG( "[ALARM] delay_add\n" ) if DEBUGGING;
565
566 die unless $_[1];
567 die unless $_[2];
568  
569 $stuff[2] += time;
570  
571 return 0 if (defined( _internal_alarm_add(@stuff) ));
572 }
573  
574 sub alarm {
575 DEBUG( "[ALARM] alarm\n" ) if DEBUGGING;
576  
577 die unless $_[1];
578  
579 _internal_alarm_destroy(@_);
580  
581 my $result;
582
583 if (defined( $_[2] )) {
584 _internal_alarm_add(@_);
585 }
586  
587 return 0 if (defined( $result ));
588 }
589  
590 sub alarm_add {
591 DEBUG( "[ALARM] alarm_add\n" ) if DEBUGGING;
592  
593 die unless $_[1];
594 die unless $_[2];
595  
596 return 0 if (defined( _internal_alarm_add(@_) ));
597 }
598  
599 sub _internal_alarm_add {
600 my ($self, $name, $seconds, @args) = @_;
601  
602 DEBUG( "[ALARM] _internal_alarm_add\n" ) if DEBUGGING;
603  
604 my $queue = $self->[KR_QUEUE];
605  
606 my $current_session = CURRENT_SESSION;
607  
608 my $event = POE::Event::Alarm->new(
609 $self,
610 $seconds,
611 $current_session,
612 $current_session,
613 $name,
614 \@args,
615 time,
616 );
617  
618 @$queue = sort { $a <=> $b } (
619 @$queue,
620 $event
621 );
622  
623 return $event->alarm_id;
624 }
625  
626 sub _internal_alarm_destroy {
627 my ($self, $event) = @_;
628  
629 my $queue = $self->[KR_QUEUE];
630  
631 my $current_session = CURRENT_SESSION;
632  
633 # This algorithm is completely wrong, I don't know what I was thinking when I wrote it
634
635 @$queue = grep { not ( $_->can('alarm_id') and $current_session == $_->from and $event eq $_->name() ) } @$queue;
636 }
637  
638 sub alarm_adjust {
639 my ($self, $alarm_id, $delta) = @_;
640  
641 DEBUG( "[ALARM] Adjusting $alarm_id by $delta\n" ) if DEBUGGING;
642
643 my $queue = $self->[KR_QUEUE];
644  
645 my @alarms = grep { $_->can('alarm_id') and $_->alarm_id == $alarm_id } @$queue;
646  
647 if (@alarms == 1) {
648 return $alarms[0]->adjust_when( $delta );
649 }
650 }
651  
652 sub alarm_set {
653 DEBUG( "[ALARM] Setting Alarm @_\n" ) if DEBUGGING;
654 return _internal_alarm_add(@_);
655 }
656  
657 sub alarm_remove {
658 my ($self, $alarm_id) = @_;
659  
660 my $queue = $self->[KR_QUEUE];
661 my @events;
662  
663 my $current_session = CURRENT_SESSION;
664  
665 DEBUG( "[ALARM] Attempting removal of ID# $alarm_id from $current_session\n" ) if DEBUGGING;
666  
667 @$queue = map {
668 DEBUG( "[ALARM] Iterating: $_ from " . $_->from . "\n" ) if DEBUGGING;
669 if ($_->can('alarm_id') and $_->alarm_id == $alarm_id and $current_session == $_->from) {
670 push @events, $_;
671 ();
672 }
673 else {
674 $_;
675 }
676 } @$queue;
677  
678 DEBUG( "[ALARM] " . @events . " matching events found, and removed\n" ) if DEBUGGING;
679  
680 if (@events == 1) {
681 my $event = shift @events;
682 my $things = [ $event->name, $event->when, $event->args ];
683  
684 if (wantarray) {
685 return @$things;
686 }
687 else {
688 return $things;
689 }
690 }
691  
692 return;
693 }
694  
695 sub alarm_remove_all {
696 my ($self) = @_;
697  
698 my $queue = $self->[KR_QUEUE];
699 my @events;
700  
701 my $current_session = CURRENT_SESSION;
702  
703 DEBUG( "[ALARM] alarm_remove_all\n" ) if DEBUGGING;
704  
705 @$queue = map {
706 if ($_->can('alarm_id') and $current_session == $_->from) {
707 push @events, $_;
708 ();
709 }
710 else {
711 $_;
712 }
713 } @$queue;
714  
715 DEBUG( "[ALARM] alarm_remove_all: removed " . @events . " alarm events for $current_session.\n" ) if DEBUGGING;
716  
717 my $things = [ map { [ $_->name, $_->when, $_->args ] } @events ];
718  
719 if (wantarray) {
720 return @$things;
721 }
722 else {
723 return $things;
724 }
725 }
726  
727 sub delay_set {
728 my @stuff = @_;
729  
730 DEBUG( "[ALARM] delay_set\n" ) if DEBUGGING;
731
732 die unless $_[1];
733 die unless $_[2];
734  
735 $stuff[2] += time;
736  
737 _internal_alarm_add(@stuff);
738 }
739  
740 sub delay_adjust {
741 my ($self, $alarm_id, $when) = @_;
742  
743 DEBUG( "[ALARM] delay_adjust\n" ) if DEBUGGING;
744  
745 my $queue = $self->[KR_QUEUE];
746  
747 my @alarms = grep { $_->can('alarm_id') and $_->alarm_id == $alarm_id } @$queue;
748  
749 if (@alarms == 1) {
750 return $alarms[0]->set_when( $when + time );
751 }
752 }
753  
754 sub run {
755 my $self = shift;
756 DEBUG "[KERNEL] Starting Loop\n" if DEBUGGING;
757 weaken( $poe_kernel ) unless isweak( $poe_kernel );
758  
759 my $queue = $self->[KR_QUEUE];
760 my $fh_reads = $self->[KR_FH_READS];
761 my $fh_preads = $self->[KR_FH_READS_PAUSED];
762 my $fh_writes = $self->[KR_FH_WRITES];
763 my $fh_pwrites = $self->[KR_FH_WRITES_PAUSED];
764 my $fh_expedites = $self->[KR_FH_EXPEDITES];
765 my $signals = $self->[KR_SIGNALS];
766  
767 while (
768 @$queue or
769 keys %$fh_reads or
770 keys %$fh_preads or
771 keys %$fh_writes or
772 keys %$fh_pwrites or
773 keys %$fh_expedites or
774 keys %$signals
775 ) {
776 my $when;
777 while (@$queue) {
778 $when = $queue->[0]->when();
779 my $now = time;
780 if ($when <= $now) {
781 my $event = shift @$queue;
782 my $from = $event->from;
783 my $name = $event->name;
784 DEBUG "[DISPATCH] $event @$event From: $from Event: $name Args: " . join(',', $event->args) . "\n" if DEBUGGING;
785 $event->dispatch();
786 DEBUG "[DISPATCH] Completed\n" if DEBUGGING;
787 if (@$queue) {
788 $when = $queue->[0]->when();
789 }
790 }
791 else {
792 last;
793 }
794 }
795
796 if (defined( $when )) {
797 $when -= time;
798 if ($when < 0) {
799 $when = 0;
800 }
801 }
802
803 $self->_select($when);
804 }
805
806 POE::Callstack::POP;
807 POE::Callstack::CLEAN;
808 POE::Callstack::PUSH( $poe_kernel );
809  
810 DEBUG "[RUN] Kernel exited cleanly\n" if DEBUGGING;
811 }
812  
813 sub run_one_timeslice {
814 my $self = shift;
815  
816 my $queue = $self->[KR_QUEUE];
817  
818 $self->_select( 0 );
819  
820 my $when;
821
822 while (@$queue) {
823 $when = $queue->[0]->when();
824 if ($when <= time) {
825 my $event = shift @$queue;
826 my $from = $event->from;
827 my $name = $event->name;
828 $event->dispatch();
829 if (@$queue) {
830 $when = $queue->[0]->when();
831 }
832 }
833 else {
834 last;
835 }
836 }
837  
838 if (defined( $when )) {
839 $when -= time;
840 if ($when < 0) {
841 $when = 0;
842 }
843 }
844  
845 return $when;
846 }
847  
848 sub _select {
849 my $self = shift;
850 my $timeout = shift;
851  
852 my $reads = $self->[KR_FH_READS];
853 my $preads = $self->[KR_FH_READS_PAUSED];
854 my $writes = $self->[KR_FH_WRITES];
855 my $pwrites = $self->[KR_FH_WRITES_PAUSED];
856 my $expedites = $self->[KR_FH_EXPEDITES];
857 my $signals = $self->[KR_SIGNALS];
858  
859 my $rin = my $win = my $ein = '';
860
861 my $read_count = 0;
862 foreach my $fd (keys %$reads) {
863 $read_count++;
864 vec($rin, $fd, 1) = 1;
865 }
866  
867 my $pread_count = 0;
868 foreach my $fd (keys %$preads) {
869 $pread_count++;
870 }
871  
872 my $write_count = 0;
873 foreach my $fd (keys %$writes) {
874 $write_count++;
875 vec($win, $fd, 1) = 1;
876 }
877  
878 my $pwrite_count = 0;
879 foreach my $fd (keys %$pwrites) {
880 $pwrite_count++;
881 }
882  
883 my $expedite_count = 0;
884 foreach my $fd (keys %$expedites) {
885 $expedite_count++;
886 vec($ein, $fd, 1) = 1;
887 }
888  
889 my $signal_count = 0;
890 foreach my $signal (keys %$signals) {
891 $signal_count++;
892 }
893  
894 if (DEBUGGING) {
895 if (defined( $timeout )) {
896 DEBUG "[POLL] Waiting a maximum of $timeout for $read_count reads, $pread_count paused reads, $write_count writes, $pwrite_count paused writes, $expedite_count expedite reads, and $signal_count signals.\n";
897 }
898 else {
899 DEBUG "[POLL] Waiting for $read_count reads, $pread_count paused reads, $write_count writes, $pwrite_count paused writes, $expedite_count expedite reads, and $signal_count signals.\n";
900 }
901 }
902  
903 my $nfound = CORE::select( my $rout = $rin, my $wout = $win, my $eout = $ein, $timeout );
904  
905 while (my ($fd, $watchers) = each %$reads) {
906 if (vec( $rout, $fd, 1 )) {
907 foreach my $watcher (@$watchers) {
908 $self->post( $watcher->{session}, $watcher->{event}, $watcher->{fh} );
909 }
910 }
911 }
912 while (my ($fd, $watchers) = each %$writes) {
913 if (vec( $wout, $fd, 1 )) {
914 foreach my $watcher (@$watchers) {
915 $self->post( $watcher->{session}, $watcher->{event}, $watcher->{fh} );
916 }
917 }
918 }
919 while (my ($fd, $watchers) = each %$expedites) {
920 if (vec( $eout, $fd, 1 )) {
921 foreach my $watcher( @$watchers) {
922 $self->post( $watcher->{session}, $watcher->{event}, $watcher->{fh} );
923 }
924 }
925 }
926 }
927  
928 sub alias_set {
929 my $self = shift;
930 my $alias = $_[0];
931
932 my $session = CURRENT_SESSION;
933 my $aliases = $self->[KR_ALIASES];
934  
935 if (exists( $aliases->{$alias} )) {
936 if ($aliases->{$alias} == $session) {
937 return 0;
938 }
939 else {
940 return EEXIST;
941 }
942 }
943 else {
944 $self->[KR_ALIASES]->{$alias} = $session;
945 return 0;
946 }
947  
948 # We can either hook into the session and wait for a DESTROY event to come back to us to clean up the alias, or we can stipulate that the session object must clean up all of it's own aliases. The latter may be better for speed and clean code.
949 }
950
951 sub alias_remove {
952 my $self = shift;
953 my $alias = $_[0];
954  
955 croak( "Called alias_remove with no arguments\n" ) unless @_;
956  
957 my $aliases = $self->[KR_ALIASES];
958  
959 if (exists( $aliases->{$alias} )) {
960 if ($aliases->{$alias} == CURRENT_SESSION) {
961 delete $aliases->{$alias};
962 return 0
963 }
964 else {
965 return EPERM;
966 }
967 }
968 else {
969 return ESRCH;
970 }
971 }
972  
973 sub alias_resolve {
974 my $self = shift;
975 my $alias = $_[0];
976  
977 my $aliases = $self->[KR_ALIASES];
978 my $ids = $self->[KR_IDS];
979 my $sessions = $self->[KR_SESSIONS];
980  
981 if (exists( $aliases->{$alias} )) {
982 return $aliases->{$alias};
983 }
984 elsif (exists( $ids->{$alias} )) {
985 return $ids->{$alias};
986 }
987 elsif (exists( $sessions->{$alias} )) {
988 return $ids->{$sessions->{$alias}};
989 }
990  
991 $! = ESRCH;
992 return undef;
993 }
994  
995 sub alias_list {
996 my $self = shift;
997 my $session = (@_ ? shift : CURRENT_SESSION);
998  
999 my $aliases = $self->[KR_ALIASES];
1000
1001 return grep { $aliases->{$_} == $session } keys %$aliases;
1002 }
1003  
1004 sub _data_alias_loggable {
1005 my $self = shift;
1006 my $session = shift;
1007  
1008 my @aliases = $self->alias_list( $session );
1009  
1010 "session " . $session->ID . " (" .
1011 ( @aliases
1012 ? join( ", ", @aliases )
1013 : $session
1014 ) . ")"
1015 }
1016  
1017 sub _warn {
1018 # DEBUG( @_ );
1019 }
1020  
1021 sub refcount_increment {
1022 my $self = shift;
1023 my $session = CURRENT_SESSION;
1024 my $refs = $self->[KR_REFS];
1025 unless (exists $refs->{$session} and ref $refs->{$session} eq 'ARRAY') {
1026 $refs->{$session} = [];
1027 }
1028  
1029 push @{$refs->{$session}}, $session;
1030 }
1031  
1032 sub refcount_decrement {
1033 my $self = shift;
1034 my $session = CURRENT_SESSION;
1035 my $refs = $self->[KR_REFS];
1036  
1037 shift @{$refs->{$session}};
1038  
1039 unless (@{$refs->{$session}}) {
1040 delete $refs->{$session};
1041 }
1042 }
1043  
1044 sub state {
1045 my $self = shift;
1046 my $session = CURRENT_SESSION;
1047  
1048 return $session->register_state( @_ );
1049 }
1050  
1051 sub register_state {
1052 my $self = shift;
1053 DEBUG "[[[BAD]]] State removal attempted as kernel, @_\n";
1054 }
1055  
1056 sub sig {
1057 my $self = shift;
1058 my $signal_name = shift;
1059 my $event = shift;
1060  
1061 if (ASSERT_USAGE) {
1062 die "undefined signal in sig" unless(defined( $signal_name ));
1063 }
1064  
1065 my $signals = $self->[KR_SIGNALS];
1066
1067 my $session = CURRENT_SESSION;
1068  
1069 if ($event) {
1070 DEBUG "[SIGNAL] Session: $session Signal: $signal_name Event: $event\n" if DEBUGGING;
1071 unless (exists( $signals->{$signal_name} )) {
1072 $signals->{$signal_name} = {};
1073 }
1074 my $watcher = $signals->{$signal_name}->{$session} = [ $session, $event ];
1075  
1076 # weaken( $watcher->[0] );
1077  
1078 if ($signal_name eq 'CHLD' or $signal_name eq 'CLD') {
1079 $self->_install_chld_handler;
1080 }
1081 elsif (exists( $SIG{$signal_name} )) {
1082 $self->_install_sig_handler( $signal_name );
1083 }
1084 }
1085 else {
1086 DEBUG "[SIGNAL] Session: $session Signal: $signal_name\n" if DEBUGGING;
1087 if (exists( $signals->{$signal_name} )) {
1088 delete $signals->{$signal_name}->{$session};
1089 }
1090 unless( keys %{$signals->{$signal_name}} ) {
1091 delete $signals->{$signal_name};
1092 $SIG{$signal_name} = "DEFAULT" if exists $SIG{$signal_name};
1093 }
1094 }
1095 }
1096  
1097 sub signal {
1098 my $self = shift;
1099 my $session = shift;
1100 my $signal = $_[0];
1101 my @args = @_;
1102
1103 my $queue = $self->[KR_QUEUE];
1104 @$queue = sort { $a <=> $b } (
1105 @$queue,
1106 POE::Event::Signal->new(
1107 $self,
1108 time(),
1109 CURRENT_SESSION,
1110 $session,
1111 $signal,
1112 \@args,
1113 )
1114 );
1115 }
1116  
1117 sub signal_ui_destroy {
1118 die( "Not implemented at this time\n" );
1119 }
1120  
1121 use POSIX ":sys_wait_h";
1122  
1123 sub _install_chld_handler {
1124 DEBUG "Installing CHLD Handler\n" if DEBUGGING;
1125 my $kernel = shift;
1126
1127 $SIG{CHLD} = sub {
1128 # Since this could happen between any two perl opcodes we should localize the error variable... waitpid plays with it.
1129 local $!;
1130 DEBUG( "Got CHLD SIGNAL\n" ) if DEBUGGING;
1131 my $child;
1132 while (($child = waitpid( -1, WNOHANG)) > 0) {
1133 my $status = $?;
1134 my $watchers = $kernel->[KR_SIGNALS]->{CHLD};
1135 DEBUG( "Reaped pid $child with result $status\n" ) if DEBUGGING;
1136 while (my ($session, $watcher) = each %$watchers) {
1137 DEBUG( " Dispatching 'CHLD' to $watcher->[0]\n" ) if DEBUGGING;
1138 $kernel->signal( $watcher->[0], 'CHLD', $child, $status );
1139 }
1140 }
1141 DEBUG( "waitpid( -1, WNOHANG ) ended with status $child ($!)\n" ) if DEBUGGING;
1142 $kernel->_install_chld_handler; # This line could be keeping the kernel alive wrongly, not sure.
1143 };
1144 }
1145  
1146 sub _install_sig_handler {
1147 my $kernel = shift;
1148 my $signal_name = shift;
1149 my @args = @_;
1150  
1151 $SIG{$signal_name} = sub {
1152 my $watchers = $kernel->[KR_SIGNALS]->{$signal_name};
1153 while (my ($session, $watcher) = each %$watchers) {
1154 $kernel->signal( $watcher->[0], $signal_name, @args );
1155 }
1156 $kernel->_install_sig_handler( $signal_name );
1157 }
1158 }
1159  
1160 sub _data_sig_get_safe_signals {
1161 return keys %SIG;
1162 }
1163  
1164 sub sig_handled {
1165 POE::Event::Signal::HANDLED(1);
1166 }
1167  
1168 sub DESTROY {
1169 DEBUG "Kernel Destruction!\n" if DEBUGGING;
1170 }
1171  
1172 sub _invoke_state {
1173  
1174 }
1175  
1176 sub initialize_kernel {
1177 $poe_kernel = __PACKAGE__->new();
1178 weaken( $poe_kernel->[KR_IDS]->{$poe_kernel->ID} = $poe_kernel );
1179 $poe_kernel->[KR_SESSIONS]->{$poe_kernel} = $poe_kernel->ID;
1180 }
1181  
1182 initialize_kernel();
1183 POE::Callstack::PUSH( $poe_kernel );
1184  
1185 1;