aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Core/LoadConfig.pm
blob: e209848d0261c603f91bb8d944dd8b971b00a06e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package MasterServer::Core::LoadConfig;

use strict;
use warnings;
use AnyEvent;
use POSIX qw/strftime/;
use Exporter 'import';
use DBI;

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;

  # loop through config entries
  foreach my $master_applet (@{$self->{master_applet}}) {
    # master_applet contains
    #   address --> domain
    #   port    --> tcp port
    #   games   --> array of gamenames
    
    # iterate through all games per entry
    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) 
      {
        # add to database
        $self->add_master_applet(
            ip        => $applet_ip,
            port      => $master_applet->{port},
            gamename  => $gamename,
          );
        
        #log
        $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 to last current time
  $self->reset_master_applets();
  
  # clear out the original variable, we don't use it anymore
  $self->{master_applet} = ();
  
  # report
  $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;

  # loop through config entries
  foreach my $sync_host (@{$self->{sync_masters}}) {
  
    # add them to database
    $self->add_sync_master($sync_host);
  }
  
  # clear out the original variable, we don't use it anymore
  $self->{sync_masters} = ();
  
  # report
  $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) = @_;
  
  # sync_host contains
  #   address --> domain
  #   port    --> tcp port
  #   beacon  --> udp port
  
  # resolve domain names
  my $sync_ip = $self->host2ip($sync_host->{address});
  
  # check if all credentials are valid
  if ($sync_ip && 
      $sync_host->{beacon} && 
      $sync_host->{port}) 
  {
    # select sync master from serverlist
    my $entry = $self->get_server(ip => $sync_ip, 
                                  port => $sync_host->{beacon})->[0];

    # was found, update the entry
    if (defined $entry) {
      # update the serverlist with 
      my $sa = $self->update_server_list(
        ip       => $sync_ip,
        port     => $sync_host->{beacon},
        hostport => $sync_host->{port},
        gamename => "333networks",
      );
    }
    # was not found, insert clean entry
    else {
      my $sa = $self->add_server_list(
        ip       => $sync_ip,
        port     => $sync_host->{beacon},
        hostport => $sync_host->{port},
        gamename => "333networks",
      );
      
      #log
      $self->log("add", "added sync $sync_host->{address}:$sync_host->{port},$sync_host->{beacon}");
    }
  } # else: insufficient info available
  else {
    $self->log("fail", "Could not add sync master: ".
        ($sync_ip             || "ip"). ", ".
        ($sync_host->{beacon} || "0"). ", ".
        ($sync_host->{port}   || "0"). "."
      );
  }
}

1;