From 534626943a0a5e251e5465376f3de3fb71b25e91 Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Wed, 11 Feb 2015 21:12:44 +0100 Subject: ability to query UCC applets (Pg only) --- lib/MasterServer/Database/Pg/dbBeacon.pm | 150 ++++++++++++++++++--------- lib/MasterServer/Database/Pg/dbCore.pm | 6 +- lib/MasterServer/Database/Pg/dbServerlist.pm | 28 ++--- 3 files changed, 114 insertions(+), 70 deletions(-) (limited to 'lib/MasterServer/Database/Pg') diff --git a/lib/MasterServer/Database/Pg/dbBeacon.pm b/lib/MasterServer/Database/Pg/dbBeacon.pm index e396810..735eaf9 100755 --- a/lib/MasterServer/Database/Pg/dbBeacon.pm +++ b/lib/MasterServer/Database/Pg/dbBeacon.pm @@ -6,14 +6,16 @@ use warnings; use Exporter 'import'; our @EXPORT = qw| add_beacon + add_pending remove_pending - set_direct_beacon get_pending_beacon get_pending_info get_next_pending |; +################################################################################ ## Update beacon in serverlist or pending list. Add if beacon does not exist in -## either list. Return 0,1,2 if success in adding or -1 on error +## either list. Return 0,1,2 if success in adding or -1 on error. +################################################################################ sub add_beacon { my ($self, $ip, $beaconport, $heartbeat, $gamename, $secure) = @_; @@ -35,7 +37,7 @@ sub add_beacon { return 0 if ($u > 0); # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( + $u = $self->{dbh}->do( "UPDATE pending SET added = NOW(), beaconport = ?, @@ -52,8 +54,13 @@ sub add_beacon { return 1 if ($u > 0); # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending (ip, beaconport, heartbeat, gamename, secure) + $u = $self->{dbh}->do( + "INSERT INTO pending ( + ip, + beaconport, + heartbeat, + gamename, + secure) SELECT ?, ?, ?, ?, ?", undef, $ip, $beaconport, $heartbeat, lc $gamename, $secure); @@ -68,83 +75,128 @@ sub add_beacon { return -1; } -## server checks out, remove entry from the pending list. -sub remove_pending { - my ($self, $id) = @_; - +################################################################################ +## Add an address to the database that was obtained via a method other than +## an udp beacon. Return 0,1,2 if success in adding or -1 on error. +################################################################################ +sub add_pending { + my ($self, $ip, $port, $gamename, $secure) = @_; + # if address is in list, update the timestamp - my $u = $self->{dbh}->do("DELETE FROM pending WHERE id = ?", undef, $id); + my $u = $self->{dbh}->do( + "UPDATE serverlist + SET updated = NOW() + WHERE ip = ? + AND port = ?", + undef, $ip, $port); + + # notify + $self->log("update", "updated serverlist with $ip:$port") if ($u > 0); - # notify - $self->log("deleted", "removed pending id $id from the list of pending servers") if ($u > 0); + # if updated, return 0 + return 0 if ($u > 0); + + # if it is already in the pending list, update it with a new challenge + $u = $self->{dbh}->do( + "UPDATE pending + SET added = NOW(), + secure = ? + WHERE ip = ? + AND heartbeat = ?", + undef, $secure, $ip, $port); + + # notify + $self->log("update", "updated pending with $ip:$port") if ($u > 0); - # it was added to pending - return 2 if ($u > 0); + # return 1 if updated + return 1 if ($u > 0); - # or else report error - $self->log("error", "an error occurred deleting server $id from the pending list"); + # if not found, add it + $u = $self->{dbh}->do( + "INSERT INTO pending ( + ip, + heartbeat, + gamename, + secure) + SELECT ?, ?, ?, ?", + undef, $ip, $port, $gamename, $secure); + + # notify + $self->log("add", "$ip:$port added pending $gamename") if ($u > 0); + + # return 2 if added new + return 2 if ($u > 0); + + # else return -1; } - -## mark server as "direct beacon to this masterserver" -sub set_direct_beacon { - my ($self, $ip, $port) = @_; +################################################################################ +## Remove an entry from the pending list. Returns 0 if removed or -1 in case +## of error(s). +################################################################################ +sub remove_pending { + my ($self, $id) = @_; - # update or add server to serverlist - my $u = $self->{dbh}->do("UPDATE serverlist - SET b333ms = TRUE - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "$ip:$port is a direct beacon.") if ($u > 0); + # if address is in list, update the timestamp + my $u = $self->{dbh}->do( + "DELETE FROM pending + WHERE id = ?", + undef, $id); - # if found, updated; done - return 0 if ($u > 0); + # notify + $self->log("delete", "removed pending id $id from pending") if ($u > 0); + + # it was removed from pending + return 2 if ($u > 0); # or else report error - $self->log("error", "an error occurred setting server $ip:$port as direct beacon"); + $self->log("error", "error deleting server $id from pending"); return -1; } - - -## Get pending server by ip, beacon port. +################################################################################ +## Get pending server by ip, beacon port. Returns * or undef +################################################################################ sub get_pending_beacon { my ($self, $ip, $port) = @_; # if address is in list, update the timestamp return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND beaconport = ?", - undef, $ip, $port)->[0]; + "SELECT * FROM pending + WHERE ip = ? + AND beaconport = ?", + undef, $ip, $port)->[0]; } -## Same as get_pending_beacon, but with heartbeat port as identifier +################################################################################ +## Get pending server by ip, heartbeat port. Returns * or undef +################################################################################ sub get_pending_info { my ($self, $ip, $port) = @_; # if address is in list, update the timestamp return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND heartbeat = ?", - undef, $ip, $port)->[0]; + "SELECT * FROM pending + WHERE ip = ? + AND heartbeat = ?", + undef, $ip, $port)->[0]; } -## Get server info from any entry with an id higher than the provided one. +################################################################################ +## Get server info from any entry with an id higher than the provided one. The +## server is added to pending at least 15 seconds ago. Returns info or undef. +################################################################################ sub get_next_pending { my ($self, $id) = @_; # get 1 pending id that is older than 15s return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, heartbeat, secure FROM pending - WHERE added < (NOW() - INTERVAL '15 SECONDS') - AND id > ? - ORDER BY id ASC LIMIT 1", undef, $id)->[0]; + "SELECT id, ip, heartbeat, secure FROM pending + WHERE added < (NOW() - INTERVAL '15 SECONDS') + AND id > ? + ORDER BY id ASC LIMIT 1", + undef, $id)->[0]; } diff --git a/lib/MasterServer/Database/Pg/dbCore.pm b/lib/MasterServer/Database/Pg/dbCore.pm index f89cc68..8f55ebb 100755 --- a/lib/MasterServer/Database/Pg/dbCore.pm +++ b/lib/MasterServer/Database/Pg/dbCore.pm @@ -8,9 +8,8 @@ use Exporter 'import'; our @EXPORT = qw| database_login |; ################################################################################ -## database_login ## login to the database with credentials provided in the config file. -## returns dbh object +## returns dbh object or quits application on error. ################################################################################ sub database_login { my $self = shift; @@ -20,6 +19,7 @@ sub database_login { # verify that the database connected if (defined $dbh) { + # log the event $self->log("load","Connected to the Postgres database."); @@ -37,7 +37,7 @@ sub database_login { $self->halt(); } - # unreachable + # return empty element return undef; } diff --git a/lib/MasterServer/Database/Pg/dbServerlist.pm b/lib/MasterServer/Database/Pg/dbServerlist.pm index b1a787e..832a08f 100755 --- a/lib/MasterServer/Database/Pg/dbServerlist.pm +++ b/lib/MasterServer/Database/Pg/dbServerlist.pm @@ -9,8 +9,10 @@ our @EXPORT = qw| add_to_serverlist update_serverlist get_next_server |; -## beacon was verified or otherwise accepted and will noe now be added to the +################################################################################ +## beacon was verified or otherwise accepted and will now be added to the ## serverlist. +################################################################################ sub add_to_serverlist { my ($self, $ip, $port, $gamename) = @_; @@ -44,13 +46,9 @@ sub add_to_serverlist { } ################################################################################ -## -## Subroutine update_serverlist -## -## Same as add_to_serverlist (above), but does not add the server to serverlist -## if it does not exist in serverlist. -## -## Args: ip, port %info +## same as add_to_serverlist above, but does not add the server to serverlist +## if it does not exist in serverlist. it must be added by another function +## first. ################################################################################ sub update_serverlist { my ($self, $ip, $port, $s) = @_; @@ -69,7 +67,7 @@ sub update_serverlist { $ip, $port); # notify - $self->log("update", "server $ip:$port was updated: $s->{hostname}") if ($u > 0); + $self->log("update", "server $ip:$port info updated") if ($u > 0); # return 0 if updated return 0 if ($u > 0); @@ -80,15 +78,9 @@ sub update_serverlist { } ################################################################################ -## -## Subroutine get_next_server -## -## Get a server address of the next server in line to be -## queried for game info. Query must be older than 30 seconds (in case it just -## got added) and not older than 3 hours. -## -## Args: $id --> id of a server address entry -## Returns: hash {id, ip, port} of the NEXT entry in line. +## get a server address of the next server in line to be queried for game info. +## query must be older than 30 seconds (in case it just got added) and not +## older than 3 hours. ################################################################################ sub get_next_server { my ($self, $id) = @_; -- cgit v1.2.3