hitagi-entrada/hitagi-entrada-swoole.php

103 lines
2.9 KiB
PHP

<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/wrapper-swoole.php';
use Hitlabs\Hitagi\AGIEntrada;
use Swoole\Runtime;
use Swoole\Table;
use Swoole\Server;
Runtime::enableCoroutine();
$table = new Table(1024);
$table->column('data', Swoole\Table::TYPE_STRING, 65536);
$table->column('agi_arg', Swoole\Table::TYPE_STRING, 65536);
$table->create();
$server = new Swoole\Server("0.0.0.0", 8073);
$server->table = $table;
$server->on('start', function ($server) {
echo "LOG ======> Servidor iniciou na porta 8073...." . PHP_EOL;
});
$server->on("connect", function (Server $server, $fd) {
echo "LOG ======> Cliente $fd conectado...." . PHP_EOL;
});
$server->on("receive", function ($server, $fd, $reactor_id, $data) {
$table_data = $server->table->get($fd);
// echo "LOG ======> table_data: " . print_r($table_data, true) . PHP_EOL;
if (!is_array($table_data)) {
// FIRST CONNECTION
echo 'LOG ======> data (FIRST): ' . PHP_EOL . $data . PHP_EOL;
$table_data = [
'data' => $data,
'agi_arg' => json_encode([])
];
$server->table->set($fd, $table_data);
} else {
// ALREADY CONNECTED
echo 'LOG ======> data (MORE): ' . PHP_EOL . $data . PHP_EOL;
$table_data['data'] .= $data;
$server->table->set($fd, $table_data);
}
// GET ARGV
$argv = json_decode($table_data['agi_arg'], true);
$explodido = explode(PHP_EOL, $data);
foreach ($explodido as $linha) {
$linha = trim($linha);
if (empty($linha)) {
continue;
}
$partes = explode(':', $linha);
$comando = trim($partes[0]);
if (substr($comando, 0, 8) == 'agi_arg_') {
$number = str_replace('agi_arg_', '', $comando);
$argv[$number] = trim($partes[$number]);
}
}
$table_data['agi_arg'] = json_encode($argv);
$server->table->set($fd, $table_data);
if (isset($argv['1'])) {
$fgId = $argv[1]; // Numero do DID
// $hostname = $argv[2];
// $hostip = $argv[3];
// $fgId = '8999';
$hostname = 'hitpbx-050';
$hostip = '177.107.205.248';
$logEnabled = true;
$isXampp = false;
$appdir = dirname(__FILE__);
chdir($appdir);
date_default_timezone_set('America/Bahia');
$agi = new AGIEntrada(
array(
'agiRunCmd' => agiRunCmdWrapper($fd, $server),
'agiLog' => agiLogWrapper()
),
array(
'fgId' => $fgId,
'hostname' => $hostname,
'hostip' => $hostip,
'logEnabled' => $logEnabled,
'appdir' => $appdir,
'isXampp' => $isXampp
)
);
$agi->run();
}
});
$server->on("close", function ($server, $fd) {
echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL;
});
$server->start();