Games/AssaultCube/ServerQuery.pm

filter: Statementstmt 60.3% 38 / 63 Branchbran 9.0% 2 / 22 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 83.3% 10 / 12 Podpod 50.0% 1 / 2 totaltotal 43.0% 53 / 123 CC 17 SCAR 40.0
CC
17
Cov
42%
run
132.0
BUILDARGS
12.4
__ANON__
3.8
CRAP 54.8
« Games/AssaultCube/Log/Line.pm ↑ Summary POE/Component/AssaultCube/ServerQuery.pm »
1 # Declare our package
2 package Games::AssaultCube::ServerQuery;
3  
4 # import the Moose stuff
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Initialize our version
10 2 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $VERSION = '0.04';
12  
13 # get some utility stuff
14 2 ▶use Games::AssaultCube::Utils qw( default_port tostr get_ac_pingport );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Games::AssaultCube::ServerQuery::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # TODO make validation so we accept either hostname or IPv4/6...
19 2 has 'server' => (
20 isa => 'Str',
21 is => 'ro',
22 required => 1,
23 );
24  
25 # <mst> Apocalypse: { my $port_spec = subtype as Int => where { ... }; has 'attr' => (isa => $port_spec, ...); }
26 {
27 2 ▶ my $port_type = subtype as 'Int' => where {
Subroutine __ANON__: called CC=3 SCAR=13.3
28 1 ▶ if ( $_ <= 0 or $_ > 65535 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ <= 0 or $_ > 65535) { } 0 1 1
Condition33%1/3
$_ <= 0 or $_ > 65535
l!l&&r!l&&!r
001
MC/DC0%0/2
$_ <= 0 or $_ > 65535
$_ <= 0 $_ > 65535
Truth table33%1/3
A or B
A= $_ <= 0B= $_ > 65535
ABresult
1X1
011
000
29 0 return 0;
30 } else {
31 1 return 1;
32 }
33 2 };
34  
35 2 has 'port' => (
36 isa => $port_type,
37 # isa => 'Int',
38 is => 'rw',
39 default => default_port(),
40 );
41 }
42  
43 2 has 'timeout' => (
44 isa => 'Int',
45 is => 'rw',
46 default => 30,
47 );
48  
49 2 has 'get_players' => (
50 isa => 'Bool',
51 is => 'rw',
52 default => 0,
53 );
54  
55 1 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=5 SCAR=25.2
Pod documented
56 1 my $class = shift;
57  
58 1 ▶ if ( @_ == 1 && ! ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 1 0 1
elsif (@_ == 2 and not ref $_[0]) { } 0 0 0
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
59 # set the server as the first argument
60 1 return { server => $_[0] };
61 ▶ } elsif ( @_ == 2 && ! ref $_[0] ) {
Condition0%0/3
@_ == 2 and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 2 and not ref $_[0]
@_ == 2 not ref $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 2B= not ref $_[0]
ABresult
0X0
100
111
62 # server/port argument
63 0 return { server => $_[0], port => $_[1] };
64 } else {
65 # normal hash/hashref way
66 0 return $class->SUPER::BUILDARGS(@_);
67 }
68 }
69  
70 0 ▶sub run {
Subroutine run: not called CC=11 SCAR=48.8
Pod undocumented
71 0 my $self = shift;
72  
73 # Ok, get our socket and send off the PING!
74 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("Proto", "udp", "PeerPort", get_ac_pingport($self->port), "PeerAddr", $self->server) or die "Could not create socket: $!" 0 0 0
75 Proto => 'udp',
76 PeerPort => get_ac_pingport( $self->port ),
77 PeerAddr => $self->server,
78 ) or die "Could not create socket: $!";
79 0 ▶ binmode $sock, ":utf8" or die "Unable to set binmode: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $sock, ":utf8" or die "Unable to set binmode: $!" 0 0 0
80  
81 # generate the PING packet
82 # TODO support the EXT_XYZ options
83 0 my $pingpacket;
84 0 ▶ if ( $self->get_players ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_players) { } 0 0 0
85 0 $pingpacket = tostr('1') . tostr('1');
86 } else {
87 0 $pingpacket = tostr('1') . tostr('0');
88 }
89  
90 # send it!
91 0 ▶ $sock->send( $pingpacket ) or die "Unable to send: $!";
Branch: 1 branch
BranchTrueFalseTotal
$sock->send($pingpacket) or die "Unable to send: $!" 0 0 0
92  
93 # set the alarm, and wait for the response
94 0 my( $datagram, $result );
95 0 eval {
96 # perldoc -f alarm says I need to put \n in the die... weird!
97 0 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 alarm $self->timeout;
99 0 ▶ $result = $sock->recv( $datagram, 1024 ) or die "Unable to recv: $!";
Branch: 1 branch
BranchTrueFalseTotal
$result = $sock->recv($datagram, 1024) or die "Unable to recv: $!" 0 0 0
100 0 alarm 0;
101 };
102 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
103 0 ▶ if ( $@ =~ /^alarm/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^alarm/) { } 0 0 0
104 0 die "Unable to query server: Timed out";
105 } else {
106 0 die "Unable to query server: $@";
107 }
108 } else {
109 0 ▶ if ( defined $result and defined $datagram and length( $datagram ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and defined $datagram and length $datagram > 0) { } 0 0 0
Condition0%0/6
defined $result and defined $datagram and length $datagram > 0
!ll&&!rl&&r
000
defined $result and defined $datagram
!ll&&!rl&&r
000
MC/DC0%0/3
defined $result and defined $datagram and length $datagram > 0
defined $result defined $datagram length $datagram > 0
Truth table0%0/4
A and B and C
A= defined $resultB= defined $datagramC= length $datagram > 0
ABCresult
0XX0
10X0
1100
1111
110 0 return Games::AssaultCube::ServerQuery::Response->new( $self, $datagram );
111 } else {
112 0 return;
113 }
114 }
115 }
116  
117 # from Moose::Manual::BestPractices
118 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 __PACKAGE__->meta->make_immutable;
120  
121 2 1;
122 __END__
« Games/AssaultCube/Log/Line.pm ↑ Summary POE/Component/AssaultCube/ServerQuery.pm »