diff --git a/src/server/user_mgr.c b/src/server/user_mgr.c --- a/src/server/user_mgr.c +++ b/src/server/user_mgr.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -111,7 +112,11 @@ int deleteUser(struct user *user_ptr) free(user_ptr->username); memset(user_ptr, '\0', sizeof(struct user)); + user_mgr_info.current_users--; + return 1; + + backup_list_XML(); } int createUser(struct user *user_ptr, char *nameOfUser) @@ -123,6 +128,8 @@ int createUser(struct user *user_ptr, ch user_ptr->render_power = 0; user_ptr->last_job = 0; + user_mgr_info.current_users++; + return 1; } @@ -131,6 +138,7 @@ int placeUser(int index, char *nameOfUse { int higher; int lower; + int total_moves; // I shift data in the array to create an open the space where the user should be added // but first I figure out which way is the shortest @@ -146,16 +154,25 @@ int placeUser(int index, char *nameOfUse // here the data is shifted to open up a space if(index - lower < higher - index) - for(; lower < index; lower++) + { + total_moves = index - lower; + for(; lower < index; lower++) memcpy(&user_mgr_info.user_array[lower], &user_mgr_info.user_array[lower + 1], sizeof(struct user)); + } else - for(; higher > index; higher--) + { + total_moves = higher - index; + for(; higher > index; higher--) memcpy(&user_mgr_info.user_array[higher], &user_mgr_info.user_array[higher - 1], sizeof(struct user)); + } } // add the user to the array createUser(&user_mgr_info.user_array[index], nameOfUser); + if(total_moves > 50){ + resize_user_array(); + } return 1; } @@ -215,16 +232,30 @@ int addUser(char *nameOfUser) } } + backup_list_XML(); return 0; } int initialize_users() { - // pull data from XML file + struct stat buffer; + // cif user_list.xml exists + if(stat("user_list.xml", &buffer) == 0) + { + restart_From_XML_backup(); + } + else + { + user_mgr_info.current_users = 0; - // if XML file is not found create new array of size 50 + user_mgr_info.user_array_size = 50; + user_mgr_info.user_array = malloc(sizeof(struct user) * 50); + } - return 1; + // if XML file is not found create new array of size 50 + + + return 1; } /********************************** XMLness *****************************/ @@ -232,7 +263,7 @@ int initialize_users() int backup_list_XML() { xmlTextWriterPtr writer; - char *tmp; + char *tmp; int counter; @@ -242,6 +273,10 @@ int backup_list_XML() // create root element user_list xmlTextWriterStartElement(writer, (xmlChar*)"user_list"); + _distren_asprintf(&tmp, "%d", user_mgr_info.current_users); + xmlTextWriterWriteAttribute(writer, (xmlChar*)"amount_of_users", (xmlChar*)tmp); + free(tmp); + for(counter = 0; counter < user_mgr_info.user_array_size; counter++) { if(user_mgr_info.user_array[counter].username != 0) @@ -252,6 +287,11 @@ int backup_list_XML() _distren_asprintf(&tmp, "%d", user_mgr_info.user_array[counter].last_job); xmlTextWriterWriteAttribute(writer, (xmlChar*)"last_job", (xmlChar*)tmp); + free(tmp); + + _distren_asprintf(&tmp, "%d", user_mgr_info.user_array[counter].render_power); + xmlTextWriterWriteAttribute(writer, (xmlChar*)"render_power", (xmlChar*)tmp); + free(tmp); xmlTextWriterEndElement(writer); } @@ -260,3 +300,33 @@ int backup_list_XML() return 0; } + +int restart_From_XML_backup(){ + xmlDocPtr doc; + xmlNodePtr cur; + int counter; + + doc = xmlParseFile("user_list.xml"); + cur = xmlDocGetRootElement(doc); + if (xmlStrcmp(cur->name, (xmlChar*)"user_list")) + { + fprintf(stderr, "xml document is wrong type"); + xmlFreeDoc(doc); + return 1; + } + + user_mgr_info.current_users = atoi((char*)xmlGetProp(cur, (xmlChar*)"amount_of_users")); + + user_mgr_info.user_array_size = user_mgr_info.current_users * 2; + user_mgr_info.user_array = malloc(sizeof(struct user) * user_mgr_info.user_array_size); + + cur = cur->xmlChildrenNode; + for(counter = 0; cur->next; counter++){ + user_mgr_info.user_array[counter*2].username = (char*)xmlGetProp(cur, (xmlChar*)"amount_of_users"); + user_mgr_info.user_array[counter*2].last_job = atoi((char*)xmlGetProp(cur, (xmlChar*)"last_job")); + user_mgr_info.user_array[counter*2].render_power = atoi((char*)xmlGetProp(cur, (xmlChar*)"render_power")); + cur = cur->next; + } + + return 0; +}