/*****************************************************************************
* 	Project 	: Network Based Message Queue System		    **
*	Course		: CIS 650 - Software Engineering		    **
*	Location 	: /research/paraducks3/courses/cis650/proj/rt	    **
*	File		: rtlconn.cc					    **
*	Description 	: List of all connections.			    **
*****************************************************************************/
#include <rtincl.h>

ListOfConnectionsItem::ListOfConnectionsItem(int sfd)          
/* creates a list item with socket*/
{                                    /* file descriptor equal to sfd */
  sockFD=sfd;                        /* pointer to next list element is null */
  next=0;
}

ListOfConnectionsItem::ListOfConnectionsItem()                
 /* creates a dummy list item */
{                                    /* pointer to next list element is null */
  next=0;
}


ListOfConnections::ListOfConnections()                        
/* creates a list with head and tail null*/
{                                   /* the current cursor is also null */
  counter=first=last=0;             /* cardinality is 0 as there are no items*/
  cardinality=0;
}
 
ListOfConnections::~ListOfConnections() 
{                     /*deletes the pointers one by one */
  ListOfConnectionsItem* listItem=first;
  while (listItem != 0)
    { 
      first=first->next;
      delete(listItem);
      listItem=first;
    }
  return;
}

void ListOfConnections::add_item(int sockID)    
/* add an item to the tail of the list */
{
  ListOfConnectionsItem *listItem=new ListOfConnectionsItem(sockID);
  if (is_empty()) {first=last=listItem;counter=first;} 
  else {
    last->next=listItem;
    last=listItem;
  }
  cardinality++;
  return;
}

int ListOfConnections::max()                    
/* go thru the entire list and find the */
{                                  /* maximum socket descriptor (assumes no*/
  int max=-1;                    /* duplicates - returns -1 if list is empty*/
  ListOfConnectionsItem* temp=first;           /* and gives error message */
  if (is_empty()) {
                   return -1;}
  else { 
    while (temp!=0) {
      max=((temp->sockFD)>max)?(temp->sockFD):max;
      temp=temp->next;}
    return max;
  }
}       
 
  
void ListOfConnections::delete_item(int key)  
/* delete an item key - a sockID - from*/
{                                /* the list */
  if (first->sockFD==key) {
    if (last==first) {      
      first=first->next;
      last=first;}
    else first=first->next;
    cardinality--;
  }
  else {
    ListOfConnectionsItem  *prev,*temp;
    temp=first;prev=0;
    while (temp!=0) {
      if ((temp->sockFD)==key) {prev->next=temp->next;
			  	if (last==temp) last=prev;
                                cardinality--;
                                return;}
      else { prev=temp;
	     temp=temp->next;
	   }
    }
  }
  return;                         
}

int ListOfConnections::is_empty()          
/* check whether the head of the list is a */
{                             /* null pointer to see whether list  is empty*/
	if (first == (ListOfConnectionsItem *) NULL)
		return 1;
	else 
		return 0;
/*
  return (first==0);
*/
}             
                      
int ListOfConnections::next()              
/* output the next element in the list */
{                             /* return -1 when the end is reached */
  if (counter==0) { counter=first ;return -1;}
  int temp= counter->sockFD;
  counter=counter->next;

  return temp;
}

void ListOfConnections::reset()            
/* make counter point to the head of the list */ 
{
  counter=first;
}       

int ListOfConnections::number_of_elements () 
{
  return cardinality;
}  


