From 2c7d62f38944f61e7eafea155c6128521d16aed9 Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Tue, 19 May 2015 22:00:40 +0200 Subject: Beta with support for Pg and SQLite --- lib/MasterServer/Database/Pg/dbClientList.pm | 45 +++++++++++++++++++++++ lib/MasterServer/Database/Pg/dbCore.pm | 4 +- lib/MasterServer/Database/Pg/dbServerlist.pm | 55 ++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100755 lib/MasterServer/Database/Pg/dbClientList.pm (limited to 'lib/MasterServer/Database/Pg') diff --git a/lib/MasterServer/Database/Pg/dbClientList.pm b/lib/MasterServer/Database/Pg/dbClientList.pm new file mode 100755 index 0000000..718bf8a --- /dev/null +++ b/lib/MasterServer/Database/Pg/dbClientList.pm @@ -0,0 +1,45 @@ + +package MasterServer::Database::Pg::dbClientList; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| get_gamenames + get_game_list |; + + +################################################################################ +## get a list of distinct gamenames currently in the database. it does not +## matter whether they are recent or old, as long as the game is currently in +## the database. +## +## returns: hashref of gamenames +################################################################################ +sub get_gamenames { + my $self = shift; + + return $self->{dbh}->selectall_arrayref( + "SELECT distinct gamename + FROM serverlist"); +} + +################################################################################ +## get the list of games of a certain $gamename, excluding the ones excempted +## via the blacklist +## only returns server addresses that are no more than 1 hours old +################################################################################ +sub get_game_list { + my ($self, $gamename) = @_; + + return $self->{dbh}->selectall_arrayref( + "SELECT ip, port + FROM serverlist + WHERE updated > (NOW() - INTERVAL '1 HOUR') + AND gamename = ? + AND NOT blacklisted", + undef, lc $gamename); +} + + +1; diff --git a/lib/MasterServer/Database/Pg/dbCore.pm b/lib/MasterServer/Database/Pg/dbCore.pm index 8f55ebb..5cd194b 100755 --- a/lib/MasterServer/Database/Pg/dbCore.pm +++ b/lib/MasterServer/Database/Pg/dbCore.pm @@ -15,7 +15,7 @@ sub database_login { my $self = shift; # create the dbi object - my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => 0}); + my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => $self->{db_print}}); # verify that the database connected if (defined $dbh) { @@ -37,7 +37,7 @@ sub database_login { $self->halt(); } - # return empty element + # unreachable return undef; } diff --git a/lib/MasterServer/Database/Pg/dbServerlist.pm b/lib/MasterServer/Database/Pg/dbServerlist.pm index 832a08f..5a58717 100755 --- a/lib/MasterServer/Database/Pg/dbServerlist.pm +++ b/lib/MasterServer/Database/Pg/dbServerlist.pm @@ -7,6 +7,7 @@ use Exporter 'import'; our @EXPORT = qw| add_to_serverlist update_serverlist + syncer_add get_next_server |; ################################################################################ @@ -77,6 +78,60 @@ sub update_serverlist { return -1; } + +################################################################################ +## add new addresses to the pending list, but do not update timestamps. masters +## that sync with each other would otherwise update the timestamp for a server +## which is no longer online. +################################################################################ +sub syncer_add { + my ($self, $ip, $port, $gamename, $secure) = @_; + + # if address is in list, update the timestamp + my $u = $self->{dbh}->do( + "SELECT * FROM serverlist + WHERE ip = ? + AND port = ?", + undef, $ip, $port); + + # notify + $self->log("read","syncer found entry for $ip:$port") if ($u > 0); + + # if found, 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 secure = ? + WHERE ip = ? + AND heartbeat = ?", + undef, $secure, $ip, $port); + + # notify + $self->log("update","$ip:$port was updated by syncer", + $self->{log_settings}->{db_updated}) if ($u > 0); + + # return 1 if found + return 1 if ($u > 0); + + # 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","beacon: $ip:$port was added for $gamename after sync") if ($u > 0); + + # return 2 if added new + return 2 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred adding $ip:$port after sync"); + return -1; +} + ################################################################################ ## 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 -- cgit v1.2.3