Files
@ 93ef605ce0f6
Branch filter:
Location: DistRen/src/server/user_mgr.c
93ef605ce0f6
8.3 KiB
text/plain
Add a sample animated blender file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | /*
Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
This file is a part of DistRen.
DistRen is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DistRen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with DistRen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/config.h"
#include "user_mgr.h"
#include "common/asprintf.h"
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <libxml/xmlreader.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
struct user_mgr_info
{
struct user *user_array;
int current_users;
int user_array_size;
} user_mgr_info;
int resize_user_array()
{
int counter;
int counter2;
// create array twice the size of the current amount of users
user_mgr_info.user_array_size = user_mgr_info.current_users * 2;
struct user *new_user_array = malloc(sizeof(struct user) * user_mgr_info.user_array_size);
// this copies the original user_array over to the new one
// using two counters allows the array to be resized at any time
// leaving exactly 1 open space between each user when it is done;
counter2 = 0;
for(counter = 0; counter < user_mgr_info.current_users; counter++)
{
if(user_mgr_info.user_array[counter].username != 0)
{
new_user_array[counter2*2] = user_mgr_info.user_array[counter];
counter2++;
}
}
// cleanup old array
free(user_mgr_info.user_array);
// change the pointer to point to the new user array
user_mgr_info.user_array = new_user_array;
return 1;
}
struct user *findUser(char *nameOfUser)
{
int high;
int low;
int middle;
int result;
high = user_mgr_info.user_array_size - 1;
low = 0;
result = -1;
for(middle = (low+high)/2; 1 == 1; middle = (low+high)/2)
{
// in case middle lands on a part of the array with no user
while(user_mgr_info.user_array[middle].username == 0)
{
if(result < 0)
middle --;
else
middle ++;
}
// this is where the array is cut in half and the half that the nameOfUser is on is kept
result = strcmp(nameOfUser, user_mgr_info.user_array[middle].username);
if(result == 0)
return &user_mgr_info.user_array[middle];
else if(result < 0)
high = middle;
else
low = middle;
// in case the user doesn't exist
if(high-low <= 0)
return 0;
}
}
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)
{
// clear old memory
memset(user_ptr, '\0', sizeof(struct user));
user_ptr->username = nameOfUser;
user_ptr->render_power = 0;
user_ptr->last_job = 0;
user_mgr_info.current_users++;
return 1;
}
// places the new user at position index in the array, moves other users if it needs to
int placeUser(int index, char *nameOfUser)
{
int higher;
int lower;
int total_moves;
total_moves = 0;
// 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
if(user_mgr_info.user_array[index].username != 0)
{
higher = index + 1;
while(user_mgr_info.user_array[higher].username != 0)
higher++;
lower = index - 1;
while(user_mgr_info.user_array[lower].username != 0)
lower--;
// here the data is shifted to open up a space
if(index - lower < higher - index)
{
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
{
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;
}
int addUser(char *nameOfUser)
{
int high;
int low;
int middle;
int result;
high = user_mgr_info.user_array_size - 1;
low = 0;
result = -1;
for(middle = (low+high)/2; 1 == 1; middle = (low+high)/2)
{
// in case middle lands on a part of the array with no user
while(user_mgr_info.user_array[middle].username == 0)
{
if(result < 0)
middle--;
else
middle++;
}
// this is where the array is cut in half and the half that the nameOfUser is on is kept
result = strcmp(nameOfUser, user_mgr_info.user_array[middle].username);
if(result == 0)
return 0;
else if(result < 0)
high = middle;
else
low = middle;
// once there are less than 10 possible places for the user to be placed
// break out of this loop and begin next loop
if(high-low <= 10)
break;
}
// this function starts at the low index number, and goes up until it finds a
// username that is bigger than it alphabetically, and tells the userPlacer
// that it needs to go 1 before that spot
for(; low <= high; low++)
{
while(user_mgr_info.user_array[low].username == 0)
low++;
result = strcmp(nameOfUser, user_mgr_info.user_array[low].username) < 0;
if(result < 0)
{
placeUser(low - 1, nameOfUser);
return 1;
}
if(result == 0)
{
fprintf(stderr, "user already exists");
return 0;
}
}
backup_list_XML();
return 0;
}
int initialize_users()
{
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;
user_mgr_info.user_array_size = 50;
user_mgr_info.user_array = malloc(sizeof(struct user) * 50);
}
// if XML file is not found create new array of size 50
return 1;
}
/********************************** XMLness *****************************/
int backup_list_XML()
{
xmlTextWriterPtr writer;
char *tmp;
int counter;
writer = xmlNewTextWriterFilename("user_list.xml", 0);
xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
// 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)
{
xmlTextWriterStartElement(writer, (xmlChar*)"user");
xmlTextWriterWriteAttribute(writer, (xmlChar*)"name", (xmlChar*)user_mgr_info.user_array[counter].username);
_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);
}
}
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;
}
|