<?php

$SERVERS_COUNT=3;
$TIMEOUT=50;


if(file_exists(".config.php")) include(".config.php");


require("$komputery_arr");

$ADM_USER="$SQL_USER";
$ADM_USER_PASS="$SQL_USER_PASS";

$error5="ERROR: Przykro nam, serwis jest dostepny wylacznie dla autoryzowanych uzytkownikow\n";
if(in_array("$REMOTE_ADDR",$KOMPUTERY))  $czy_error_5=1 ;
if(!$czy_error_5) die($error5);

if (!$nodeid) die("ERROR: Blad danych\n");

$conn=mysql_pconnect($INTERFACE_ADDR,$ADM_USER,$ADM_USER_PASS);
if (!$conn) die("ERROR: Blad polaczenia z baza\n");
mysql_select_db($SQL_DATABASE) or die("ERROR: Blad wyboru bazy\n");


$query="SELECT unix_timestamp(now())-unix_timestamp(TIMESTAMP) AS DELAY FROM AUTOSHAPE WHERE SERVER='$nodeid'";
do {
	$result=mysql_query($query,$conn) or die("ERROR: 1\n");
	if ($row=mysql_fetch_object($result)) {
		$delay="$row->DELAY";
		if ($delay>60) {
			$queries[]="DELETE FROM AUTOSHAPE WHERE SERVER='$nodeid'";
			$queries[]="DELETE FROM AUTOSHAPE_GET WHERE SERVER='$nodeid'";
			foreach ($queries as $query2) mysql_query($query2,$conn) or die("ERROR: 2\n$query2\n");
		} else sleep(1);
	}
} while ($row);

foreach ($_GET as $tr => $val) {
	if ($tr != "nodeid") $query = "INSERT INTO AUTOSHAPE_GET (SERVER,TR,VAL) VALUES ('$nodeid','$tr','$val')\n";
	mysql_query($query,$conn) or die("ERROR: 3\n$query\n");
}
$query = "INSERT INTO AUTOSHAPE (SERVER) VALUES ('$nodeid');\n";
mysql_query($query,$conn) or die("ERROR: 3\n$query\n");

$query = "SELECT COUNT(*) AS COUNT FROM AUTOSHAPE";
$result=mysql_query($query,$conn) or die("ERROR: 3\n$query\n");
$row=mysql_fetch_object($result);
$count="$row->COUNT";

