package be.iminds.ilabt.jfed.experimenter_gui.connectivity_tester;

import be.iminds.ilabt.jfed.connectivity_tester.ConnectivityTest;
import javafx.concurrent.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A ConnectivityTestTask is an instance of an {@link javafx.concurrent.Task} that actually executes a
 * {@link be.iminds.ilabt.jfed.connectivity_tester.ConnectivityTest}
 * <p>
 * User: twalcari
 * Date: 1/6/14
 * Time: 11:57 AM
 */
public class ConnectivityTestTask extends Task<ConnectivityTest.ConnectivityTestResult> {
    private static final Logger LOG = LoggerFactory.getLogger(ConnectivityTestTask.class);

    public static final double TASK_STARTED = 0.5, TASK_ENDED = 1;
    private static final double END_STATUS = 1;

    private final ConnectivityTest test;
    public ConnectivityTestTask(ConnectivityTest test) {
        this.test = test;
    }

    @Override
    protected ConnectivityTest.ConnectivityTestResult call() throws Exception {
        updateProgress(TASK_STARTED, END_STATUS);

        try {
            return test.call();
        } catch (Exception | AssertionError t) {
            LOG.error("AssertionError in ConnectivityTest: " + t.getMessage(), t);
            return null;
        } finally {
            updateProgress(TASK_ENDED, END_STATUS);
        }
    }

    /**
     * @return  The test associated with this task
     */
    public ConnectivityTest getTest() {
        return test;
    }

}
