package market.thread;

import market.*;
import market.util.*;

/**
 * Control all modules for grab source
 * Creation date: (14.4.2000 “. 19:44:01)
 * @author: tin
 */
public class Graber extends ThreadGroup {
	private Netrunner mNetrunner;
	public LangTable langTable; // This is a table whit Language
	public SrcElement baseSrcElem;
	public int grabNum;
	public static Queue_rate wr_rw_Count_Stream;
	public static Wr_Rw_RateMeter mRateMeter = null;

	public long startTime;
	public long stopTime;
	// Number of WRProcessors	
	public int numWRProcessors = -1;

/**
 * GraberGroup constructor comment.
 * @param num int ( Graber number )
 * @param baseSrcElem market.util.SrcElement 
 * @param langTable LangTable
 */
public Graber( int num, SrcElement baseSrcElem, LangTable langTable, Wr_Rw_RateMeter rateMeter ) {
	super("Graber"+num);
	grabNum = num;
	this.langTable = langTable;
	this.baseSrcElem = baseSrcElem;
	this.mRateMeter = rateMeter;
	wr_rw_Count_Stream = rateMeter.getQueue();
	
}
/**
 * Graber constructor comment.
 * @param name java.lang.String
 */
public Graber(String name) {
	super(name);
}
/**
 * Graber constructor comment.
 * @param parent java.lang.ThreadGroup
 * @param name java.lang.String
 */
public Graber(ThreadGroup parent, String name) {
	super(parent, name);
}
/**
 *This method call from WrProcessors when it finish.
 * Creation date: (31.7.00 11:57:16)
 */
public void finish() {
	numWRProcessors--;
	if (0 == numWRProcessors) {
		baseSrcElem.processing = false;
		stopTime = System.currentTimeMillis();
		long procTime = (stopTime - startTime) / 1000; // Time for procced baseSrc in sec
		baseSrcElem.procTime = procTime;
		Netrunner.sendFinishSrcAccount( grabNum, baseSrcElem.srcKey, procTime, mNetrunner.numSubSrc );
		ConfigMarket.logWriter.log(getName() + ": Finish baseSrc:" + baseSrcElem.srcString + 
											" with downloaded pages: " + mNetrunner.numSubSrc + 
											" for processing time: " + procTime, LogPrinter.INFO);
		ConfigMarket.logWriter.flush(); 
	}
}
/**
 * Return word row speed in word row per second.
 * Creation date: (9.6.00 15:00:31)
 * @return float
 */
public static float getRwRate() {
	if ( null == mRateMeter)
		return 0;
	return mRateMeter.rw_rate;
}
/**
 * Return word row speed in word row per second.
 * Creation date: (9.6.00 15:00:31)
 * @return float
 */
public static float getWrRate() {
	if ( null == mRateMeter)
		return 0;	
	return mRateMeter.wr_rate;
}
/**
 * Init URLGrabber groupe.
 * 1. Init Wr_Rw_RateMeter if already not load
 * 2. Init Netrunner
 * Creation date: (1/2/00 10:30:16 PM)
 */
public void init() {

// 1. Init Wr_Rw_RateMeter if already not load
	try {
		if (null == mRateMeter) {
			wr_rw_Count_Stream = new Queue_rate( 30 );
			mRateMeter = new Wr_Rw_RateMeter(this, wr_rw_Count_Stream);
			wr_rw_Count_Stream.setReaderThread( mRateMeter );
			mRateMeter.setPriority(Const.RATE_METER_PRIORITY);
		}
	} catch (Exception e) {
		ConfigMarket.logWriter.log(getName() + ": Can't create WR_RW_RateMeter!!!", LogPrinter.ERROR);
	}

// 2. Init Netrunner
	try {
		mNetrunner = new Netrunner(grabNum, this, baseSrcElem, langTable);
		mNetrunner.setPriority(Const.NETRUNNER_PRIORITY);
		numWRProcessors = ConfigMarket.number_WrProcessor;
	} catch (Exception e) {
		ConfigMarket.logWriter.log(getName() + ": Can't create Netrunner!!!", LogPrinter.ERROR);
	}
	try {
		mNetrunner.init();
	} catch (Exception e) {
		ConfigMarket.logWriter.log(getName() + ": Can't init Netrunner!!!", LogPrinter.ERROR);
	}
}
/**
 * Return true if Grabber groupe working.
 * Creation date: (31.7.00 13:08:33)
 * @return boolean
 */
public boolean isWorking() {
	if ( numWRProcessors > 0 )
		return true;
	else
		return false;
}
/**
 * Starts the application.
 * @param args an array of command-line arguments
 */
public static void main(java.lang.String[] args) {
	// Insert code to start the application here.
}
/**
 *This method call from WrProcessors when it finish.
 * Creation date: (31.7.00 11:57:16)
 */
public void sendError() {
	baseSrcElem.error = true;
}
/**
 * Start grabing URL.
 * Creation date: (14.4.2000 “. 20:22:49)
 */
public void start() {
	
	mNetrunner.start();
	baseSrcElem.processing = true;
	startTime = System.currentTimeMillis();

}
}

