File Coverage

blib/lib/POE/Component/AssaultCube/ServerQuery/Server.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # Declare our package
2             package POE::Component::AssaultCube::ServerQuery::Server;
3              
4             # import the Moose stuff
5 1     1   2402 use Moose;
  0            
  0            
6             use MooseX::StrictConstructor;
7              
8             # Initialize our version
9             use vars qw( $VERSION );
10             $VERSION = '0.04';
11              
12             use Games::AssaultCube::Utils qw( default_port );
13             use Time::HiRes qw( time );
14              
15             # TODO improve validation for everything here, ha!
16              
17             has 'ID' => (
18             isa => 'Str',
19             is => 'ro',
20             lazy => 1,
21             default => sub {
22             my $self = shift;
23             return $self->server . ':' . $self->port;
24             },
25             );
26              
27             has 'server' => (
28             isa => 'Str',
29             is => 'ro',
30             required => 1,
31             );
32              
33             has 'port' => (
34             isa => 'Int',
35             is => 'ro',
36             lazy => 1,
37             default => sub {
38             return default_port;
39             },
40             );
41              
42             has 'last_pingtime' => (
43             isa => 'Num',
44             is => 'rw',
45             default => 0,
46             );
47              
48             has 'frequency' => (
49             isa => 'Num',
50             is => 'rw',
51             default => 300,
52             );
53              
54             has 'get_players' => (
55             isa => 'Bool',
56             is => 'rw',
57             default => 0,
58             );
59              
60             sub nextping {
61             my $self = shift;
62              
63             # if it was never pinged, do it now!
64             if ( $self->last_pingtime == 0 ) {
65             return 0;
66             }
67              
68             my $pingtime = ( $self->last_pingtime + $self->frequency ) - time();
69              
70             #warn "server(" . $self->ID . ") last_pingtime(" . $self->last_pingtime . ") frequency(" . $self->frequency . ") time(" . time() . ") pingtime(" . $pingtime . ")";
71              
72             if ( $pingtime < 0 ) {
73             return 0;
74             } else {
75             return $pingtime;
76             }
77             }
78              
79             sub BUILDARGS {
80             my $class = shift;
81              
82             if ( @_ == 1 && ! ref $_[0] ) {
83             # set the server as the first argument
84             return { server => $_[0] };
85             } elsif ( @_ == 2 && ! ref $_[0] ) {
86             # server/port argument
87             return { server => $_[0], port => $_[1] };
88             } else {
89             # normal hash/hashref way
90             return $class->SUPER::BUILDARGS(@_);
91             }
92             }
93              
94             # from Moose::Manual::BestPractices
95             no Moose;
96             __PACKAGE__->meta->make_immutable;
97              
98             1;
99             __END__
100              
101             =for stopwords nextping playerlist PoCo hostname ip
102              
103             =head1 NAME
104              
105             POE::Component::AssaultCube::ServerQuery::Server - Holds the server info
106              
107             =head1 SYNOPSIS
108              
109             use POE::Component::AssaultCube::ServerQuery;
110              
111             sub _start {
112             my $query = POE::Component::AssaultCube::ServerQuery->new;
113             $query->register;
114             my $server = POE::Component::AssaultCube::ServerQuery::Server->new( {
115             server => '123.123.123.123',
116             frequency => 60,
117             } );
118             $query->addserver( $server );
119             }
120              
121             =head1 ABSTRACT
122              
123             Holds the server info
124              
125             =head1 DESCRIPTION
126              
127             This module represents a server for the PoCo to ping. There are a few values to twiddle.
128              
129             =head2 Constructor
130              
131             This module uses Moose, so you can pass either a hash or a hashref to the constructor.
132              
133             The attributes are:
134              
135             =head3 server
136              
137             The server ip.
138              
139             NOTE: Input in the form of a hostname is not currently supported. Please resolve it before
140             instantiation of this object! A good module to use would be L<POE::Component::Client::DNS> or
141             anything else.
142              
143             =head3 port
144              
145             The server port. Defaults to 28763.
146              
147             WARNING: AssaultCube uses $port+1 for the query port. Please do not do pass $port+1 to the constructor,
148             we do it internally. Maybe in the future AC will use $port+2 or another system, so let us deal with it :)
149              
150             =head3 frequency
151              
152             A number in seconds ( can be floating-point )
153              
154             How long we should wait before sending the next ping.
155              
156             Default: 300
157              
158             =head3 get_players
159              
160             Should we also retrieve the playerlist? This is a boolean which defaults to false.
161              
162             =head2 Methods
163              
164             There are some methods you can call on the object:
165              
166             =head3 ID
167              
168             Returns the PoCo-assigned ID for this server.
169              
170             =head3 nextping
171              
172             Returns how many seconds to the next ping, or 0 if it should be done now.
173              
174             =head2 Attributes
175              
176             You can modify some attributes while the server is being pinged:
177              
178             =head3 frequency
179              
180             Same as the constructor
181              
182             =head3 get_players
183              
184             Same as the constructor
185              
186             =head1 AUTHOR
187              
188             Apocalypse E<lt>apocal@cpan.orgE<gt>
189              
190             Props goes to Getty and the BS clan for the support!
191              
192             This project is sponsored by L<http://cubestats.net>
193              
194             =head1 COPYRIGHT AND LICENSE
195              
196             Copyright 2009 by Apocalypse
197              
198             This library is free software; you can redistribute it and/or modify
199             it under the same terms as Perl itself.
200              
201             =cut