/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1994 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the Computer Systems
 *	Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @(#) $Header: /usr/local/cvsroot/ns2/queue/drop-tail.h,v 1.3 2003/12/28 13:18:13 rkx Exp $ (LBL)
 */

#ifndef ns_drop_tail_h
#define ns_drop_tail_h

#include <string.h>
#include "queue.h"
#include "config.h"
#include "trace.h"
class LinkDelay;
/*
 * A bounded, drop-tail queue
 */
class DropTail : public Queue {
  public:
	DropTail() : tchan_(0), curq_(0), link_(NULL), ptc_(0.0), 
		ave_(0.0), q_w_(1.0), idle_(1), idletime_(0.0),
		drop_front_(0), summarystats(0),
		qib_(0), dropped_(0), arrived_(0),
		mean_pktsize_(100),
		idle_pktsize_(100) { 
		q_ = new PacketQueue; 
		pq_ = q_;
		bind_bool("drop_front_", &drop_front_);
		bind_bool("summarystats_", &summarystats);
		bind_bool("queue_in_bytes_", &qib_);  // boolean: q in bytes?
		bind("mean_pktsize_", &mean_pktsize_);
		bind("idle_pktsize_", &idle_pktsize_);
		bind("curq_", &curq_);                  // current queue size
		/* rkx */
		bind("ave_", &ave_);                    // average queue size
		bind("q_weight_", &q_w_);                // Q weight
		bind("dropped_", &dropped_); 
		bind("arrived_", &arrived_); 
	}
	~DropTail() {
		delete q_;
	}
  protected:
	void reset();
	int command(int argc, const char*const* argv); 
	void enque(Packet*);
	Packet* deque();
	PacketQueue *q_;	/* underlying FIFO queue */
	int drop_front_;	/* drop-from-front (rather than from tail) */
	int summarystats;
	void print_summarystats();
	int qib_;       	/* bool: queue measured in bytes? */
	int mean_pktsize_;	/* configured mean packet size in bytes */
	        
	Tcl_Channel tchan_;     /* place to write trace records */
        TracedInt curq_;        /* current qlen seen by arrivals */

	/* rkx */
	LinkDelay* link_;	/* outgoing link */
	double ptc_;		/* packet time constant */
        TracedDouble ave_;      /* agerave qlen seen by arrivals */
	double q_w_;		/* Q weight in computing average */
	int idle_pktsize_;	/* configured idle packet size in bytes */
	int idle_;
	double idletime_;
	double estimator(int, int); /* estimate average Q length */
	int	dropped_;	// counter, dropped pkts
	int	arrived_;
        void trace(TracedVar*); /* routine to write trace records */

};


// Congestion Warning queue derived from drop tail queue
// for TCP-Jersey (to appear in JSAC'04)
// -- rkx

class CWDropTail : public DropTail {
public:
	CWDropTail();
	~CWDropTail() { delete q_; }
protected:
	void	enque(Packet*);
	void	reset() {
		dropped_ = 0;
		marked_ = 0;
		arrived_ = 0;
		passed_ = 0;
		DropTail::reset();
	}

	int	thresh_;	// warning threshold
	int	cwmark_;	// mark or drop

	int	marked_;	// counter, marked pkts
	int	passed_;	// counter, forwarded pkts
};

#endif

