-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_node_list.c
227 lines (213 loc) · 8.36 KB
/
client_node_list.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
#include "include/client_node_list.h"
// ===========================================================================
// cnl_create
// ===========================================================================
int cnl_create(clientNodeList **root){
*root = malloc(sizeof(clientNodeList));
if(!(*root)){
fprintf(stderr, "cb_create: error while allocating memory.\n");
return PROG_ERROR;
}
(*root)->first = NULL;
(*root)->last = NULL;
return PROG_SUCCESS;
}
// ===========================================================================
// cnl_exists_node
// ===========================================================================
int cnl_exists_node(clientNodeList *root, clientData *data, clientNode **client){
clientNode *current = root->first;
while(current){
if(strcmp(current->networkData->hostName, data->hostName) == 0){
*client = current;
return TRUE;
}
current = current->next;
}
*client = NULL;
return FALSE;
}
// ===========================================================================
// __copy_networkData [SUPPORT FUNCTION]
// ===========================================================================
int __copy_networkData(clientData *data, clientData **copy){
*copy = malloc(sizeof(clientData));
if(!(*copy)){
fprintf(stderr, "%s\n", "__copy_networkData: error while allocating memory.\n");
return PROG_ERROR;
}
(*copy)->hostName = malloc(sizeof(char) * (strlen(data->hostName) + 1));
if(!(*copy)->hostName){
fprintf(stderr, "__copy_networkData: error while allocating memory.\n");
return PROG_ERROR;
}
strcpy((*copy)->hostName, data->hostName);
(*copy)->clientLen = data->clientLen;
(*copy)->clientAddress = data->clientAddress;
return PROG_SUCCESS;
}
// ===========================================================================
// cnl_add_client_node
// ===========================================================================
int cnl_add_client_node(clientNodeList *root, clientData *data, clientNode **addedNode){
clientNode *newNode = malloc(sizeof(clientNode));
if(!newNode){
fprintf(stderr, "cnl_add_client_node: error while allocating memory.\n");
return PROG_ERROR;
}
/*making a copy of network data.*/
if(__copy_networkData(data, &(newNode->networkData)) == PROG_ERROR){
fprintf(stderr, "cnl_add_client_node: error while copying the network data.\n");
return PROG_ERROR;
}
newNode->next = NULL;
if(ll_create(&(newNode->updates)) == PROG_ERROR){
fprintf(stderr, "cnl_add_client_node: error while creating the linked list.\n");
return PROG_ERROR;
}
newNode->numRegisteredPaths = 0;
if(root->first == NULL){
root->first = newNode;
}else{
(root->last)->next = newNode;
}
root->last = newNode;
newNode->deletionMark = 0;
newNode->registeredPaths = NULL;
*addedNode = newNode;
return PROG_SUCCESS;
}
// ===========================================================================
// cnl_remove_client_node
// ===========================================================================
int cnl_remove_client_node(clientNodeList *root, clientNode *node){
clientNode *current = root->first;
clientNode *prev = NULL;
while(current){
if(current == node){
if(prev == NULL){ //it's the first node
root->first = current->next;
}else{
prev->next = current->next;
}
if(root->last == current){ //it's the last node
root->last = prev;
}
//we can free the structure
free(current->networkData->hostName);
free(current->networkData);
ll_free(current->updates);
free(current);
return PROG_SUCCESS;
}
prev = current;
current = current->next;
}
return PROG_ERROR;
}
// ===========================================================================
// cnl_add_notification
// ===========================================================================
int cnl_add_notification(clientNode *client, char *notString){
char *tmp = malloc(sizeof(char) * (strlen(notString) + 1));
if(!tmp){
fprintf(stderr, "cnl_add_notification: error while allocating memory.\n");
return PROG_ERROR;
}
strcpy(tmp, notString);
if(ll_add_item(client->updates, tmp) == PROG_ERROR){
fprintf(stderr, "cnl_add_notification: error while adding a notification to a client's updates list.\n");
return PROG_ERROR;
}
return PROG_SUCCESS;
}
// ===========================================================================
// cnl_signal_deletion
// ===========================================================================
int cnl_signal_deletion(clientNode *client, char *fld){
char formatted[] = "The path \"%s\" has been deleted and one of your registrations has been removed accordingly.\r\n.\r\n";
char *not = malloc(sizeof(char) * (strlen(fld) + strlen(formatted) + 1));
if(!not){
fprintf(stderr, "cnl_add_notification: error while allocating memory.\n");
return PROG_ERROR;
}
if(sprintf(not, formatted, fld) < 0){
fprintf(stderr, "cnl_signal_deletion: error while creating a string.\n");
return PROG_ERROR;
}
if(ll_add_item(client->updates, not) == PROG_ERROR){
fprintf(stderr, "cnl_add_notification: error while adding a notification to a client's updates list.\n");
return PROG_ERROR;
}
if(client->numRegisteredPaths == 0) client->deletionMark = 1;
else if(client->numRegisteredPaths < 0){ //DEBUG PURPOSES
fprintf(stderr, "cnl_signal_deletion: inconsistent number of registed path.\n");
return PROG_ERROR;
}
return PROG_SUCCESS;
}
// ===========================================================================
// cnl_send_notifications
// ===========================================================================
int cnl_send_notifications(clientNodeList *list, char *udpPort){
clientNode *current = list->first;
clientNode *prev = NULL;
while(current){ //create an udp socket for each client
/*create udp socket*/
int s = create_connection(current->networkData->hostName, udpPort, SOCK_DGRAM);
if(s == PROG_ERROR){
printf("ADDR: %s\n", current->networkData->hostName);
fprintf(stderr, "cnl_send_notifications: error while creating an UDP socket.\n");
return PROG_ERROR;
}
linkedItem *prevItem = NULL;
linkedItem *currItem = current->updates->first;
current->updates->first = NULL;
current->updates->last = NULL;
while(currItem){
if(send_data(s, currItem->item, strlen(currItem->item)) == PROG_ERROR){
fprintf(stderr, "cnl_send_notifications: error while sending updates to a client.\n");
closesocket(s);
return PROG_ERROR;
}
//delete notification
prevItem = currItem;
currItem = currItem->next;
free(prevItem->item);
free(prevItem);
}
closesocket(s);
if(current->deletionMark){
if(prev == NULL)
list->first = current->next;
else
prev->next = current->next;
if(current->next == NULL) list->last = prev;
clientNode *tmp = current;
current = current->next;
ll_free(tmp->updates);
free(tmp->networkData->hostName);
free(tmp->networkData);
free(tmp);
}else{
prev = current;
current = current->next;
}
}
return PROG_SUCCESS;
}
// ===========================================================================
// cnl_print_list
// ===========================================================================
void cnl_print_list(clientNodeList *list){
printf("________CLIENT NODE LIST_____________\n\n");
clientNode *current = list->first;
while(current != NULL){
printf("NODE: %s - marked: %d\n Reg path (%d)\n", current->networkData->hostName, current->deletionMark, current->numRegisteredPaths);
int i;
for(i = 0; i < current->numRegisteredPaths; i++){
printf(" -> %s %c\n", current->registeredPaths[i]->path, current->registeredPaths[i]->mode == RECURSIVE ? 'r' : 'R');
}
current = current->next;
}
}