Server.pm

filter: Statementstmt 20.1% 30 / 149 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 24.3% 9 / 37 Podpod 5.5% 1 / 18 totaltotal 16.2% 40 / 246 CC 22 SCAR 23.9
CC
22
Cov
16%
_handler
30.0
_all_tracks
30.0
new
20.0
CRAP 10.9
↑ Summary Server/Playlist.pm »
1 package Net::DMAP::Server;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POE::Component::Server::HTTP 0.05; # for keep alive
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POE::Component::Server::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::Rendezvous::Publish;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::DAAP::DMAP qw( dmap_pack );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 __PACKAGE__->mk_accessors(qw( debug port name path db_uuid tracks playlists
12 revision waiting_clients poll_interval ),
13 qw( httpd uri ),
14 # Rendezvous::Publish stuff
15 qw( publisher service ));
16 1 our $VERSION = '0.05';
17  
18 =head1 NAME
19  
20 Net::DMAP::Server - base class for D[A-Z]AP servers
21  
22 =head1 SYNOPSIS
23  
24 package Net::DZAP::Server;
25 use base qw( Net::DMAP::Server );
26 sub protocol { 'dzap' }
27  
28 1;
29  
30 =head1 NAME
31  
32 Net::DZAP::Server - Digital Zebra Access Protocol (iZoo) Server
33  
34 =cut
35  
36 =head1 DESCRIPTION
37  
38 Net::DMAP::Server is a base class for implementing DMAP servers. It's
39 probably not hugely useful to you directly, and you're better off
40 looking at Net::DPAP::Server or Net::DAAP::Server.
41  
42 =cut
43  
44 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
45 0 my $class = shift;
46 0 my $self = $class->SUPER::new( {
47 db_uuid => '13950142391337751523',
48 revision => 42,
49 tracks => {},
50 playlists => {},
51 waiting_clients => [],
52 poll_interval => 20,
53 @_ } );
54 0 ▶ $self->name( ref($self) ." " . hostname . " $$" ) unless $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->name 0 0 0
55 0 ▶ $self->port( $self->default_port ) unless $self->port;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->port 0 0 0
56 0 $self->find_tracks;
57 #print Dump $self;
58 $self->httpd( POE::Component::Server::HTTP->new(
59 Port => $self->port,
60 0 ▶ ContentHandler => { '/' => sub { $self->_handler(@_) } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 ▶ StreamHandler => sub { $self->stream_handler(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 ) );
63  
64 0 ▶ my $publisher = Net::Rendezvous::Publish->new
Branch: 1 branch
BranchTrueFalseTotal
my $publisher = "Net::Rendezvous::Publish"->new or die "couldn't make a Responder object" 0 0 0
65 or die "couldn't make a Responder object";
66 0 $self->publisher( $publisher );
67 0 $self->service( $publisher->publish(
68 name => $self->name,
69 type => '_'.$self->protocol.'._tcp',
70 port => $self->port,
71 txt => "Database ID=".$self->db_uuid."\x{1}Machine Name=".$self->name,,
72 ) );
73  
74 POE::Session->create(
75 inline_states => {
76 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 $_[KERNEL]->alarm( poll_changed => time + $self->poll_interval );
78 },
79 0 ▶ poll_changed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 $self->poll_changed;
81 0 $_[KERNEL]->yield('_start');
82 },
83 0 });
84  
85 0 return $self;
86 }
87  
88 0 ▶sub stream_handler {
Subroutine stream_handler: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my $self = shift;
90 0 my ($request, $response) = @_;
91 }
92  
93 0 ▶sub _handler {
Subroutine _handler: not called CC=5 SCAR=34.0
94 0 my $self = shift;
95 0 my ($request, $response) = @_;
96 # always the same
97 0 $response->code( RC_OK );
98 0 $response->content_type( 'application/x-dmap-tagged' );
99  
100 0 local $self->{uri};
101 0 $self->uri( $request->uri );
102 0 ▶ print $request->uri, "\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
103  
104 # first match wins
105 0 my @methods = (
106 [ database_item => qr{^/databases/\d+/items/(\d+)\.} ],
107 [ database_items => qr{^/databases/(\d+)/items} ],
108 [ playlist_items => qr{^/databases/(\d+)/containers/(\d+)} ],
109 [ database_playlists => qr{^/databases/(\d+)/containers} ],
110 [ databases => qr{^/databases} ],
111 [ server_info => qr{^/server-info} ],
112 [ content_codes => qr{^/content-codes} ],
113 [ update => qr{^/update} ],
114 [ login => qr{^/login} ],
115 [ logout => qr{^/logout} ],
116 [ ignore => qr{^/this_request_is_simply_to_send_a_close_connection_header} ],
117 );
118  
119 0 for (@methods) {
120 0 my ($method, $pattern) = @$_;
121 0 ▶ if (my @matched = ($self->uri->path =~ $pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matched) = $self->uri->path =~ /$pattern/) 0 0 0
122 #print "dispatching as $method\n" if $self->debug;
123 0 $self->$method( $request, $response, @matched );
124 0 return $response->code;
125 }
126 }
127  
128 0 ▶ print "Can't handle ".$self->uri->path."\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
129 0 $response->code( 500 );
130 0 return 500;
131 }
132  
133  
134 0 ▶sub _dmap_pack {
Subroutine _dmap_pack: not called CC=1 SCAR=6.9
135 0 my $self = shift;
136 0 my $dmap = shift;
137 0 return dmap_pack $dmap;
138 }
139  
140 0 ▶sub find_tracks {
Subroutine find_tracks: not called CC=1 SCAR=6.9
Pod undocumented
141 0 die "override me";
142 }
143  
144 0 ▶sub database_item {
Subroutine database_item: not called CC=1 SCAR=6.9
Pod undocumented
145 0 my ($self, $request, $response) = @_;
146 0 my $id = shift;
147 0 $response->content( $self->tracks->{$1}->data );
148 }
149  
150 0 ▶sub content_codes {
Subroutine content_codes: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my ($self, $request, $response) = @_;
152 $response->content($self->_dmap_pack(
153 [[ 'dmap.contentcodesresponse' => [
154 [ 'dmap.status' => 200 ],
155 0 map { [ 'dmap.dictionary' => [
156 [ 'dmap.contentcodesnumber' => $_->{ID} ],
157 [ 'dmap.contentcodesname' => $_->{NAME} ],
158 0 [ 'dmap.contentcodestype' => $_->{TYPE} ],
159 ] ] } values %$Net::DAAP::DMAP::Types,
160 ]]] ));
161 }
162  
163 0 ▶sub login {
Subroutine login: not called CC=1 SCAR=6.9
Pod undocumented
164 0 my ($self, $request, $response) = @_;
165 0 $response->content( $self->_dmap_pack(
166 [[ 'dmap.loginresponse' => [
167 [ 'dmap.status' => 200 ],
168 [ 'dmap.sessionid' => 42 ],
169 ]]] ));
170 }
171  
172 0 ▶sub logout { }
Subroutine logout: not called CC=1 SCAR=0.0
Pod undocumented
173  
174 0 ▶sub ignore { }
Subroutine ignore: not called CC=1 SCAR=0.0
Pod undocumented
175  
176 0 ▶sub update {
Subroutine update: not called CC=4 SCAR=30.0
Pod undocumented
177 0 my ($self, $request, $response) = @_;
178 0 ▶ if ($self->uri =~ m{revision-number=(\d+)} && $1 >= $self->revision) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri =~ /revision-number=(\d+)/ and $1 >= $self->revision) 0 0 0
Condition0%0/3
$self->uri =~ /revision-number=(\d+)/ and $1 >= $self->revision
!ll&&!rl&&r
000
MC/DC0%0/2
$self->uri =~ /revision-number=(\d+)/ and $1 >= $self->revision
$self->uri =~ /revision-number=(\d+)/ $1 >= $self->revision
Truth table0%0/3
A and B
A= $self->uri =~ /revision-number=(\d+)/B= $1 >= $self->revision
ABresult
0X0
100
111
179 0 ▶ print "queueing $response\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
180 0 push @{ $self->waiting_clients }, $response;
181 0 $response->code( RC_WAIT );
182 0 return;
183 }
184 0 $self->update_answer( $request, $response );
185 }
186  
187 0 ▶sub has_changed { 0 }
Subroutine has_changed: not called CC=1 SCAR=6.9
Pod undocumented
188  
189 0 ▶sub poll_changed {
Subroutine poll_changed: not called CC=4 SCAR=30.0
Pod undocumented
190 0 my $self = shift;
191 0 ▶ if ($self->has_changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_changed) 0 0 0
192 0 $self->revision( $self->revision + 1 );
193 0 for my $response (@{ $self->waiting_clients }) {
194 0 ▶ print "continuing $response\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
195 0 $self->update_answer( undef, $response );
196 0 $response->code( RC_OK );
197 0 $response->continue;
198 }
199 0 $self->waiting_clients([]);
200 }
201 }
202  
203 0 ▶sub update_answer {
Subroutine update_answer: not called CC=1 SCAR=6.9
Pod undocumented
204 0 my ($self, $request, $response) = @_;
205  
206 0 $response->content( $self->_dmap_pack(
207 [[ 'dmap.updateresponse' => [
208 [ 'dmap.status' => 200 ],
209 [ 'dmap.serverrevision' => $self->revision ],
210 ]]] ));
211 }
212  
213 0 ▶sub databases {
Subroutine databases: not called CC=1 SCAR=6.9
Pod undocumented
214 0 my ($self, $request, $response) = @_;
215  
216 $response->content( $self->_dmap_pack(
217 [[ 'daap.serverdatabases' => [
218 [ 'dmap.status' => 200 ],
219 [ 'dmap.updatetype' => 0 ],
220 [ 'dmap.specifiedtotalcount' => 1 ],
221 [ 'dmap.returnedcount' => 1 ],
222 [ 'dmap.listing' => [
223 [ 'dmap.listingitem' => [
224 [ 'dmap.itemid' => 35 ],
225 [ 'dmap.persistentid' => $self->db_uuid ],
226 [ 'dmap.itemname' => $self->name ],
227 0 [ 'dmap.itemcount' => scalar keys %{ $self->tracks } ],
228 [ 'dmap.containercount' => 1 ],
229 ],
230 ],
231 ],
232 ],
233 ]]] ));
234 }
235  
236 0 ▶sub database_items {
Subroutine database_items: not called CC=1 SCAR=6.9
Pod undocumented
237 0 my ($self, $request, $response, $database_id) = @_;
238 0 my $tracks = $self->_all_tracks;
239 0 $response->content( $self->_dmap_pack(
240 [[ 'daap.databasesongs' => [
241 [ 'dmap.status' => 200 ],
242 [ 'dmap.updatetype' => 0 ],
243 [ 'dmap.specifiedtotalcount' => scalar @$tracks ],
244 [ 'dmap.returnedcount' => scalar @$tracks ],
245 [ 'dmap.listing' => $tracks ]
246 ]]] ));
247 }
248  
249 0 ▶sub database_playlists {
Subroutine database_playlists: not called CC=1 SCAR=6.9
Pod undocumented
250 0 my ($self, $request, $response, $database_id) = @_;
251  
252 0 my $tracks = $self->_all_tracks;
253 my $playlists = [
254 [ 'dmap.listingitem' => [
255 [ 'dmap.itemid' => 39 ],
256 [ 'dmap.persistentid' => '13950142391337751524' ],
257 [ 'dmap.itemname' => $self->name ],
258 [ 'com.apple.itunes.smart-playlist' => 0 ],
259 [ 'dmap.itemcount' => scalar @$tracks ],
260 ],
261 ],
262 map {
263 [ 'dmap.listingitem' => [
264 [ 'dmap.itemid' => $_->dmap_itemid ],
265 [ 'dmap.persistentid' => $_->dmap_persistentid ],
266 [ 'dmap.itemname' => $_->dmap_itemname ],
267 [ 'com.apple.itunes.smart-playlist' => 0 ],
268 0 [ 'dmap.itemcount' => scalar @{ $_->items } ],
269 ],
270 ],
271 0 } values %{ $self->playlists },
272 ];
273 0 $response->content( $self->_dmap_pack(
274 [[ 'daap.databaseplaylists' => [
275 [ 'dmap.status' => 200 ],
276 [ 'dmap.updatetype' => 0 ],
277 [ 'dmap.specifiedtotalcount' => 1 ],
278 [ 'dmap.returnedcount' => 1 ],
279 [ 'dmap.listing' => $playlists ],
280 ]]] ));
281 }
282  
283 0 ▶sub playlist_items {
Subroutine playlist_items: not called CC=2 SCAR=17.9
Pod undocumented
284 0 my ($self, $request, $response, $database_id, $playlist_id) = @_;
285  
286 0 my $playlist = $self->playlists->{ $playlist_id };
287  
288 0 ▶ my $tracks = $self->_all_tracks( $playlist ? @{ $playlist->items } : () );
Branch: 1 branch
BranchTrueFalseTotal
$playlist ? : 0 0 0
289 0 $response->content( $self->_dmap_pack(
290 [[ 'daap.playlistsongs' => [
291 [ 'dmap.status' => 200 ],
292 [ 'dmap.updatetype' => 0 ],
293 [ 'dmap.specifiedtotalcount' => scalar @$tracks ],
294 [ 'dmap.returnedcount' => scalar @$tracks ],
295 [ 'dmap.listing' => $tracks ]
296 ]]] ));
297 }
298  
299  
300  
301 0 ▶sub item_field {
Subroutine item_field: not called CC=2 SCAR=17.9
Pod undocumented
302 0 my $self = shift;
303 0 my $track = shift;
304 0 my $field = shift;
305  
306 0 (my $method = $field) =~ s{[.-]}{_}g;
307 # kludge
308 0 ▶ if ($field =~ /dpap\.(thumb|hires)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /dpap\.(thumb|hires)/) 0 0 0
309 0 $field = 'dpap.picturedata';
310 }
311  
312 0 [ $field => eval { $track->$method() } ]
313 }
314  
315 0 ▶sub uniq {
Subroutine uniq: not called CC=1 SCAR=6.9
Pod undocumented
316 0 my %seen;
317 0 return grep { !$seen{$_}++ } @_;
318 }
319  
320 # some things are always present in the listings returned, whether you
321 # ask for them or not
322 0 ▶sub _always_answer {
Subroutine _always_answer: not called CC=1 SCAR=6.9
323 0 qw( dmap.itemkind dmap.itemid dmap.itemname );
324 }
325  
326 0 ▶sub _response_fields {
Subroutine _response_fields: not called CC=2 SCAR=17.9
327 0 my $self = shift;
328  
329 0 ▶ my $meta = { $self->_uri_arguments }->{meta} || '';
Condition0%0/2
{$self->_uri_arguments}->{'meta'} || ''
l!l
00
MC/DC0%0/1
{$self->_uri_arguments}->{'meta'} || ''
{$self->_uri_arguments}->{'meta'}
Truth table0%0/2
A
A= {$self->_uri_arguments}->{'meta'}
Aresult
11
00
330 0 my @fields = uniq $self->_always_answer, split /(?:,|%2C)/, $meta;
331 0 return @fields;
332 }
333  
334 0 ▶sub _uri_arguments {
Subroutine _uri_arguments: not called CC=2 SCAR=17.9
335 0 my $self = shift;
336 0 ▶ my @chunks = split /&/, $self->uri->query || '';
Condition0%0/2
$self->uri->query || ''
l!l
00
MC/DC0%0/1
$self->uri->query || ''
$self->uri->query
Truth table0%0/2
A
A= $self->uri->query
Aresult
11
00
337 0 return map { split /=/, $_, 2 } @chunks;
338 }
339  
340 0 ▶sub _all_tracks {
Subroutine _all_tracks: not called CC=5 SCAR=34.0
341 0 my $self = shift;
342  
343 # cheat for playlist support
344 0 my @tracks;
345 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
346 0 @tracks = @_;
347 }
348 else {
349 # sometimes, all isn't really all (DPAP)
350 0 ▶ my $query = { $self->_uri_arguments }->{query} || '';
Condition0%0/2
{$self->_uri_arguments}->{'query'} || ''
l!l
00
MC/DC0%0/1
{$self->_uri_arguments}->{'query'} || ''
{$self->_uri_arguments}->{'query'}
Truth table0%0/2
A
A= {$self->_uri_arguments}->{'query'}
Aresult
11
00
351 @tracks = $query =~ /dmap\.itemid/
352 0 ? map { $self->tracks->{$_} } $query =~ /dmap\.itemid:(\d+)/g
353 0 ▶ : values %{ $self->tracks };
Branch: 1 branch
BranchTrueFalseTotal
$query =~ /dmap\.itemid/ ? : 0 0 0
354 }
355  
356 0 my @fields = $self->_response_fields;
357 0 my @results;
358 0 for my $track (@tracks) {
359 push @results, [ 'dmap.listingitem' => [
360 0 map { $self->item_field( $track => $_ ) } @fields ] ];
361 }
362 0 return \@results;
363 }
364  
365  
366  
367 =head1 BUGS
368  
369 The Digital Zebra Access Protocol does not exist, so you'll have to
370 manually acquire your own horses and paint them.
371  
372  
373 =head1 AUTHOR
374  
375 Richard Clamp <richardc@unixbeard.net>
376  
377 =head1 COPYRIGHT
378  
379 Copyright 2004, 2005, 2006 Richard Clamp. All Rights Reserved.
380  
381 This program is free software; you can redistribute it and/or modify
382 it under the same terms as Perl itself.
383  
384 =head1 SEE ALSO
385  
386 Net::DAAP::Server, Net::DPAP::Server
387  
388 =cut
389  
390 1 1;
↑ Summary Server/Playlist.pm »