package org.asteriskjava.pbx.agi;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.asteriskjava.fastagi.AgiChannel;
import org.asteriskjava.fastagi.AgiException;
import org.asteriskjava.fastagi.AgiHangupException;
import org.asteriskjava.pbx.AgiChannelActivityAction;
import org.asteriskjava.pbx.Channel;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;

/**
 * Waits in silence for 10 seconds, then hangs up. This is useful where channels
 * are waiting for some external action to redirect/bridge them.
 * 
 * @author rsutton
 */
public class AgiChannelActivityTransientHoldSilence implements AgiChannelActivityAction
{
    private final Log logger = LogFactory.getLog(this.getClass());

    CountDownLatch latch = new CountDownLatch(1);
    volatile boolean callReachedAgi = false;
    long timer = System.currentTimeMillis();

    @Override
    public void execute(AgiChannel channel, Channel ichannel) throws AgiException, InterruptedException
    {
        try
        {
            callReachedAgi = true;
            channel.answer();
            long secondsOnHold = Math.abs(System.currentTimeMillis() - timer) / 1000;
            if (secondsOnHold > 8)
            {
                logger.info(ichannel + " is still on hold after " + secondsOnHold + " seconds, Hanging up");
                channel.hangup();
            }
            else
            {
                latch.await(10, TimeUnit.SECONDS);
            }
        }
        catch (AgiHangupException e)
        {
            logger.warn(e);
        }

    }

    @Override
    public boolean isDisconnect()
    {
        return false;
    }

    @Override
    public void cancel()
    {
        latch.countDown();

    }

    public boolean hasCallReachedAgi()
    {
        return callReachedAgi;
    }
}

@@ -858,47 +858,32 @@
     
-long timer = System.currentTimeMillis()
+boolean firstPass = true
 ;%0A%0A 
@@ -1108,115 +1108,22 @@
     
-long secondsOnHold = Math.abs(System.currentTimeMillis() - timer) / 1000;%0A            if (secondsOnHold %3E 8
+if (!firstPass
 )%0A  
@@ -1161,12 +1161,13 @@
 ger.
-info
+error
 (ich
@@ -1203,36 +1203,17 @@
 ter 
-%22 + secondsOnHold + %22 second
+first pas
 s, H
@@ -1459,24 +1459,91 @@
 );%0A        %7D
+%0A        finally%0A        %7B%0A            firstPass = false;%0A        %7D
 %0A%0A    %7D%0A%0A   

add hard hangup on second iteration