view src/alice/topology/manager/keeparive/SchedulerViewer.java @ 299:48de3510fb00

Scheduler has bug
author sugi
date Mon, 04 Nov 2013 13:09:14 +0900
parents
children
line wrap: on
line source

package alice.topology.manager.keeparive;

/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.util.Duration;

/**
 * A simulated stock line chart.
 *
 * @see javafx.scene.chart.Chart
 * @see javafx.scene.chart.LineChart
 * @see javafx.scene.chart.NumberAxis
 * @see javafx.scene.chart.XYChart
 */
public class SchedulerViewer extends Application {
	private PingScheduler ps;
	private XYChart.Series<Number,Number> hourDataSeries;
	private NumberAxis xAxis;
	private Timeline animation;

	private double hours = 0;
	private double minutes = 0;
	private double timeInHours = 0;
	

	private void init(Stage primaryStage) {
		Group root = new Group();
		primaryStage.setScene(new Scene(root));
		root.getChildren().add(createChart());
		// create timeline to add new data every 60th of second
		animation = new Timeline();
		animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000/60), new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent actionEvent) {
				// 6 minutes data per frame
				for(int count=0; count < 6; count++) {
					nextTime();
					plotTime();
				}
			}
		}));
		animation.setCycleCount(Animation.INDEFINITE);
	}

	protected LineChart<Number, Number> createChart() {
		xAxis = new NumberAxis(0,100,10);
		final NumberAxis yAxis = new NumberAxis(0,100,10);
		final LineChart<Number,Number> lc = new LineChart<Number,Number>(xAxis,yAxis);
		// setup chart
		lc.setId("lineStockDemo");
		lc.setCreateSymbols(false);
		lc.setAnimated(false);
		lc.setLegendVisible(false);
		lc.setTitle("Task Viewer");
		xAxis.setLabel("Time");
		xAxis.setForceZeroInRange(false);
		yAxis.setLabel("Task Name");
		yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,"$",null));
		// add starting data
		hourDataSeries = new XYChart.Series<Number,Number>();
		for (double m=0; m<(60); m++) {
			nextTime();
			plotTime();
			plotGraph();
		}
		lc.getData().add(hourDataSeries);
		return lc;
	}

	private void plotGraph() {
		if (ps.updateFlag){
			if(!hourDataSeries.getData().isEmpty())
				hourDataSeries.getData().clear();
			int count = 0;
			hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, ps.nowTask.getTaskNum()));
			count += ps.nowTask.getTime()/1000;
			hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, ps.nowTask.getTaskNum()));
			
			for (TaskInfo info : ps.getTaskList()){
				hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, info.getTaskNum()));
				count += info.getTime()/1000;
				hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, info.getTaskNum()));	
			}
			ps.updateFlag = false;
		}
		
		
	}

	private void nextTime() {
		if (minutes == 59) {
			hours ++;
			minutes = 0;
		} else {
			minutes ++;
		}
		timeInHours = hours + ((1d/60d)*minutes);
	}

	private void plotTime() {
		if ((timeInHours % 1) == 0) {
				xAxis.setLowerBound(xAxis.getLowerBound()+1);
				xAxis.setUpperBound(xAxis.getUpperBound()+1);
			
		}
	}
	
	public void StartScheduler(){
		ps = new PingScheduler();
		TaskInfo a = new TaskInfo("a", 10 * 1000);
		
		
		TaskInfo d = new TaskInfo("d", 100 * 1000);
		TaskInfo c = new TaskInfo("c", 40 * 1000);
		TaskInfo b = new TaskInfo("b", 20 * 1000);
		
		
		ps.addTask(a);
		ps.addTask(b);
		ps.addTask(c);
		
		ps.addTask(d);
		
		for (TaskInfo info :ps.getTaskList()){
			System.out.println(info.getTime());
		}
		ps.execute();
	}

	public void play() {
		animation.play();
	}

	@Override public void stop() {
		animation.pause();
	}    

	@Override public void start(Stage primaryStage) throws Exception {
		StartScheduler();
		init(primaryStage);
		primaryStage.show();
		play();
	}
	
}