-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_path_tree.c
593 lines (567 loc) · 24.1 KB
/
client_path_tree.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "include/client_path_tree.h"
/*function prototypes*/
int __support_remove_client_registration(pathNode *node, char **tokens, int numTokens, int index,
int *mustBeRemoved, clientData *data, clientNode **client);
// ===========================================================================
// cpt_create
// ===========================================================================
int cpt_create(pathNode **root){
*root = malloc(sizeof(pathNode));
if(!(*root)){
fprintf(stderr, "cpt_create: error while allocating memory.\n");
return PROG_ERROR;
}
(*root)->name = NULL;
(*root)->children = NULL;
(*root)->registrations = NULL;
(*root)->numChildren = 0;
(*root)->numRegistrations = -1;
return PROG_SUCCESS;
}
// ===========================================================================
// cpt_contains_child [SUPPPORT FUNCTION]
// ===========================================================================
int cpt_contains_child(pathNode *father, char *name, pathNode **node){
int i; //counter
for(i = 0; i < father->numChildren; i++){
if(fname_compare(name, (father->children[i])->name) == 0){
*node = father->children[i];
return TRUE;
}
}
return FALSE;
}
// ===========================================================================
// cpt_add_child [SUPPPORT FUNCTION]
// ===========================================================================
int cpt_add_child(pathNode *father, char *name, pathNode **node){
father->children = realloc(father->children, sizeof(pathNode *) * (father->numChildren + 1));
if(!(father->children)){
fprintf(stderr, "cpt_add_child: error while allocating memory.\n");
return PROG_ERROR;
}
pathNode *newNode = malloc(sizeof(pathNode));
if(!newNode){
fprintf(stderr, "cpt_add_child: error while allocating memory.\n");
return PROG_ERROR;
}
newNode->name = malloc(sizeof(char) * (strlen(name) + 1));
if(!(newNode->name)){
fprintf(stderr, "cpt_add_child: error while allocating memory.\n");
return PROG_ERROR;
}
strcpy(newNode->name, name);
newNode->numChildren = 0;
newNode->children = NULL;
newNode->numRegistrations = 0;
newNode->registrations = NULL;
*node = newNode;
father->children[father->numChildren++] = newNode;
return PROG_SUCCESS;
}
// ===========================================================================
// cpt_remove_child [SUPPPORT FUNCTION]
// NOTE: assumption: child is assumed to be present in father's children list.
// this beacuse child is a pointer that has been taken from that list and no
// other function could have modified the list befare this call.
// ===========================================================================
int cpt_remove_child(pathNode *father, pathNode *child){
if(father->numChildren < 1) return PROG_ERROR;
else if(father->numChildren == 1){
free(father->children);
father->children = NULL;
father->numChildren = 0;
}else{
pathNode **newList = malloc(sizeof(pathNode *) *(father->numChildren - 1));
if(!newList){
fprintf(stderr, "cpt_remove_child: error while allocating memory.\n");
return PROG_ERROR;
}
int i, k; //counter
for(i = 0, k = 0; i < father->numChildren; i++){
if(father->children[i] != child){
newList[k++] = father->children[i];
}
}
free(father->children); //free the old list
father->children = newList;
father->numChildren--;
}
/*delete the child node children and registrations are null, otherwise the node could not
be deleted.*/
free(child->name);
free(child);
return PROG_SUCCESS;
}
// ===========================================================================
// __support_add_path [SUPPPORT FUNCTION]
// ===========================================================================
int __support_add_path(pathNode *node, char **tokens, int numTokens, int index, pathNode **added, char *host){
pathNode *p;
if(cpt_contains_child(node, tokens[index], &p)){ //node already present
if(numTokens > (index + 1)){
return __support_add_path(p, tokens, numTokens, index + 1, added, host);
}
}else{ //the node must be added
while(index < numTokens){
if(cpt_add_child(node, tokens[index++], &p) == PROG_ERROR){
fprintf(stderr, "__support_add_path: error while adding a pathNode child.\n");
return PROG_ERROR;
}
node = p;
}
}
/*at this point, p contains a pointer to the pathNode corresponding to the last token*/
*added = p;
return PROG_SUCCESS;
}
// ===========================================================================
// cpt_add_client_registration
// ===========================================================================
int cpt_add_client_registration(pathNode *root, char *path, clientNodeList *cnl, clientData *data,
registrationMode mode){
char **tokens;
int numTokens;
int i; //counter
/*first we check if the client already receives notifications for this path*/
int updated = 0;
registration *updateReg = NULL;
clientNode *foundNode;
if(cnl_exists_node(cnl, data, &foundNode)){
for(i = 0; i < foundNode->numRegisteredPaths; i++){
if(fname_compare(path, foundNode->registeredPaths[i]->path) == 0 && mode == NONRECURSIVE){
foundNode->registeredPaths[i]->mode = mode;
return PROG_SUCCESS;
}else if(is_prefix(path, foundNode->registeredPaths[i]->path) && foundNode->registeredPaths[i]->mode == RECURSIVE){
return PATH_ALREADY_REGISTERED;
}else if(is_prefix(foundNode->registeredPaths[i]->path, path) && mode == RECURSIVE){
//we must update the registration. Delete the old one.
pathNode *father = foundNode->registeredPaths[i]->father;
if(father->numRegistrations == 1){
free(father->registrations[0]->path);
free(father->registrations[0]);
free(father->registrations);
father->registrations = NULL;
}else{
registration **newRegList = malloc(sizeof(registration *) * (father->numRegistrations - 1));
if(!newRegList){
fprintf(stderr, "cpt_add_client_registration: error while allocating memory.\n");
return PROG_ERROR;
}
int k, j;
for(k = 0, j = 0; k < father->numRegistrations; k++){
if(father->registrations[k] != foundNode->registeredPaths[i]){
newRegList[j++] = father->registrations[k];
}else{
//delete registration structure
free(father->registrations[k]->path);
free(father->registrations[k]);
}
}
free(father->registrations);
father->registrations = newRegList;
}
father->numRegistrations--;
foundNode->registeredPaths[i] = NULL;
updated++;
}
}
if(updated > 0){
registration **newPathList = malloc(sizeof(registration *) * (foundNode->numRegisteredPaths - updated + 1));
if(!newPathList){
fprintf(stderr, "cpt_add_client_registration: error while allocating memory.\n");
return PROG_ERROR;
}
int k;
for(i = 0, k = 0; i < foundNode->numRegisteredPaths; i++){
if(foundNode->registeredPaths[i] != NULL){
newPathList[k++] = foundNode->registeredPaths[i];
}
}
newPathList[k] = malloc(sizeof(registration));
if(!(newPathList[k])){
fprintf(stderr, "cpt_add_client_registration: error while allocating memory.\n");
return PROG_ERROR;
}
newPathList[k]->path = path;
newPathList[k]->mode = RECURSIVE;
newPathList[k]->client = foundNode;
newPathList[k]->father = NULL;
free(foundNode->registeredPaths);
foundNode->registeredPaths = newPathList;
foundNode->numRegisteredPaths = foundNode->numRegisteredPaths - updated + 1;
updateReg = newPathList[k];
if(cpt_clean_tree(root) == PROG_ERROR){
fprintf(stderr, "cpt_add_client_registration: error while cleaning the tree.\n");
return PROG_ERROR;
}
}
}else{
if(cnl_add_client_node(cnl, data, &foundNode) == PROG_ERROR){
fprintf(stderr, "cpt_add_client_registration: error while adding a client node.\n");
return PROG_ERROR;
}
}
//PART 2
pathNode *added;
if(tokenize_path(path, &tokens, &numTokens) == PROG_ERROR){
fprintf(stderr, "cpt_add_client_registration: error while tokenizing the path.\n");
return PROG_ERROR;
}
int retSAP = __support_add_path(root, tokens, numTokens, 0, &added, data->hostName);
if(retSAP == PROG_ERROR){
fprintf(stderr, "cpt_add_client_registration: error while adding a new path.\n");
return PROG_ERROR;
}
if(updated){
//just need to add updateReg to registrations list
updateReg->father = added;
added->registrations = realloc(added->registrations, added->numRegistrations + 1);
if(!added->registrations){
fprintf(stderr, "cpt_add_client_registration: error while allocating memory.\n");
return PROG_ERROR;
}
added->registrations[added->numRegistrations++] = updateReg;
return PATH_UPDATED;
}else{
foundNode->numRegisteredPaths++;
foundNode->registeredPaths = realloc(foundNode->registeredPaths, sizeof(registration *) * foundNode->numRegisteredPaths);
if(!foundNode->registeredPaths){
fprintf(stderr, "cpt_add_client_registration: error while allocating memory.\n");
return PROG_ERROR;
}
registration *p = malloc(sizeof(registration));
if(!p){
fprintf(stderr, "cpt_add_client_registration: error while allocating memory.\n");
return PROG_ERROR;
}
p->father = added;
p->client = foundNode;
p->path = path;
p->mode = mode;
foundNode->registeredPaths[foundNode->numRegisteredPaths - 1] = p;
added->registrations = realloc(added->registrations, sizeof(registration *) * (added->numRegistrations + 1));
if(!added->registrations){
fprintf(stderr, "cpt_add_client_registration: error while reallocating memory.\n");
return PROG_ERROR;
}
added->registrations[added->numRegistrations++] = p;
return PROG_SUCCESS;
}
}
// ===========================================================================
// __support_remove_client_registration [SUPPPORT FUNCTION]
// ===========================================================================
int __support_remove_client_registration(pathNode *node, char **tokens, int numTokens, int index,
int *mustBeRemoved, clientData *data, clientNode **client){
if(index <= numTokens - 1){ //recursive call
pathNode *p;
if(cpt_contains_child(node, tokens[index], &p)){ //node already present
int removeChild;
int ret = __support_remove_client_registration(p, tokens, numTokens, index + 1, &removeChild, data, client);
if(ret != PROG_SUCCESS) return ret;
if(removeChild){
if(cpt_remove_child(node, p) == PROG_ERROR){
fprintf(stderr, "__support_remove_client_registration: error while removing a child node.\n");
return PROG_ERROR;
}
if(node->numChildren == 0 && node->numRegistrations == 0)
*mustBeRemoved = 1;
else
*mustBeRemoved = 0;
}else{
*mustBeRemoved = 0;
}
return PROG_SUCCESS;
}else{ //the path was not registered by any client
return PATH_NOT_REGISTERED;
}
}else{ //BASE CASE
/*at this point, node contains a pointer to the pathNode corresponding to the last token.
We need to check if the client was registered for this path*/
int i;
int found = 0;
for(i = 0; i < node->numRegistrations; i++){
if(strcmp(node->registrations[i]->client->networkData->hostName, data->hostName) == 0){ //same IP
found = 1;
break;
}
}
if(!found) return PATH_NOT_REGISTERED;
registration **newList = NULL;
if(node->numRegistrations > 1){
/*copy all registrations but the one with index "i"*/
newList = malloc(sizeof(registration *) * (node->numRegistrations - 1));
if(!newList){
fprintf(stderr, "%s\n", "__support_remove_path: error while allocating memory.");
return PROG_ERROR;
}
int j, k;
for(j = 0, k = 0; j < node->numRegistrations; j++){
if(j != i){
newList[k++] = node->registrations[j];
}
}
}
*client = node->registrations[i]->client;
/*remove the path from the client's registeredPaths list*/
if((*client)->numRegisteredPaths > 1){
registration **newRegPaths = malloc(sizeof(registration *) * ((*client)->numRegisteredPaths - 1));
if(!newRegPaths){
fprintf(stderr, "__support_remove_path: error while allocating memory.\n");
return PROG_ERROR;
}
int j, k;
for(j = 0, k = 0; j < (*client)->numRegisteredPaths; j++){
if((*client)->registeredPaths[j] != node->registrations[i]){
newRegPaths[k++] = (*client)->registeredPaths[j];
}
}
free((*client)->registeredPaths);
(*client)->registeredPaths = newRegPaths;
}else{
free((*client)->registeredPaths);
(*client)->registeredPaths = NULL;
}
(*client)->numRegisteredPaths--;
/*delete registration*/
free(node->registrations[i]->path);
free(node->registrations[i]);
/*delete registration (but not the clientNode, we still need it)*/
free(node->registrations);
node->numRegistrations--;
if(node->numRegistrations == 0){
node->registrations = NULL;
if(node->numChildren == 0)
*mustBeRemoved = 1;
else
*mustBeRemoved = 0;
}else{
node->registrations = newList;
*mustBeRemoved = 0; //at least one registration is still present
}
return PROG_SUCCESS;
}
}
// ===========================================================================
// cpt_remove_client_registration
// ===========================================================================
int cpt_remove_client_registration(pathNode *root, char *path, clientNodeList *cnl, clientData *data){
char **tokens;
int numTokens;
int i; //counter
pathNode *added;
if(tokenize_path(path, &tokens, &numTokens) == PROG_ERROR){
fprintf(stderr, "cpt_remove_client_registration: error while tokenizing the path.\n");
return PROG_ERROR;
}
int mustBeRemoved;
clientNode *client;
int retVal = __support_remove_client_registration(root, tokens, numTokens, 0, &mustBeRemoved, data, &client);
if( retVal == PROG_ERROR){
fprintf(stderr, "cpt_remove_client_registration: error in __support_remove_path.\n");
return PROG_ERROR;
}else if(retVal == PATH_NOT_REGISTERED){
return PATH_NOT_REGISTERED;
}
if(client->numRegisteredPaths == 0){
/*there were just the path that has been deleted. So now there are no registered path for
the client; we can delete its entry in the clientNodeList*/
if(cnl_remove_client_node(cnl, client) == PROG_ERROR){
fprintf(stderr, "cpt_remove_client_registration: error in cnl_remove_client_node.\n");
return PROG_ERROR;
}
}
return PROG_SUCCESS;
}
// ===========================================================================
// cpt_delete_subtree
// ===========================================================================
int cpt_delete_subtree(pathNode *node, char *str){
int i;
for(i = 0; i < node->numChildren; i++){
int res = cpt_delete_subtree(node->children[i], str);
if(res == PROG_ERROR){
return PROG_ERROR;
}
}
//delete node
//send notification to clients
int j;
for(j = 0; j < node->numRegistrations; j++){
clientNode *client = node->registrations[j]->client;
if(cnl_signal_deletion(client, str) == PROG_ERROR){
fprintf(stderr, "cnl_delete_subtree: error while signaling a deletion.\n");
return PROG_ERROR;
}
/*delete registration from clientNode paths*/
registration **newList = malloc(sizeof(registration *) * (client->numRegisteredPaths -1));
if(!newList){
fprintf(stderr, "cnl_delete_subtree: error while allocating memory.\n");
return PROG_ERROR;
}
int k;
for(i = 0, k = 0; i < client->numRegisteredPaths; i++){
if(client->registeredPaths[i] != node->registrations[j]){
newList[k++] = client->registeredPaths[i];
}
}
free(client->registeredPaths);
client->registeredPaths = newList;
client->numRegisteredPaths--;
/*delete registration*/
free(node->registrations[j]->path);
free(node->registrations[j]);
}
//delete node
if(j > 0) free(node->registrations);
if(node->numChildren > 0) free(node->children);
free(node->name);
free(node);
return PROG_SUCCESS;
}
// ===========================================================================
// cpt_clean_tree
// ===========================================================================
int cpt_clean_tree(pathNode *node){
int canDelete = node->numRegistrations == -1 ? 0 : 1;
if(node->numChildren > 0){
int i;
int numDeleted = 0;
for(i = 0; i < node->numChildren; i++){
int ret = cpt_clean_tree(node->children[i]);
if(ret == PROG_ERROR) return PROG_ERROR;
else if(ret){
node->children[i] = NULL;
numDeleted++;
}
else canDelete = 0;
}
if(numDeleted > 0){
if(numDeleted != node->numChildren){
pathNode **newList = malloc(sizeof(pathNode *) * (node->numChildren - numDeleted));
if(!newList){
fprintf(stderr, "cpt_clen_tree: error while allocating memory.\n");
return PROG_ERROR;
}
int k;
for(i = 0, k = 0; i < node->numChildren; i++){
if(node->children[i] != NULL){
newList[k++] = node->children[i];
}
}
free(node->children);
node->children = newList;
node->numChildren = node->numChildren - numDeleted;
}else{
free(node->children);
node->children = NULL;
node->numChildren = 0;
}
}
}
if(canDelete){
if(node->numRegistrations == 0){
free(node);
}else{
canDelete = 0;
}
}
return canDelete;
}
// ===========================================================================
// cpt_push_notification
// ===========================================================================
int cpt_push_notification(pathNode *root, receivedNotification *not, char *stringNot){
char **tokens;
int numTokens;
if(tokenize_path(not->path, &tokens, &numTokens) == PROG_ERROR){
fprintf(stderr, "cpt_push_notification: error while tokenizing the path.\n");
return PROG_ERROR;
}
pathNode *current = root;
pathNode *child;
int i = 0;
int maxIndex = numTokens - 1;
int maxInternIndex = maxIndex - 1;
/*now we get to the tree node corresponding to not->path, if any.
During the visit, it must be checked whether there is a node along the
path that presents a client which is registered in RECURSIVE mode.
In that case the client will receive the notification, too.*/
while(i < maxIndex){
if(cpt_contains_child(current, tokens[i], &child)){ //node present
int j; //counter
for(j = 0; j < child->numRegistrations; j++){
if((i < maxInternIndex && child->registrations[j]->mode == RECURSIVE) || i == maxInternIndex) {
//add this notification
if(cnl_add_notification(child->registrations[j]->client, stringNot) == PROG_ERROR){
fprintf(stderr, "cpt_push_notification: error while adding a notification to a client.\n");
return PROG_ERROR;
}
}
}
if(i++ != maxInternIndex) current = child; //beacuse we'll need father and child
}else{ //there are no more clients we can send this notification to
for(i = 0; i < numTokens; i++) free(tokens[i]);
free(tokens);
return PROG_SUCCESS;
}
}
/*there is one last case to handle. A branch of the filesystem is deleted and all the clients
registered for a subdirectory of that branch must be notified. If this is the case, we will set child
represents the deleted node. All the clients registered to descendants of this node must be
notified.*/
current = child;
if(not->isDir == 1 && not->type == deletion){
if(!cpt_contains_child(current, tokens[i], &child)){
return PROG_SUCCESS;
}
if(cpt_delete_subtree(child, not->path) == PROG_ERROR){
fprintf(stderr, "cpt_push_notification: error while removing a subtree.\n");
return PROG_ERROR;
}
if(current->numChildren == 1){
free(current->children);
current->numChildren = 0;
if(cpt_clean_tree(root) == PROG_ERROR){
fprintf(stderr, "cpt_push_notification: error while cleaning the tree.\n");
return PROG_ERROR;
}
}else{
pathNode **newList = malloc(sizeof(pathNode *) *(current->numChildren - 1));
if(!newList){
fprintf(stderr, "cpt_push_notification: error while allocating memory.\n");
return PROG_ERROR;
}
int i, k; //counter
for(i = 0, k = 0; i < current->numChildren; i++){
if(current->children[i] != child){
newList[k++] = current->children[i];
}
}
free(current->children); //free the old list
current->children = newList;
current->numChildren--;
}
}
for(i = 0; i < numTokens; i++) free(tokens[i]);
free(tokens);
return PROG_SUCCESS;
}
// ===========================================================================
// __cpt_print_tree_rec [SUPPPORT FUNCTION]
// ===========================================================================
void __cpt_print_tree_rec(pathNode *node, int lev){
int i;
for(i = 0; i < node->numChildren; i++){
printf("%*s| - Name: %s, numReg: %d\n", lev, " ", node->children[i]->name, node->children[i]->numRegistrations);
__cpt_print_tree_rec(node->children[i], lev + 3); //recursive call
}
}
// ===========================================================================
// cpt_print_tree
// ===========================================================================
void cpt_print_tree(pathNode *root){
printf("_________CLIENT PATH TREE______________\n\n");
__cpt_print_tree_rec(root, 1);
}