hitagi-entrada/hitagi-entrada-swoole.php

103 lines
2.9 KiB
PHP
Raw Normal View History

2024-12-12 18:39:48 +00:00
<?php
2024-12-16 19:01:16 +00:00
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/wrapper-swoole.php';
2024-12-12 18:39:48 +00:00
2024-12-14 01:14:11 +00:00
use Hitlabs\Hitagi\AGIEntrada;
2024-12-12 18:39:48 +00:00
use Swoole\Runtime;
use Swoole\Table;
use Swoole\Server;
2024-12-16 19:01:16 +00:00
2024-12-12 18:39:48 +00:00
Runtime::enableCoroutine();
$table = new Table(1024);
$table->column('data', Swoole\Table::TYPE_STRING, 65536);
2024-12-16 19:01:16 +00:00
$table->column('agi_arg', Swoole\Table::TYPE_STRING, 65536);
2024-12-12 18:39:48 +00:00
$table->create();
2024-12-16 19:01:16 +00:00
$server = new Swoole\Server("0.0.0.0", 8073);
2024-12-12 18:39:48 +00:00
$server->table = $table;
$server->on('start', function ($server) {
2024-12-16 19:01:16 +00:00
echo "LOG ======> Servidor iniciou na porta 8073...." . PHP_EOL;
2024-12-12 18:39:48 +00:00
});
$server->on("connect", function (Server $server, $fd) {
2024-12-16 19:01:16 +00:00
echo "LOG ======> Cliente $fd conectado...." . PHP_EOL;
2024-12-12 18:39:48 +00:00
});
$server->on("receive", function ($server, $fd, $reactor_id, $data) {
2024-12-16 19:01:16 +00:00
$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;
2024-12-12 18:39:48 +00:00
}
2024-12-16 19:01:16 +00:00
$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();
}
2024-12-12 18:39:48 +00:00
});
$server->on("close", function ($server, $fd) {
2024-12-16 19:01:16 +00:00
echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL;
2024-12-12 18:39:48 +00:00
});
2024-12-16 19:01:16 +00:00
$server->start();