package org.bench4q.agent.scenario.engine;

import java.util.Date;
import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;

public class Supervisor extends Observable {
	private final ScenarioContext context;
	private final Timer timer = new Timer();

	public Supervisor(final ScenarioContext context) {
		this.context = context;
	}

	void start() {
		this.context.getSchedule().begin();
		long time = context.getSchedule().getScheduleRange()
				+ context.getStartDate().getTime();
		this.timer.schedule(new TimerTask() {
			@Override
			public void run() {
				stop();
				notifyObservers();
			}
		}, new Date(time));
	}

	void stop() {
		this.context.stop();
		this.timer.cancel();
	}
}
