# HG changeset patch # User sugi # Date 1383591429 -32400 # Node ID 650c0f1270a31a4c539d7ef9e19f8d53b248c53d # Parent 0a99effcfd3b1ebc9a7073618763cea7d7359ee9 add package alice.topology.manager.keeparive diff -r 0a99effcfd3b -r 650c0f1270a3 src/alice/topology/manager/keeparive/PingScheduler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/topology/manager/keeparive/PingScheduler.java Tue Nov 05 03:57:09 2013 +0900 @@ -0,0 +1,92 @@ +package alice.topology.manager.keeparive; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +import alice.codesegment.CodeSegment; + +public class PingScheduler extends CodeSegment{ + private LinkedList list = new LinkedList(); + private ArrayList regist = new ArrayList(); + private HashMap map = new HashMap(); + public boolean updateFlag = false; + public long time = 0; + public int taskNum = 0; + public TaskInfo nowTask; + private long totalTime = 0; + + public long getTotalTime(){ + return totalTime; + } + + public List nameList(){ + return regist; + } + + public List getTaskList(){ + return list; + } + + public synchronized void wake(){ + long t = System.currentTimeMillis(); + long addTime = nowTask.getTime() - (t - time); + TaskInfo ti = list.get(0); + ti.setTime(ti.getTime() + addTime); + this.notify(); + } + + public synchronized void addTask(TaskInfo newInfo){ + if (map.containsKey(newInfo.getName())){ + newInfo.setTaskNum(map.get(newInfo.getName())); + } else { + taskNum +=10; + map.put(newInfo.getName(), taskNum); + newInfo.setTaskNum(taskNum); + } + if (list.isEmpty()) { + list.add(newInfo); + } else { + for (int cnt = 0; cnt < list.size(); cnt++){ + TaskInfo info = list.get(cnt); + if (info.getTime() < newInfo.getTime()){ + newInfo.setTime(newInfo.getTime() - info.getTime()); + if (cnt+1 == list.size()){ + list.add(newInfo); + break; + } + } else if (info.getTime() == newInfo.getTime()){ + newInfo.setTime(newInfo.getTime() - info.getTime()); + list.add(cnt+1, newInfo); + break; + } else if (info.getTime() > newInfo.getTime()){ + info.setTime(info.getTime() - newInfo.getTime()); + list.add(cnt, newInfo); + break; + } + } + } + updateFlag = true; + } + + @Override + public synchronized void run() { + try { + while(true){ + if(list.size()== 0)System.exit(0); + nowTask = list.poll(); + System.out.print("taskNum "+nowTask.getTaskNum()); + System.out.println(" taskTime "+nowTask.getTime()); + time = System.currentTimeMillis(); + if (nowTask.getTime()!=0) + this.wait(nowTask.getTime()); + totalTime +=nowTask.getTime(); + nowTask.setTime(30 * 1000); + addTask(nowTask); + + } + } catch (InterruptedException e) { + } + } +} diff -r 0a99effcfd3b -r 650c0f1270a3 src/alice/topology/manager/keeparive/SchedulerViewer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/topology/manager/keeparive/SchedulerViewer.java Tue Nov 05 03:57:09 2013 +0900 @@ -0,0 +1,154 @@ +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 hourDataSeries; + private NumberAxis xAxis; + private Timeline animation; + + private double hours = 0; + private double minutes = 0; + private double timeInHours = 0; + private int count; + + + 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() { + @Override public void handle(ActionEvent actionEvent) { + // 6 minutes data per frame + for(int count=0; count < 6; count++) { + nextTime(); + plotTime(); + plotGraph(); + } + } + })); + animation.setCycleCount(Animation.INDEFINITE); + } + + protected LineChart createChart() { + xAxis = new NumberAxis(0,200,10); + final NumberAxis yAxis = new NumberAxis(0,100,10); + final LineChart lc = new LineChart(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(); + lc.getData().add(hourDataSeries); + return lc; + } + + private void plotGraph() { + if (ps.updateFlag){ + if(!hourDataSeries.getData().isEmpty()) + hourDataSeries.getData().clear(); + count = (int) (ps.getTotalTime()/1000); + hourDataSeries.getData().add(new XYChart.Data(count, ps.nowTask.getTaskNum())); + count +=ps.nowTask.getTime()/1000; + hourDataSeries.getData().add(new XYChart.Data(count, ps.nowTask.getTaskNum())); + + for (TaskInfo info : ps.getTaskList()){ + + hourDataSeries.getData().add(new XYChart.Data(count, info.getTaskNum())); + count += info.getTime()/1000; + hourDataSeries.getData().add(new XYChart.Data(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()); + xAxis.setUpperBound(xAxis.getUpperBound()); + + } + } + + public void StartScheduler(){ + ps = new PingScheduler(); + TaskInfo a = new TaskInfo("a", 10 * 1000); + TaskInfo b = new TaskInfo("b", 20 * 1000); + + + TaskInfo c = new TaskInfo("c", 60* 1000); + TaskInfo d = new TaskInfo("d", 40 * 1000); + TaskInfo e = new TaskInfo("e", 80 * 1000); + + + ps.addTask(a); + ps.addTask(b); + ps.addTask(c); + ps.addTask(d); + ps.addTask(e); + 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(); + } + +} diff -r 0a99effcfd3b -r 650c0f1270a3 src/alice/topology/manager/keeparive/StartViewer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/topology/manager/keeparive/StartViewer.java Tue Nov 05 03:57:09 2013 +0900 @@ -0,0 +1,10 @@ +package alice.topology.manager.keeparive; + +import javafx.application.Application; + +public class StartViewer { + public static void main(String[] args) { + Application.launch(SchedulerViewer.class, args); + + } +} diff -r 0a99effcfd3b -r 650c0f1270a3 src/alice/topology/manager/keeparive/TaskInfo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/topology/manager/keeparive/TaskInfo.java Tue Nov 05 03:57:09 2013 +0900 @@ -0,0 +1,32 @@ +package alice.topology.manager.keeparive; + +public class TaskInfo { + private long nextPingTime; + private String taskName; + private int taskNum = 0; + + public TaskInfo(String n, long t){ + nextPingTime = t; + taskName = n; + } + + public long getTime(){ + return nextPingTime; + } + + public String getName(){ + return taskName; + } + + public void setTime(long time){ + nextPingTime = time; + } + + public int getTaskNum(){ + return taskNum; + } + + public void setTaskNum(int num){ + taskNum = num; + } +}