/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */

/* Modified and extended by Pablo Martin and Paula Ballester,
 * Strathclyde University, Glasgow.
 * June, 2003.
*/

/* Copyright (c) 2003 Strathclyde University of Glasgow, Scotland.
 * 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 and binary code must contain
 * the above copyright notice, this list of conditions and the following
 * disclaimer.
 *
 * 2. All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 * This product includes software developed at Strathclyde University of
 * Glasgow, Scotland.
 *
 * 3. The name of the University may not be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 * STRATHCLYDE UNIVERSITY OF GLASGOW, MAKES NO REPRESENTATIONS
 * CONCERNING EITHER THE MERCHANTABILITY OF THIS SOFTWARE OR THE
 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software
 * is provided "as is" without express or implied warranty of any kind.
*/

/* By Sandeep Kumar, Kopparapu Suman and Richa Jain,
 * Indian Institute of Technology, Bombay.
 * June, 2001.
*/

/* Copyright (c) 2001 Indian Insitute of Technology, Bombay.
 * 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 and binary code must contain
 * the above copyright notice, this list of conditions and the following
 * disclaimer.
 *
 * 2. All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 * This product includes software developed at Indian Insitute of
 * Technology, Bombay.
 *
 * 3. The name of the Institute may not be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 * INDIAN INSTITUTE OF TECHNOLOGY, BOMBAY, MAKES NO REPRESENTATIONS
 * CONCERNING EITHER THE MERCHANTABILITY OF THIS SOFTWARE OR THE
 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software
 * is provided "as is" without express or implied warranty of any kind.
*/

#ifndef ns_rlc_umts_nodeb_h
#define ns_rlc_umts_nodeb_h

#include "marshall.h"
#include <delay.h>
#include <connector.h>
#include <packet.h>
#include <random.h>
#include <mac.h>
#include "rlc-umts.h"
#include <queue.h>
#include <classifier.h>

#define MAX_LOAD	(MAX_FLOWS * MAX_NUM_UE)	//max of flows that can support

/*************************** Timers *****************************/

class RlcUmtsNodeb;
class rTxNodebTimer : public Handler {
public:
	rTxNodebTimer(RlcUmtsNodeb* m, double s = 0) : rlc(m), Next(0) {
		busy_ = paused_ = 0;stime = rtime = 0.0; flow_ = -1; src_ = -1;}

	virtual void handle(Event *e);
	virtual void start(double time);
	virtual void stop(void);
	virtual void pause(void) { assert(0); }
	virtual void resume(void) { assert(0); }

	RlcUmtsNodeb	 	*rlc;
  	int 		flow_;
	nsaddr_t	src_;
	int		busy_;
	int		paused_;
	Event		intr;
	double	stime;	// start time
	double	rtime;	// remaining time
	rTxNodebTimer* 	Next;
};

typedef rTxNodebTimer* rTxNodebTimerPtr;

class rTxNodebList
{ public:
  rTxNodebList(RlcUmtsNodeb* m) : rlc(m) {
   	Head = new rTxNodebTimer(rlc); Tail=Head; CurrentPtr = Head;
  }
  ~rTxNodebList();
  rTxNodebTimerPtr Previous(rTxNodebTimerPtr);
  void AddANode(nsaddr_t src, int flow);
  void DeleteANode(rTxNodebTimerPtr);
  void DeleteANode(nsaddr_t src, int flow);
  rTxNodebTimerPtr GetNode(nsaddr_t src, int flow);

  void start(nsaddr_t src, int flow, double time);
  void stop(nsaddr_t src, int flow);

  RlcUmtsNodeb *rlc;
  rTxNodebTimerPtr Head,Tail,CurrentPtr;
};

/******************************* Class RlcUmts ***********************************/

struct flows_nodeb {
	int flow_;
	int src_;
	int fraged_;
	int acked_;
	int seqno_;				// link-layer sequence number
	int ackno_;				// ACK received so far
	int rackno_;			// seq no of left most pkt
	int window_;			// window size for sack
	PacketQueue* Txbuf_;		// Tx buffer
	PacketQueue* Rxbuf_;		// Rx buffer
	int unackseqno_;
	int inseq_;
	int numdups_;
	Packet* lastRx_;			// last packet received in sequence
	int handover_;
};


class RlcUmtsNodeb : public LinkDelay {
public:
	RlcUmtsNodeb();
	virtual void recv(Packet* p, Handler* h);
	void handle(Event* e) { recv((Packet*)e, 0); }
	inline int initialized() {
		return (uptarget_ && downtarget_);
	}

	int look_for(int flow, nsaddr_t src);
	virtual void recvACK(Packet* p);
	virtual void recvDATA(Packet* p);
	virtual void sendUpDATA(Packet* p, int pos);
	virtual void enqueDATA(Packet* p, int pos);
	virtual void sendDownDATA(int pos);
	virtual void sendACK(Packet* p, int pos);
	virtual void sendDownDATAonACK(int pos, int seqno);
	void store_flow(int acked, int fraged, int flowid, nsaddr_t src, int handover);
	void remove_flow(int flowid, nsaddr_t src);
	void rTxHandler(nsaddr_t src, int flow);

	inline nsaddr_t& ip_nodeb() { return ip_nodeb_; }
	inline NsObject* downtarget() { return downtarget_; }
      inline NsObject* uptarget() { return uptarget_; }
	inline int rlcfragsize() { return rlcfragsz_; }

protected:
	int command(int argc, const char*const* argv);
	int getip(hdr_ip* hdr);

  	static int rlcverbose_;
	flows_nodeb info_[MAX_LOAD];	// array with each flow information
	PacketQueue* buf_;		//sandy's queue
	nsaddr_t ip_nodeb_;

	NsObject* downtarget_;		// for outgoing packet
	NsObject* uptarget_;		// for incoming packet
	int rlcfragsz_;
	rTxNodebList *rtxnodeb_;			// list of timers to retransmit packets not acknowledged

private:
	int length_;		// length for buf_

};

#endif

