blib/lib/Net/UPnP/AV/MediaServer.pm

filter: Statementstmt 89.8% 160 / 178 Branchbran 77.5% 62 / 80 Conditioncond 65.3% 17 / 26 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 95.0% 19 / 20 Podpod 44.4% 4 / 9 totaltotal 81.9% 268 / 327 CC 51 SCAR 28.5
CC
51
Cov
84%
getcontentlist
32.2
walkcontent
17.3
__ANON__
7.1
CRAP 17.4
« blib/lib/Net/UPnP/HTTP.pm ↑ Summary examples/dms2vodcast.pl »
1 package Net::UPnP::AV::MediaServer;
2  
3 #-----------------------------------------------------------------
4 # Net::UPnP::AV::MediaServer
5 #-----------------------------------------------------------------
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Net::UPnP::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Net::UPnP::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Net::UPnP::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Net::UPnP::AV::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Net::UPnP::AV::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use vars qw($_DEVICE $DEVICE_TYPE $CONTENTDIRECTORY_SERVICE_TYPE);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 $_DEVICE = 'device';
20  
21 3 $DEVICE_TYPE = 'urn:schemas-upnp-org:device:MediaServer:1';
22 3 $CONTENTDIRECTORY_SERVICE_TYPE = 'urn:schemas-upnp-org:service:ContentDirectory:1';
23  
24 #------------------------------
25 # new
26 #------------------------------
27  
28 23 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 23 my($class) = shift;
30 23 my($this) = {
31 $Net::UPnP::AV::MediaServer::_DEVICE => undef,
32 };
33 23 bless $this, $class;
34 }
35  
36 #------------------------------
37 # device
38 #------------------------------
39  
40 23 ▶sub setdevice() {
Subroutine setdevice: called CC=2 SCAR=7.1
Pod documented
41 23 my($this) = shift;
42 23 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 0 23
43 23 $this->{$Net::UPnP::AV::MediaServer::_DEVICE} = $_[0];
44 }
45 }
46  
47 57 ▶sub getdevice() {
Subroutine getdevice: called CC=1 SCAR=0.0
Pod undocumented
48 57 my($this) = shift;
49 57 $this->{$Net::UPnP::AV::MediaServer::_DEVICE};
50 }
51  
52 #------------------------------
53 # browse
54 #------------------------------
55  
56 56 ▶sub browse {
Subroutine browse: called CC=1 SCAR=0.0
Pod documented
57 56 my($this) = shift;
58 56 my %args = (
59 ObjectID => 0,
60 BrowseFlag => 'BrowseDirectChildren',
61 Filter => '*',
62 StartingIndex => 0,
63 RequestedCount => 0,
64 SortCriteria => '',
65 @_,
66 );
67
68 56 my ($objid, $browseFlag, $filter, $startIdx, $reqCount, $sortCriteria) = @_;
69 my (
70 56 $dev,
71 $condir_service,
72 %req_arg,
73 $action_res,
74 );
75
76 56 $dev = $this->getdevice();
77 56 $condir_service = $dev->getservicebyname($Net::UPnP::AV::MediaServer::CONTENTDIRECTORY_SERVICE_TYPE);
78
79 %req_arg = (
80 'ObjectID' => $args{ObjectID},
81 'BrowseFlag' => $args{BrowseFlag},
82 'Filter' => $args{Filter},
83 'StartingIndex' => $args{StartingIndex},
84 'RequestedCount' => $args{RequestedCount},
85 'SortCriteria' => $args{SortCriteria},
86 56 );
87
88 56 $condir_service->postaction("Browse", \%req_arg, $args{http_options});
89 }
90  
91 50 ▶sub browsedirectchildren {
Subroutine browsedirectchildren: called CC=1 SCAR=0.0
Pod undocumented
92 50 my($this) = shift;
93 50 my %args = (
94 ObjectID => 0,
95 Filter => '*',
96 StartingIndex => 0,
97 RequestedCount => 0,
98 SortCriteria => '',
99 @_,
100 );
101 $this->browse (
102 ObjectID => $args{ObjectID},
103 BrowseFlag => 'BrowseDirectChildren',
104 Filter => $args{Filter},
105 StartingIndex => $args{StartingIndex},
106 RequestedCount => $args{RequestedCount},
107 SortCriteria => $args{SortCriteria},
108 http_options => $args{http_options}
109 50 );
110 }
111  
112 6 ▶sub browsemetadata {
Subroutine browsemetadata: called CC=1 SCAR=0.0
Pod undocumented
113 6 my($this) = shift;
114 6 my %args = (
115 ObjectID => 0,
116 Filter => '*',
117 StartingIndex => 0,
118 RequestedCount => 0,
119 SortCriteria => '',
120 @_,
121 );
122 $this->browse (
123 ObjectID => $args{ObjectID},
124 BrowseFlag => 'BrowseMetadata',
125 Filter => $args{Filter},
126 StartingIndex => $args{StartingIndex},
127 RequestedCount => $args{RequestedCount},
128 SortCriteria => $args{SortCriteria},
129 http_options => $args{http_options}
130 6 );
131 }
132  
133 #------------------------------
134 # getdirectchildren
135 #------------------------------
136  
137 44 ▶sub getcontentlist {
Subroutine getcontentlist: called CC=26 SCAR=34.7
138 44 my($this) = shift;
139 44 $this->{content_error} = '';
140 44 my %args = (
141 ObjectID => 0,
142 Filter => '*',
143 StartingIndex => 0,
144 RequestedCount => 0,
145 SortCriteria => '',
146 @_,
147 );
148 44 ▶ if (defined $args{max_items}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"max_items"}) 40 4 44
149 croak 'Invalid content item limit' unless $args{max_items} =~ /\A\d+\z/
150 40 ▶ && Net::UPnP::HTTP::_isfinite(0 + $args{max_items}) && $args{max_items} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"max_items"} =~ /\A\d+\z/ and Net::UPnP::HTTP::_isfinite(0 + $args{"max_items"}) and $args{"max_items"} > 0 0 40 40
Condition33%2/6
$args{"max_items"} =~ /\A\d+\z/ and Net::UPnP::HTTP::_isfinite(0 + $args{"max_items"}) and $args{"max_items"} > 0
!ll&&!rl&&r
0040
$args{"max_items"} =~ /\A\d+\z/ and Net::UPnP::HTTP::_isfinite(0 + $args{"max_items"})
!ll&&!rl&&r
0040
MC/DC0%0/3
$args{"max_items"} =~ /\A\d+\z/ and Net::UPnP::HTTP::_isfinite(0 + $args{"max_items"}) and $args{"max_items"} > 0
$args{"max_items"} =~ /\A\d+\z/ Net::UPnP::HTTP::_isfinite(0 + $args{"max_items"}) $args{"max_items"} > 0
Truth table25%1/4
A and B and C
A= $args{"max_items"} =~ /\A\d+\z/B= Net::UPnP::HTTP::_isfinite(0 + $args{"max_items"})C= $args{"max_items"} > 0
ABCresult
0XX0
10X0
1100
1111
151 }
152 my (
153 44 @content_list,
154 $action_res,
155 $arg_list,
156 $result,
157 $content,
158 $container,
159 $item,
160 $numberReturned,
161 $requestedCount,
162 $totalMatches
163 );
164
165 44 @content_list = ();
166 $action_res = $this->browsedirectchildren(
167 ObjectID => $args{ObjectID},
168 Filter => $args{Filter},
169 StartingIndex => $args{StartingIndex},
170 RequestedCount => $args{RequestedCount},
171 SortCriteria => $args{SortCriteria},
172 http_options => $args{http_options}
173 44 );
174 44 $numberReturned = 0;
175 44 ▶ if ($action_res->getstatuscode() == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action_res->getstatuscode == 200) 40 4 44
176 40 $arg_list = $action_res->getargumentlist();
177 40 $numberReturned = int($arg_list->{'NumberReturned'});
178 }
179 44 ▶ if ($numberReturned <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numberReturned <= 0) 6 38 44
180 $action_res = $this-> browsemetadata(
181 ObjectID => $args{ObjectID},
182 Filter => $args{Filter},
183 StartingIndex => $args{StartingIndex},
184 RequestedCount => $args{RequestedCount},
185 SortCriteria => $args{SortCriteria},
186 http_options => $args{http_options}
187 6 );
188 6 $requestedCount = 999;
189 6 ▶ if ($action_res->getstatuscode() == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action_res->getstatuscode == 200) 2 4 6
190 2 $arg_list = $action_res->getargumentlist();
191 2 $totalMatches = int($arg_list->{'TotalMatches'});
192 2 ▶ if (1 < $totalMatches) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < $totalMatches) 0 2 2
193 0 $requestedCount = $totalMatches;
194 }
195 }
196 $action_res = $this->browsedirectchildren(
197 ObjectID => $args{ObjectID},
198 Filter => $args{Filter},
199 StartingIndex => $args{StartingIndex},
200 RequestedCount => $requestedCount,
201 SortCriteria => $args{SortCriteria},
202 http_options => $args{http_options}
203 6 );
204 6 ▶ if ($action_res->getstatuscode() != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action_res->getstatuscode != 200) 4 2 6
205 4 $this->{content_error} = 'Content browse failed (HTTP ' . $action_res->getstatuscode() . ')';
206 4 return @content_list;
207 }
208 }
209 40 $arg_list = $action_res->getargumentlist();
210 40 ▶ unless ($arg_list->{'Result'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg_list->{"Result"}) 2 38 40
211 2 return @content_list;
212 }
213 38 $result = $arg_list->{'Result'};
214  
215 38 ▶ while ($result =~ m/<container(.*?)<\/container>/sgi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ m[<container(.*?)</container>]sig) 28 38 66
216 28 $content = $1;
217 28 $container = Net::UPnP::AV::Container->new();
218 28 ▶ if ($content =~ m/id=\"(.*?)\"/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /id=\"(.*?)\"/is) 28 0 28
219 28 $container->setid($1);
220 }
221 28 ▶ if ($content =~ m/<dc:title>(.*)<\/dc:title>/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<dc:title>(.*)</dc:title>]is) 28 0 28
222 28 $container->settitle($1);
223 }
224 28 ▶ if ($content =~ m/<dc:date>(.*)<\/dc:date>/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<dc:date>(.*)</dc:date>]is) 0 28 28
225 0 $container->setdate($1);
226 }
227 die "Content item limit exceeded\n"
228 28 ▶ if defined($args{max_items}) && @content_list >= $args{max_items};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"max_items"} and @content_list >= $args{"max_items"} 0 28 28
Condition66%2/3
defined $args{"max_items"} and @content_list >= $args{"max_items"}
!ll&&!rl&&r
4240
MC/DC0%0/2
defined $args{"max_items"} and @content_list >= $args{"max_items"}
defined $args{"max_items"} @content_list >= $args{"max_items"}
Truth table66%2/3
A and B
A= defined $args{"max_items"}B= @content_list >= $args{"max_items"}
ABresult
0X0
100
111
229 28 push (@content_list, $container);
230 #print "container(" . $container->getid() . ") = " . $container->gettitle() . "\n";
231 #print $1;
232 }
233  
234 38 ▶ while ($result =~ m/<item(.*?)<\/item>/sgi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ m[<item(.*?)</item>]sig) 26 36 62
235 26 $content = $1;
236 26 $item= Net::UPnP::AV::Item->new();
237 26 ▶ if ($content =~ m/id=\"(.*?)\"/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /id=\"(.*?)\"/is) 26 0 26
238 26 $item->setid($1);
239 }
240 26 ▶ if ($content =~ m/<dc:title>(.*)<\/dc:title>/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<dc:title>(.*)</dc:title>]is) 26 0 26
241 26 $item->settitle($1);
242 }
243 26 ▶ if ($content =~ m/<dc:date>(.*)<\/dc:date>/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<dc:date>(.*)</dc:date>]is) 0 26 26
244 0 $item->setdate($1);
245 }
246 26 ▶ if ($content =~ m/<res[^>]*>(.*?)<\/res>/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<res[^>]*>(.*?)</res>]is) 0 26 26
247 0 $item->seturl(Net::UPnP::HTTP::xmldecode($1));
248 }
249 26 ▶ if ($content =~ m/protocolInfo=\"http-get:[^:]*:([^:]*):.*\"/si) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /protocolInfo=\"http-get:[^:]*:([^:]*):.*\"/is) { } 0 26 26
elsif ($content =~ /protocolInfo=\"[^:]*:[^:]:([^:]*):.*\"/is) { } 0 26 26
250 0 $item->setcontenttype($1);
251 }
252 elsif ($content =~ m/protocolInfo=\"[^:]*:[^:]:([^:]*):.*\"/si) {
253 0 $item->setcontenttype($1);
254 }
255 die "Content item limit exceeded\n"
256 26 ▶ if defined($args{max_items}) && @content_list >= $args{max_items};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"max_items"} and @content_list >= $args{"max_items"} 2 24 26
Condition100%3/3
defined $args{"max_items"} and @content_list >= $args{"max_items"}
!ll&&!rl&&r
4202
MC/DC100%2/2
defined $args{"max_items"} and @content_list >= $args{"max_items"}
defined $args{"max_items"} @content_list >= $args{"max_items"}
Truth table100%3/3
A and B
A= defined $args{"max_items"}B= @content_list >= $args{"max_items"}
ABresult
0X0
100
111
257 24 push (@content_list, $item);
258 }
259  
260 36 @content_list;
261 }
262  
263  
264 # Depth-first traversal without recursive Perl calls. getcontentlist remains
265 # repeatable and one-level; state and budgets belong to this walk only.
266 33 ▶sub walkcontent {
Subroutine walkcontent: called CC=17 SCAR=28.5
Pod documented
267 33 my ($this, %args) = @_;
268 33 my %options = (ObjectID => '0', max_depth => 32, max_nodes => 4096,
269 timeout => 30, %args);
270 33 ▶ croak 'A content callback is required' unless ref($options{callback}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options{"callback"} eq "CODE" 0 33 33
271 33 foreach my $key (qw(max_depth max_nodes timeout)) {
272 93 ▶ my $pattern = $key eq 'timeout' ? qr/\A\d+(?:\.\d+)?\z/ : qr/\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'timeout' ? : 29 64 93
273 croak "Invalid walk $key" unless defined($options{$key})
274 && $options{$key} =~ $pattern && Net::UPnP::HTTP::_isfinite(0 + $options{$key})
275 93 ▶ && $options{$key} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{$key} and $options{$key} =~ /$pattern/ and Net::UPnP::HTTP::_isfinite(0 + $options{$key}) and $options{$key} > 0 6 87 93
Condition66%6/9
defined $options{$key} and $options{$key} =~ /$pattern/ and Net::UPnP::HTTP::_isfinite(0 + $options{$key}) and $options{$key} > 0
!ll&&!rl&&r
4287
defined $options{$key} and $options{$key} =~ /$pattern/ and Net::UPnP::HTTP::_isfinite(0 + $options{$key})
!ll&&!rl&&r
0489
defined $options{$key} and $options{$key} =~ /$pattern/
!ll&&!rl&&r
0093
MC/DC50%2/4
defined $options{$key} and $options{$key} =~ /$pattern/ and Net::UPnP::HTTP::_isfinite(0 + $options{$key}) and $options{$key} > 0
defined $options{$key} $options{$key} =~ /$pattern/ Net::UPnP::HTTP::_isfinite(0 + $options{$key}) $options{$key} > 0
Truth table60%3/5
A and B and C and D
A= defined $options{$key}B= $options{$key} =~ /$pattern/C= Net::UPnP::HTTP::_isfinite(0 + $options{$key})D= $options{$key} > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
276 }
277 27 $this->{walk_error} = '';
278 27 my $deadline = Net::UPnP::HTTP::_now() + $options{timeout};
279 27 my ($parsed, $visited) = (0, 0);
280 27 my %seen = ($options{ObjectID} => 1);
281 43 ▶ my $fetch = sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
282 43 my ($id) = @_;
283 43 ▶ if (Net::UPnP::HTTP::_now() >= $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::UPnP::HTTP::_now() >= $deadline) 0 43 43
284 0 $this->{walk_error} = 'Content walk deadline exceeded';
285 0 return undef;
286 }
287 43 ▶ if ($parsed >= $options{max_nodes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed >= $options{"max_nodes"}) 2 41 43
288 2 $this->{walk_error} = 'Content walk node limit reached';
289 2 return undef;
290 }
291 41 my @list;
292 41 my $ok = eval {
293 @list = $this->getcontentlist(ObjectID => $id,
294 41 max_items => $options{max_nodes} - $parsed,
295 http_options => {deadline => $deadline});
296 39 1;
297 };
298 41 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 39 41
299 2 ▶ $this->{walk_error} = $@ || 'Content browse failed';
Condition50%1/2
$@ || 'Content browse failed'
l!l
20
MC/DC0%0/1
$@ || 'Content browse failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
300 2 return undef;
301 }
302 39 ▶ if ($this->{content_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"content_error"}) 4 35 39
303 4 $this->{walk_error} = $this->{content_error};
304 4 return undef;
305 }
306 35 ▶ if (Net::UPnP::HTTP::_now() >= $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::UPnP::HTTP::_now() >= $deadline) 2 33 35
307 2 $this->{walk_error} = 'Content walk deadline exceeded';
308 2 return undef;
309 }
310 33 $parsed += @list;
311 33 return \@list;
312 27 };
313 27 my $root = $fetch->($options{ObjectID});
314 27 ▶ return 0 unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 6 21 27
315 21 my @stack = ({list => $root, index => 0, depth => 1});
316 21 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 56 13 69
317 56 my $frame = $stack[-1];
318 56 ▶ if ($frame->{index} >= @{$frame->{list}}) { pop @stack; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"index"} >= @{$frame->{"list"};}) 21 35 56
319 35 ▶ if (Net::UPnP::HTTP::_now() >= $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::UPnP::HTTP::_now() >= $deadline) 0 35 35
320 0 $this->{walk_error} = 'Content walk deadline exceeded'; last;
321 }
322 35 my $content = $frame->{list}[$frame->{index}++];
323 35 my $id = $content->getid();
324 35 ▶ next if $content->iscontainer() && $seen{$id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $content->iscontainer and $seen{$id}++ 4 31 35
Condition100%3/3
$content->iscontainer and $seen{$id}++
!ll&&!rl&&r
13184
MC/DC100%2/2
$content->iscontainer and $seen{$id}++
$content->iscontainer $seen{$id}++
Truth table100%3/3
A and B
A= $content->iscontainerB= $seen{$id}++
ABresult
0X0
100
111
325 31 ++$visited;
326 31 my $start = Net::UPnP::HTTP::_now();
327 31 $options{callback}->($content, $frame->{depth});
328 # Account for browsing, not caller-owned media conversion/processing.
329 29 $deadline += Net::UPnP::HTTP::_now() - $start;
330 29 ▶ next unless $content->iscontainer();
Branch: 1 branch
BranchTrueFalseTotal
unless $content->iscontainer 11 18 29
331 18 ▶ if ($frame->{depth} >= $options{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"depth"} >= $options{"max_depth"}) 2 16 18
332 2 $this->{walk_error} = 'Content walk depth limit reached'; last;
333 }
334 16 my $children = $fetch->($id);
335 16 ▶ last unless defined $children;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $children 4 12 16
336 12 push @stack, {list => $children, index => 0, depth => $frame->{depth} + 1};
337 }
338 19 return $visited;
339 }
340  
341 21 ▶sub getwalkerror { $_[0]->{walk_error} || '' }
Subroutine getwalkerror: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"walk_error"} or "" 12 9 21
342  
343 #------------------------------
344 # getsystemupdateid
345 #------------------------------
346  
347 0 ▶sub getsystemupdateid {
Subroutine getsystemupdateid: not called CC=2 SCAR=17.9
Pod undocumented
348 0 my($this) = shift;
349  
350 my (
351 0 $dev,
352 $condir_service,
353 $query_res,
354 );
355
356 0 $dev = $this->getdevice();
357 0 $condir_service = $dev->getservicebyname($Net::UPnP::AV::MediaServer::CONTENTDIRECTORY_SERVICE_TYPE);
358
359 0 $query_res = $condir_service->postquery("SystemUpdateID");
360  
361 0 ▶ if ($query_res->getstatuscode() != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_res->getstatuscode != 200) 0 0 0
362 0 return "";
363 }
364
365 0 return $query_res->getvalue();
366 }
367  
368 3 1;
369  
370 __END__
« blib/lib/Net/UPnP/HTTP.pm ↑ Summary examples/dms2vodcast.pl »