view src/alice/topology/manager/keeparive/TaskExecuter.java @ 321:7de56e995f09

minor change
author one
date Tue, 17 Dec 2013 18:54:47 +0900
parents aafc88985822
children 4b5bf9cf1505
line wrap: on
line source

package alice.topology.manager.keeparive;

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

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

	private TaskExecuter(){}
	public static TaskExecuter getInstance() {
		return instance;
	}

	public void setKey(){
		ids.init();
		info.setKey("_WAITINGLIST");
	}

	@Override
	public synchronized void run() {
		ListManager list = info.asClass(ListManager.class);
		if (list.getTaskList().size() == 0){
			ods.update("_WAITINGLIST", list);
			setKey();
			return;
		}
		nowTask = list.getTaskList().poll();
		ods.update("_WAITINGLIST", list);
		time = System.currentTimeMillis();
		if (nowTask.getSleepTime()!=0){
			try {
				this.wait(nowTask.getSleepTime());
			} catch (InterruptedException e) {}
		}
		if (interruptFlag){				
			interruptFlag = false;
		} else {
			if (nowTask.getType() == TaskType.PING) {
				ods.ping(nowTask.getManagerKey(), nowTask.getReturnKey());
				new RespondPing(nowTask.getReturnKey());
			} else {
				DataSegment.get(nowTask.getManagerKey()).close();
			}
			
		}
		nowTask = null;
		setKey();
	}
	public synchronized void interrupt(){
		interruptFlag = true;
		notify();
	}

	public TaskInfo getNowTask() {
		return nowTask;
	}

	public long getTime(){
		return time;
	}
}