Revision
14660
Author
gled
Date
2009-08-28 07:20:41 -0500 (Fri, 28 Aug 2009)

Log Message

a little refactor, and added kill on hangup ;)

Modified Paths

Added Paths

Property Changed

Diff

Property changes: freeswitch/trunk/contrib/gled/php


Name: svn:ignore
   + pid.event_listener.php

Added: freeswitch/trunk/contrib/gled/php/README (0 => 14660)


--- freeswitch/trunk/contrib/gled/php/README	                        (rev 0)
+++ freeswitch/trunk/contrib/gled/php/README	2009-08-28 12:20:41 UTC (rev 14660)
@@ -0,0 +1,33 @@
+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 ;)
+	

Modified: freeswitch/trunk/contrib/gled/php/class.ivr.php (14659 => 14660)


--- freeswitch/trunk/contrib/gled/php/class.ivr.php	2009-08-28 09:58:06 UTC (rev 14659)
+++ freeswitch/trunk/contrib/gled/php/class.ivr.php	2009-08-28 12:20:41 UTC (rev 14660)
@@ -1,17 +1,22 @@
 <?php
+require_once('ESL.php');
+
 declare(ticks=1);
 pcntl_signal(SIGPIPE,  "sig_fs");
+pcntl_signal(SIGHUP,  "sig_fs");
+pcntl_signal(SIGTERM,  "sig_fs");
 
 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();
 }
 
 class IVR {
 	var $in = NULL;
 	var $out = NULL;
+	var $esl = NULL;
 	var $con_data = array();
-	var $logfile = '/devel/log/ivr.log';
+	var $logfile = '/telephony/freeswitch/log/ivr.log';
 
 	function IVR() {
 		ob_implicit_flush(true);
@@ -28,10 +33,11 @@
 		}
 		$this->con_data = $this->parse_response($buf);
 		$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) {
@@ -137,8 +143,12 @@
 			return $ret;
 		$tmp = explode("\n",$tmp);
 		foreach ($tmp as $line) {
-			list($key,$val) = explode(':',$line);
-			$ret[strtolower($key)] = strtolower(trim( urldecode($val) ));
+			if ( eregi(':',$line) ) {
+				list($key,$val) = explode(':',$line);
+				$ret[strtolower($key)] = strtolower(trim( urldecode($val) ));
+			}
+			else
+				mail('tristan@telemaque.fr',"Wrong line",$line);
 		}
 		return $ret;
 	}
@@ -167,6 +177,12 @@
 		}	
 	}
 
+	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() {
 		$ret = $this->parse_response($this->send_cmd('hangup'));
 		if ( ! $this->check_retcode($ret) )

Added: freeswitch/trunk/contrib/gled/php/event_listener.php (0 => 14660)


--- freeswitch/trunk/contrib/gled/php/event_listener.php	                        (rev 0)
+++ freeswitch/trunk/contrib/gled/php/event_listener.php	2009-08-28 12:20:41 UTC (rev 14660)
@@ -0,0 +1,29 @@
+<?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;
+		}
+	}
+}
+
+?>

Added: freeswitch/trunk/contrib/gled/php/processhandler.php (0 => 14660)


--- freeswitch/trunk/contrib/gled/php/processhandler.php	                        (rev 0)
+++ freeswitch/trunk/contrib/gled/php/processhandler.php	2009-08-28 12:20:41 UTC (rev 14660)
@@ -0,0 +1,65 @@
+<?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;
+      }
+  }
+}
+?>
+

Added: freeswitch/trunk/contrib/gled/php/test.php (0 => 14660)


--- freeswitch/trunk/contrib/gled/php/test.php	                        (rev 0)
+++ freeswitch/trunk/contrib/gled/php/test.php	2009-08-28 12:20:41 UTC (rev 14660)
@@ -0,0 +1,15 @@
+#!/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();
+
+?>
Property changes on: freeswitch/trunk/contrib/gled/php/test.php
___________________________________________________________________
Name: svn:executable
   + *