forked from NavroopKaur/Notification-Junction
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnp_dcll.c
More file actions
511 lines (417 loc) · 11.5 KB
/
np_dcll.c
File metadata and controls
511 lines (417 loc) · 11.5 KB
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
/*
Notification-Junction is an interface between multiple applications and multiple Notification Providers.
Copyright (C) 2015
Navroop Kaur<watwanichitra@gmail.com>,
Shivani Marathe<maratheshivani94@gmail.com>,
Kaveri Sangale<sangale.kaveri9@gmail.com>
All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* This file contains the code for the dcll and hash implementation of
* Initialising,
* Adding an np,
* Printing the nps,
* Searching an np (in the list),
* Deleting an np,
* Getting the count of the applications registered with this particular np,
* Incrementing the count of the applications registered with this particular np, and
* Decrementing the count of the applications registered with this particular np
*/
#include "np_dcll.h"
/* Initialise */
void init_np(np_dcll * l)
{
pthread_rdwr_init_np(&(l->np_list_lock), NULL);
l->head = NULL;
l->count = 0;
}
/* Add an NP with the given name to the linked list for NPs, return 0 on success */
int add_np(np_dcll * l, char *val, char *usage, char ***key_val_arr)
{
char **np_key_val_arr;
int i = 0;
main_np_node *new, *temp, *p, *q;
temp = search_np(l, val);
pthread_rdwr_wlock_np(&(l->np_list_lock));
/* case when np already exists */
if (temp != NULL) {
/* only one node in the np list */
if ((temp == (l->head)) && (l->count == 1)) {
l->head->prev = NULL;
l->head->next = NULL;
l->head = NULL;
}
/* first node and more nodes in np list */
else if ((temp == (l->head)) && (l->count > 1)) {
l->head = temp->next;
p = temp->prev;
(temp->next)->prev = p;
p->next = temp->next;
}
/* other case */
else {
p = temp->prev;
q = temp->next;
p->next = q;
q->prev = p;
}
np_key_val_arr = temp->key_val_arr;
i = 0;
while(np_key_val_arr[i] != NULL) {
free(np_key_val_arr[i]);
np_key_val_arr[i] = NULL;
i++;
}
free(temp->usage);
temp->usage = NULL;
free(temp->data);
temp->data = NULL;
free(temp);
temp = NULL;
l->count = l->count - 1;
}
new = (main_np_node *) malloc(sizeof(main_np_node));
if (new == NULL) {
errno = ECANCELED;
perror("NP_DCLL : ERROR IN MALLOC");
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return -1;
}
new->data = (char *)malloc((strlen(val) + 1) * sizeof(char));
new->usage = usage;
new->key_val_arr = *key_val_arr;
if (new->data == NULL) {
errno = ECANCELED;
perror("NP_DCLL : ERROR IN MALLOC");
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return -1;
}
new->app_count = 0;
/* first node in the list */
if (l->count == 0) {
l->head = new;
new->prev = new;
new->next = new;
}
/* only one node in the list */
else if (l->count == 1) {
l->head->prev = new;
l->head->next = new;
new->prev = l->head;
new->next = l->head;
}
/* other case */
else {
l->head->prev->next = new;
new->prev = l->head->prev;
new->next = l->head;
l->head->prev = new;
}
l->count++;
strcpy(new->data, val);
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return 0;
}
/* print the NP list */
void print_np(np_dcll * l) {
int i ;
main_np_node *ptr;
char **kptr;
pthread_rdwr_rlock_np(&(l->np_list_lock));
i = l->count;
if (l->count == 0) {
printf("> %s %d print_np() : NP_DCLL : Nothing to print_np\n", __FILE__, __LINE__);
pthread_rdwr_runlock_np(&(l->np_list_lock));
return;
}
printf("\n> %s %d print_np() : Total number of NPs : %d\n", __FILE__, __LINE__, l->count);
ptr = l->head;
kptr = ptr->key_val_arr;
printf("\n> %s %d print_np() : NP\t\t\tApp_Count\t\t\tKeyValue", __FILE__, __LINE__);
printf("\n==================================================\n");
while (i) {
printf("> %s %d print_np() : %s ", __FILE__, __LINE__, ptr->data);
printf("\t\t\t%d\n", ptr->app_count);
kptr = ptr->key_val_arr;
while (*kptr) {
printf("\t\t\t\t%s\n", *kptr);
kptr++;
}
printf("\n");
ptr = ptr->next;
i--;
}
printf("====================================xx======================================\n");
pthread_rdwr_runlock_np(&(l->np_list_lock));
}
/* Search for an np with the given name and return a pointer to that node if found */
main_np_node *search_np(np_dcll * l, char *val) {
int i;
int found = 0;
main_np_node *ptr;
pthread_rdwr_rlock_np(&(l->np_list_lock));
i = l->count;
if (l->count == 0) {
errno = ENODEV;
pthread_rdwr_runlock_np(&(l->np_list_lock));
return NULL;
}
ptr = l->head;
while (i) {
if (strcmp(ptr->data, val) == 0) {
found = 1;
break;
}
i--;
ptr = ptr->next;
}
if (found == 0) {
errno = ENODEV;
pthread_rdwr_runlock_np(&(l->np_list_lock));
return NULL;
}
else {
errno = EEXIST;
pthread_rdwr_runlock_np(&(l->np_list_lock));
return ptr;
}
}
/*Delete the main_np_node whose pointer is passed as argument*/
int del_np_node(np_dcll * l, main_np_node * np_to_del) {
char **np_key_val_arr;
int i = 0;
main_np_node *p,*temp, *q;
pthread_rdwr_wlock_np(&(l->np_list_lock));
temp = np_to_del;
if ((temp == (l->head)) && (l->count == 1)) {
l->head->prev = NULL;
l->head->next = NULL;
l->head = NULL;
}
else if ((temp == (l->head)) && (l->count > 1)) {
l->head = temp->next;
p = temp->prev;
(temp->next)->prev = p;
p->next = temp->next;
}
else {
p = temp->prev;
q = temp->next;
p->next = q;
q->prev = p;
}
np_key_val_arr = temp->key_val_arr;
i = 0;
while(np_key_val_arr[i] != NULL) {
free(np_key_val_arr[i]);
np_key_val_arr[i] = NULL;
i++;
}
temp->usage = NULL;
free(temp->data);
temp->data = NULL;
free(temp);
temp = NULL;
l->count = (l->count - 1);
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return 0;
}
/* Delete the np with the given name, if found */
int del_np(np_dcll * l, char *val) {
char **np_key_val_arr;
int i = 0;
main_np_node *p, *temp, *q;
temp = search_np(l, val);
pthread_rdwr_wlock_np(&(l->np_list_lock));
l->count = ( l->count - 1) ;
if (temp == NULL) {
errno = ENODEV;
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return -1;
}
else if ((temp == (l->head)) && (l->count == 1)) {
l->head->prev = NULL;
l->head->next = NULL;
l->head = NULL;
}
else if ((temp == (l->head)) && (l->count > 1)) {
l->head = temp->next;
p = temp->prev;
(temp->next)->prev = p;
p->next = temp->next;
}
else {
p = temp->prev;
q = temp->next;
p->next = q;
q->prev = p;
}
np_key_val_arr = temp->key_val_arr;
i = 0;
while(np_key_val_arr[i] != NULL) {
free(np_key_val_arr[i]);
np_key_val_arr[i] = NULL;
i++;
}
free(temp->usage);
temp->usage = NULL;
free(temp->data);
temp->data = NULL;
free(temp);
temp = NULL;
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return 0;
}
/* For the np with the given name, if found, find the count of the applications that have registered with it */
int get_np_app_cnt(np_dcll * l, char *nval)
{
main_np_node *temp;
temp = search_np(l, nval);
pthread_rdwr_rlock_np(&(l->np_list_lock));
if (temp == NULL) {
errno = ENODEV;
pthread_rdwr_runlock_np(&(l->np_list_lock));
return -1;
}
pthread_rdwr_runlock_np(&(l->np_list_lock));
return temp->app_count;
}
/* For the np with the given name, if found, increment the count of the applications that have registered with it */
void incr_np_app_cnt(np_dcll * l, char *nval) {
main_np_node *temp;
temp = search_np(l, nval);
pthread_rdwr_wlock_np(&(l->np_list_lock));
if (temp == NULL) {
errno = ENODEV;
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return;
}
temp->app_count++;
pthread_rdwr_wunlock_np(&(l->np_list_lock));
}
/* For the np with the given name, if found, decrement the count of the applications that have registered with it */
void decr_np_app_cnt(np_dcll * l, char *nval) {
main_np_node *temp;
temp = search_np(l, nval);
pthread_rdwr_wlock_np(&(l->np_list_lock));
if (temp == NULL) {
errno = ENODEV;
pthread_rdwr_wunlock_np(&(l->np_list_lock));
return;
}
temp->app_count = (temp->app_count - 1);
printf("> %s %d decr_np_app_cnt() :\t%d\n", __FILE__, __LINE__, temp->app_count);
pthread_rdwr_wunlock_np(&(l->np_list_lock));
}
/****************************************** Hash functions ****************************************/
/* Add the NP to the hash structure */
int add_np_to_hash(hash_struct_np *hstruct, char *val, char *usage, char ***key_val_arr) {
main_np_node *new, *s;
new = (main_np_node *) malloc(sizeof(main_np_node));
if (new == NULL) {
errno = ECANCELED;
perror("NP_DCLL : ERROR IN MALLOC");
return -1;
}
new->data = (char *)malloc((strlen(val) + 1) * sizeof(char));
if (new->data == NULL) {
errno = ECANCELED;
perror("NP_DCLL : ERROR IN MALLOC");
pthread_rdwr_wunlock_np(&(hstruct->np_hash_lock));
return -1;
}
new->usage = usage;
new->key_val_arr = *key_val_arr;
new->app_count = 0;
strcpy(new->data, val);
pthread_rdwr_wlock_np(&(hstruct->np_hash_lock));
HASH_FIND_STR(hstruct->np_hash, new->data, s);
if (s == NULL) {
HASH_ADD_KEYPTR( hh, hstruct->np_hash, new->data, strlen(new->data), new);
}
else {
errno = EEXIST;
pthread_rdwr_wunlock_np(&(hstruct->np_hash_lock));
return -1;
}
pthread_rdwr_wunlock_np(&(hstruct->np_hash_lock));
return 0;
}
/* delete the NP from hash */
int del_np_from_hash(hash_struct_np *hstruct, char *val) {
main_np_node *s;
char **np_key_val_arr;
int i = 0;
pthread_rdwr_wlock_np(&(hstruct->np_hash_lock));
/* check if NP exists*/
HASH_FIND_STR(hstruct->np_hash, val, s);
/* if it does, delete it and free memory */
if(s != NULL) {
np_key_val_arr = s->key_val_arr;
i = 0;
while(np_key_val_arr[i] != NULL) {
free(np_key_val_arr[i]);
np_key_val_arr[i] = NULL;
i++;
}
free(s->usage);
s->usage = NULL;
free(s->data);
s->data = NULL;
HASH_DEL(hstruct->np_hash, s);
free(s);
s = NULL;
}
/* if it doesn't exist, error */
else {
errno = EINVAL;
pthread_rdwr_wunlock_np(&(hstruct->np_hash_lock));
return -1;
}
pthread_rdwr_wunlock_np(&(hstruct->np_hash_lock));
print_hash_np(hstruct);
return 0;
}
/* print hash */
void print_hash_np(hash_struct_np *hstruct) {
struct main_np_node *s;
printf("\nPrinting NPs in hash :\n");
pthread_rdwr_rlock_np(&(hstruct->np_hash_lock));
for(s=hstruct->np_hash; s != NULL; s=(struct main_np_node*)(s->hh.next)) {
printf("NP Name : %s\n", s->data);
}
pthread_rdwr_runlock_np(&(hstruct->np_hash_lock));
}
/* increment app_count of NP in the hash */
void incr_np_app_cnt_hash(hash_struct_np *hstruct, char *np_name){
main_np_node *s;
HASH_FIND_STR(hstruct->np_hash, np_name, s);
if(s != NULL)
(s->app_count)++;
}
/* decrement app_count of NP in the hash */
void decr_np_app_cnt_hash(hash_struct_np *hstruct, char *np_name) {
main_np_node *s;
HASH_FIND_STR(hstruct->np_hash, np_name, s);
if(s != NULL)
(s->app_count)--;
}
/* get app_count of NP */
int get_np_app_cnt_hash(hash_struct_np *hstruct, char* np_name) {
main_np_node *s;
HASH_FIND_STR(hstruct->np_hash, np_name, s);
if(s != NULL)
return s->app_count;
else {
errno = EINVAL;
return -1;
}
}