Skip to content
Browse files

MembershipList data structure, basic testing of all functions have be…

…en done but not strenuous 100% testing
  • Loading branch information...
1 parent 851aedd commit df304361ad5a1e4afd0305a708edb45fef6cbc5c @zoukai957 zoukai957 committed Nov 26, 2012
Showing with 172 additions and 0 deletions.
  1. +124 −0 MembershipList.c
  2. +46 −0 MembershipList.h
  3. +2 −0 configFile.c
View
124 MembershipList.c
@@ -0,0 +1,124 @@
+//NOTE:Though these functions are NOT thread functions, thread functions
+//may call these functions and there is a mutex lock called membershipListMutex
+//that locks down the membershipList when it is in use
+
+#include "MembershipList.h"
+
+
+/**Creates a membershipList and returns what it has created*/
+linkedListHolder membershipListCreate(){
+ return create();
+}
+
+/**Adds member to membership list, if membershipList is NULL it fails
+ * Name - Name(IP address or DNS name [keep consistent you will
+ * need to use the same exact name when removing it from list]) user
+ * wishes to remove
+ * timeIn - number of seconds after January 1, 1970 at midnight new member was discovered;
+ * use time() function to get this value
+ * membershipList - list of members we are adding, must be created using membershipLIstCreate first
+ * returns: 0 on success 1 on failure
+ * NOTE: user is responsible for marking down when new member was discovered as soon as it
+ * is discovered!!!**/
+int addMember(char *Name, int expires, time_t timeIn,linkedListHolder membershipList){
+ //membershipList was not created
+ if(membershipList == NULL){
+ printf("Error: User sent in a bad membershipList\n");
+ return 1;
+ }
+
+ //Creates member
+ memberNode *newMember = (memberNode *)malloc(sizeof(memberNode));
+ newMember->memberName = Name;
+ newMember->timeEntered = timeIn;
+ newMember->timeOut = timeIn + expires;
+
+ //Adds member to membershipList
+ pthread_mutex_lock(&membershipListMutex);
+ add(membershipList, newMember, newMember->timeOut);
+ pthread_mutex_unlock(&membershipListMutex);
+
+ return 0;
+}
+
+/**removes member from membership list, but does not use the linkedList
+ * function to do this for various reasons
+ * memberName - Name (use the exact format used to store the member) of
+ * memberNode that user wants to remove;
+ * membershipList - list containing each proxy in the VLAN
+ * returns:0 on success 1 on failure**/
+int removeMember(char *Name, linkedListHolder membershipList){
+
+ pthread_mutex_lock(&membershipListMutex);
+ if (membershipList==NULL){
+ printf("Error: User sent in a bad membershipList\n");
+ return 1;
+ }
+
+ linkedList ptr = membershipList->head;
+ if (ptr==NULL){
+ printf("Error: List is empty\n");
+ return 1;
+ }
+ do{
+ if(strcmp(Name, ((memberNode*)ptr->data)->memberName)==0){
+ if(membershipList->head->next == membershipList->head){
+ free((memberNode*)ptr->data);
+ removeNode(membershipList, ptr->key);
+ return 0;
+ }
+ ptr->prev->next = ptr->next;
+ ptr->next->prev = ptr->prev;
+ if(ptr == membershipList->head){
+ membershipList->head = membershipList->head->next;
+ }
+ free(ptr);
+ return 0;
+ }
+ ptr = ptr->next;
+ }while(ptr!=membershipList->head);
+ pthread_mutex_unlock(&membershipListMutex);
+
+ printf("Error: Could not find member in the membership list\n");
+ return 1;
+}
+
+/**Endlessly and reptedly checks every member in the membershipList
+ * and removes all expired members
+ * membershipList - list of proxies within VLAN
+ * returns: number of removed proxies**/
+int removeExpired(linkedListHolder membershipList){
+ pthread_mutex_lock(&membershipListMutex);
+ if (membershipList==NULL){
+ printf("Error: User sent in a bad membershipList\n");
+ return 0;
+ }
+
+ linkedList ptr = membershipList->head;
+ time_t currentTime;
+ currentTime = time(NULL);
+ int count = 0;
+ do{
+ if(currentTime > ptr->key){
+ if(membershipList->head->next == membershipList->head){
+ free((memberNode*)ptr->data);
+ removeNode(membershipList, ptr->key);
+ return 1;
+ }
+ ptr->prev->next = ptr->next;
+ ptr->next->prev = ptr->prev;
+ if(ptr == membershipList->head){
+ membershipList->head = membershipList->head->next;
+ }
+ linkedList temp = ptr;
+ ptr = ptr->next;
+ free((memberNode*)temp->data);
+ free(temp);
+ count++;
+ continue;
+ }
+ ptr = ptr->next;
+ }while(ptr!=membershipList->head);
+ pthread_mutex_unlock(&membershipListMutex);
+ return count;
+}
View
46 MembershipList.h
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <string.h>
+#include "linkedlist.h"
+#include <time.h>
+#include <string.h>
+#include <pthread.h>
+
+//global variables
+pthread_mutex_t membershipListMutex; //Mutex for when membershipList is accessed
+
+/**Node structure in linklist that holds each member of the membership
+ * list**/
+typedef struct Node{
+ char * memberName; //name of member; can be decimal format or DNS name
+ int timeEntered; //number of seconds after January 1, 1970 at midnight member was discoverd
+ int timeOut; //seconds from time stored member should expire
+}memberNode;
+
+/**Creates a membershipList and returns what it has created*/
+linkedListHolder membershipListCreate();
+
+
+/**Adds member to membership list
+ * Name - Name(IP address or DNS name [keep consistent you will
+ * need to use the same exact name when removing it from list]) user
+ * wishes to remove
+ * timeIn - number of seconds after January 1, 1970 at midnight new member was discovered;
+ * use time() function to get this value
+ * returns: 0 on success 1 on failure
+ * NOTE: user is responsible for marking down when new member was discovered as soon as it
+ * is discovered!!!**/
+int addMember(char *Name, int expires, time_t timeIn, linkedListHolder membershipList);
+
+/**removes member from membership list, but does not use the linkedList
+ * function to do this for various reasons
+ * memberName - Name (use the exact format used to store the member) of
+ * memberNode that user wants to remove;
+ * membershipList - list containing each proxy in the VLAN
+ * returns:0 on success 1 on failure**/
+int removeMember(char *Name, linkedListHolder membershipList);
+
+/**removes all members from the membership list that is expired
+ * membershipList - list of proxies within VLAN
+ * returns: number of removed proxies**/
+int removeExpired(linkedListHolder membershipList);
+
View
2 configFile.c
@@ -16,6 +16,8 @@ int linkTimeout;
int quitAfter;
+//Please define each function in the header as well
+
/**readConfigFile reads the configuration file required to run the VLAN
* program.
* Parameters:

0 comments on commit df30436

Please sign in to comment.
Something went wrong with that request. Please try again.