blib/lib/Net/Starnet/DataAccounting.pm

filter: Statementstmt 77.6% 94 / 121 Branchbran 46.1% 12 / 26 Conditioncond 33.3% 8 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 82.1% 23 / 28 Podpod 100% 5 / 5 totaltotal 64.5% 142 / 220 CC 22 SCAR 26.8
CC
22
Cov
67%
new
36.9
_sda_send
12.0
_sda_connect
6.0
CRAP 14.5
↑ Summary
1 package Net::Starnet::DataAccounting;
2  
3 =head1 NAME
4  
5 Net::Starnet::DataAccounting - interface to the SDA protocol
6  
7 =head1 SYNOPSIS
8  
9 use constant SDA_UPDATE_TIME 60;
10 my $sda = Net::Starnet::DataAccounting->new(
11 user => $user,
12 pass => $pass,
13 verbose => $VERBOSE,
14 login => \&login,
15 logout => \&logout,
16 update => \&update,
17 (defined($hostname) ? ( host => $hostname ) : ()),
18 (defined($server) ? ( server => $server ) : ()),
19 );
20 my $connected = $sda->login();
21 if ($connected)
22 {
23 $SIG{INT} = $SIG{TERM} = sub {
24 $sda->logout();
25 exit 0;
26 };
27 while ($connected)
28 {
29 sleep SDA_UPDATE_TIME;
30 $connected = $sda->update();
31 }
32 my $disconnected = $sda->logout();
33 }
34  
35 =head1 DESCRIPTION
36  
37 The Net::Starnet::DataAccounting module provides an interface to the
38 protocol used by the Starnet Data Accounting System. It allows simple
39 login, logout and health checking.
40  
41 =cut
42  
43 2 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 $|++;
51  
52 2 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use constant SDA_HOST => '150.203.223.8:8000';
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 2 ▶use constant SDA_LOGIN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use constant SDA_LOGOUT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use constant SDA_UPDATE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 2 ▶use constant SDA_LOGIN_YES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use constant SDA_LOGIN_NO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use constant SDA_LOGIN_INCORRECT_USERPASS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use constant SDA_LOGIN_NO_QUOTA => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use constant SDA_LOGIN_ALREADY_CONNECTED => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 ▶use constant SDA_UPDATE_YES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use constant SDA_UPDATE_NO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 2 ▶use constant SDA_UPDATE_TIME => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 2 our @ISA = qw//;
71 2 our ( $VERSION ) = '$Revision: 1.2 $ ' =~ /\$Revision:\s+([^\s]+)/;
72  
73 2 my %defaults = (
74 server => '150.203.223.8',
75 port => '8000',
76 client => "Spoon-v$VERSION",
77 );
78  
79  
80 # ========================================================================
81 # Methods
82  
83 =head1 METHODS
84  
85 =over 4
86  
87 =item Net::Starnet::DataAccounting->new(
88  
89 host => $yourhostname,
90 server => $remotehostname,
91 port => $remoteport,
92 user => $username,
93 pass => $password,
94 client => $clientname,
95 login => \&login,
96 logout => \&logout,
97 update => \&update,
98 verbose => $verbose,
99 )
100  
101 Creates a new SDA connection. Host and server should be either IPs or
102 hostnames. Port is a port number, user and pass are the appropriate
103 username and password. Client is a custom client string for the
104 connection to use.
105  
106 Login, logout and update are routines that will be called after an
107 attempt to send the appropriate message. The routines in question will
108 be passed two parameters: the SDA object and the text response from the
109 server (decoded).
110  
111 Verbose determines whether debugging information will be shown.
112  
113 =cut
114  
115 sub new
116 1 ▶{
Subroutine new: called CC=17 SCAR=36.1
Pod documented
117 1 my $class = shift;
118 1 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
119  
120 1 my %opts = @_;
121  
122 1 my $hostname;
123 1 ▶ if (exists $opts{host})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'host'}) { } 0 1 1
124 {
125 0 $hostname = $opts{host};
126 }
127 else
128 {
129 $hostname = eval
130 1 {
131 1 require Sys::Hostname;
132 1 Sys::Hostname::hostname();
133 };
134 }
135 1 ▶ my $ip = gethostbyname($hostname) or die "Couldn't resolve $hostname.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $ip = gethostbyname $hostname or die "Couldn't resolve $hostname.\n" 1 0 1
136 1 $ip = join('.', unpack('C4', $ip));
137  
138 1 ▶ my $server = $opts{server} || $defaults{server};
Condition33%1/3
$opts{'server'} || $defaults{'server'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'server'} || $defaults{'server'}
$opts{'server'} $defaults{'server'}
Truth table33%1/3
A || B
A= $opts{'server'}B= $defaults{'server'}
ABresult
1X1
011
000
139 1 ▶ die "Invalid server name.\n" unless (defined $server and length $server);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server and length $server 0 1 1
Condition33%1/3
defined $server and length $server
!ll&&!rl&&r
001
MC/DC0%0/2
defined $server and length $server
defined $server length $server
Truth table33%1/3
A and B
A= defined $serverB= length $server
ABresult
0X0
100
111
140 1 ▶ $server = gethostbyname($server) or die "Couldn't resolve $server.\n";
Branch: 1 branch
BranchTrueFalseTotal
$server = gethostbyname $server or die "Couldn't resolve $server.\n" 1 0 1
141 1 $server = join('.', unpack('C4', $server));
142  
143 1 ▶ my $port = $opts{port} || $defaults{port};
Condition33%1/3
$opts{'port'} || $defaults{'port'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'port'} || $defaults{'port'}
$opts{'port'} $defaults{'port'}
Truth table33%1/3
A || B
A= $opts{'port'}B= $defaults{'port'}
ABresult
1X1
011
000
144 1 ▶ die "Invalid port number.\n" unless ($port < 65536 and $port > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $port < 65536 and $port > 0 0 1 1
Condition33%1/3
$port < 65536 and $port > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$port < 65536 and $port > 0
$port < 65536 $port > 0
Truth table33%1/3
A and B
A= $port < 65536B= $port > 0
ABresult
0X0
100
111
145
146 1 ▶ die "Invalid username.\n" unless (defined $opts{user} and $opts{user} =~ /^\w+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"user"} and $opts{"user"} =~ /^\w+$/ 0 1 1
Condition33%1/3
defined $opts{"user"} and $opts{"user"} =~ /^\w+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts{"user"} and $opts{"user"} =~ /^\w+$/
defined $opts{"user"} $opts{"user"} =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $opts{"user"}B= $opts{"user"} =~ /^\w+$/
ABresult
0X0
100
111
147 1 ▶ die "Invalid password.\n" unless (defined $opts{pass} and $opts{pass} =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"pass"} and $opts{"pass"} =~ /^\d+$/ 0 1 1
Condition33%1/3
defined $opts{"pass"} and $opts{"pass"} =~ /^\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts{"pass"} and $opts{"pass"} =~ /^\d+$/
defined $opts{"pass"} $opts{"pass"} =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $opts{"pass"}B= $opts{"pass"} =~ /^\d+$/
ABresult
0X0
100
111
148  
149 my $self = bless {
150 host => $ip,
151 server => $server,
152 port => $port,
153 client => $opts{client} || $defaults{client},
154 login => $opts{login},
155 logout => $opts{logout},
156 update => $opts{update},
157 user => $opts{user},
158 pass => $opts{pass},
159 1 ▶ verbose => $opts{verbose} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts{'verbose'} ? : 0 1 1
Condition33%1/3
$opts{'client'} || $defaults{'client'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'client'} || $defaults{'client'}
$opts{'client'} $defaults{'client'}
Truth table33%1/3
A || B
A= $opts{'client'}B= $defaults{'client'}
ABresult
1X1
011
000
160 }, $class;
161  
162  
163 1 return $self;
164 }
165  
166 =item $sda->verbose($value|)
167  
168 If given a parameter, sets the verbosity. Returns the verbosity in all
169 cases.
170  
171 =cut
172  
173 sub verbose
174 4 ▶{
Subroutine verbose: called CC=3 SCAR=11.0
Pod documented
175 4 my $self = shift;
176 4 ▶ $self->{verbose} = $_[0] ? 1 : 0 if (@_);
Branch: 2 branches
BranchTrueFalseTotal
if @_ 2 2 4
$_[0] ? : 1 1 2
177 4 return $self->{verbose};
178 }
179  
180 =item $sda->login()
181  
182 Directs the SDA object to attempt to connect to the server. Calls the
183 login routine specified on construction after the attempt is made.
184  
185 =cut
186  
187 sub login
188 0 ▶{
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
189 0 my $self = shift;
190 0 my $response = $self->_sda_send(SDA_LOGIN);
191 0 $self->{login}($self,$response);
192 }
193  
194 =item $sda->logout()
195  
196 Directs the SDA object to attempt to disconnect to the server. Calls the
197 logout routine specified on construction after the attempt is made.
198  
199 =cut
200  
201 sub logout
202 0 ▶{
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 my $response = $self->_sda_send(SDA_LOGOUT);
205 0 $self->{logout}($self,$response);
206 }
207  
208 =item $sda->update()
209  
210 Directs the SDA object to attempt to update the client's status on the
211 server. Calls the update routine specified on construction after the
212 attempt is made. This function should be called every two minutes or so;
213 ideally more frequently.
214  
215 =cut
216  
217 sub update
218 0 ▶{
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220 0 my $response = $self->_sda_send(SDA_UPDATE);
221 0 $self->{update}($self,$response);
222 }
223  
224 =back
225  
226 =cut
227  
228 # ========================================================================
229 # Private
230  
231 =begin private
232  
233 =head1 PRIVATE METHODS
234  
235 =over 4
236  
237 =cut
238  
239 # ------------------------------------------------------------------------
240 # SDA Server stuff
241 # ------------------------------------------------------------------------
242  
243 =item $sock = $sda->_sda_connect()
244  
245 Connects to the object's server on the given port and returns the
246 socket.
247  
248 =cut
249  
250 sub _sda_connect
251 0 ▶{
Subroutine _sda_connect: not called CC=2 SCAR=17.9
252 0 my $self = shift;
253 0 my ($host,$port) = @{$self}{qw/server port/};
254 0 ▶ my $sock = IO::Socket::INET->new
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "Proto", "tcp", "Type", 1) or die "Couldn't connect to ${host}:${port}: $@\n" 0 0 0
255 (
256 PeerAddr => $host,
257 PeerPort => $port,
258 Proto => 'tcp',
259 Type => SOCK_STREAM
260 ) or die "Couldn't connect to $host:$port: $@\n";
261  
262 0 return $sock;
263 }
264  
265 =item $result = $sda->_sda_send(ACTION)
266  
267 Where ACTION is one of SDA_UPDATE, SDA_LOGIN, SDA_LOGOUT.
268  
269 Sends an appropriately encoded message to the server (does the
270 connection by calling _sda_connect()), waits for a response and returns
271 the result (decoded).
272  
273 =cut
274  
275 sub _sda_send
276 0 ▶{
Subroutine _sda_send: not called CC=3 SCAR=24.8
277 0 my ($self, $number) = @_;
278 0 my $sock = $self->_sda_connect();
279 0 my ($user, $pass, $ip, $client) = @{$self}{qw/user pass host client/};
280 0 my $line = "$number $user $pass $ip 0 $client \n";
281 0 ▶ print ">$line" if DEBUG or $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
282 0 print $sock ''._encode($line);
283 0 my $response = <$sock>;
284 0 $response = _decode($response);
285 0 ▶ print "<$response\n" if DEBUG or $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
286 0 $sock->close;
287 0 return $response;
288 }
289  
290 # ------------------------------------------------------------------------
291 # Generic Routines
292 # ------------------------------------------------------------------------
293  
294 =item $encoded = _encode($plain)
295  
296 Encodes $plain according to the SDA encoding method.
297  
298 =cut
299  
300 sub _encode
301 4 ▶{
Subroutine _encode: called CC=1 SCAR=0.0
302 4 my $i = 0;
303 4 my ($s) = (@_);
304 4 $s =~ s/(.)/chr ord($1)+$i++%7/eg;
305 4 return $s;
306 }
307  
308 =item $plain = _decode($encoded)
309  
310 Decodes $plain according to the SDA encoding method.
311  
312 =cut
313  
314 sub _decode
315 4 ▶{
Subroutine _decode: called CC=1 SCAR=0.0
316 4 my $i = 0;
317 4 my ($s) = (@_);
318 4 $s =~ s/(.)/chr ord($1)-$i++%7/eg;
319 4 return $s;
320 }
321  
322 =back
323  
324 =end private
325  
326 =cut
327  
328 2 1;
329 __END__
↑ Summary