aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Core/LoadConfig.pm
blob: f0d0671d0d170f224f95df9fa9b5485c54f77db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package MasterServer::Core::LoadConfig;

use strict;
use warnings;
use DBI;
use POSIX qw/strftime/;
use Exporter 'import';
our @EXPORT = qw | load_applet_masters 
                     load_sync_masters
                      add_sync_master |;

################################################################################
## Load configuration variables to the database, helper functions
################################################################################
sub load_applet_masters {
  my $self = shift;

  # iterate through all games per entry
  foreach my $master_applet (@{$self->{master_applet}}) {
    for my $gamename (@{$master_applet->{games}}) {
      
      # resolve domain names
      my $applet_ip = $self->host2ip($master_applet->{address});

      # check if all credentials are valid
      if ($applet_ip && $master_applet->{port} && $gamename) {
        $self->add_master_applet(
            ip        => $applet_ip,
            hostport  => $master_applet->{port},
            gamename  => $gamename,
          );
        $self->log("add", "added applet $master_applet->{address}:$master_applet->{port} for $gamename");
      } # else: insufficient info available
      else { $self->log("fail", "could not add master applet: ".
              ($applet_ip             || "unknown ip"). ", ".
              ($master_applet->{port} || "0"). ", ".
              ($gamename              || "game"));}
    } # end gamename
  } # end master_applet
  
  # reset added/updated time clear the applet list from memory
  $self->reset_master_applets;
  $self->{master_applet} = ();
  $self->log("info", "applet database successfully updated");

}

################################################################################
## There are three ways to load new masterservers to sync with.
##   1: from the config file; address, port and beaconport are provided
##   2: from a heartbeat; this automatically parses like all other servers
##   3: from another sync request. Add if sufficient info is available
################################################################################
sub load_sync_masters {
  my $self = shift;

  # add config entries to database
  foreach my $sync_host (@{$self->{sync_masters}}) {
    $self->add_sync_master($sync_host);}
  
  # clear list from memory
  $self->{sync_masters} = ();
  $self->log("info", "sync server database successfully updated");
  
}

################################################################################
## Add a sync master according to cases 1 and 3.
## Check for valid IP, port and/or beaconport
################################################################################
sub add_sync_master {
  my ($self, $sync_host) = @_;
  my $sync_ip = $self->host2ip($sync_host->{address});
  
  # check if all credentials are valid
  if ($sync_ip && $sync_host->{beacon}) {
    # add it to the pending list so it gets picked up with the "normal" status update
    $self->insert_pending(ip => $sync_ip, port => $sync_host->{beacon});
    $self->log("add", "added sync $sync_host->{address}:$sync_host->{beacon}");
  } # else: insufficient info available
  else { $self->log("fail", "failed to add sync master: ".
          ($sync_host->{address}|| "domain"). ", ".
          ($sync_ip             || "invalid ip"). ", ".
          ($sync_host->{beacon} || "invalid beacon port") );
  }
}

1;