view src/alice/topology/manager/keeparive/PingScheduler.java @ 310:f1cd920ea913

recreate "PingScheduler" with CS and DS
author sugi
date Tue, 26 Nov 2013 16:46:26 +0900
parents 797267843126
children
line wrap: on
line source

package alice.topology.manager.keeparive;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;

public class PingScheduler extends CodeSegment{
	private Receiver info = ids.create(CommandType.TAKE);
	private TaskInfo nowTask;
	private boolean interruptFlag = false;
	private long time = 0;

	static class SingletonHolder {
		private static PingScheduler Instance = new PingScheduler();		
	}
	public static PingScheduler getInstance(){
		return SingletonHolder.Instance;
	}
	public void setKey(){
		info.setKey("_WAITINGLIST");
	}
	
	@Override
	public synchronized void run() {
		ListManager list = info.asClass(ListManager.class);
		try {
			nowTask = list.getTaskList().pollFirst();
			ods.update("_WAITINGLIST", list);
			time = System.currentTimeMillis();
			if (nowTask.getSleepTime()!=0)
				this.wait(nowTask.getSleepTime());
			if (interruptFlag){				
				interruptFlag = false;
			} else {
				ods.ping(nowTask.getManagerKey(), nowTask.getReturnKey());
			}
		} catch (InterruptedException e) {		
		}
		PingScheduler.getInstance().setKey();
	}
	public synchronized void interrupt(){
		interruptFlag = true;
		notify();
	}
	
	public TaskInfo getNowTask() {
		return nowTask;
	}
	
	public long getTime(){
		return time;
	}
}