File Coverage

blib/lib/Net/BitTorrent/Torrent/Tracker.pm
Criterion Covered Total %
statement 65 89 73.0
branch 12 20 60.0
condition 12 15 80.0
subroutine 15 19 78.9
pod 3 3 100.0
total 107 146 73.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2             package Net::BitTorrent::Torrent::Tracker;
3             {
4 11     11   192389 use strict;
  11         25  
  11         462  
5 11     11   62 use warnings;
  11         24  
  11         415  
6 11     11   62 use Carp qw[carp];
  11         21  
  11         752  
7 11     11   65 use Scalar::Util qw[blessed weaken refaddr];
  11         21  
  11         681  
8 11     11   60 use List::Util qw[shuffle];
  11         22  
  11         743  
9 11     11   60 use lib q[./../../../];
  11         20  
  11         95  
10 11     11   9328 use Net::BitTorrent::Torrent::Tracker::HTTP;
  11         47  
  11         450  
11 11     11   8689 use Net::BitTorrent::Torrent::Tracker::UDP;
  11         53  
  11         463  
12 11     11   80 use version qw[qv];
  11         22  
  11         59  
13             our $VERSION_BASE = 50; our $UNSTABLE_RELEASE = 0; our $VERSION = sprintf(($UNSTABLE_RELEASE ? q[%.3f_%03d] : q[%.3f]), (version->new(($VERSION_BASE))->numify / 1000), $UNSTABLE_RELEASE);
14             my (@CONTENTS) = \my (%torrent, %urls);
15             my %REGISTRY;
16              
17             sub new {
18 44     44 1 220 my ($class, $args) = @_;
19 44         75 my $self;
20 44 100 100     346 if ((!$args) || (ref($args) ne q[HASH])) {
21 2         434 carp
22             q[Net::BitTorrent::Torrent::Tracker->new({}) requires parameters to be passed as a hashref];
23 2         25 return;
24             }
25 42 100 100     330 if ( (!$args->{q[URLs]})
  39   100     343  
26             || (ref $args->{q[URLs]} ne q[ARRAY])
27             || (!scalar(@{$args->{q[URLs]}})))
28 5         737 { carp
29             q[Net::BitTorrent::Torrent::Tracker->new({}) requires a list of URLs];
30 5         195 return;
31             }
32 37 100 66     464 if ( (!$args->{q[Torrent]})
      33        
33             || (!blessed $args->{q[Torrent]})
34             || (!$args->{q[Torrent]}->isa(q[Net::BitTorrent::Torrent])))
35 3         408 { carp
36             q[Net::BitTorrent::Torrent::Tracker->new({}) requires a Torrent];
37 3         127 return;
38             }
39 34         162 $self = bless(\$args->{q[URLs]}->[0], $class);
40 34         362 $torrent{refaddr $self} = $args->{q[Torrent]};
41 34         171 weaken $torrent{refaddr $self};
42 34         148 $urls{refaddr $self} = [];
43 34         74 for my $_url (@{$args->{q[URLs]}}) {
  34         114  
44 35 100       57 push @{$urls{refaddr $self}},
  35         1005  
45             ($_url =~ m[^http://]i
46             ? q[Net::BitTorrent::Torrent::Tracker::HTTP]
47             : q[Net::BitTorrent::Torrent::Tracker::UDP]
48             )->new({URL => $_url, Tier => $self});
49             }
50 34         225 weaken($REGISTRY{refaddr $self} = $self);
51 34         73 @{$urls{refaddr $self}} = shuffle(@{$urls{refaddr $self}});
  34         146  
  34         183  
52 34         184 return $self;
53             }
54              
55             # Accessors | Public
56 89     89 1 635 sub urls { return $urls{refaddr +shift}; }
57              
58             # Accessors | Private
59 197     197   1693 sub _client { return $torrent{refaddr +shift}->_client; }
60 157     157   1587 sub _torrent { return $torrent{refaddr +shift}; }
61              
62             sub _nodes {
63 0     0   0 my ($self) = @_;
64 0         0 return compact(map { $_->_nodes } @{$urls{refaddr $self}});
  0         0  
  0         0  
65             }
66              
67             # Methods | Private
68             sub _shuffle {
69 0     0   0 my ($self) = @_;
70             return (
71 0         0 push(@{$urls{refaddr $self}}, shift(@{$urls{refaddr $self}})));
  0         0  
  0         0  
72             }
73              
74             sub _announce {
75 22     22   62 my ($self, $event) = @_;
76 22 50       77 return if not defined $self;
77 22 50       123 return if not defined $urls{refaddr $self};
78 22 50       39 return if not scalar @{$urls{refaddr $self}};
  22         9752  
79 22 50       212 return $urls{refaddr $self}->[0]->_announce($event ? $event : ());
80             }
81              
82             sub as_string {
83 0     0 1 0 my ($self, $advanced) = @_;
84 0         0 my $dump = !$advanced ? $$self : sprintf <<'END',
85             Net::BitTorrent::Torrent::Tracker
86              
87             Complete: %d
88             Incomplete: %d
89             Number of URLs: %d
90             %s
91             END
92 0         0 scalar(@{$urls{refaddr $self}}),
93 0 0       0 join qq[\r\n ], map { $_->url() } @{$urls{refaddr $self}};
  0         0  
94 0 0       0 return defined wantarray ? $dump : print STDERR qq[$dump\n];
95             }
96              
97             sub CLONE {
98 0     0   0 for my $_oID (keys %REGISTRY) {
99 0         0 my $_obj = $REGISTRY{$_oID};
100 0         0 my $_nID = refaddr $_obj;
101 0         0 for (@CONTENTS) {
102 0         0 $_->{$_nID} = $_->{$_oID};
103 0         0 delete $_->{$_oID};
104             }
105 0         0 weaken $torrent{$_nID};
106 0         0 weaken($REGISTRY{$_nID} = $_obj);
107 0         0 delete $REGISTRY{$_oID};
108             }
109 0         0 return 1;
110             }
111             DESTROY {
112 20     20   44 my ($self) = @_;
113 20         52 for (@CONTENTS) { delete $_->{refaddr $self}; }
  40         427  
114 20         1261 return delete $REGISTRY{refaddr $self};
115             }
116             1;
117             }
118              
119             =pod
120              
121             =head1 NAME
122              
123             Net::BitTorrent::Torrent::Tracker - Single BitTorrent Tracker Tier
124              
125             =head1 Description
126              
127             Objects of this class should not be created directly.
128              
129             =head1 Methods
130              
131             =over
132              
133             =item C
134              
135             Constructor. Don't use this.
136              
137             =item C
138              
139             Returns the number of complete seeds the tracker says are present in the
140             swarm.
141              
142             =item C
143              
144             Returns the number of incomplete peers the tracker says are present in
145             the swarm.
146              
147             =item C
148              
149             Returns a list of related
150             L
151             and L
152             objects.
153              
154             =item C
155              
156             Returns a 'ready to print' dump of the object's data structure. If
157             called in void context, the structure is printed to C.
158             C is a boolean value.
159              
160             =back
161              
162             =head1 Author
163              
164             Sanko Robinson - http://sankorobinson.com/
165              
166             CPAN ID: SANKO
167              
168             =head1 License and Legal
169              
170             Copyright (C) 2008-2009 by Sanko Robinson Esanko@cpan.orgE
171              
172             This program is free software; you can redistribute it and/or modify
173             it under the terms of The Artistic License 2.0. See the F
174             file included with this distribution or
175             http://www.perlfoundation.org/artistic_license_2_0. For
176             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
177              
178             When separated from the distribution, all POD documentation is covered
179             by the Creative Commons Attribution-Share Alike 3.0 License. See
180             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For
181             clarification, see http://creativecommons.org/licenses/by-sa/3.0/us/.
182              
183             Neither this module nor the L is affiliated with
184             BitTorrent, Inc.
185              
186             =for svn $Id: Tracker.pm d3c97de 2009-09-12 04:31:46Z sanko@cpan.org $
187              
188             =cut