| 1 | # Declare our package | ||
| 2 | package POE::Component::Lightspeed::Hack::Events; | ||
| 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 | # These methods are folded into POE::Kernel; | ||
| 12 | package POE::Kernel; | ||
| 13 | |||
| 14 | # Standard stuff to catch errors | ||
| 15 | use strict qw(subs vars refs); # Make sure we can't mess up | ||
| 16 | use warnings FATAL => 'all'; # Enable warnings to catch errors | ||
| 17 | |||
| 18 | my %event_count; | ||
| 19 | # ( $session => $count, | ||
| 20 | # ..., | ||
| 21 | # ); | ||
| 22 | |||
| 23 | my %post_count; | ||
| 24 | # ( $session => $count, | ||
| 25 | # ..., | ||
| 26 | # ); | ||
| 27 | |||
| 28 | ### End-run leak checking. | ||
| 29 | |||
| 30 | sub _data_ev_finalize_31 { | ||
| 31 | my $finalized_ok = 1; | ||
| 32 | while (my ($ses, $cnt) = each(%event_count)) { | ||
| 33 | $finalized_ok = 0; | ||
| 34 | _warn("!!! Leaked event-to count: $ses = $cnt\n"); | ||
| 35 | } | ||
| 36 | |||
| 37 | while (my ($ses, $cnt) = each(%post_count)) { | ||
| 38 | $finalized_ok = 0; | ||
| 39 | _warn("!!! Leaked event-from count: $ses = $cnt\n"); | ||
| 40 | } | ||
| 41 | return $finalized_ok; | ||
| 42 | } | ||
| 43 | |||
| 44 | ### Enqueue an event. | ||
| 45 | |||
| 46 | sub _data_ev_enqueue_31 { | ||
| 47 | my ( $self, $session, $source_session, $event, $type, $etc, $file, $line, $fromstate, $time ) = @_; | ||
| 48 | |||
| 49 | if (ASSERT_DATA) { | ||
| 50 | unless ($self->_data_ses_exists($session)) { | ||
| 51 | _trap( "<ev> can't enqueue event ``$event'' for nonexistent session $session\n" ); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | # This is awkward, but faster than using the fields individually. | ||
| 56 | my $event_to_enqueue = [ @_[1..8] ]; | ||
| 57 | |||
| 58 | my $old_head_priority = $self->[ KR_QUEUE ]->get_next_priority(); | ||
| 59 | my $new_id = $self->[ KR_QUEUE ]->enqueue($time, $event_to_enqueue); | ||
| 60 | |||
| 61 | if (TRACE_EVENTS) { | ||
| 62 | _warn( "<ev> enqueued event $new_id ``$event'' from ", | ||
| 63 | $self->_data_alias_loggable($source_session), " to ", | ||
| 64 | $self->_data_alias_loggable($session), | ||
| 65 | " at $time" | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | if ($self->[ KR_QUEUE ]->get_item_count() == 1) { | ||
| 70 | $self->loop_resume_time_watcher($time); | ||
| 71 | } | ||
| 72 | elsif ($time < $old_head_priority) { | ||
| 73 | $self->loop_reset_time_watcher($time); | ||
| 74 | } | ||
| 75 | |||
| 76 | $self->_data_ses_refcount_inc($session); | ||
| 77 | $event_count{$session}++; | ||
| 78 | |||
| 79 | # Lightspeed stuff | ||
| 80 | if ( ref( $source_session ) ne 'POE::Component::Lightspeed::Hack::Session' ) { | ||
| 81 | $self->_data_ses_refcount_inc($source_session); | ||
| 82 | } | ||
| 83 | $post_count{$source_session}++; | ||
| 84 | |||
| 85 | return $new_id; | ||
| 86 | } | ||
| 87 | |||
| 88 | ### Remove events sent to or from a specific session. | ||
| 89 | |||
| 90 | sub _data_ev_clear_session_31 { | ||
| 91 | my ($self, $session) = @_; | ||
| 92 | |||
| 93 | my $my_event = sub { | ||
| 94 | ($_[0]->[EV_SESSION] == $session) || ($_[0]->[EV_SOURCE] == $session) | ||
| 95 | }; | ||
| 96 | |||
| 97 | # TODO - This is probably incorrect. The total event count will be | ||
| 98 | # artificially inflated for events from/to the same session. That | ||
| 99 | # is, a yield() will count twice. | ||
| 100 | my $total_event_count = ( | ||
| 101 | ($event_count{$session} || 0) + | ||
| 102 | ($post_count{$session} || 0) | ||
| 103 | ); | ||
| 104 | |||
| 105 | foreach ($self->[ KR_QUEUE ]->remove_items($my_event, $total_event_count)) { | ||
| 106 | $self->_data_ev_refcount_dec(@{$_->[ITEM_PAYLOAD]}[EV_SOURCE, EV_SESSION]); | ||
| 107 | } | ||
| 108 | |||
| 109 | croak if delete $event_count{$session}; | ||
| 110 | croak if delete $post_count{$session}; | ||
| 111 | } | ||
| 112 | |||
| 113 | ### Decrement a post refcount | ||
| 114 | |||
| 115 | sub _data_ev_refcount_dec_31 { | ||
| 116 | my ($self, $source_session, $dest_session) = @_; | ||
| 117 | |||
| 118 | if (ASSERT_DATA) { | ||
| 119 | _trap $dest_session unless exists $event_count{$dest_session}; | ||
| 120 | _trap $source_session unless exists $post_count{$source_session}; | ||
| 121 | } | ||
| 122 | |||
| 123 | $self->_data_ses_refcount_dec($dest_session); | ||
| 124 | $event_count{$dest_session}--; | ||
| 125 | |||
| 126 | $post_count{$source_session}--; | ||
| 127 | |||
| 128 | # Lightspeed stuff | ||
| 129 | if ( ref( $source_session ) ne 'POE::Component::Lightspeed::Hack::Session' ) { | ||
| 130 | $self->_data_ses_refcount_dec($source_session); | ||
| 131 | } else { | ||
| 132 | delete $post_count{$source_session} if $post_count{$source_session} == 0; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | ### Fetch the number of pending events sent to a session. | ||
| 137 | |||
| 138 | sub _data_ev_get_count_to_31 { | ||
| 139 | my ($self, $session) = @_; | ||
| 140 | return $event_count{$session} || 0; | ||
| 141 | } | ||
| 142 | |||
| 143 | ### Fetch the number of pending events sent from a session. | ||
| 144 | |||
| 145 | sub _data_ev_get_count_from_31 { | ||
| 146 | my ($self, $session) = @_; | ||
| 147 | return $post_count{$session} || 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | # Scope our evil stuff :) | ||
| 151 | BEGIN { | ||
| 152 | # Turn off strictness for our nasty stuff | ||
| 153 | no strict 'refs'; | ||
| 154 | |||
| 155 | # Oh boy, no warnings too :( | ||
| 156 | no warnings 'redefine'; | ||
| 157 | |||
| 158 | # Now, decide which version we should load... | ||
| 159 | my %finalize_versions = ( | ||
| 160 | '0.31' => \&_data_ev_finalize_31, | ||
| 161 | '0.3101' => \&_data_ev_finalize_31, | ||
| 162 | ); | ||
| 163 | my %enqueue_versions = ( | ||
| 164 | '0.31' => \&_data_ev_enqueue_31, | ||
| 165 | '0.3101' => \&_data_ev_enqueue_31, | ||
| 166 | ); | ||
| 167 | my %clear_versions = ( | ||
| 168 | '0.31' => \&_data_ev_clear_session_31, | ||
| 169 | '0.3101' => \&_data_ev_clear_session_31, | ||
| 170 | ); | ||
| 171 | my %dec_versions = ( | ||
| 172 | '0.31' => \&_data_ev_refcount_dec_31, | ||
| 173 | '0.3101' => \&_data_ev_refcount_dec_31, | ||
| 174 | ); | ||
| 175 | my %count_to_versions = ( | ||
| 176 | '0.31' => \&_data_ev_get_count_to_31, | ||
| 177 | '0.3101' => \&_data_ev_get_count_to_31, | ||
| 178 | ); | ||
| 179 | my %count_from_versions = ( | ||
| 180 | '0.31' => \&_data_ev_get_count_from_31, | ||
| 181 | '0.3101' => \&_data_ev_get_count_from_31, | ||
| 182 | ); | ||
| 183 | |||
| 184 | # Make sure we have this version in the dispatch table | ||
| 185 | if ( ! exists $finalize_versions{ $POE::VERSION } ) { | ||
| 186 | die 'Your version of Lightspeed does not yet support POE-' . $POE::VERSION; | ||
| 187 | } | ||
| 188 | |||
| 189 | # Do our symbol table hackery | ||
| 190 | *{'POE::Kernel::_data_ev_finalize'} = $finalize_versions{ $POE::VERSION }; | ||
| 191 | *{'POE::Kernel::_data_ev_enqueue'} = $enqueue_versions{ $POE::VERSION }; | ||
| 192 | *{'POE::Kernel::_data_ev_clear_session'} = $clear_versions{ $POE::VERSION }; | ||
| 193 | *{'POE::Kernel::_data_ev_refcount_dec'} = $dec_versions{ $POE::VERSION }; | ||
| 194 | *{'POE::Kernel::_data_ev_get_count_to'} = $count_to_versions{ $POE::VERSION }; | ||
| 195 | *{'POE::Kernel::_data_ev_get_count_from'} = $count_from_versions{ $POE::VERSION }; | ||
| 196 | } | ||
| 197 | |||
| 198 | # End of module | ||
| 199 | 1; | ||
| 200 | __END__ |