# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
require_once "config.inc.php";
require_once "ClientSock.php";
require_once "ServerInfo.php";
require_once "ServerList.php";
require_once "MasterList.php";
require_once "MasterInfo.php";
require_once "UDPListener.php";
require_once "TCPListener.php";
set_time_limit(0);
$udpListener = new UDPListener($listenip, $listenport);
$tcpListener = new TCPListener($listenip, $listenport);
ServerList::Initialize();
MasterList::Initialize();
SocketManager::Initialize();
$testms = new MasterInfo("83.142.228.166", 28900, false);
MasterList::addServer($testms);
SocketManager::addSocket($udpListener);
SocketManager::addSocket($tcpListener);
$isRunning = true;
while ($isRunning) {
    ServerList::checkTimeouts();
    MasterList::checkTimeouts();
    MasterList::dumpServers();
    ServerList::dumpServers();
    SocketManager::handleEvents();
}
 private function handleHeartbeat($attributes)
 {
     if (!isset($attributes['port']) || $attributes['port'] < 1 || $attributes['port'] > 65535) {
         $this->sendError("Wrong port for heartbeat");
         $this->removeme = true;
     } elseif (!isset($attributes['gamename'])) {
         $this->sendError("Wrong message, needs gamename");
         $this->removeme = true;
     } elseif ($attributes['gamename'] == "netpanzer") {
         if (isset($attributes['protocol']) && $attributes['protocol'] > 1 && $attributes['protocol'] < 65535) {
             $sinfo = ServerList::getServer($this->ip, $attributes['port']);
             if (!isset($sinfo)) {
                 print "New server on {$this->ip}:{$attributes['port']}\n";
                 $sinfo = new ServerInfo($this->ip, $attributes['port'], $attributes['protocol'], $this);
                 ServerList::addServer($sinfo);
             } else {
                 print "Heartbeat of server on {$this->ip}:{$attributes['port']}\n";
                 $sinfo->clientSock = $this;
                 $sinfo->protocol = $attributes['protocol'];
             }
             $sinfo->sendQuery();
         }
     } elseif ($attributes['gamename'] == "master") {
         global $MASTERPASSWORD;
         if (isset($attributes['password']) && $attributes['password'] == $MASTERPASSWORD) {
             $sinfo = MasterList::getServer($this->ip, $attributes['port']);
             if (isset($sinfo)) {
                 $sinfo->touch();
             } else {
                 print "New MasterServer connected: {$this->ip}:{$attributes['port']}\n";
                 $sinfo = new MasterInfo($this->ip, $attributes['port']);
                 MasterList::addServer($sinfo);
             }
         }
     }
 }