package system.util;

//import market.util.*;
//import market.*;
/**
 * The RateMeter class
 * Creation date: (07.6.2000 “. 16:46:05)
 * @author: tin
 */
public class RateMeter extends Thread {
	LogPrinter slog;
	private CountElement rateObject;
	public long time_start;
	private long base_time_start;
	private long time_stop;
	private long base_time_stop;
	private final double TO_SEC = 0.001;
	public int rw_count=0;
	public float rw_rate=0;

	public long All_count=0;
	public float Average_rate=0;

	Queue_rate inPipe;

	
	protected boolean working;

/**
 * RateMeter constructor comment.
 * @param num int
 * @param threadGroup java.lang.ThreadGroup
 * @param inPipe market.util.Queue
 */
public RateMeter( ThreadGroup threadGroup, String name, int queueSize , int numElement, LogPrinter log ) {
	super( threadGroup , name );
	slog = log;
	inPipe = new Queue_rate(queueSize);
	inPipe.setReaderThread( this );

	rw_count=0;
	rw_rate=0;

	All_count=0;
	Average_rate=0;
 
}
/**
 * Wr_Rw_rateMeter constructor comment.
 * @param num int
 * @param threadGroup java.lang.ThreadGroup
 * @param inPipe market.util.Queue
 */
public RateMeter( ThreadGroup threadGroup, Queue_rate inPipe) {
	super( threadGroup , "RateMeter" );

	rw_rate=0; 
	this.inPipe=inPipe;
//	canRead= true;
//	init();
}
/**
 * Wr_Rw_rateMeter constructor comment.
 * @param num int
 * @param threadGroup java.lang.ThreadGroup
 * @param inPipe market.util.Queue
 */
public RateMeter( Queue_rate inPipe) {
	super( "RateMeter" );

	rw_rate=0; 
	this.inPipe=inPipe;
//	canRead= true;
//	init();
}
/**
 * Return queue for wr and rw counts.
 * Creation date: (19.6.2000 “. 22:17:59)
 * @return market.util.Queue_rate
 */
public Queue_rate getQueue() {
	return inPipe;
}
/**
 * Insert the method's description here.
 * Creation date: (14.5.2000 “. 18:20:16)
 */
public void init() {
	
	System.out.println("Wr_Rw_Ratemetr....started");
	slog.log( getName()+"Wr_Rw_Ratemetr....started",LogPrinter.DEBUG );		
	
	}
/**
 * Kill V_rw_wr.
 * Creation date: (03.3.2000 ?. 04:34:59)
 */
public void kill() {
	
// Kill V_rw_wr

	stop();
}
/**
 * Insert the method's description here.
 * Creation date: (07.6.2000 “. 18:48:52)
 */
public void rateCalculate(long time) {
	rw_rate = (float) (rw_count / (time * TO_SEC));
	Average_rate += rw_rate;
	All_count += rw_count;
	rw_count = 0;
	slog.log( getName()+": Average Rate: !"+ Average_rate + "___ Rate of Rw:"+ rw_rate, LogPrinter.DEBUG);		
//	System.out.println("Rates: Wr :" + wr_rate + "....Rw :" + rw_rate);
}
/**
 * Insert the method's description here.
 * Creation date: (06.6.2000 ?. 22:19:36)
 */
public void run() {
	working = true;
	int numOfReaded = 0;
	base_time_start = System.currentTimeMillis();

//	System.out.println(time_start);
	//
	while (working) { //It is nessecery
		try {
			while ( inPipe.canRead() ) { //inPipe.canRead    inPipe.numOfElement > inPipe.Num_forWake

				rateObject = (CountElement) inPipe.readObject();
				numOfReaded++;
				rw_count += rateObject.count;

// 				System.out.println(inPipe.canRead + "--" + numOfReaded + "--" + inPipe.numOfElement);

				// If readed object are more than inPipe.Num_forWake
				if (inPipe.Num_forWake <= numOfReaded) {
					time_stop = System.currentTimeMillis();
					rateCalculate(time_stop - time_start);
					time_start = time_stop;

					// if elements if inPipe are less than inPipe.Num_forWake , then suspend
					if (inPipe.numOfElement < inPipe.Num_forWake) {
						sleep_rateMeter();
						numOfReaded = 0;
					}
					//time_start = System.currentTimeMillis();
				}
			}
			sleep_rateMeter();
		} catch (ThreadDeath t) {
			slog.log(getName() + ": Force stop (kill):", LogPrinter.ERROR);
			t.printStackTrace(slog.getPrintWriter());
			working = false;
			return; // exit
		} catch (Throwable t) {
			slog.log(getName() + ": ! Eny error !:" + t, LogPrinter.ERROR);
			t.printStackTrace( slog.getPrintWriter() );	
			working = false;
		}
	}
}
/**
 * Insert the method's description here.
 * Creation date: (07.6.2000 “. 17:39:38)
 */
public void sleep_rateMeter() {
	 suspend();
}
/**
 * Insert the method's description here.
 * Creation date: (15.6.00 18:52:40)
 */
private void stop_rateMeter() {
	base_time_stop=System.currentTimeMillis();
//	rateCalculate(document_time_stop-document_time_start , true);
	stop();
	
}
}

