提交 3073ede7 authored 作者: Tristan Mahé's avatar Tristan Mahé

a little refactor, and added kill on hangup ;)

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/contrib@14660 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 e4b97d6d
Simple translation for AGI based scripts.
* TODO:
+ transform class.ivr.php to phpagi names so that scripts are not aware they're no more under *.
+ add bindings to more functions ( record session, get variable, dial, ...).
+ config file for event_listener ESL connection and other stuff...
+ refactor check_hangup to already read data, and not reading and discarding stuff like that, it's awfull...
* Files:
+ class.ivr.php:
- simple bindings for IVR functions.
+ processhandler.php:
- included by event_listener.php, ease the creation of always running daemons launched by cron, to be sure there's only
one instance running at the same time.
+ event_listener.php:
- listen via event_socket ( inbound mode ) to CHANNEL_HANGUP_COMPLETE events, checking if variable_pid2kill is present, and
if so, will send a SIGHUP to that pid ( AGI behaviour ).
* Usage example:
+ launch event_listener.php via cron on the box running the scripts ( be sure that you edited the socket connection part ).
+ launch fs_ivrd on that same box ( ./fs_ivrd -h host -p port ).
+ write an extension to be handled by your scripts ( be carefull of the full path to the script ):
<condition field="destination_number" expression="^sockettest$">
<action application="log" data="WARNING ${caller_id_number}:${destination_number} EVENTSOCKET_TEST"/>
<action application="set" data="ivr_path=/usr/local/src/freeswitch/contrib/gled/php/test.php"/>
<action application="socket" data="127.0.0.1:9099 full"/>
<action application="hangup"/>
</condition>
+ be sure the test.php is executable ( chmod +755 test.php ).
+ call that extention ;)
<?php <?php
require_once('ESL.php');
declare(ticks=1); declare(ticks=1);
pcntl_signal(SIGPIPE, "sig_fs"); pcntl_signal(SIGPIPE, "sig_fs");
pcntl_signal(SIGHUP, "sig_fs");
pcntl_signal(SIGTERM, "sig_fs");
function sig_fs($signo) { function sig_fs($signo) {
file_put_contents('/devel/log/ivr.log',date("Y-m-d H:i:s").": BROKEN PIPE, call has been hung up\n",FILE_APPEND); file_put_contents('/telephony/freeswitch/log/ivr.log',date("Y-m-d H:i:s")." call has been hung up by $signo signal\n",FILE_APPEND);
exit(); exit();
} }
class IVR { class IVR {
var $in = NULL; var $in = NULL;
var $out = NULL; var $out = NULL;
var $esl = NULL;
var $con_data = array(); var $con_data = array();
var $logfile = '/devel/log/ivr.log'; var $logfile = '/telephony/freeswitch/log/ivr.log';
function IVR() { function IVR() {
ob_implicit_flush(true); ob_implicit_flush(true);
...@@ -28,10 +33,11 @@ class IVR { ...@@ -28,10 +33,11 @@ class IVR {
} }
$this->con_data = $this->parse_response($buf); $this->con_data = $this->parse_response($buf);
$this->log("New call uuid:{$this->con_data['channel-unique-id']}"); $this->log("New call uuid:{$this->con_data['channel-unique-id']}");
/*
fwrite($this->out,"myevents\n\n"); /*
$buf = $this->wait_answer(); Set pid to kill on hangup :)
*/ */
$this->set_var('pid2kill',getmypid());
} }
function log($str) { function log($str) {
...@@ -137,8 +143,12 @@ class IVR { ...@@ -137,8 +143,12 @@ class IVR {
return $ret; return $ret;
$tmp = explode("\n",$tmp); $tmp = explode("\n",$tmp);
foreach ($tmp as $line) { foreach ($tmp as $line) {
list($key,$val) = explode(':',$line); if ( eregi(':',$line) ) {
$ret[strtolower($key)] = strtolower(trim( urldecode($val) )); list($key,$val) = explode(':',$line);
$ret[strtolower($key)] = strtolower(trim( urldecode($val) ));
}
else
mail('tristan@telemaque.fr',"Wrong line",$line);
} }
return $ret; return $ret;
} }
...@@ -167,6 +177,12 @@ class IVR { ...@@ -167,6 +177,12 @@ class IVR {
} }
} }
function set_var($var,$value) {
$ret = $this->parse_response($this->execute_wait('Set',"$var=$value"));
if ( ! $this->check_retcode($ret) )
$this->log("Huhooo Set_Var wasn't ack'd");
}
function hangup() { function hangup() {
$ret = $this->parse_response($this->send_cmd('hangup')); $ret = $this->parse_response($this->send_cmd('hangup'));
if ( ! $this->check_retcode($ret) ) if ( ! $this->check_retcode($ret) )
......
<?php
define ("PID","pid.event_listener.php");
require_once("processhandler.php");
require_once("ESL.php");
if ( ProcessHandler::isActive () )
die();
else {
/* Init stuff */
ProcessHandler::activate ();
$esl = new eslConnection('127.0.0.1', '8021', 'ClueCon');
$esl->send('event PLAIN CHANNEL_HANGUP_COMPLETE');
while ($esl->connected() && $e = $esl->recvEvent() ) {
switch($e->getType()) {
case 'CHANNEL_HANGUP_COMPLETE':
$pid2kill = $e->getHeader('variable_pid2kill');
if ($pid2kill)
posix_kill($pid2kill,SIGHUP);
default:
usleep(500);
break;
}
}
}
?>
<?php
if (!defined ('PID'))
{
define ("PID", "/tmp/process_pid.php");
}
class ProcessHandler
{
function isActive ()
{
$pid = ProcessHandler::getPID ();
if ($pid == null)
{
$ret = false;
}
else
{
$ret = posix_kill ($pid, 0);
}
if ($ret == false)
{
ProcessHandler::activate ();
}
return $ret;
}
function activate ()
{
$pidfile = PID;
$pid = ProcessHandler::getPID ();
if ($pid != null && $pid == getmypid ())
{
return "Already running!\n";
}
else
{
$fp = fopen ($pidfile, "w+");
if ($fp)
{
if (!fwrite ($fp, "<"."?php\n\$pid = ".getmypid ().";\n?".">"))
{
die ("Can not create pid file!\n");
}
fclose ($fp);
}
else
{
die ("Can not create pid file!\n");
}
}
}
function getPID ()
{
if (file_exists (PID))
{
require (PID);
return $pid;
}
else
{
return null;
}
}
}
?>
#!/usr/bin/php -q
<?php
require_once('class.ivr.php');
$ivr = new IVR();
$ivr->answer();
$ivr->log('Answered call');
sleep(10);
$ivr->log('Calling ivr_sleep');
$ivr->ivr_sleep(50);
$ivr->log('Hanging up');
$ivr->hangup();
$ivr->close();
?>
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论