changeset 299:48de3510fb00

Scheduler has bug
author sugi
date Mon, 04 Nov 2013 13:09:14 +0900
parents 4fe924c9f504
children 6e18e08281e6
files .classpath src/alice/daemon/Connection.java src/alice/datasegment/RemoteDataSegmentManager.java src/alice/topology/manager/keeparive/PingScheduler.java src/alice/topology/manager/keeparive/SchedulerViewer.java src/alice/topology/manager/keeparive/StartViewer.java src/alice/topology/manager/keeparive/TaskInfo.java
diffstat 7 files changed, 290 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/.classpath	Tue Oct 22 20:02:15 2013 +0900
+++ b/.classpath	Mon Nov 04 13:09:14 2013 +0900
@@ -5,9 +5,11 @@
 	<classpathentry exported="true" kind="lib" path="lib/log4j-1.2.16.jar"/>
 	<classpathentry exported="true" kind="lib" path="lib/slf4j-api-1.6.1.jar"/>
 	<classpathentry exported="true" kind="lib" path="lib/slf4j-log4j12-1.6.1.jar"/>
+	<classpathentry exported="true" kind="con" path="at.bestsolution.efxclipse.tooling.jdt.core.JAVAFX_CONTAINER"/>
+	<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
-	<classpathentry kind="lib" path="lib/msgpack-0.6.8-SNAPSHOT-sources.jar"/>
-	<classpathentry kind="lib" path="lib/msgpack-0.6.8-SNAPSHOT.jar" sourcepath="lib/msgpack-0.6.8-SNAPSHOT-sources.jar"/>
-	<classpathentry kind="lib" path="lib/com.alexmerz.graphviz.jar"/>
+	<classpathentry exported="true" kind="lib" path="lib/msgpack-0.6.8-SNAPSHOT-sources.jar"/>
+	<classpathentry exported="true" kind="lib" path="lib/msgpack-0.6.8-SNAPSHOT.jar" sourcepath="lib/msgpack-0.6.8-SNAPSHOT-sources.jar"/>
+	<classpathentry exported="true" kind="lib" path="lib/com.alexmerz.graphviz.jar"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
--- a/src/alice/daemon/Connection.java	Tue Oct 22 20:02:15 2013 +0900
+++ b/src/alice/daemon/Connection.java	Mon Nov 04 13:09:14 2013 +0900
@@ -7,12 +7,13 @@
 
 import alice.codesegment.SingletonMessage;
 import alice.datasegment.Command;
+import alice.topology.manager.keeparive.States;
 
 public class Connection {
 
 	public Socket socket;
 	public LinkedBlockingQueue<Command> sendQueue = new LinkedBlockingQueue<Command>();
-	
+
 	public Connection(Socket socket) {
 		this.socket = socket;
 	}
@@ -45,4 +46,7 @@
 		}
 	}
 
+	public States closeAndRetry(){
+		return null;
+	}
 }
--- a/src/alice/datasegment/RemoteDataSegmentManager.java	Tue Oct 22 20:02:15 2013 +0900
+++ b/src/alice/datasegment/RemoteDataSegmentManager.java	Mon Nov 04 13:09:14 2013 +0900
@@ -34,7 +34,6 @@
 						logger.info("Connect to " + connection.getInfoString());
 					} catch (IOException e) {
 						try {
-							System.out.println("WAITING");
 							Thread.sleep(50);
 						} catch (InterruptedException e1) {
 							e1.printStackTrace();
@@ -45,7 +44,6 @@
 				new OutboundTcpConnection(connection).start();
 				// if connection failed need to stop these thread 
 				if (connect){
-					System.out.println("send error");
 					new SendError(new HostMessage(hostName, port)).execute();
 				}
 			}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/topology/manager/keeparive/PingScheduler.java	Mon Nov 04 13:09:14 2013 +0900
@@ -0,0 +1,80 @@
+package alice.topology.manager.keeparive;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import alice.codesegment.CodeSegment;
+
+public class PingScheduler extends CodeSegment{
+	private LinkedList<TaskInfo> list = new LinkedList<TaskInfo>();
+	private ArrayList<TaskInfo> regist = new ArrayList<TaskInfo>();
+	public boolean updateFlag = false;
+	public long time = 0;
+	public int taskNum = 0;
+	public TaskInfo nowTask;
+	
+	public List<TaskInfo> nameList(){
+		return regist;
+	}
+	
+	public List<TaskInfo> 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){
+		boolean newTaskFlag = true;
+		for (TaskInfo t : regist){
+			if(newInfo.getName().equals(t.getName())){
+				newInfo.setTaskNum(t.getTaskNum());
+				newTaskFlag = false;
+				break;
+			}
+		}
+		if (newTaskFlag){
+			taskNum +=10;
+			newInfo.setTaskNum(taskNum);
+		}
+		boolean addFlag = true;
+		for (int i = 0;i< list.size(); i++){
+			TaskInfo info = list.get(i);
+			Long t = info.getTime();
+			if (newInfo.getTime() > t ){
+				newInfo.setTime(newInfo.getTime() -t);
+			} else {
+				list.add(i, newInfo);
+				if (i != list.size() -1 ){
+					TaskInfo nextInfo = list.get(i+1);
+					newInfo.setTime(nextInfo.getTime() - newInfo.getTime());
+				}
+				addFlag = false;
+				break;
+			}
+		}
+		if (addFlag)
+			list.add(newInfo);
+		
+		this.updateFlag = true;
+	}
+
+	@Override
+	public synchronized void run() {
+		try {
+			while(true){
+				if(list.size()== 0)System.exit(0);
+				nowTask = list.poll();
+				time = System.currentTimeMillis();
+				this.wait(nowTask.getTime());
+			}
+		} catch (InterruptedException e) {
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/topology/manager/keeparive/SchedulerViewer.java	Mon Nov 04 13:09:14 2013 +0900
@@ -0,0 +1,158 @@
+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();
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/topology/manager/keeparive/StartViewer.java	Mon Nov 04 13:09:14 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);
+		
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/topology/manager/keeparive/TaskInfo.java	Mon Nov 04 13:09:14 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;
+	}
+}