view src/alice/topology/manager/keeparive/SendPing.java @ 316:9e84deb9b1e0

Respond Worked
author sugi
date Tue, 10 Dec 2013 17:40:13 +0900
parents f9ec5bead150
children 32d033a4b3c7
line wrap: on
line source

package alice.topology.manager.keeparive;

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

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

	private SendPing(){}
	public static SendPing 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;
		} else {
			try {
				nowTask = list.getTaskList().poll();
				ods.update("_WAITINGLIST", list);
				System.out.print("managerkey is "+ nowTask.getManagerKey());
				System.out.println(" returnkey is "+ nowTask.getReturnKey());
				time = System.currentTimeMillis();
				if (nowTask.getSleepTime()!=0)
					this.wait(nowTask.getSleepTime());
				if (interruptFlag){				
					interruptFlag = false;
				} else {
					ods.ping(nowTask.getManagerKey(), nowTask.getReturnKey());
					new RespondPing(nowTask.getReturnKey());
				}
			} catch (InterruptedException e) {		
			}
		}
		setKey();
	}
	public synchronized void interrupt(){
		interruptFlag = true;
		notify();
	}

	public TaskInfo getNowTask() {
		return nowTask;
	}

	public long getTime(){
		return time;
	}
}