if ($count==$SERVERS_COUNT) {
	$query="SELECT TR,MAX(VAL) AS VAL FROM AUTOSHAPE_GET GROUP BY TR";
	$result=mysql_query($query,$conn) or die("ERROR: 4\n");
	while ($row=mysql_fetch_object($result)) {
		$tr="$row->TR";
		$val="$row->VAL";
		$t[$tr]=$val;
		if (isset($req)) $req=$req . ",";
		$req=$req . "$tr=$val";
				
	}

if ($t[a2_3]*$t[a2_4]*$t[a2_5]*$t[a3_2]*$t[a3_4]*$t[a3_5]*$t[a4_2]*$t[a4_3]*$t[a5_2]*$t[a5_3]==0) $wynik="ERROR: Blad danych";
else {

require("autoshape-config.php");

$query="SELECT VAL FROM AUTOSHAPE_HIST WHERE VAL NOT LIKE 'ERROR%' ORDER BY TIMESTAMP DESC LIMIT 1";
$result=mysql_query($query,$conn) or die("ERROR: 4.2\n");
if ($row=mysql_fetch_object($result)) {
	$last="$row->VAL";
	$last=str_replace("=","=>",$last);
	eval("\$tl=Array($last);");
}



if ($t[a4_3]<$CIR) $t[a4_3]=$CIR;
if ($t[a5_3]<$CIR) $t[a5_3]=$CIR;
if ($t[a3_4]<$CIR) $t[a3_4]=$CIR;
if ($t[a3_5]<$CIR) $t[a3_5]=$CIR;
if ($t[a2_3]<$CIR) $t[a2_3]=$CIR;
if ($t[a2_4]<$CIR) $t[a2_4]=$CIR;
if ($t[a2_5]<$CIR) $t[a2_5]=$CIR;
if ($t[a3_2]<$CIR) $t[a3_2]=$CIR;
if ($t[a4_2]<$CIR) $t[a4_2]=$CIR;
if ($t[a5_2]<$CIR) $t[a5_2]=$CIR;

if ($tl[b4_3]<$CIR) $tl[b4_3]=$CIR;
if ($tl[b5_3]<$CIR) $tl[b5_3]=$CIR;
if ($tl[b3_4]<$CIR) $tl[b3_4]=$CIR;
if ($tl[b3_5]<$CIR) $tl[b3_5]=$CIR;
if ($tl[b2_3]<$CIR) $tl[b2_3]=$CIR;
if ($tl[b2_4]<$CIR) $tl[b2_4]=$CIR;
if ($tl[b2_5]<$CIR) $tl[b2_5]=$CIR;
if ($tl[b3_2]<$CIR) $tl[b3_2]=$CIR;
if ($tl[b4_2]<$CIR) $tl[b4_2]=$CIR;
if ($tl[b5_2]<$CIR) $tl[b5_2]=$CIR;


//Ruch do Internetu
$as_2=$t[a3_2]+$t[a4_2]+$t[a5_2]+$tl[b3_2]+$tl[b4_2]+$tl[b5_2];
$t2[b3_2]=round(($t[a3_2]+$tl[b3_2])*$ATM/$as_2);
if ($t2[b3_2]>$RL_M_NET_MAX) $t2[b3_2]=$RL_M_NET_MAX;

$t2[b4_2]=round(($t[a4_2]+$tl[b4_2])*$ATM/$as_2);
$t2[b5_2]=round(($t[a5_2]+$tl[b5_2])*$ATM/$as_2);
if (($c_z=$t2[b4_2]+$t2[b5_2])>$RL_C_NET_MAX) {
//	$t2[b3_2]=$ATM-$RL_C_NET_MAX;
	$t2[b4_2]=round($t2[b4_2]*$RL_C_NET_MAX/$c_z);
	$t2[b5_2]=round($t2[b5_2]*$RL_C_NET_MAX/$c_z);
}

//Ruch z Internetu
$a2_s=$t[a2_3]+$t[a2_4]+$t[a2_5]+$tl[b2_3]+$tl[b2_4]+$tl[b2_5];
$t2[b2_3]=round(($t[a2_3]+$tl[b2_3])*$ATM/$a2_s);
if ($t2[b2_3]>$RL_M_NET_MAX) $t2[b2_3]=$RL_M_NET_MAX;

$t2[b2_4]=round(($t[a2_4]+$tl[b2_4])*$ATM/$a2_s);
$t2[b2_5]=round(($t[a2_5]+$tl[b2_5])*$ATM/$a2_s);
if (($z_c=$t2[b2_4]+$t2[b2_5])>$RL_C_NET_MAX) {
//	$t2[b2_3]=$ATM-$RL_C_NET_MAX;
	$t2[b2_4]=round($t2[b2_4]*$RL_C_NET_MAX/$z_c);
	$t2[b2_5]=round($t2[b2_5]*$RL_C_NET_MAX/$z_c);
}		

//Ruch z Moreny do Chelmu
$as_c=$t[a3_4]+$t[a3_5]+$tl[b3_4]+$tl[b3_5];
$bs_c=$RL_M-$t2[b3_2];
if ($bs_c+$t2[b2_4]+$t2[b2_5]>$RL_C) $bs_c=$RL_C-$t2[b2_4]-$t2[b2_5];
$t2[b3_4]=round(($t[a3_4]+$tl[b3_4])*$bs_c/$as_c);
$t2[b3_5]=round(($t[a3_5]+$tl[b3_5])*$bs_c/$as_c);

//Ruch z Chelmu do Moreny
$as_m=$t[a4_3]+$t[a5_3]+$tl[b4_3]+$tl[b5_3];
$bs_m=$RL_M-$t2[b2_3];
if ($bs_m+$t2[b4_2]+$t2[b5_2]>$RL_C) $bs_m=$RL_C-$t2[b4_2]-$t2[b5_2];
$t2[b4_3]=round(($t[a4_3]+$tl[b4_3])*$bs_m/$as_m);
$t2[b5_3]=round(($t[a5_3]+$tl[b5_3])*$bs_m/$as_m);

#$as_m=$t[a4_3]+$t[a5_3]+$tl[b4_3]+$tl[b5_3];
#$bs_m=$RL_C-$t2[b4_2]-$t2[b5_2];
#if ($bs_m+$t2[b2_3]>$RL_M) $bs_m=$RL_M-$t2[b2_3];
#$t2[b4_3]=round(($t[a4_3]+$tl[b4_3])*$bs_m/$as_m);
#$t2[b5_3]=round(($t[a5_3]+$tl[b5_3])*$bs_m/$as_m);

//Ruch hub1-hub3
$t2[b4_5]=0;
$t2[b5_4]=0;

foreach ($t2 as $tr => $val) {
	if (isset($wynik)) $wynik=$wynik . ",";
	$wynik=$wynik . "$tr=$val";
}

//$wynik="b2_3=$b2_3,b2_4=$b2_4,b2_5=$b2_5,b3_2=$b3_2,b3_4=$b3_4,b3_5=$b3_5,b4_2=$b4_2,b4_3=$b4_3,b4_5=$b4_5,b5_2=$b5_2,b5_3=$b5_3,b5_4=$b5_4";
	
}

	$queries[]="UPDATE AUTOSHAPE SET VAL='$wynik'";
	$queries[]="DELETE FROM AUTOSHAPE WHERE SERVER='$nodeid'";
	$queries[]="TRUNCATE TABLE AUTOSHAPE_GET";
	$queries[]="INSERT INTO AUTOSHAPE_HIST (VAL,REQ) VALUES ('$wynik','$req')";
	foreach ($queries as $query) mysql_query($query,$conn) or die("ERROR: 4\n$query\n");
} else {
	while (!$wynik) {
		$query="SELECT VAL FROM AUTOSHAPE WHERE SERVER='$nodeid' AND VAL IS NOT NULL";
		$result=mysql_query($query,$conn) or die("ERROR: 5\n");
		if ($row=mysql_fetch_object($result)) {
			$wynik="$row->VAL";
			$query="DELETE FROM AUTOSHAPE WHERE SERVER='$nodeid'";
			mysql_query($query,$conn) or die("ERROR: 6\n");
		} else {
			$w+=1;
			if ($w==$TIMEOUT) {
				$queries[]="DELETE FROM AUTOSHAPE WHERE SERVER='$nodeid'";
				$queries[]="DELETE FROM AUTOSHAPE_GET WHERE SERVER='$nodeid'";
				foreach ($queries as $query) mysql_query($query,$conn) or die("ERROR: 7\n$query\n");
				die("ERROR: Timeout\n");
			}
			sleep(1);
		}
	}
}

echo "$wynik";

?>

