view src/alice/topology/manager/keeparive/SendPing.java @ 314:f1f0638861ab

ping responce receivable
author sugi
date Fri, 06 Dec 2013 11:42:51 +0900
parents 6e0a3c680b82
children f9ec5bead150
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);
			new GetTask();
			return;
		}
		try {
			nowTask = list.getTaskList().pollFirst();
			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) {		
		}
		new GetTask();
	}
	public synchronized void interrupt(){
		interruptFlag = true;
		notify();
	}
	
	public TaskInfo getNowTask() {
		return nowTask;
	}
	
	public long getTime(){
		return time;
	}
}