Component/Audio/Mad/Handle.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 118 CC 10 SCAR 33.0
CC
10
Cov
0%
create
72.0
put
6.0
input
6.0
CRAP 27.0
1 package POE::Component::Audio::Mad::Handle;
2 require 5.6.0;
3  
4 $| = 1;
5  
6 use warnings;
7 use strict;
8  
9 use Carp qw(carp);
10 use POE qw(Wheel::ReadWrite Driver::SysRW Filter::Audio::Mad::Stdio);
11 use POE::Wheel::Audio::Mad;
12  
13 our $VERSION = '0.2';
14  
15 ## this is a simpler solution, as it merely relies on parsing commands
16 ## fed to it on STDIN and it returns output on STDOUT. There are problems
17 ## with this design, however, we need to find a way to allow spaces
18 ## in command arguments, or at least do simple quoting. I have a feeling
19 ## I'm about to get pretty intimate with non-backtracking regexes or
20 ## something..
21  
22 sub create {
23 my ($class, $args) = @_;
24  
25 ## we process some of our arguments and set some
26 ## defaults rather early, we use this hash to
27 ## hold the arguments for the wheel to be created..
28  
29 $args->{filter} = new POE::Filter::Audio::Mad::Stdio unless (defined($args->{filter}));
30 $args->{driver} = new POE::Driver::SysRW unless (defined($args->{driver}));
31
32 my %wheel = (
33 Driver => $args->{driver},
34 Filter => $args->{filter},
35
36 InputEvent => 'input',
37 ErrorEvent => 'error',
38 );
39
40 ## check to see what the user wants, if unspecified
41 ## STDIN will be used for input and STDOUT will
42 ## be used for output. we do this here so we can
43 ## fail a bit more gracefully.. better to fail
44 ## creation than to die in _start.
45
46 if (defined($args->{handle})) {
47 $wheel{Handle} = $args->{handle};
48 } else {
49 $wheel{InputHandle} = $args->{input_handle} if (defined($args->{input_handle} ));
50 $wheel{OutputHandle} = $args->{output_handle} if (defined($args->{output_handle}));
51
52 unless (defined($wheel{InputHandle})) {
53 open (my $stdin, '-') || do {
54 carp "failed to open STDIN for reading, aborting!";
55 return undef;
56 };
57
58 $wheel{InputHandle} = $stdin;
59 }
60
61 unless (defined($wheel{OutputHandle})) {
62 open (my $stdout, '>-') || do {
63 carp "failed to open STDOUT for writing, aborting!";
64 return undef;
65 };
66
67 $wheel{OutputHandle} = $stdout;
68 }
69 }
70  
71 ## create our own session, and return it back
72 ## to the caller.
73 POE::Session->create(
74 inline_states => {
75 _start => \&_start,
76 _stop => \&_stop,
77 _shutdown_flushed => \&_shutdown_flushed,
78
79 shutdown => \&shutdown,
80 put => \&put,
81 input => \&input,
82 error => \&error
83 },
84 args => [\%wheel]
85 );
86 }
87  
88  
89 sub _start {
90 my ($heap, $kernel, $args) = @_[HEAP, KERNEL, ARG0];
91  
92 ## fix: we need to do something about this..
93 $kernel->alias_set('mad-decoder');
94  
95 ## just use the arguments that were generated for us,
96 ## above.
97 $heap->{wheel} = POE::Wheel::ReadWrite->new( %{$args} );
98 $heap->{decoder} = POE::Wheel::Audio::Mad->new( message_event => 'put' );
99 }
100  
101 sub _stop { undef }
102  
103 ##############################################################################
104  
105 sub shutdown {
106 my ($kernel, $heap) = @_[KERNEL, HEAP];
107
108 $heap->{wheel}->event( FlushedEvent => '_shutdown_flushed' );
109 $kernel->yield('put', {
110 id => 'IPC_SHUTDOWN_SUCCESS',
111 data => ''
112 });
113 }
114  
115 sub _shutdown_flushed {
116 my ($kernel, $heap) = @_[KERNEL, HEAP];
117
118 undef $heap->{wheel};
119 $kernel->alias_remove('mad-decoder');
120 }
121
122 sub put {
123 my ($heap, $msg) = @_[HEAP, ARG0];
124 $heap->{wheel}->put($msg) if (defined($heap->{wheel}));
125 }
126  
127 sub input {
128 my ($kernel, $raw) = @_[KERNEL, ARG0];
129
130 return undef unless (defined($raw->{id}) && $raw->{id} ne '');
131 $kernel->yield("decoder_$raw->{id}", $raw->{data});
132 }
133  
134 sub error {
135 my ($kernel, $efunc, $error) = @_[KERNEL, ARG0, ARG2];
136
137 $kernel->yield('put', {
138 id => 'IPC_INPUT_ERROR',
139 data => "failure during '$efunc': $error",
140 });
141
142 $kernel->post('mad-decoder', 'shutdown');
143 }
144  
145 ##############################################################################
146 1;
147 __END__