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

/* 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.
*/

#include "ll-flows.h"
#include <iostream.h>


FlowList:: FlowList()
{ Head = new FList;
  Tail=Head;
  CurrentPtr = Head;
}
FlowList:: ~FlowList()
{ FListPtr temp = Head;
  CurrentPtr = Head;

  while(CurrentPtr != NULL)
  {CurrentPtr = CurrentPtr->Next;
   delete temp;
   temp=CurrentPtr;
  }
}
 
void FlowList::AddANode()
{Tail->Next = new FList;
 Tail=Tail->Next;
}

void FlowList::AddANode(nsaddr_t src, nsaddr_t dest, int flowid, int wait)
{Tail->Next = new FList;
Tail=Tail->Next;
Tail->srcaddr_ = src;
Tail->destaddr_ = dest;
Tail->flowid_ = flowid;
Tail->wait_ = wait;
}

FListPtr FlowList::Previous(FListPtr index)
{FListPtr temp=Head;
 if(index==Head) //special case, index IS the head :)
  { return Head;
  }

 while(temp->Next != index)
 { temp=temp->Next;
 }
 return temp;
}

/*
void FlowList::Advance()
{ if(CurrentPtr->Next != NULL)
  {  CurrentPtr=CurrentPtr->Next;
  }
}

void FlowList::Rewind()
{ if(CurrentPtr != Head)
  { CurrentPtr=Previous(CurrentPtr);
  }
}
*/

void FlowList::DeleteANode(FListPtr corpse)
{ FListPtr temp;

  if(corpse == Head) //case 1 corpse = Head
   {temp=Head;
    Head=Head->Next;
    delete temp;
   }
  else if(corpse == Tail) //case 2 corpse is at the end
  { temp=Previous(corpse);
    temp->Next=NULL;
    delete corpse;
    Tail=temp;
  }
  else //case 3 corpse is in middle somewhere
  {temp=Previous(corpse);
   temp->Next=corpse->Next;
   delete corpse;
  }

  CurrentPtr=Head; //Reset the class tempptr
}

void FlowList::DeleteANode(nsaddr_t src, nsaddr_t dest, int flowid)
{ FListPtr temp, corpse;
  corpse = GetNode(src, dest, flowid);
  if(corpse == Head) //case 1 corpse = Head
   {temp=Head;
    Head=Head->Next;
    delete temp;
   }
  else if(corpse == Tail) //case 2 corpse is at the end
  { temp=Previous(corpse);
    temp->Next=NULL;
    delete corpse;
    Tail=temp;
  }
  else //case 3 corpse is in middle somewhere
  {temp=Previous(corpse);
   temp->Next=corpse->Next;
   delete corpse;
  }

  CurrentPtr=Head; //Reset the class tempptr
}


void FlowList::wait(nsaddr_t src, nsaddr_t dest, int flowid, int wait)
{ FListPtr t=Head;
  while (t!=NULL){
  	if ((t->srcaddr_ == src) && (t->destaddr_==dest) && (t->flowid_==flowid)){
		t->wait_ = wait;
		break;
  	}
  	t=t->Next;
  }
  return;
}

FListPtr FlowList::GetNode(nsaddr_t src, nsaddr_t dest, int flowid)
{ FListPtr t=Head;
  while (t!=NULL){
  	if ((t->srcaddr_ == src) && (t->destaddr_==dest) && (t->flowid_==flowid)){
		return t;
  	}
  	t=t->Next;
  }
  return NULL;
}

FListPtr FlowList::GetNode(nsaddr_t dest)
{ FListPtr t=Head;
  while (t!=NULL){
  	if (t->destaddr_==dest){
		return t;
  	}
  	t=t->Next;
  }
  return NULL;
}




