Component/Audio/Mad/Dispatch.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 141 CC 7 SCAR 16.6
CC
7
Cov
0%
add_listener
12.0
_start
6.0
input
6.0
CRAP 5.3
1 package POE::Component::Audio::Mad::Dispatch;
2 require 5.6.0;
3  
4 use warnings;
5 use strict;
6  
7 use POE;
8 use POE::Wheel::Audio::Mad;
9  
10 our $VERSION = '0.3';
11  
12 ## NOTE: this isn't an elegant solution.. but it's what I've come
13 ## up with. There are POE::Session subclasses out there that allow
14 ## multi-dispatch, but I'm not sure they work the way I want.. so
15 ## we have this.. let me know if you have a beter idea.
16  
17 sub create {
18 my ($class, $args) = @_;
19  
20 ## create our own session and return the newly
21 ## created session reference back to the caller..
22 POE::Session->create(
23 inline_states => {
24 _start => \&_start,
25
26 shutdown => \&shutdown,
27 input => \&input,
28
29 add_listener => \&add_listener,
30 update_listener => \&update_listener,
31 del_listener => \&del_listener
32 },
33 args => [$args]
34 );
35 }
36  
37 sub _start {
38 my ($heap, $kernel, $args) = @_[HEAP, KERNEL, ARG0];
39
40 $args->{alias} = 'mad-decoder' unless (defined($args->{alias}));
41 $heap->{alias} = delete($args->{alias});
42
43 $heap->{wheel} = POE::Wheel::Audio::Mad->new( message_event => 'input', %{$args} );
44 $kernel->alias_set($heap->{alias});
45
46 ## these are here to save information about our listeners..
47  
48 ## listenpost: key => listener_id, value => [session, state],
49 ## each key is our listener id number, and the value is an
50 ## arrayref which tracks that listeners postback session and
51 ## state..
52 $heap->{listenpost} = {};
53  
54 ## listeners: key => listner_id, value => [events...],
55 ## each key is our listener id number, and the value is an
56 ## arraref which tracks which events we are currently listening
57 ## for..
58  
59 $heap->{listeners} = {};
60
61 ## listenmap: key => event_id, value => { listener_id => 1 }
62 ## each key is an event id that is currently being listened for,
63 ## and the value is a hashref; each key in that hashref is a
64 ## listener id and the value is simply one. we use a nested
65 ## hash for easier insertion and deletion of listeners under
66 ## a particular event..
67
68 $heap->{listenmap} = {};
69 }
70  
71 ##############################################################################
72  
73 ## gets us to shut ourselvs down and disappear, this probably
74 ## shouldn't get called directly from anywhere..
75 sub shutdown {
76 my ($kernel, $heap) = @_[KERNEL, HEAP];
77
78 ## generate a message about our shutdown..
79 $kernel->yield('input', {
80 id => 'IPC_SHUTDOWN_SUCCESS',
81 data => ''
82 });
83
84 ## hmm.. race condition? release the references to
85 ## all our sessions so they can die..
86  
87 foreach my $lid (keys(%{$heap->{listenpost}})) {
88 $kernel->refcount_decrement( $heap->{listenpost}->{$lid}->[0]->ID, 'POE::Component::Audio::Mad::Dispatch' );
89 }
90
91 $kernel->alias_remove($heap->{alias});
92 undef $heap->{wheel};
93 }
94
95 sub input {
96 my ($kernel, $heap, $raw) = @_[KERNEL, HEAP, ARG0];
97 return undef unless (defined($raw));
98
99 foreach my $lid (keys(%{$heap->{listenmap}->{$raw->{id}}})) {
100
101 ## we post back to our listener, and send them
102 ## a copy of the message
103  
104 $kernel->post(@{$heap->{listenpost}->{$lid}}, $raw);
105 }
106 }
107
108  
109 ##############################################################################
110  
111 ## don't know where I picked this idiom up from, but it sure
112 ## looks cute, dosen't it?
113  
114 my $_NEXT_LISTENER_ID = 0;
115 sub NEXT_LISTENER_ID { ++$_NEXT_LISTENER_ID }
116  
117 ## this state adds a listener into our session.
118  
119 sub add_listener {
120 my ($kernel, $heap, $l_session, $l_state, $l_list) = @_[KERNEL, HEAP, ARG0..ARG2];
121  
122 return undef unless (defined($l_session) && defined($l_state) && ref($l_list) eq 'ARRAY' && @{$l_list} > 0);
123 $l_session = $kernel->alias_resolve( $l_session ) unless (ref($l_session));
124
125 ## assign a new listener ID.. which will probably wrap at 2**31, but
126 ## I don't think we're that popular..
127 my $LID = NEXT_LISTENER_ID;
128
129 ## track our postback session and state in our 'listenpost' hash..
130 $heap->{listenpost}->{$LID} = [$l_session, $l_state];
131  
132 ## track the events we are listening for in 'listeners'..
133 $heap->{listeners}->{$LID} = $l_list;
134
135 ## then we map ourselvs into each even we are listening
136 ## for in listenmap..
137 foreach (@{$l_list}) { $heap->{listenmap}->{$_}->{$LID} = 1 }
138  
139 ## bump the refcount for the calling session, as long as we
140 ## have a hold of them, we expect them to persist to get
141 ## our messages..
142  
143 $kernel->refcount_increment( $l_session->ID, 'POE::Component::Audio::Mad::Dispatch' );
144  
145 ## send back our LID.. this state is definitely meant to
146 ## be used via ->call(), rather than ->post() so we can
147 ## get back our LID. Although, if you don't plan to
148 ## ever update or delete yourself from the listener
149 ## chain, it isn't necessary..
150 return $LID;
151 }
152  
153 ## this method might be cleaner, but this seems to work
154 ## prety well, and it's generally not often called..
155  
156 sub update_listener {
157 my ($heap, $lid, $list) = @_[HEAP, ARG0..ARG1];
158 return undef unless (defined($lid) && defined($heap->{listenpost}->{$lid}) && ref($list) eq 'ARRAY' && @{$list} > 0);
159
160 ## to update our information, we just cycle through the list
161 ## of currently listened for events, and remove ourselvs
162 ## from that event in the map..
163 foreach (@{$heap->{listeners}->{$lid}}) { delete($heap->{listenmap}->{$_}->{$lid}) }
164
165 ## then we update our listened for events, with the new list
166 ## passed in as an argument to this state..
167 $heap->{listeners}->{$lid} = $list;
168
169 ## then we remap our new list of states back into
170 ## the event map..
171 foreach (@{$list}) { $heap->{listenmap}->{$_}->{$lid} = 1 }
172  
173 }
174  
175 sub del_listener {
176 my ($kernel, $heap, $lid) = @_[KERNEL, HEAP, ARG0];
177 return undef unless (defined($lid) && defined($heap->{listenpost}->{$lid}));
178
179 my $target_session = $heap->{listenpost}->{$lid}->[0];
180  
181 ## cycle through the event map, and be sure to remove
182 ## ourselvs..
183  
184 foreach (@{$heap->{listeners}->{$lid}}) { delete($heap->{listenmap}->{$_}->{$lid}) }
185
186 ## remove our entries in the listeners and listenpost maps..
187  
188 delete($heap->{listeners}->{$lid});
189 delete($heap->{listenpost}->{$lid});
190
191 ## drop the reference count so we don't hold them unecessarily..
192 $kernel->refcount_decrement( $target_session->ID, 'POE::Component::Audio::Mad::Dispatch' );
193
194 ## return something..
195
196 return 1;
197 }
198  
199 ##############################################################################
200 1;
201 __END__