aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/Pg/dbAddServers.pm
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2017-08-22 11:00:13 +0200
committerDarkelarious <darkelarious@333networks.com>2017-08-22 11:00:13 +0200
commitc06322da38b4cb76b2036af1a5448083adb8ff20 (patch)
tree189c9f0fec3325be927f763aba23cf18aa68cfe4 /lib/MasterServer/Database/Pg/dbAddServers.pm
parente0d727670cbeda0db0812c5c9efc503d75f8d0a4 (diff)
downloadMasterServer-Perl-2.4.0.tar.gz
MasterServer-Perl-2.4.0.zip
new server checking mechanism, complete recode of major functionsv2.4.0
Diffstat (limited to 'lib/MasterServer/Database/Pg/dbAddServers.pm')
-rwxr-xr-xlib/MasterServer/Database/Pg/dbAddServers.pm167
1 files changed, 43 insertions, 124 deletions
diff --git a/lib/MasterServer/Database/Pg/dbAddServers.pm b/lib/MasterServer/Database/Pg/dbAddServers.pm
index 31deb08..6befd83 100755
--- a/lib/MasterServer/Database/Pg/dbAddServers.pm
+++ b/lib/MasterServer/Database/Pg/dbAddServers.pm
@@ -4,159 +4,78 @@ use strict;
use warnings;
use MasterServer::Core::Util 'sqlprint';
use Exporter 'import';
-
-our @EXPORT = qw| add_server_new
- add_server_list
- update_server_list
- syncer_add |;
+our @EXPORT = qw| insert_server
+ update_server
+ insert_pending |;
################################################################################
-## Update an existing address or add a new address to the pending list.
-## opts: direct beacon, set update, game
+## Insert minimalistic game data into serverlist
+## params: ip, port, hostport
################################################################################
-sub add_server_new {
+sub insert_server {
my $self = shift;
- my %o = (
- updated => time,
- @_);
-
- # try updating it in serverlist
- my %H = (
- $o{direct} ? ( 'b333ms = CAST(? AS BOOLEAN)' => $o{direct}) : (),
- $o{updated} ? ( 'updated = to_timestamp(?)' => $o{updated}) : (),
- $o{beacon} ? ( 'beacon = to_timestamp(?)' => $o{beacon}) : (),
-
- # some applets have incorrect gamename lists, let udpticker update this
- # entry instead. this way, applets don't overwrite with incorrect data
- #$o{gamename} ? ('gamename = ?' => lc $o{gamename}) : (),
- );
-
- my($q, @p) = sqlprint("UPDATE serverlist !H
- WHERE ip = ? AND port = ?", \%H, $o{ip}, $o{heartbeat});
-
- my $n = $self->{dbh}->do($q, undef, @p);
-
- # if serverlist was updated
- return 0 if ($n > 0);
-
- # try updating it in pending
- %H = (
- $o{added} ? ( 'added = ?' => $o{added}) : (),
- $o{secure} ? ( 'secure = ?' => $o{secure}) : (),
- $o{beaconport} ? ('beaconport = ?' => $o{beaconport}) : (),
- );
-
- ($q, @p) = sqlprint("UPDATE pending !H
- WHERE ip = ? AND heartbeat = ?", \%H, $o{ip}, $o{heartbeat});
-
- # exec query
- $n = $self->{dbh}->do($q, undef, @p);
+ my %o = (@_);
- # if beacon was updated
- return 1 if ($n > 0);
-
# if not found at all, add to pending
- $n = $self->{dbh}->do(
- "INSERT INTO pending (
- ip,
- beaconport,
- heartbeat,
- gamename,
- secure)
- SELECT ?, ?, ?, ?, ?",
- undef, $o{ip}, $o{beaconport}, $o{heartbeat}, lc $o{gamename}, $o{secure});
-
- # it was added to pending
- return 2 if ($n > 0);
+ return $self->{dbh}->do(
+ "INSERT INTO serverlist (ip, port, hostport, country) VALUES (?, ?, ?, ?)",
+ undef, $o{ip}, $o{port}, $o{hostport}, $self->ip2country($o{ip}) );
}
################################################################################
## Update the server info in the serverlist
+## required: id or ip + port/hostport
################################################################################
-sub update_server_list {
+sub update_server {
my $self = shift;
- my %o = (
- updated => time,
- @_);
+ my %o = (updated => time, @_);
+
+ # either id, ip+port or ip+hostport are provided.
+ my %W = (
+ $o{id} ? ( 'id = ?' => $o{id}) : (),
+ $o{ip} ? ( 'ip = ?' => $o{ip}) : (),
+ $o{port} ? ( 'port = ?' => $o{port}) : (),
+ $o{hostport} ? ('hostport = ?' => $o{hostport}) : (),
+ );
- # try updating it in serverlist
+ # update where possible
my %H = (
$o{gamename} ? ('gamename = ?' => lc $o{gamename}) : (),
$o{gamever} ? ( 'gamever = ?' => $o{gamever}) : (),
$o{hostname} ? ('hostname = ?' => $o{hostname}) : (),
$o{hostport} ? ('hostport = ?' => $o{hostport}) : (),
- $o{updated} ? ( 'updated = to_timestamp(?)' => $o{updated}) : (),
+ $o{direct} ? ( 'b333ms = CAST(? AS BOOLEAN)' => $o{direct}) : (),
+ $o{direct} ? ( 'beacon = to_timestamp(?)' => $o{updated}) : (),
+ $o{updated} ? ( 'updated = to_timestamp(?)' => $o{updated}) : (),
);
- my($q, @p) = sqlprint("UPDATE serverlist !H
- WHERE ip = ? AND port = ?", \%H, $o{ip}, $o{port});
-
+ my($q, @p) = sqlprint("UPDATE serverlist !H !W", \%H, \%W);
return $self->{dbh}->do($q, undef, @p);
}
################################################################################
-## beacon was verified or otherwise accepted and will now be added to the
-## serverlist.
+## check if an ip, port/hostport combination is recent in the serverlist.
+## if not, add the address to the pending list
################################################################################
-sub add_server_list {
+sub insert_pending {
my $self = shift;
- my %o = @_;
+ my %o = (updated => 3600, @_ );
- # insert basic data
- return $self->{dbh}->do("INSERT INTO serverlist (ip, port, gamename, country)
- SELECT ?, ?, ?, ?", undef,
- $o{ip}, $o{port}, lc $o{gamename}, $self->ip2country($o{ip}));
-}
-
-################################################################################
-## 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 the list AND up to date,
- # acknowledge its existance but don't do anything with it
- my $u = $self->{dbh}->do(
- "SELECT count(*) FROM serverlist
- WHERE ip = ?
- AND port = ?
- AND updated > to_timestamp(?)",
- undef, $ip, $port, time-7200);
-
- # if found, return 0
- return 0 if ($u > 0);
+ # selection criteria
+ my %W = (
+ $o{ip} ? ( 'ip = ?' => $o{ip}) : (),
+ $o{port} ? ( 'port = ?' => $o{port}) : (),
+ $o{updated} ? ('updated > to_timestamp(?)' => (time-$o{updated})) : (),
+ );
- # 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);
+ # determine if it already exsits
+ my($q, @p) = sqlprint("SELECT id FROM serverlist !W", \%W);
+ my $u = $self->{dbh}->do($q, undef, @p);
+ return 0 if int($u);
- # notify (debug)
- #$self->log("update","$ip:$port was updated by syncer") if ($u > 0);
-
- # return 1 if found
- return 1 if ($u > 0);
-
- # if not found or out of date, add it to pending to be checked again
- $u = $self->{dbh}->do(
- "INSERT INTO pending (ip, heartbeat, gamename, secure)
- SELECT ?, ?, ?, ?",
- undef, $ip, $port, lc $gamename, $secure);
-
- # notify (debug)
- #$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;
+ # else, insert in pending (duplicates may exist -- see remove_pending)
+ return $self->{dbh}->do("INSERT INTO pending (ip, heartbeat) VALUES (?, ?)",
+ undef, $o{ip}, $o{port} );
}
1;