package net.kldp.logviewer.work;

import net.kldp.logviewer.View;
import net.kldp.logviewer.common.log.LogViews;
import net.kldp.logviewer.model.LogViewerEntry;

import org.apache.log4j.Logger;

/**
 * Http 방식일 경우 규칙적으로.. 화면을 호출해 준다.
 * 
 * @author hangumkj
 *
 */
public class WorkerHttpTail extends Thread implements Tail {
	private static final Logger logger = LogViews.getLogger(WorkerFileTail.class);
	
	View tailMe = null;
	LogViewerEntry viewEntry = null;
	private volatile boolean shutdownThread = false;
	private volatile boolean sleepThread = false;
    
	public WorkerHttpTail(LogViewerEntry viewEntry, View tailMe) {
		if(logger.isDebugEnabled()) { 
			logger.debug("WorkerHttpTail Started......[name]" + viewEntry.getName());
		}
		
		this.tailMe = tailMe;				
		this.viewEntry = viewEntry;
		
		logger.debug(viewEntry.toString());
	}
	
	/**
	 * 잠시중단
	 */
	public void sleep() {
		sleepThread = true;
		
		try {
			Thread.currentThread().wait();
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 다시시작
	 */
	public void reStart() {
		sleepThread = false;
		
		Thread.currentThread().notify();
	}
	
	/**
	 * 쓰레드 종료
	 *
	 */
	public void shutdown() {
		shutdownThread = true;
		
		interrupt();
	}
	
	public void run() {
		try {		
			while(!shutdownThread) {
	    		// wait 상태
				if(sleepThread) {

				// working 상태					
				} else {
					tailMe.httpRefershData();
					
					// 원하는 시간만큼 딜레이 된다.
					try {
						Thread.sleep(viewEntry.getDelaySecond() * 1000);
					} catch(InterruptedException e) {}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			logger.debug("###[WorkerHttpTail가 쓰레드 작업종료] ");// + Thread.currentThread().getId());
		}
	}
}
