WebRadio.pm

filter: Statementstmt 45.0% 27 / 60 Branchbran 8.3% 1 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 9 / 18 Podpod 70.0% 7 / 10 totaltotal 43.6% 45 / 103 CC 9 SCAR 20.6
CC
9
Cov
39%
receive
30.0
add_station
6.0
remove_station
6.0
CRAP 7.8
« WebRadio/Station/Shoutcast.pm ↑ Summary
1  
2 package Netx::WebRadio;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Exporter ();
10 1 ▶ use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = 0.03;
12 #@ISA = qw (Exporter);
13 #@EXPORT = qw ();
14 #@EXPORT_OK = qw ();
15 #%EXPORT_TAGS = ();
16  
17 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use IO::Poll 0.04 qw(POLLIN POLLOUT POLLERR POLLHUP);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 use Class::MethodMaker
21 1 ▶ new_with_init => 'new',
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 get_set => [ qw /timeout poll stations station_sockets/ ];
23  
24 }
25  
26  
27 =head1 NAME
28  
29 Netx::WebRadio - receive one or more webradio-stations
30  
31 =head1 SYNOPSIS
32  
33 use Netx::WebRadio
34 my $receiver = Netx::WebRadio->new();
35  
36 my $station = Netx::WebRadio::Station::Shoutcast->new();
37 $station->host( $server->[0] );
38 $station->port( $server->[1] );
39 $receiver->add_station( $station ) if $station->connect( $server->[0], $server->[1] ) ;
40
41 while ($receiver->number_of_stations) {
42 $receiver->receive();
43 }
44  
45 =head1 DESCRIPTION
46  
47 THIS IS BETA SOFTWARE!
48  
49 Netx::WebRadio is a framework for receiving one or more webradio streams.
50  
51 It's implemented with the so-called 'template-pattern' - inherit from it and overload some mehtods.
52  
53 Netx::WebRadio works as a multiplexer for one or more Netx::WebRadio::Station-objects (eg Netx::WebRadio::Station::Shoutcast).
54  
55 =head1 USAGE
56  
57 To change the handling of certain events (timeout, disconnect) you have to overload some methods.
58  
59 Look at the Examples/ directory for examples.
60  
61 =head1 METHODS
62  
63 =head2 add_station
64  
65 Usage : $receiver->add_station( $station )
66 Purpose :
67 Adds a (already connected) station for receiving.
68 Returns : nothing
69 Argument : station-object
70 Throws : nothing
71 See Also : remove_station
72  
73 =cut
74  
75 0 ▶sub add_station {
Subroutine add_station: not called CC=2 SCAR=17.9
Pod documented
76 0 my ($self, $station) = @_;
77 0 ▶ croak "no station specified" unless $station;
Branch: 1 branch
BranchTrueFalseTotal
unless $station 0 0 0
78 0 $self->poll->mask( $station->socket => $station->pollmode );
79 0 $self->store_station_socket( $station );
80 }
81  
82 =head2 remove_station
83  
84 Usage : $receiver->remove_station( $station )
85 Purpose :
86 Removes a station.
87 Returns : nothing
88 Argument : station-object
89 Throws : nothing
90  
91 =cut
92  
93 0 ▶sub remove_station {
Subroutine remove_station: not called CC=2 SCAR=17.9
Pod documented
94 0 my ($self, $station) = @_;
95 0 ▶ croak "no station specified" unless $station;
Branch: 1 branch
BranchTrueFalseTotal
unless $station 0 0 0
96 0 $self->poll->remove( $station->socket );
97 0 $self->remove_station_socket( $station );
98 }
99  
100 =head2 number_of_stations
101  
102 Usage : $receiver->number_of_stations()
103 Purpose :
104 Returns the number of stations.
105 Returns : number of stations
106 Argument : nothing
107 Throws : nothing
108 See Also :
109  
110 =cut
111  
112 0 ▶sub number_of_stations {
Subroutine number_of_stations: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 return $self->poll->handles
115 };
116  
117 =head2 receive
118  
119 Usage : $receiver->receive()
120 Purpose :
121 Tries to receive next chunk from all stations.
122 Call it in a loop.
123 Returns : nothing
124 Argument : nothing
125 Throws : nothing
126 See Also :
127  
128 =cut
129  
130 0 ▶sub receive {
Subroutine receive: not called CC=5 SCAR=34.0
Pod documented
131 0 my $self = shift;
132  
133 0 ▶ if ($self->poll->handles) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->poll->handles) 0 0 0
134 0 $self->poll->poll( $self->timeout );
135 0 my @ready = $self->poll->handles(POLLIN|POLLHUP|POLLERR|POLLOUT);
136 0 ▶ unless ( scalar @ready ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @ready) 0 0 0
137 0 $self->timeout_all_stations();
138 }
139  
140 0 foreach my $socket ( @ready ) {
141 0 my $station = $self->get_station_by_socket( $socket );
142 0 my $return = $station->receive();
143 0 ▶ if ( $return ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return) { } 0 0 0
144 0 $self->poll->mask( $socket => $station->pollmode );
145 }
146 else {
147 0 $self->error_in_station( $station );
148 }
149 }
150 }
151 }
152  
153 =head2 timeout
154  
155 Usage : $receiver->timeout( 30 )
156 Purpose :
157 Sets the timeout value for all stations.
158 Returns : nothing
159 Argument : timeout in seconds
160 Throws : nothing
161
162 See Also : timeout_all_stations
163  
164 Overload the following methods:
165  
166 =head2 init
167  
168 Usage : init is called from new
169 Purpose :
170 Initializes some values, create Poll-Object.
171 Always call SUPER::init if you overload this method.
172 Returns : nothing
173 Argument : nothing
174 Throws : nothing
175 See Also :
176  
177 =cut
178  
179 1 ▶sub init {
Subroutine init: called CC=3 SCAR=11.4
Pod documented
180 1 my $self = shift;
181 1 ▶ $self->poll( IO::Poll->new()
Condition50%1/2
'IO::Poll'->new || croak(q[Couldn't create IO::Poll-Object])
l!l
10
MC/DC0%0/1
'IO::Poll'->new || croak(q[Couldn't create IO::Poll-Object])
'IO::Poll'->new
Truth table50%1/2
A
A= 'IO::Poll'->new
Aresult
11
00
182 or croak "Couldn't create IO::Poll-Object" );
183 1 ▶ $self->timeout(30) unless $self->timeout();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->timeout 1 0 1
184 1 $self->station_sockets( {} );
185 }
186  
187 =head2 timeout_all_stations
188  
189 Usage : timeout_all_stations is called if there is a network-timeout.
190 Purpose :
191 overload it :)
192 You can change the timeout-time with the 'timeout'-method.
193 Returns : nothing
194 Argument : nothing
195 Throws : nothing
196 See Also : timeout
197  
198 =cut
199  
200 0 ▶sub timeout_all_stations {
Subroutine timeout_all_stations: not called CC=1 SCAR=6.9
Pod documented
201 0 my ($self) = @_;
202 0 return;
203 }
204  
205 =head2 error_in_station
206  
207 Usage : error_in_station is called if a station returns an error.
208 Purpose :
209 overload it :)
210 The default implementation removes the station from the receiver.
211 Returns : nothing
212 Argument : nothing
213 Throws : nothing
214 See Also : timeout
215  
216 =cut
217  
218 0 ▶sub error_in_station {
Subroutine error_in_station: not called CC=1 SCAR=6.9
Pod documented
219 0 my ($self, $station) = @_;
220 0 carp "error in station\n";
221 0 $self->remove_station( $station );
222 }
223  
224  
225 =head1 BUGS
226  
227 Doesn't work under Win32... please send patches :-)
228  
229  
230 =head1 SUPPORT
231  
232  
233  
234 =head1 AUTHOR
235  
236 Nathanael Obermayer
237 CPAN ID: nathanael
238 natom-pause@smi2le.net
239  
240 =head1 COPYRIGHT
241  
242 This program is free software; you can redistribute
243 it and/or modify it under the same terms as Perl itself.
244  
245 The full text of the license can be found in the
246 LICENSE file included with this module.
247  
248  
249 =head1 SEE ALSO
250  
251 perl(1).
252  
253 =cut
254  
255  
256 0 ▶sub get_station_by_socket {
Subroutine get_station_by_socket: not called CC=1 SCAR=6.9
Pod undocumented
257 0 my ($self, $socket) = @_;
258 0 return $self->station_sockets->{ $socket };
259 }
260  
261 0 ▶sub store_station_socket {
Subroutine store_station_socket: not called CC=1 SCAR=6.9
Pod undocumented
262 0 my ($self, $station) = @_;
263 0 $self->station_sockets->{ $station->socket } = $station;
264 }
265  
266 0 ▶sub remove_station_socket {
Subroutine remove_station_socket: not called CC=1 SCAR=6.9
Pod undocumented
267 0 my ($self, $station) = @_;
268 0 $self->station_sockets->{ $station->socket } = undef;
269 }
270  
271  
272 1 1; #this line is important and will help the module return a true value
273 __END__
« WebRadio/Station/Shoutcast.pm ↑ Summary