asterisk+phpdaemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Default application resolver | |
* | |
* @package Core | |
* @author Zorin Vasily <maintainer@daemon.io> | |
*/ | |
class MyAppResolver extends \PHPDaemon\Core\AppResolver { | |
/** | |
* Routes incoming request to related application. Method is for overloading. | |
* @param object Request. | |
* @param object AppInstance of Upstream. | |
* @return string Application's name. | |
*/ | |
public function getRequestRoute($req, $upstream) { | |
/* | |
This method should return application name to handle incoming request ($req). | |
*/ | |
if (preg_match('~^/(asterisk|mysqltest)/?~', $req->attrs->server['DOCUMENT_URI'], $m)) { | |
return $m[1]; | |
} | |
} | |
} | |
return new MyAppResolver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PHPDaemon\Applications; | |
//namespace PHPDaemon\Examples; | |
//namespace PHPDaemon\Clients\Asterisk; | |
use PHPDaemon\Clients\Asterisk\Pool; | |
//use PHPDaemon\Clients\Asterisk\Connection; | |
use PHPDaemon\Core\AppInstance; | |
use PHPDaemon\HTTPRequest\Generic; | |
use PHPDaemon\Core\Daemon; | |
class Asterisk extends AppInstance { | |
public $asteriskclient; | |
public $asteriskconn; | |
public $sql; | |
public $job; | |
public $session; | |
public $tempsession; | |
/** | |
* Setting default config options | |
* Overriden from AppInstance::getConfigDefaults | |
* @return array|false | |
*/ | |
protected function getConfigDefaults() { | |
return [ | |
'url' => 'xxx', | |
'reconnect' => 1, | |
'asteriskclient-name' => '' | |
]; | |
} | |
/** | |
* Constructor. | |
* @return void | |
*/ | |
public function init() { | |
if ($this->isEnabled()) { | |
$this->asteriskclient = Pool::getInstance($this->config->asteriskclientname->value); | |
$this->sql = \PHPDaemon\Clients\MySQL\Pool::getInstance(); | |
$job = $this->job = new \PHPDaemon\Core\ComplexJob(function ($job) { // called when job is done | |
\PHPDaemon\Core\Daemon::log(print_r($this->job->getResult('select'),true)); | |
$job->keep(); | |
//$this->wakeup(); // wake up the request immediately | |
}); | |
} | |
} | |
/**n vs new row | |
* Called when the worker is ready to go. | |
* @return void | |
*/ | |
public function onReady() { | |
if ($this->asteriskclient) { | |
$this->asteriskclient->onReady(); | |
$this->connect(); | |
} | |
} | |
public function connect() { | |
$this->asteriskclient->getConnection($this->config->url->value, function ($conn) { | |
$this->asteriskconn = $conn; | |
if ($conn->connected) { | |
$conn->bind('event_newchannel', function ($conn) { | |
}); | |
$conn->bind('event', function ($conn) { | |
self::agregate($conn); | |
}); | |
$conn->bind('disconnect', function ($conn) { | |
\PHPDaemon\Core\Daemon::log('Connection lost... Reconnect in ' . $this->config->reconnect->value . ' sec'); | |
$this->connect(); | |
}); | |
} | |
else { | |
\PHPDaemon\Core\Daemon::log(get_class($this) . ': couldn\'t connect to ' . $this->config->url->value); | |
} | |
}); | |
} | |
public function agregate ($arr) { | |
foreach ($arr->packets as $k=>$p) | |
{ | |
$this->session[$p['uniqueid']]=$p; | |
} | |
} | |
public function save_to_db ($arr) { | |
//save to mysql db function | |
} | |
/** | |
* Called when application instance is going to shutdown. | |
* @return boolean Ready to shutdown? | |
*/ | |
public function onShutdown($graceful = false) { | |
if ($this->asteriskclient) { | |
return $this->asteriskclient->onShutdown(); | |
} | |
return true; | |
} | |
public function beginRequest($req, $upstream) { | |
return new RunRequest($this, $upstream, $req); | |
} | |
} | |
class RunRequest extends Generic { | |
public function init() { | |
} | |
public function run() { | |
try { | |
$this->header('Content-Type: text/html'); | |
} catch (\Exception $e) { | |
} | |
print_r($this->appInstance->session); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Config file | |
user www-data; | |
group www-data; | |
max-workers 1; | |
min-workers 1; | |
start-workers 1; | |
max-idle 0; | |
logstorage /var/log/phpdaemon.log; | |
log-errors true; | |
logging true; | |
#add-include-path '/path/to/your/folder'; | |
Pool:\PHPDaemon\Clients\MySQL\Pool{ | |
enable 1; | |
server 'tcp://xxx:xxx@127.0.0.1/xxx'; | |
privileged; | |
} | |
# слушаем 80 и 443 порт | |
Pool:\PHPDaemon\Servers\HTTP\Pool{ | |
listen "tcp://0.0.0.0:85"; | |
port 85; | |
privileged; | |
maxconcurrency 1; | |
} | |
Asterisk { | |
enable 1; | |
Path Asterisk.php; | |
} | |
Mysqltest { | |
enable 1; | |
Path Mysqltest.php; | |
} | |
# other applications... | |
include conf.d/*.conf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment