diff options
| author | Darkelarious <darkelarious@333networks.com> | 2015-02-09 07:58:06 +0100 |
|---|---|---|
| committer | Darkelarious <darkelarious@333networks.com> | 2015-02-09 07:58:06 +0100 |
| commit | 5057ec47aa9a1702b2483e0a0b3ba325bb0b7abb (patch) | |
| tree | b1a394f64ec7ec2cf69f33bceb54a5b51199c0a4 /lib/MasterServer/Database/Pg | |
| parent | 6ac9d390e417b868b6ed79441b8cc6e1b2ebeb13 (diff) | |
| download | MasterServer-Perl-5057ec47aa9a1702b2483e0a0b3ba325bb0b7abb.tar.gz MasterServer-Perl-5057ec47aa9a1702b2483e0a0b3ba325bb0b7abb.zip | |
receive UDP beacons, validate them and store with country indicator
Diffstat (limited to 'lib/MasterServer/Database/Pg')
| -rwxr-xr-x | lib/MasterServer/Database/Pg/dbBeacon.pm | 123 | ||||
| -rwxr-xr-x | lib/MasterServer/Database/Pg/dbCore.pm | 44 | ||||
| -rwxr-xr-x | lib/MasterServer/Database/Pg/dbServerlist.pm | 44 |
3 files changed, 211 insertions, 0 deletions
diff --git a/lib/MasterServer/Database/Pg/dbBeacon.pm b/lib/MasterServer/Database/Pg/dbBeacon.pm new file mode 100755 index 0000000..a234b25 --- /dev/null +++ b/lib/MasterServer/Database/Pg/dbBeacon.pm @@ -0,0 +1,123 @@ + +package MasterServer::Database::Pg::dbBeacon; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| add_beacon get_pending_beacon remove_pending set_direct_beacon |; + +## 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 +sub add_beacon { + my ($self, $ip, $beaconport, $heartbeat, $gamename, $secure) = @_; + + # if address is in list, update the timestamp + my $u = $self->{dbh}->do( + "UPDATE serverlist + SET beacon = NOW(), + updated = NOW(), + gamename = ?, + b333ms = TRUE + WHERE ip = ? + AND port = ?", + undef, lc $gamename, $ip, $heartbeat); + + # notify + $self->log("updated", "beacon heartbeat for $ip:$heartbeat") if ($u > 0); + + # if serverlist was 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(), + beaconport = ?, + gamename = ?, + secure = ? + WHERE ip = ? + AND heartbeat = ?", + undef, $beaconport, lc $gamename, $secure, $ip, $heartbeat); + + # notify + $self->log("updated", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); + + # beacon was already in pending list and was updated + return 1 if ($u > 0); + + # if not found, add it + $u = $self->{dbh}->do( + "INSERT INTO pending (ip, beaconport, heartbeat, gamename, secure) + SELECT ?, ?, ?, ?, ?", + undef, $ip, $beaconport, $heartbeat, lc $gamename, $secure); + + # notify + $self->log("added", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); + + # it was added to pending + return 2 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred adding beacon $ip:$beaconport with $gamename:$heartbeat to the pending list"); + return -1; +} + + +## Get pending server by ip, beacon port. +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]; +} + + +## server checks out, remove entry from the pending list. +sub remove_pending { + my ($self, $id) = @_; + + # if address is in list, update the timestamp + my $u = $self->{dbh}->do("DELETE FROM pending WHERE id = ?", undef, $id); + + # notify + $self->log("deleted", "removed pending id $id from the list of pending servers") if ($u > 0); + + # it was added to pending + return 2 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred deleting server $id from the pending list"); + return -1; +} + + +## mark server as "direct beacon to this masterserver" +sub set_direct_beacon { + my ($self, $ip, $port) = @_; + + # 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("updated", "$ip:$port is a direct beacon.") if ($u > 0); + + # if found, updated; done + return 0 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred setting server $ip:$port as direct beacon"); + return -1; +} + + + +1; diff --git a/lib/MasterServer/Database/Pg/dbCore.pm b/lib/MasterServer/Database/Pg/dbCore.pm new file mode 100755 index 0000000..c6e3182 --- /dev/null +++ b/lib/MasterServer/Database/Pg/dbCore.pm @@ -0,0 +1,44 @@ + +package MasterServer::Database::Pg::dbCore; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| database_login |; + +################################################################################ +## database_login +## login to the database with credentials provided in the config file. +## returns dbh object +################################################################################ +sub database_login { + my $self = shift; + + # create the dbi object + my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => 0}); + + # verify that the database connected + if (defined $dbh) { + # log the event + $self->log("database","Connected to the Postgres database."); + + # turn on error printing + $dbh->{printerror} = 1; + + # return the dbi object for further use + return $dbh; + } + else { + # fatal error + $self->log("fatal", "$DBI::errstr!"); + + # end program + $self->halt(); + } + + # unreachable + return undef; +} + +1; diff --git a/lib/MasterServer/Database/Pg/dbServerlist.pm b/lib/MasterServer/Database/Pg/dbServerlist.pm new file mode 100755 index 0000000..8d1a2b2 --- /dev/null +++ b/lib/MasterServer/Database/Pg/dbServerlist.pm @@ -0,0 +1,44 @@ + +package MasterServer::Database::Pg::dbServerlist; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| add_to_serverlist |; + +## beacon was verified or otherwise accepted and will noe now be added to the +## serverlist. +sub add_to_serverlist { + my ($self, $ip, $port, $gamename) = @_; + + # update or add server to serverlist + my $u = $self->{dbh}->do("UPDATE serverlist + SET updated = NOW() + WHERE ip = ? + AND port = ?", + undef, $ip, $port); + + # notify + $self->log("updated", "$ip:$port timestamp updated") if ($u > 0); + + # if found, updated; done + return 0 if ($u > 0); + + # if not found, add it. + $u = $self->{dbh}->do("INSERT INTO serverlist (ip, port, gamename, country) + SELECT ?, ?, ?, ?", + undef, $ip, $port, $gamename, $self->ip2country($ip)); + + # notify + $self->log("added", "$ip:$port added to serverlist") if ($u > 0); + + # return added + return 1 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred adding server $ip:$port ($gamename) to the serverlist"); + return -1; +} + +1; |
