#!/usr/bin/perl
# This is the MasterServer Program
# (c) ETHZ 2005
# you may port this to any other language you prefer over perl...


#see: init.sql

use IO::Socket::INET;
use DBI;


################################
# mysql db params

	$a_mysql_server	=	"localhost"; 
	$a_username	=	"here comes the database username";
	$a_password	=	"enter your database password here";
	$a_database	=	"enter your database here";


	$a_table	=	"serverlist";
	$db = "DBI:mysql:$a_database:$a_mysql_server";


################################


	$| = 1;

	print "EM - EiffelMedia masterserver program\n";


	$a_socket=new IO::Socket::INET->new(LocalPort=>35699,Proto=>'udp');

################################
# the main loop

	while(1)

	{

		$a_socket->recv($a_message,128);
	
		$a_address 	= $a_socket->peerhost();
		$a_port		= $a_socket->peerport();


		@field = split(/::/,$a_message);

		if($#field ==5 && @field[1]=="HEARTBEAT")
		{
			$a_game=@field[2];
			$a_server_name=@field[3];
			$some_info=@field[4];

			print "\n$a_address:$a_port ($a_server_name) sent a heartbeat for:\n";
			print "      ", $a_game,"\n";
			print "      ",	$some_info,"\n";

################################
# database update
			if( $a_dbhandle = DBI->connect($db, $a_username, $a_password))
			{
				$time = time();

				$a_query = $a_dbhandle->prepare(qq{SELECT address, port from $a_table WHERE address='$a_address' AND port='$a_port'});
				$a_query->execute();
				if (($address, $port) = $a_query->fetchrow_array())
				{
					$a_dbhandle->do("UPDATE $a_table SET game='$a_game', servername='$a_server_name', info='$some_info', time='$time' WHERE address='$a_address' AND port='$a_port'");
				}
				else
				{
					$a_dbhandle->do("INSERT INTO $a_table (address, port, game, servername, info, time) VALUES ('$a_address', '$a_port','$a_game','$a_server_name','$some_info','$time')");
				}
				$a_query->finish();

				$a_dbhandle->disconnect();
			}
			else
			{
				print "\nConnect failed...\n";
			}
################################

		}
################################
# get server list from databse over UDP (this is currently unused)
#
#		elsif($#field ==3  && @field[1]=="GETSERVERLIST")
#		{
#			$a_game=@field[2];
#
#			print "\n$a_address:$a_port has requested the serverlist for the game: $game\n\n";
#
#
#			if( $a_dbhandle = DBI->connect($db, $a_username, $a_password))
#			{
#
#				$a_query = $a_dbhandle->prepare(qq{SELECT address, port, game, info,timestamp FROM $a_table WHERE game='$a_game'});
#				$a_query->execute();
#
#				$a_reply_socket = new IO::Socket::INET->new(PeerPort=>$a_port,Proto=>'udp',PeerAddr=>$a_address);
#
#				while (($address, $port,$game,$info,$timestamp) = $a_query->fetchrow_array())
#				{
#					$a_msg = "::" . $address . "::" . $port . "::" . $game . "::" . $info . "::" .$timestamp . "::\n";
#					print $a_msg;
#					$a_reply_socket->send($a_msg);
#				}
#				$a_query->finish();
#
#				$a_dbhandle->disconnect();
#			}
#			else
#			{
#				print "\nConnect failed...\n";
#			}
#
#		}
################################
		else
		{
			print "\n$a_address:$a_port sent crap:\n";
			print $a_message,"\n";
		}

	}
