[asterisk-users] PHP/AGI/SetVar Issue

Steve Edwards asterisk.org at sedwards.com
Fri Jun 5 16:13:24 CDT 2009


On Fri, 5 Jun 2009, Peder wrote:

> Of course I just figured it out.  If I send a print with \n, it works 
> now. Not really sure why though:

Because you are communicating with another process (Asterisk) over a pair 
of pipes. One you write to, one you read from.

If you don't "terminate" your request, how will Asterisk know where the 
end is?

> echo 'SET VARIABLE ISLOCALCONTEXT CONTEXT3';
> print "\n";
> echo 'SET VARIABLE ISLOCALDID 4444';
> print "\n";

This is still wrong.

If you violate the protocol (not reading the AGI environment, not flushing 
stdout if your language does not do it automagically for you, not reading 
the response to each request, printing ANYTHING to stdout), your AGI will 
not work correctly.

If it does work "correctly" now, it will not at some time in the future.

The following C snippet (not using a library for illustration only):

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);

does not work at all.

If I "slow C down" like this:

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         for (a = 0; a < 3000; ++a) for (b = 0; b < 3000; ++b) c = a * b;

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         for (a = 0; a < 3000; ++a) for (b = 0; b < 3000; ++b) c = a * b;

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         for (a = 0; a < 3000; ++a) for (b = 0; b < 3000; ++b) c = a * b;

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         for (a = 0; a < 3000; ++a) for (b = 0; b < 3000; ++b) c = a * b;

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         for (a = 0; a < 3000; ++a) for (b = 0; b < 3000; ++b) c = a * b;

it works on my very slow 500MHz AMD Geode test system. Unfortunately, it 
completely fails on my somewhat faster 2.3GHz AMD Phenom 8650 Triple-Core 
Processor.

If I follow the protocol:

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         fgets(response, sizeof(response), stdin);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         fgets(response, sizeof(response), stdin);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         fgets(response, sizeof(response), stdin);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         fgets(response, sizeof(response), stdin);

         printf("STREAM FILE \"hit\" \"\"\n");
         fflush(stdout);
         fgets(response, sizeof(response), stdin);

it works everywhere, every time.

As I and others have suggested, stand on the shoulders of others -- please 
use an established library. You will save time and hair. And your code 
will be easier to write and maintain:

 	agi_stream_file("hit", "");
 	agi_stream_file("hit", "");
 	agi_stream_file("hit", "");
 	agi_stream_file("hit", "");
 	agi_stream_file("hit", "");

Thanks in advance,
------------------------------------------------------------------------
Steve Edwards      sedwards at sedwards.com      Voice: +1-760-468-3867 PST
Newline                                             Fax: +1-760-731-3000



More information about the asterisk-users mailing list