File Coverage

blib/lib/Slackware/Slackget/Network/Connection/DEBUG.pm
Criterion Covered Total %
statement 8 73 10.9
branch 0 18 0.0
condition 0 6 0.0
subroutine 3 9 33.3
pod n/a
total 11 106 10.3


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Network::Connection::DEBUG;
2              
3             BEGIN {
4 1     1   38549 srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
5 1         888 print STDOUT "[Slackware::Slackget::Network::Connection::DEBUG] driver compiled.\n";
6             }
7              
8 1     1   50 use warnings;
  1         9  
  1         101  
9 1     1   7 use strict;
  1         9  
  1         1713  
10             require Slackware::Slackget::Network::Connection;
11              
12             =head1 NAME
13              
14             Slackware::Slackget::Network::Connection::DEBUG - This class iimplements the debug:// protocol driver for Slackware::Slackget::Network::Connection
15              
16             =head1 VERSION
17              
18             Version 0.5
19              
20             =cut
21              
22             our $VERSION = '0.5';
23             print STDOUT "[Slackware::Slackget::Network::Connection::DEBUG] enable Slackware::Slackget::Network::Connection debug mode.\n";
24             $Slackware::Slackget::Network::Connection::DEBUG=1;
25              
26             =head1 SYNOPSIS
27              
28             This class implements the debug:// protocol driver for Slackware::Slackget::Network::Connection.
29              
30             You can't use this class without the Slackware::Slackget::Network::Connection one.
31              
32             This class was implemented for 2 main reasons :
33              
34             * To help coding and debuging new protocol drivers
35             * As a tutorial on how to code a new driver.
36              
37             You should always remember that this class do absolutly nothing !
38              
39             All downloads are fake ones as well as the
40              
41             =cut
42              
43             sub new
44             {
45 0     0     my ($class,$url,$config) = @_ ;
46 0           my $self = {};
47 0 0         return undef unless (is_url($self,$url));
48 0           bless($self,$class);
49 0           $self->parse_url($url) ;
50 0           _debug_print("constructor called (it should not).");
51 0           return $self;
52             }
53              
54             sub _debug_print {
55 0     0     print STDOUT "[Slackware::Slackget::Network::Connection::DEBUG] @_";
56             }
57              
58             =head1 CONSTRUCTOR
59              
60             =head2 new
61              
62             This class is not designed to be instanciate alone or used alone. You have to use the Slackware::Slackget::Network::Connection.
63              
64             =head1 FUNCTIONS
65              
66             =head2 __test_server
67              
68             This method act as if it test the rapidity of a repository. It return a random number between 10 and 90.
69              
70             Moreover it output a message on the standard output (STDOUT).
71              
72             my $time = $self->test_server() ;
73              
74             =cut
75              
76             sub __test_server {
77 0     0     my $self = shift ;
78 0           _debug_print("protocol : $self->{DATA}->{protocol}\n");
79 0           _debug_print("host : $self->{DATA}->{host}\n");
80 0           return (rand(90)+10);
81             }
82              
83             =head2 __get_file
84              
85             Return the following string :
86              
87             "This is a debug output from the Slackware::Slackget::Network::Connection::DEBUG network driver.\n"
88              
89             If you want it to return an error, please set an extra data called 'debug-want-error', like that :
90              
91             $connection->object_extra_data('debug-want-error', 1);
92             $connection->get_file('TEST.TXT');
93              
94             You can use this method like any other connection driver's one :
95              
96             my $file = $connection->get_file('PACKAGES.TXT') ;
97              
98             It output some informations (protocol, host, path and remote file) on the standard output (STDOUT).
99              
100             =cut
101              
102             sub __get_file {
103 0     0     my ($self,$remote_file) = @_ ;
104 0           _debug_print("[__get_file] protocol=".$self->protocol()."\n");
105 0           _debug_print("[__get_file] host=".$self->host()."\n");
106 0           _debug_print("[__get_file] path=".$self->path()."\n");
107 0           _debug_print("[__get_file] remote file downloaded=".$remote_file."\n");
108 0           $self->post_event('progress',$remote_file,1,100);
109 0           sleep 1;
110 0           $self->post_event('progress',$remote_file,33,100);
111 0           sleep 1;
112 0           $self->post_event('progress',$remote_file,66,100);
113 0           sleep 1;
114 0           $self->post_event('progress',$remote_file,99,100);
115 0           sleep 1;
116 0           $self->post_event('progress',$remote_file,100,100);
117 0 0         return undef if($self->object_extra_data('debug-want-error'));
118 0           return "This is a debug output from the Slackware::Slackget::Network::Connection::DEBUG network driver.\n";
119             }
120              
121             =head2 __fetch_file
122              
123             Provide a
124              
125             This method return a Slackware::Slackget::Status object with the following object declaration :
126              
127             my $state = Slackware::Slackget::Status->new(codes => {
128             0 => "All goes well. Server said: $ret_code - ".status_message( $ret_code ),
129             1 => "Destination directory does not exist.\n",
130             2 => "Destination directory is not writable.\n",
131             3 => "Server error, you must choose the next host for this server. \nServer said: \nThis is a debug output from the Slackware::Slackget::Network::Connection::DEBUG network driver.",
132             });
133              
134             This method is also affected by the 'debug-want-error' extra data (if set with a true value it will generate a download error event).
135              
136             This method is also affected by the 'debug-want-success' extra data (if set with a true value, it will generate a download finished event)
137              
138             The default behavior is to randomize the generated state.
139              
140             =cut
141              
142             sub __fetch_file {
143 0     0     my ($self,$remote_file,$local_file) = @_ ;
144 0 0         $remote_file = $self->file unless(defined($remote_file));
145 0           my $state = Slackware::Slackget::Status->new(codes => {
146             0 => "All goes well.",
147             1 => "Destination directory does not exist.",
148             2 => "Destination directory is not writable.",
149             3 => "Server error, you must choose the next host for this server. \nServer said: \nThis is a debug output from the Slackware::Slackget::Network::Connection::DEBUG network driver.",
150             4 => "Module error: unable to determine the path to save $remote_file",
151             });
152 0 0         unless(defined($local_file)){
153 0 0 0       if(defined($self->{DATA}->{download_directory}) && -e $self->{DATA}->{download_directory}){
    0          
154 0           $remote_file=~ /([^\/]*)$/;
155 0           $local_file = $self->{DATA}->{download_directory}.'/'.$1 ;
156             }
157             elsif(defined($self->{DATA}->{config})){
158 0           $remote_file=~ /([^\/]*)$/;
159 0           $local_file = $self->{DATA}->{config}->{common}->{'update-directory'}.'/'.$1 ;
160             }
161             else{
162 0           warn "[Slackware::Slackget::Network::Connection::DEB$self->object_extra_data('debug-want-error')UG] unable to determine the path to save $remote_file.\n";
163 0           return $state->current(4);
164 0           return $state;
165             }
166             }
167 0           _debug_print("[__get_file] protocol=".$self->protocol()."\n");
168 0           _debug_print("[__get_file] host=".$self->host()."\n");
169 0           _debug_print("[__get_file] path=".$self->path()."\n");
170 0           _debug_print("[__get_file] remote file downloaded=".$remote_file."\n");
171 0           _debug_print("[__get_file] local file=".$local_file."\n");
172 0           $self->post_event('progress',$remote_file,1,100);
173 0           sleep 1;
174 0           $self->post_event('progress',$remote_file,33,100);
175 0           sleep 1;
176 0           $self->post_event('progress',$remote_file,66,100);
177 0           sleep 1;
178 0           $self->post_event('progress',$remote_file,99,100);
179 0           sleep 1;
180 0           $self->post_event('progress',$remote_file,100,100);
181 0           my $errno = int(rand(3));
182 0 0 0       $errno++ if($errno <= 0 && $self->object_extra_data('debug-want-error') );
183 0 0         $errno = 0 if($self->object_extra_data('debug-want-success'));
184 0           $state->current($errno);
185 0           return $state;
186             }
187              
188             sub _validate_url {
189 0     0     my ($self,$url)=@_ ;
190 0 0         if($url =~ m?^debug://.+?){
191 0           _debug_print("validating the following url as a valid debug one : $url\n");
192 0           return 1 ;
193             }else{
194 0           _debug_print("could not validate the following url as a valid debug one : $url\n");
195 0           return 0;
196             }
197             }
198              
199             =head1 AUTHOR
200              
201             DUPUIS Arnaud, C<< >>
202              
203             =head1 BUGS
204              
205             Please report any bugs or feature requests to C, or through
206             the web interface at L. I will be notified, and then you'll
207             automatically be notified of progress on your bug as I make changes.
208              
209              
210              
211              
212             =head1 SUPPORT
213              
214             You can find documentation for this module with the perldoc command.
215              
216             perldoc Slackware::Slackget::Network::Connection::DEBUG
217              
218              
219             You can also look for information at:
220              
221             =over 4
222              
223             =item * Infinity Perl website
224              
225             L
226              
227             =item * RT: CPAN's request tracker
228              
229             L
230              
231             =item * AnnoCPAN: Annotated CPAN documentation
232              
233             L
234              
235             =item * CPAN Ratings
236              
237             L
238              
239             =item * Search CPAN
240              
241             L
242              
243             =back
244              
245              
246             =head1 ACKNOWLEDGEMENTS
247              
248              
249             =head1 COPYRIGHT & LICENSE
250              
251             Copyright 2007 DUPUIS Arnaud, all rights reserved.
252              
253             This program is free software; you can redistribute it and/or modify it
254             under the same terms as Perl itself.
255              
256              
257             =cut
258              
259             1; # End of Slackware::Slackget::Network::Connection::DEBUG