package kg.apc.jmeter.modifiers;

import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.processor.PreProcessor;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;

/**
 *
 * @author undera
 */
public class FifoPopPreProcessor extends AbstractTestElement
        implements PreProcessor, TestListener {

    public static final String queueName = "FifoName";
    public static final String variableName = "Variable";
    public static final String TIMEOUT = "Timeout";

    public FifoPopPreProcessor() {
        setTimeout(Long.toString(JMeterUtils.getPropDefault(FifoMap.TIMEOUT_PROP, Long.MAX_VALUE)));
    }

    public void testStarted() {
        FifoMap.getInstance().clear();
    }

    public void testStarted(String host) {
        testStarted();
    }

    public void testEnded() {
        FifoMap.getInstance().clear();
    }

    public void testEnded(String host) {
        testEnded();
    }

    public void testIterationStart(LoopIterationEvent event) {
    }

    public void process() {
        String value;
        try {
            value = FifoMap.getInstance().pop(getQueueName(), getTimeoutAsLong());
        } catch (InterruptedException ex) {
            value = "INTERRUPTED";
        }
        final JMeterVariables vars = JMeterContextService.getContext().getVariables();
        if (vars != null) {
            vars.put(getVarName(), value);
        }
    }

    public String getVarName() {
        return getPropertyAsString(variableName);
    }

    private long getTimeoutAsLong() {
        return Long.parseLong(getTimeout());
    }

    public String getTimeout() {
        return getPropertyAsString(TIMEOUT);
    }

    public String getQueueName() {
        return getPropertyAsString(queueName);
    }

    public final void setTimeout(String atimeout) {
        setProperty(TIMEOUT, atimeout);
    }

    public void setVarName(String text) {
        setProperty(variableName, text);
    }

    public void setQueueName(String text) {
        setProperty(queueName, text);
    }
}

@@ -396,37 +396,8 @@
 s;%0A%0A
-/**%0A *%0A * @author undera%0A */%0A
 publ
@@ -1712,24 +1712,152 @@
 tAsLong() %7B%0A
+        String timeout = getTimeout();%0A        if (timeout.isEmpty()) %7B%0A            return Long.MAX_VALUE;%0A        %7D else %7B%0A    
         retu
@@ -1878,22 +1878,27 @@
 ong(
-getT
+t
 imeout
-()
 );
+%0A        %7D
 %0A   

Set very long timeout by default