/*
 * Copyright (c) 2003-2005 The BISON Project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package pacemaker;

import peersim.config.*;
import peersim.core.*;
import peersim.util.*;


public class EmissionObserver implements Control
{

/**
 * The param
 */
private static final String PAR_THRESHOLD = "threshold";

private static final String PAR_FROM = "from";

private static final String PAR_UNTIL = "until";

private static EmissionObserver singleton;

private final String prefix;

private final long threshold;

private long lasttime;

private long starttime;

private int count;

private long from;

private long until;

private IncrementalStats stats;

public EmissionObserver(String prefix)
{
	singleton = this;
	this.prefix = prefix;
	this.threshold = Configuration.getInt(prefix + "." + PAR_THRESHOLD);
	this.from = Configuration.getLong(prefix + "." + PAR_FROM, 0);
	this.until = Configuration.getLong(prefix + "." + PAR_UNTIL, Long.MAX_VALUE);
	this.starttime = -1;
	this.count = 0;
	this.stats = new IncrementalStats();
}

public static void addFlash(long time, long period)
{
	singleton.add(time, period);
}

private void add(long time, long period)
{
	if (starttime < 0) {
		starttime = time;
		lasttime = time;
		stats.add(period);
	} else {
		if (time-lasttime < threshold || (lasttime-starttime == 0)) {
			lasttime = time;
			stats.add(period);
		} else {
			if (time >= from && time < until) {
				System.out.println(prefix + 
						": FLASH " + count + 
						" START " + starttime + 
						" LEN " + (lasttime-starttime) + 
						" TOTPER " + (time-starttime) +
						" AVGPER " + stats);
			}
			starttime = time;
			lasttime = time;
			count++;
			stats.reset();
			stats.add(period);
		}
	}
}

public boolean execute()
{
	return false;
}
  

}
