static
void CompareTestList(My402List *pList1, My402List *pList2, int num_items)
{
    My402ListElem *elem1=NULL, *elem2=NULL;
    int idx=0;

    if (My402ListLength(pList1) != num_items) {
        fprintf(stderr, "List1 length is not %1d in CompareTestList().\n", num_items);
        exit(1);
    }
    if (My402ListLength(pList2) != num_items) {
        fprintf(stderr, "List2 length is not %1d in CompareTestList().\n", num_items);
        exit(1);
    }
    for (elem1=My402ListFirst(pList1), elem2=My402ListFirst(pList2);
            elem1 != NULL && elem2 != NULL;
            elem1=My402ListNext(pList1, elem1), elem2=My402ListNext(pList2, elem2), idx++) {
        int ival1=(int)(elem1->obj);
        int ival2=(int)(elem2->obj);

        if (ival1 != ival2) {
            fprintf(stderr, "(%1d,%1d): item %1d not identical in CompareTestList().\n", ival1, ival2, idx);
            exit(1);
        }
    }
}
void My402ListUnlinkAll(My402List *pList){
	My402ListElem *elem = NULL;
	for(elem = My402ListFirst(pList); elem != NULL; elem = My402ListNext(pList, elem)){
    	My402ListUnlink(pList, elem);
    }
	pList->num_members = 0;
}
My402ListElem *My402ListFind(My402List *pList, void *obj){
	My402ListElem *elem = NULL;
    for (elem = My402ListFirst(pList); elem != NULL; elem = My402ListNext(pList, elem)){
    	if(elem->obj == obj) return elem;
    }
    return NULL;
}
int main(){
	
	int i=100;
	int j=0;
	My402List list;
	//========== initialization part starts==========	
	memset(&list,0,sizeof(My402List));	
	My402List *pList = NULL;	
	pList = &list;		
	My402ListElem *ptrToAnchor = &(pList->anchor);
	ptrToAnchor->next = ptrToAnchor;
	ptrToAnchor->prev = ptrToAnchor;
	//=========== initialization part ends============	
	My402ListElem *current = ptrToAnchor;
	for(; j<10;j++){
		My402ListInsertAfter(&list,(void *)&i,current);	
		i= i+10;
		current = current->next;
	}
	NL
	printf("Appneded the whole list");
	NL	
		
	for(current=My402ListFirst(pList);
		current != NULL;
		current=My402ListNext(pList,current)	
		){
		
		printf("%d -->", *((int *)current->obj));	
	}	
	NL
}
Example #5
0
void insert_list(My402List * list, My402TransData * data)
{
	if(My402ListEmpty(list))
		(void)My402ListAppend(list, (void*)data);
	else
	{
		My402ListElem * elem = My402ListFirst(list);
		while(elem)
		{
			My402TransData * temp = (My402TransData *)(elem -> obj);
			if(temp -> timestamp < data -> timestamp)
			{
				elem = My402ListNext(list, elem);
				if(!elem)
					(void)My402ListAppend(list, (void*)data);
			}
			else if(temp -> timestamp > data -> timestamp)
			{
				(void)My402ListInsertBefore(list, (void*)data, elem);
				break;
			}
			else
				print_error("two identical timestamp");
		}
	}
}
Example #6
0
void printOutput(My402List *pList){

	int line_number = 0;
	int i=0;
	My402Output output;
	memset(&output, '\0', sizeof(My402Output));
	My402Output *pOutput = &output; 
	
	long long *pBalance = (long long *)malloc(sizeof(long long));
	memset(pBalance, 0, sizeof(long long)) ;
	if(pBalance == NULL){
		fprintf(stderr, "\nUnable to allocate memory");	
	}

	My402ListElem *current = NULL;
	/*for(; i<pList->num_members; i++){
		output[i] = (char *)malloc(80*sizeof(char));	
		memset(output[i], '\0', 80*sizeof(char));	
	}*/
	printf("\n");	
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	printf("|%7cDate%6c|%1cDescription%14c|%9cAmount%1c|%8cBalance%1c|\n",32,32,32,32,32,32,32,32);
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	for(current=My402ListFirst(pList);
		current!=NULL;
		current=My402ListNext(pList, current)){
		
		formatEachField((My402SortElem *)current->obj, pOutput, pBalance);
		line_number++;	
		printf("|%1c%s%1c|%1c%s%c|%c%s%1c|%c%s%c|\n",32,pOutput->printDate,32,32,pOutput->printDesc,32,32,pOutput->printAmount,32,32,pOutput->printBalance,32);	
	}	
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	
}
Example #7
0
My402ListElem *My402ListPrev(My402List* q, My402ListElem* n)
{
    if (n==My402ListFirst(q))
        return NULL;
    else
        return n->prev;
}
Example #8
0
void server_procedure(struct command_line_args *object)
{
	struct packet *p;
	My402ListElem *elem = NULL;
	struct timespec tim;
	double time;
	long time_diff_in_nsec;
	int i = 0;
	while( i < object->no_of_packets && !EndServerThread) 
	{
		pthread_mutex_lock(&token_bucket);
		while(My402ListEmpty(&Q2PacketList)&&!EndServerThread)
			pthread_cond_wait(&is_q2_empty,&token_bucket);

		if(EndServerThread == 1)
		{
			pthread_mutex_unlock(&token_bucket);
			break;
		}

		elem = My402ListFirst(&Q2PacketList);
		if(elem == NULL)
		{
			pthread_mutex_unlock(&token_bucket);
			break;
		}
		p = (struct packet *)elem->obj;
		My402ListUnlink(&Q2PacketList, elem);

		pthread_mutex_unlock(&token_bucket);
	
		gettimeofday(&(p->Q2leaves), NULL);
		time = (TIME_IN_USEC(p->Q2leaves) - TIME_IN_USEC(GlobalStartTime))/1000;
		p->time_in_Q2 = (TIME_IN_USEC(p->Q2leaves) - TIME_IN_USEC(p->Q2timestamp))/1000;

		LOG(stdout, "%012.3fms: p%d begin service at S, time in Q2 = %.3fms\n",time,p->packet_id,p->time_in_Q2);
 
		time_diff_in_nsec = (long)((((p->precise_packet_service_time)/1000) - (p->service_time/1000))*1000000000L);
		tim.tv_sec = (p->service_time)/1000;
		tim.tv_nsec = time_diff_in_nsec;
	
		nanosleep(&tim, NULL);
	
		gettimeofday(&(p->Leaves_server), NULL);
		time = (TIME_IN_USEC(p->Leaves_server) - TIME_IN_USEC(GlobalStartTime))/1000;
		p->time_in_system = (TIME_IN_USEC(p->Leaves_server) - TIME_IN_USEC(p->Arrival_timestamp))/1000;
		p->precise_packet_service_time = (TIME_IN_USEC(p->Leaves_server) - TIME_IN_USEC(p->Q2leaves))/1000;
		LOG(stdout, "%012.3fms: p%d departs from S, service time = %.3fms, time in system = %.3fms\n",time,p->packet_id,p->precise_packet_service_time,p->time_in_system);
		completed_packets ++;
		calculate_stats(p);
		if((packet_count == object->no_of_packets) &&(completed_packets == (packet_count-discarded_packets)) && My402ListEmpty(&Q2PacketList))
		{
			EndServerThread = 1;
			pthread_cond_signal(&is_q2_empty);
		}
		i++;
	}
	pthread_exit(NULL);
	
}
static
void BubbleSortForwardList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
    int i=0;

    if (My402ListLength(pList) != num_items) {
        fprintf(stderr, "List length is not %1d in BubbleSortForwardList().\n", num_items);
        exit(1);
    }
    for (i=0; i < num_items; i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *next_elem=NULL;

        for (elem=My402ListFirst(pList), j=0; j < num_items-i-1; elem=next_elem, j++) {
            int cur_val=(int)(elem->obj), next_val=0;

            next_elem=My402ListNext(pList, elem);
            next_val = (int)(next_elem->obj);

            if (cur_val > next_val) {
                BubbleForward(pList, &elem, &next_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
Example #10
0
My402ListElem * My402ListPrev(My402List * list, My402ListElem * cur)
{
    if(cur == My402ListFirst(list))
        return NULL;
    else
        return cur -> prev;
}
static
void BubbleSortForwardList(My402List *pList)
{
    My402ListElem *elem=NULL;
    int i=0;

    if (My402ListLength(pList) == 0) {
        fprintf(stderr, "List is empty!");
        PrintErrorMessage();
        exit(1);
    }
    for (i=0; i < My402ListLength(pList); i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *next_elem=NULL;

        for (elem=My402ListFirst(pList), j=0; j < My402ListLength(pList)-i-1; elem=next_elem, j++) {
           unsigned int cur_val=(unsigned int)((TransactionElem *)(elem->obj))->eleTime, next_val=0;

            next_elem=My402ListNext(pList, elem);
            next_val = (unsigned int)((TransactionElem *)(next_elem->obj))->eleTime;

            if (cur_val > next_val) {
                BubbleForward(pList, &elem, &next_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
Example #12
0
void PrintAll(My402List *List)
{
    printf("+-----------------+--------------------------+----------------+----------------+\n");
    printf("|       Date      | Description              |         Amount |        Balance |\n");
    printf("+-----------------+--------------------------+----------------+----------------+\n");
    My402ListElem *toPrint = My402ListFirst(List);
    while(toPrint != NULL)
    {
        My402ListElemObj *Pnt = (My402ListElemObj*)toPrint->obj;
        printf("|");
        time_t *timep = &(Pnt->TTime);
        print_TTime(timep);
        printf("|");

        printf(" %-24s ", Pnt->TDesc);
        printf("|");

        print_TAm(Pnt->TAm, Pnt->TSign);
        printf("|");

        print_bal(Pnt->TAm,Pnt->TSign);

        printf("|\n");
        //nextone;

        toPrint = My402ListNext(List,toPrint);

    }
    printf("+-----------------+--------------------------+----------------+----------------+\n");
}
Example #13
0
int ExistTimestamp(My402List *pList,int timeval)
{
  My402ListElem *elem=NULL;
  /* Return FALSE if list is empty*/
  if (pList->num_members == 0)
  {
    return FALSE;
  }
  else
  {
    /* Traverse the list for finding the element
       return the element if the object is found
    */
    for (elem=My402ListFirst(pList);
         elem != NULL;
         elem=My402ListNext(pList, elem)) {
            if (((trnx*)(elem->obj))->timeval == timeval)
            {
              return TRUE;
            }
       }

    return FALSE;
  }
}
Example #14
0
static
void BubbleSort(My402List *pList)
{
    My402ListElem *elem=NULL;
    int i=0;
    int num_items = My402ListLength(pList);
    for (i=0; i < num_items; i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *next_elem=NULL;

        for (elem=My402ListFirst(pList), j=0; j < num_items-i-1; elem=next_elem, j++) {
          struct transaction_info *cur_val=(struct transaction_info *)&elem->obj;
          struct transaction_info *next_val;

            next_elem=My402ListNext(pList, elem);
            next_val = (struct transaction_info *)&next_elem->obj;
            int *a = (int *)cur_val->t_date;
            int *b = (int *)next_val->t_date;
            //printf("%d %d\n",*a,*b );


            if (*a > *b) {
              //printf("ok\n");
              BubbleSwap(pList, &elem, &next_elem);
              something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
Example #15
0
 int  My402ListPrepend(My402List* list, void* obj)
 {
	 My402ListElem* first, *elem;
	 first = My402ListFirst(list);
	 if(first == NULL)
		first = &list->anchor;
	 
	elem = (My402ListElem*)malloc(sizeof(My402ListElem));
	 if(NULL == elem)
	 {
		 //error??
		 return FALSE;
	 }
	 elem->obj = obj;

	 list->anchor.next = elem;
	 elem->prev = &list->anchor;

	 elem->next = first;
	 first->prev = elem;
	 ++list->num_members;
	 return TRUE;
	 
	 return FALSE;
 }
// Returns previous element else NULL if element is first item on list
My402ListElem *My402ListPrev(My402List* myList, My402ListElem* listElement) { 
	if(listElement != My402ListFirst(myList)) {
		return listElement->prev;
	} else {
		return NULL;
	}
}
int  My402ListPrepend(My402List *list, void *obj)
{
if(list==NULL)
	return FALSE;
//the new elemenet to be prepended
My402ListElem *listelem = NULL;
listelem = (My402ListElem *) malloc( sizeof(My402ListElem) );
if(listelem == NULL)
	return FALSE;
//if list is empty just alter anchor ptrs
if(list->num_members == 0){
	list->anchor.next = listelem;
	list->anchor.prev = listelem;
	listelem->obj = (void*) obj;
	//store anchor address as it is not a ptr type
        listelem->next = &(list->anchor);
        listelem->prev = &(list->anchor);
        list->num_members++;
}
//Just prepend the new element to the first element
else{
	My402ListElem *firstelem = My402ListFirst(list);
	if (firstelem != NULL){
		firstelem->prev = listelem;
		list->anchor.next = listelem;
		listelem->prev = &(list->anchor);
		listelem->next = firstelem;
		listelem->obj = (void*) obj;
		list->num_members++;
	}
}
return TRUE;
}
Example #18
0
int My402ListPrepend(My402List * list, void * data)
{
    // Create the new node
    My402ListElem * elem;
    elem = (My402ListElem *)malloc(sizeof(My402ListElem));
    if(!elem) // Allocate memory failed
        return 0;
    elem -> obj = data;
    // Insert the node
    My402ListElem * first_elem = My402ListFirst(list);
    if(first_elem)
    {
        elem -> prev = &(list -> anchor);
        elem -> next = first_elem;
        (list -> anchor).next = elem;
        first_elem -> prev = elem;
        list -> num_members++;
    }
    else // List is empty
    {
        elem -> next = &(list -> anchor);
        elem -> prev = &(list -> anchor);
        (list -> anchor).next = elem;
        (list -> anchor).prev = elem;
        list -> num_members++;
    }
    return 1;
}
Example #19
0
void interrupt_main()
{
    //printf("<-----CTR+C RECIEVED------>\n");
    SERVER_DIE = 1;
    pthread_kill(TOKEN, SIGUSR1);
    pthread_kill(PACKET, SIGUSR2);

    pthread_mutex_lock(&m);
    //printf("Inside mutex of Man\n");
    while(!My402ListEmpty(Q2)){
        My402ListElem *elem = My402ListFirst(Q2);
        My402dataElem *topEle = (My402dataElem*)(elem->obj);
        My402ListUnlink(Q2, elem);

        TIME_AT_Q1 = diff_timeval(TIME_AT_Q1, topEle->q1duration);
    }
    /* Clear the list */
    My402ListUnlinkAll(Q1);
    pthread_mutex_unlock(&m);

    pthread_cond_broadcast(&cond_t);

    //printf("<-----MAIN DYING------>\n");
    pthread_exit(0);
}
Example #20
0
extern int  My402ListPrepend(My402List* List, void* insData)
 {
   My402ListElem *newNode = (My402ListElem *) malloc(sizeof(My402ListElem));

   if(newNode != NULL)
    {
        newNode->obj = insData;
        if(My402ListEmpty(List))
            {
                List->anchor.next = newNode;
                List->anchor.prev = newNode;
                newNode->next = &(List->anchor);
                newNode->prev = &(List->anchor);
                (List->num_members)++;
                return TRUE;
            }
        else
            {
                My402ListElem *firstNode = My402ListFirst(List);
                List->anchor.next = newNode;
                newNode->next = firstNode;
                firstNode->prev = newNode;
                newNode->prev = &(List->anchor);
                (List->num_members)++;
                return TRUE;
            }
    }
     return FALSE;

 }
Example #21
0
static
void BubbleForward(My402List *pList, My402ListElem **pp_elem1, My402ListElem **pp_elem2)
    /* (*pp_elem1) must be closer to First() than (*pp_elem2) */
{
    My402ListElem *elem1=(*pp_elem1), *elem2=(*pp_elem2);
    void *obj1=elem1->obj, *obj2=elem2->obj;
    My402ListElem *elem1prev=My402ListPrev(pList, elem1);
/*  My402ListElem *elem1next=My402ListNext(pList, elem1); */
/*  My402ListElem *elem2prev=My402ListPrev(pList, elem2); */
    My402ListElem *elem2next=My402ListNext(pList, elem2);

    My402ListUnlink(pList, elem1);
    My402ListUnlink(pList, elem2);
    if (elem1prev == NULL) {
        (void)My402ListPrepend(pList, obj2);
        *pp_elem1 = My402ListFirst(pList);
    } else {
        (void)My402ListInsertAfter(pList, obj2, elem1prev);
        *pp_elem1 = My402ListNext(pList, elem1prev);
    }
    if (elem2next == NULL) {
        (void)My402ListAppend(pList, obj1);
        *pp_elem2 = My402ListLast(pList);
    } else {
        (void)My402ListInsertBefore(pList, obj1, elem2next);
        *pp_elem2 = My402ListPrev(pList, elem2next);
    }
}
Example #22
0
void* sender(void *v){
    // Iterate the data list and send data
    My402ListElem *elem=NULL;
    bool is_retransmitted = false;
    vlong num_packets = 0;

    busy_wait_for_sync();

    gettimeofday(&globals.a_sender_start, NULL);

    printf("[SUMMARY] Start Sending.....\n");
    for (elem=My402ListFirst(&globals.datal); elem != NULL;
            elem=My402ListNext(&globals.datal, elem)) {
        struct node *data_node = (elem->obj);
        int n = send_packet(data_node, is_retransmitted);
        num_packets++;
        if (n < 0) {
            perror("Error on send");
            exit(1);
        }
    }
    printf("[SUMMARY] Sender has tried sending complete file.....\n");

    // The nodeA knows that last bit is send
    globals.last_bit_send = true;

    // Send dummy data denotes the end of sending data
    send_dummy_packet();
    printf(KGRN "[SUMMARY] Start time : %llu ms, Packets : %llu, Retransmission : %llu, Nack recv : %llu \n" RESET,
            to_milli(globals.a_sender_start), num_packets, globals.total_retrans,globals.total_nack_recv);
    fflush(stdout);
}
//Deletes all the elements from the list
void My402ListUnlinkAll(My402List* myList) { 
	My402ListElem *listElement = NULL;
	int i = 0;
	for(;i<myList->num_members;i++) {
		listElement = My402ListFirst(myList);
		My402ListUnlink(myList,listElement);
	}
}
void My402ListUnlinkAll(My402List* list)
{
	My402ListElem *curr_elem, *next_elem;
	for (curr_elem = My402ListFirst(list), next_elem = curr_elem; curr_elem != 0; curr_elem = My402ListNext(list, next_elem), next_elem = curr_elem) {
		free((void*)curr_elem);
	}
	list->num_members = 0;
}
My402ListElem *My402ListFind(My402List* list, void* obj)
{
	My402ListElem* temp_elem;
	for( temp_elem = My402ListFirst(list); temp_elem != 0; temp_elem = My402ListNext(list, temp_elem)) {
		if(temp_elem->obj == obj) break;
	}
	return temp_elem;
}
Example #26
0
void printList(My402List *pList){

	My402ListElem *current = NULL;
	for(current=My402ListFirst(pList); current != NULL; current = My402ListNext(pList, current)){
	
		printObj((My402SortElem *)current->obj);
	}
}
Example #27
0
/**
 * @brief Print nodes of list
 */
void print_list(My402List *list){
    My402ListElem *elem=NULL;
    for (elem=My402ListFirst(list); elem != NULL; elem=My402ListNext(list, elem)) {
        struct node *data_node = (elem->obj);
        //printf("[%p] : PRINT SEQ = %llu, DATA = %s\n", data_node->mem_ptr, data_node->seq_num, data_node->mem_ptr);
        DBG("NACK : SEQ = %llu", data_node->seq_num);
    }
}
Example #28
0
void printList(My402List *pList){

	My402ListElem *current = NULL;
	for(current=My402ListFirst(pList); current != NULL; current = My402ListNext(pList, current)){
	
		printObj((My402SortElem *)current->obj);
	}
	printf("\n done with the printing....\n");
}
Example #29
0
/**
 * @brief Get the current nack list
 *        that is less the max read sequence number
 */
void get_current_nack_list(){
    My402ListElem *elem=NULL;
    for (elem=My402ListFirst(&globals.nackl);
        elem != NULL && ((struct node*)(elem->obj))->seq_num < globals.current_seq;
        elem=My402ListNext(&globals.nackl, elem)) {
        struct node *data_node = (elem->obj);
        DBG("[%p] : SEQ = %llu", elem->obj, data_node->seq_num);
    }
}
Example #30
0
/* Process the pakcets in Q1 and transfer to Q2 when enough tokens */
void ProcessPacket(){
    My402ListElem *find = NULL;
    Packet_desc *p = NULL;
    
    find=My402ListFirst(&Q1_node);
    
    if(find->obj == NULL){
        fprintf(stderr,"Error: Obj is NULL");
        pthread_exit(0);
    }
    
    p = (Packet_desc*)find->obj;
    
    
    if(p->tokens <= token_limit){
        // move this to Q2
        
        token_limit -= p->tokens;
        
        // total time in Q1
        
        p->total_Q1_time = GetMTimeOfDay()- p->Q1_time_enters;
        
        
        fprintf(stdout, "%012.3lfms: p%d leaves Q1, time in Q1 = %.3lfms, tokens bucket now has %d token\n",GetMTimeOfDay(),p->name_ID,p->total_Q1_time,token_limit);
        
        
        if(My402ListEmpty(&Q2_node)){
            // signal the condition about the queue not empty
            p->Q2_time_enters = GetMTimeOfDay();
            // Q2_time_enters[p->name_ID] = GetMTimeOfDay();
            fprintf(stdout, "%012.3lfms: p%d enters Q2\n",GetMTimeOfDay(),p->name_ID);
            
            My402ListAppend(&Q2_node, (void*)p);
            curr_q2_size = 1;
            /* signal the condition variable queue*/
            pthread_cond_signal(&serverQ);
            find = My402ListFind(&Q1_node, (void*)p);
            My402ListUnlink(&Q1_node, find);
            
        }else{
            p->Q2_time_enters = GetMTimeOfDay();
            
            //   Q2_time_enters[p->name_ID] = GetMTimeOfDay();
            
            fprintf(stdout, "%012.3lfms: p%d enters Q2\n",GetMTimeOfDay(),p->name_ID);
            
            My402ListAppend(&Q2_node, (void*)p);
            /* gaurd set true */
            curr_q2_size = 1;
            find = My402ListFind(&Q1_node, (void*)p);
            My402ListUnlink(&Q1_node, find);
            
        }
        
    }
}