Jump to content

Code goes to sleep in PHPAGI


arrajput

Recommended Posts

Hi there,

 

I am not sure what is happening. I am not an expert PHP programmer, I am simply trying to get PHPAGI work with my box.

 

As soon as I fire the constructor of the class to create an instance of the class, the script goes to endless execution and times out.

 

Here is my php code,

 

#!/usr/bin/php -q
<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

require("/var/lib/asterisk/agi-bin/phpagi.php");
echo "<br />";
echo "AGI Instance Creation";
echo "<br />";

$agi = new AGI();  // The code goes to sleep here
?>

 

The phpagi.php file code is herein, see the highlighted portion where it gets stuck, it is in RED in the constrcutor of the class

 

<?php

/**
* phpagi.php : PHP AGI Functions for Asterisk
* Website: http://phpagi.sourceforge.net/
*/


if (!class_exists('AGI_AsteriskManager'))
{
    require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'phpagi-asmanager.php');
}

define('AST_CONFIG_DIR', '/etc/asterisk/');
define('AST_SPOOL_DIR', '/var/spool/asterisk/');
define('AST_TMP_DIR', AST_SPOOL_DIR . '/tmp/');
define('DEFAULT_PHPAGI_CONFIG', AST_CONFIG_DIR . '/phpagi.conf');

define('AST_DIGIT_ANY', '0123456789#*');

define('AGIRES_OK', 200);

define('AST_STATE_DOWN', 0);
define('AST_STATE_RESERVED', 1);
define('AST_STATE_OFFHOOK', 2);
define('AST_STATE_DIALING', 3);
define('AST_STATE_RING', 4);
define('AST_STATE_RINGING', 5);
define('AST_STATE_UP', 6);
define('AST_STATE_BUSY', 7);
define('AST_STATE_DIALING_OFFHOOK', 8);
define('AST_STATE_PRERING', 9);

define('AUDIO_FILENO', 3); // STDERR_FILENO + 1

/**
* AGI class
*
*/
class AGI
{
    /**
    * Request variables read in on initialization.
    *
    * Often contains any/all of the following:
    *   agi_request - name of agi script
    *   agi_channel - current channel
    *   agi_language - current language
    *   agi_type - channel type (SIP, ZAP, IAX, ...)
    *   agi_uniqueid - unique id based on unix time
    *   agi_callerid - callerID string
    *   agi_dnid - dialed number id
    *   agi_rdnis - referring DNIS number
    *   agi_context - current context
    *   agi_extension - extension dialed
    *   agi_priority - current priority
    *   agi_enhanced - value is 1.0 if started as an EAGI script
    *   agi_accountcode - set by SetAccount in the dialplan
    *   agi_network - value is yes if this is a fastagi
    *   agi_network_script - name of the script to execute
    *
    * NOTE: program arguments are still in $_SERVER['argv'].
    *
    * @var array
    * @access public
    */
    var $request;

    /**
    * Config variables
    *
    * @var array
    * @access public
    */
    var $config;

    /**
    * Asterisk Manager
    *
    * @var AGI_AsteriskManager
    * @access public
    */
    var $asmanager;

    /**
    * Input Stream
    *
    * @access private
    */
    var $in = NULL;

    /**
    * Output Stream
    *
    * @access private
    */
    var $out = NULL;

    /**
    * Audio Stream
    *
    * @access public
    */
    var $audio = NULL;


    /**
    * Application option delimiter
    *
    * @access public
    */
    public $option_delim = ",";
    
    /**
    * Constructor
    *
    * @param string $config is the name of the config file to parse
    * @param array $optconfig is an array of configuration vars and vals, stuffed into $this->config['phpagi']
    */
    function __construct($config=NULL, $optconfig=array())
    {
        // load config
        if(!is_null($config) && file_exists($config))
          $this->config = parse_ini_file($config, true);
        elseif(file_exists(DEFAULT_PHPAGI_CONFIG))
          $this->config = parse_ini_file(DEFAULT_PHPAGI_CONFIG, true);

        // If optconfig is specified, stuff vals and vars into 'phpagi' config array.
        foreach($optconfig as $var=>$val)
          $this->config['phpagi'][$var] = $val;

        // add default values to config for uninitialized values
        if(!isset($this->config['phpagi']['error_handler'])) $this->config['phpagi']['error_handler'] = true;
        if(!isset($this->config['phpagi']['debug'])) $this->config['phpagi']['debug'] = false;
        if(!isset($this->config['phpagi']['admin'])) $this->config['phpagi']['admin'] = NULL;
        if(!isset($this->config['phpagi']['tempdir'])) $this->config['phpagi']['tempdir'] = AST_TMP_DIR;

        // festival TTS config
        if(!isset($this->config['festival']['text2wave'])) $this->config['festival']['text2wave'] = $this->which('text2wave');

        // swift TTS config
        if(!isset($this->config['cepstral']['swift'])) $this->config['cepstral']['swift'] = $this->which('swift');

        ob_implicit_flush(true);

        // open stdin & stdout
        $this->in = defined('STDIN') ? STDIN : fopen('php://stdin', 'r');
        $this->out = defined('STDOUT') ? STDOUT : fopen('php://stdout', 'w');

        // initialize error handler
        if($this->config['phpagi']['error_handler'] == true)
        {
          set_error_handler('phpagi_error_handler');
          global $phpagi_error_handler_email;
          $phpagi_error_handler_email = $this->config['phpagi']['admin'];
          error_reporting(E_ALL);
        }

        // make sure temp folder exists
        $this->make_folder($this->config['phpagi']['tempdir']);

        // read the request
        $str = fgets($this->in);
        while($str != "\n") //My code gets stuck here
        {
          $this->request[substr($str, 0, strpos($str, ':'))] = trim(substr($str, strpos($str, ':') + 1));
          $str = fgets($this->in);
        }


        // open audio if eagi detected
        if($this->request['agi_enhanced'] == '1.0')
        {
          if(file_exists('/proc/' . getmypid() . '/fd/3'))
            $this->audio = fopen('/proc/' . getmypid() . '/fd/3', 'r');
          elseif(file_exists('/dev/fd/3'))
          {
            // may need to mount fdescfs
            $this->audio = fopen('/dev/fd/3', 'r');
          }
          else
            $this->conlog('Unable to open audio stream');

          if($this->audio) stream_set_blocking($this->audio, 0);
        }

        $this->conlog('AGI Request:');
        $this->conlog(print_r($this->request, true));
        $this->conlog('PHPAGI internal configuration:');
        $this->conlog(print_r($this->config, true));
    }
 

 

Link to comment
Share on other sites


your code has line with fgets()

 

check out put of that line

that is not returning your desired output to exit from loop

.

fgets requires 2 parameters

2nd is for the length of read

if you do not provide 2nd parameter

it assumes length per read as 1024 bytes

and by doing so

it never gets '\n'

because read is 1024 characters lengthy


Sent at 6:20 PM on Friday

Link to comment
Share on other sites

Actually (from the manual fgets)

Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

(emphasis added)

 

 

The problem is at the end of the input file (also from the manual)

Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, then FALSE is returned.

 

If an error occurs, FALSE is returned.

(emphasis added}

 

Usually, the code would be something like:

while ($str = fgets($this->in)) {
  // Process string
}
although, I usually just use file and foreach
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.