Solved

How do I Flush the Input buffer?

Posted on 2009-07-02
9
893 Views
Last Modified: 2013-11-12
Hi,

I am using asterisk 1.2.25-voiceone with xlite softphone for an IVR Solution.
Making use of the Asterisk Gateway Interface(agi) and using Perl for the logic. The input buffer does not empty itself every time i try to use the get_data function to fetch the data(input) from user. At that time somehow the input buffer is full and it doesn't accept the input character which I enter when I hear the playback file.

A temporary solution to this was to create one empty sound file named tempTT and use that in get_data for playing back that file and it works when I use get_data with tempTT file for 3 to 4 times in a loop. But again, this is a random behaviour; if buffer contains something more then I  need to use tempTT file more often, may be 6 to 7 times. This is completely unacceptable solution as behaviour is randomised. Can anyone suggest me some help for this?

Wherever we get any problem of get_data not accepting input I call this function like flush();
$GetMainOption = &Dialouge1("Start_Key_Menu", "timeOut3", "1");

                &Flush();

sub Dialouge1()

{

        my($file,$timeout,$len)=@_;

        $pin = '';

        &Flush();

        $pin = $AGI->get_data("$soundFilePath/$file","$timeout","$len");

        return $pin;

}
 

sub Flush()

{

        print "EXEC PLAYBACK $soundFilePath/tempTT\n";

        my $i=0;

        while($i<3)

        {

                #$tempVar = $AGI->get_data("$soundFilePath/tempTT", "1" , "1");

                $tempVar = $AGI->get_data("$soundFilePath/tempTT", "1" , "1000000");

                $i++;

        }

        $|++;

}

Open in new window

0
Comment
Question by:taurean_aj
  • 3
  • 3
  • 2
9 Comments
 
LVL 7

Accepted Solution

by:
Fairlight2cx earned 250 total points
ID: 24761826
Generally speaking, the proper way to handle filehandles as far as getting them to be as unbuffered, you need to set $| = 1 on the filehandle.  If it's not stdin/stdout/stderr, you must first select() the filehandle, set the autoflush state, then re-select the previously selected filehandle:

my $oldhandle = select(SOMEHANDLE);
$| = 1;
select($oldhandle);

My suspicion is that you need to alter AGI.  It sounds like whatever filehandle get_data() is using is not set to autoflush as it should be.  But it's not looking like you have access to that filehandle from the place in the code you're at--hence, changing AGI.

Further documentation on the issue may be found here:

http://perldoc.perl.org/perlfaq5.html#How-do-I-flush%2funbuffer-an-output-filehandle%3f--Why-must-I-do-this%3f

0
 

Author Comment

by:taurean_aj
ID: 24763135
This is the code for get_data() in the AGI. Within, there is a call to the execute() function, which further calls _execcommand() which already does this:
select ((select ($fh), $| = 1)[0]);

So it seems the get_data() already sets the enable auto flush to ON. Please let me know what exactly I would need to modify in get_data() or related functions.
sub get_data {

	my ($self, $filename, $timeout, $maxdigits) = @_;
 

	return -1 if (!defined($filename));

	return $self->execute("GET DATA $filename $timeout $maxdigits");

}
 

sub execute {

	my ($self, $command) = @_;
 

	$self->_execcommand($command);

	my $res = $self->_readresponse();

	my $ret = $self->_checkresult($res);
 

	if (defined($ret) && $ret eq '-1' && !$self->_hungup()) {

		$self->_hungup(1);

		$self->callback($ret);

	}
 

	return $ret;

}
 

sub _execcommand {

	my ($self, $command, $fh) = @_;
 

	$fh = \*STDOUT if (!$fh);
 

	select ((select ($fh), $| = 1)[0]);
 

	return -1 if (!defined($command));
 

	print STDERR "_execcommand: '$command'\n" if ($self->_debug>3);
 

	return print $fh "$command\n";

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24786139
What is $AGI?  Is this an object you've created, or is it an Asterisk::AGI object?  The _execcommand is setting the autoflush on the output file handle, not the input file handle.  It appears it is the _readresponse function that reads from the input.
0
 

Author Comment

by:taurean_aj
ID: 24792250
Yes, $AGI is the Asterisk:: AGI Object. Attaching the AGI.pm file which contains all the above mentioned functions for your references. The readresponse also appears to flush the input buffer. Please let me know what exactly I have to modify to auto flush the input buffer.
AGI.txt
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 7

Expert Comment

by:Fairlight2cx
ID: 24793467
I'll include what I'd change the part of _readresponse().  Let me know if this works.  I'm not sure why you thought it was setting STDIN to autoflush, but I'm not seeing that at all, and I've just looked it over four times.
sub _readresponse {

	my ($self, $fh) = @_;
 

	my $response = undef;

	$fh = \*STDIN if (!$fh);

##### Start modifications.

        my $oh = select($fh);

        $| = 1;

        select($oh);

##### End modifications.

	while ($response = <$fh>) {

Open in new window

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 250 total points
ID: 24814860
The modifications can be put into a single line:
select((select($fh), $|=1)[0]);

Open in new window

0
 
LVL 7

Expert Comment

by:Fairlight2cx
ID: 24815207
They can, I just prefer the clarity of my notation.  The idea is to solve the problem, not get bogged down in stylistic semantics.  If that were the goal, I'd just refer someone to comp.lang.perl.misc and let them get roasted for punctuation and formatting instead of getting a real answer regarding functionality.  :(

Let's stay focused.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24815361
I don't find the code I posted to be bogged down in stylistic semantics.  I frequently use the single line of code I posted when I want to turn off buffering for a file handle.

Whether the asker prefers a single line or several lines is up to them.  Both do the same thing, with the exception that the single line does not leave the $oh variable around.  But if it's not used anywhere else (as it isn't in this case), that makes no difference.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T1 Modules 1 51
Perl Frameworks 1 78
VOIP grade small switch 4 57
Control Number of Log Files -Perl 7 54
So you think no one can listen in on your VOIP conversations, eh? Well... if you haven't setup Secure Real Time Transport (SRTP), your voice communications can be hacked into by just about anyone! First, let's talk about the intended audience for…
Implementing Avaya's One-X portal is pretty painless, until you want to deploy this to the Android and iPhone clients when these clients are outside of your network. The clients will also work within your local network. Here is our experience and so…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now