aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Core/Core.pm
blob: db04344dc2e877691edc2ecc365df1233b6fb68c (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
package MasterServer::Core::Core;

use strict;
use warnings;
use AnyEvent;
use Exporter 'import';
use DBI;
our @EXPORT = qw | halt select_database_type main |;

################################################################################
## Handle shutting down the program in case a fatal error occurs.
## clear all other timers, network servers, etc
################################################################################
sub halt {
  my $self = shift;
  $self->log("stop", "stopping the masterserver!");
  $self->{dbh}->disconnect() if (defined $self->{dbh});
  $self->{dbh}   = undef;
  $self->{scope} = undef;
  $self->{must_halt}->send;
  exit; # time for a beer.  
}

################################################################################
## Set up the database connection
## determine the type of database and load the appropriate module
################################################################################
sub select_database_type {
  my $self = shift;
  my @db_type = split(':', $self->{dblogin}->[0]); # from config file

  # format supported?
  if ( "Pg SQLite mysql" =~ m/$db_type[1]/i) {
    # load database for this type
    MasterServer::load_recursive("MasterServer::Database::$db_type[1]");
    $self->{dbh} = $self->database_login();
    $self->halt() unless (defined $self->{dbh});
  }
  else { # we can not continue without database
    $self->log("fatal", "the masterserver could not determine the chosen database type");
    $self->halt();
  }
}

################################################################################
## Initialize all processes and start various functions
################################################################################
sub main {
  my $self = shift;
  $self->{must_halt} = AnyEvent->condvar;
  $self->version();
  
  # startup
  print "Running 333networks Master Server Application...\n";
  $self->log("info", "333networks Master Server Application.");
  $self->log("info", "hostname: $self->{masterserver_hostname}");
  $self->log("info", "build:    $self->{build_type}");
  $self->log("info", "version:  $self->{build_version}");
  $self->log("info", "author:   $self->{build_author}");
  $self->log("info", "logs:     $self->{log_dir}");
  
  # load database and set up scope for timers/network
  $self->select_database_type();
  $self->{scope} = ();  

  # load the list with ciphers from the config file if no ciphers were detected
  $self->load_ciphers() unless $self->check_cipher_count();
  $self->{game} = undef;
  
  # reload the list with masterservers and master applets from config
  $self->load_sync_masters();
  $self->load_applet_masters();
  
  # first run flag for all startup actions
  $self->{firstrun} = undef;
  $self->{firstruntime} = time;

  # beacons and serverlists (listen for UDP beacons / TCP requests)
  $self->{scope}->{beacon_catcher} = $self->beacon_catcher();
  $self->{scope}->{browser_host} = $self->browser_host();

  # recurring tasks (sync and updates)
  $self->{scope}->{long_periodic_tasks} = $self->long_periodic_tasks();
  $self->{scope}->{short_periodic_tasks} = $self->short_periodic_tasks();

  # verify and update server status
  $self->{scope}->{udp_ticker} = $self->udp_ticker() if $self->{beacon_checker_enabled};

  # all modules loaded. Running...
  $self->log("info", "all modules loaded. Masterserver is now running!");
  $self->{must_halt}->recv;
}

1;