forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.5 KiB
165 lines
4.5 KiB
#!/usr/bin/php -Cq |
|
<?php |
|
/* |
|
include 'constServerIntegra.php'; |
|
include 'config.php'; |
|
require 'shm.php'; |
|
require 'dbAbstract.php'; |
|
require 'serverFunctions.php'; |
|
* |
|
*/ |
|
require '/var/www/html/aplicativo/constantes.php'; |
|
require 'util/funcoesAmi.php'; |
|
|
|
/* |
|
* Defines debug mode. |
|
*/ |
|
list($sckHost, $sckPort, $sckUser, $sckPass) = GetSckConnect(); |
|
$sck = ConectaAmi($sckHost, $sckPort, $sckUser, $sckPass, 1); |
|
|
|
if ($sck) |
|
echo "Servidor conecttado com sucesso\n"; |
|
if ($sck) |
|
AnalisaRetornoSocket($sck); |
|
|
|
exit; |
|
|
|
$isDebug = 1; |
|
|
|
/* |
|
* Prevent rewrite infinite. |
|
*/ |
|
$writeLog = false; |
|
|
|
/* |
|
* Creates a connection to the dababase. |
|
*/ |
|
$conn = ConnectionDB(); |
|
if (!$conn) { |
|
__logStr("ConnectionDB", "Nao foi possivel carregar as configurações do servidor de banco de dados!"); |
|
exit; |
|
} |
|
/* |
|
* Variable identifies a memory block on operating system. |
|
*/ |
|
$shmSystemKey = ShmGetId(SHM_IPC_FILE); |
|
|
|
/* |
|
* Checks whether the memory is allocated in the operating system |
|
*/ |
|
$shmKey = 0; |
|
$memoryAllocated = Malloc(); |
|
if (ShmExist($shmSystemKey)) { |
|
ShmDelete($shmkey); |
|
} |
|
|
|
$shmKey = ShmOpen($shmSystemKey, SHM_ACESS_MODE, SHM_ACESS_PERMISSION, $memoryAllocated); |
|
@CreateSegment($shmKey); |
|
|
|
/* |
|
* Verifies if the memory was allocated with success! |
|
*/ |
|
if (!$shmKey) { |
|
__logStr("Start", "Nao foi possivel allocar memoria para o servidor!"); |
|
exit; |
|
} |
|
|
|
while (true) { |
|
$actvProc = GetActiveProcesses($shmKey, PROCESSESS_START); |
|
|
|
foreach ($actvProc as $idProc) { |
|
$data = trim(ShmRead($shmKey, GetOffSet($idProc), MEMORY_SEGMENT_WITH)); |
|
if ($data) { |
|
$writeLog = false; |
|
$params = explode("|", $data); |
|
$uid = $params[0]; |
|
$phone = $params[1]; |
|
$queue = ''; |
|
$dataClient = ''; |
|
|
|
$query = sprintf("EXEC %s '%s', '%s', 1", $dbCommand, $uid, $phone); |
|
/* |
|
* Executes query in database. |
|
*/ |
|
$result = db_query($dbType, $query, $conn); |
|
if ($result) { |
|
$writeLog = false; |
|
/* |
|
* Clear and rewrite memory segment with database result. |
|
*/ |
|
ClearSegment($shmKey, $idProc); |
|
/* |
|
* Verifies whether database command returns data; |
|
*/ |
|
if ($dbResult && !db_num_rows($dbType, $result)) { |
|
$queue = "00"; |
|
$dataClient = ""; |
|
} else if ($dbResult) { |
|
$ret = db_fetch_row($dbType, $result); |
|
$queue = $ret[0]; |
|
$dataClient = trim($ret[1]); |
|
$dataClient = sprintf("%s|%s", $uid, $dataClient); |
|
} |
|
|
|
$data = sprintf("%s|%s|%s|%s", $uid, $phone, $queue, $dataClient); |
|
/* |
|
* Rewrite memory segment with new information. |
|
*/ |
|
ShmWrite($shmKey, $data, GetOffSet($idProc)); |
|
|
|
/* |
|
* Updates the status of process. |
|
*/ |
|
ShmWrite($shmKey, PROCESSESS_READ, $idProc); |
|
} else { |
|
if (!@TestDB()) |
|
@ReconectDB(); |
|
$msg = sprintf("Erro ao executar query: %s msgDB: %s", $query, db_getmessage($type)); |
|
if (!$writeLog) { |
|
__logStr("Query Database", $msg); |
|
$writeLog = true; |
|
} |
|
} |
|
} else { |
|
$msg = sprintf("Nao foi possivel ler o segmento inicial que contem o status dos processos! Id: %s", $idProc); |
|
if (!$writeLog) { |
|
__logStr("Status Processo", $msg); |
|
$writeLog = true; |
|
} |
|
} |
|
} |
|
//sleep(15); |
|
usleep(SERVER_CYCLO); |
|
} |
|
ShmClose($shmKey); |
|
|
|
function ConnectionDB() { |
|
if (!GetParamConnecionDB()) |
|
return false; |
|
$type = $GLOBALS["dbType"]; |
|
$host = $GLOBALS["dbHhost"]; |
|
$port = $GLOBALS["dbPort"]; |
|
$dataBase = $GLOBALS["dbDataBase"]; |
|
$userid = $GLOBALS["dbUserid"]; |
|
$password = $GLOBALS["dbPassword"]; |
|
return db_connect($type, $host, $port, $dataBase, $userid, $password); |
|
} |
|
|
|
function CloseDB() { |
|
$conn = $GLOBALS["conn"]; |
|
$type = $GLOBALS["dbType"]; |
|
return db_close($type, $conn); |
|
} |
|
|
|
function ReconectDB() { |
|
CloseDB(); |
|
return ConnectionDB(); |
|
} |
|
|
|
function TestDB() { |
|
$conn = $GLOBALS["conn"]; |
|
$type = $GLOBALS["dbType"]; |
|
$query = "select 1"; |
|
return db_query($type, $query, $conn) ? true : false; |
|
} |
|
?>
|
|
|