-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client_List.c
123 lines (82 loc) · 2.4 KB
/
Client_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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "Client_List.h"
struct client_list* new_list(char* IP, int port){ //Constructor function
struct client_list* new=malloc(sizeof(struct client_list));
strcpy(new->IP,IP);
new->port=port;
new->next=NULL;
return (new);
}
int find_client(struct client_list* ct, char* str, int realport ){//Finding a client in the list
int found=0;
if(ct==NULL){
printf("List is empty...\n");
}
while(ct != NULL){
if( (strcmp(ct->IP,str)==0) && (ct->port == realport) ){
found=1;
}
ct=ct->next;
}
return found;
}
struct client_list* get_last(struct client_list* ct){ //Getting the last node of the list
while(ct->next != NULL){
ct = ct->next;
}
return ct;
}
struct client_list* Add_Node(char* temp,int port){ //Adding a new node in the list
struct client_list* new_node=malloc(sizeof(struct client_list));
strcpy(new_node->IP,temp);
new_node->port=port;
new_node->next=NULL;
return new_node;
}
int get_count(struct client_list* ct, char* str, int realport ){//Getting how many clients will be send in the GET_CLIENTS message, excluding the client who sent the request
int counter=0;
while(ct != NULL){
if( (strcmp(ct->IP,str)==0) && (ct->port == realport) ){ //Skip the client who sent the request
ct=ct->next;
continue;
}
counter++;
ct=ct->next;
}
return counter;
}
void Removal(struct client_list* iroot, char* str, int realport){ //Removing a single node from the list
struct client_list* temp=iroot;
struct client_list* prev=NULL;
while(temp->next!=NULL ){
if(strcmp(temp->IP,str)==0 && realport==temp->port ){
break;
}
prev=temp;
temp=temp->next;
}
if(strcmp(temp->IP,str)==0 && realport==temp->port ){
if(prev==NULL){
struct client_list* tnext=temp->next;
free(temp);
iroot=tnext;
}else{
struct client_list* tnext=temp->next;
free(temp);
prev->next=tnext;
}
}
}
void DestroyClientList(struct client_list* croot){ //Destructor function
struct client_list* itemp2=croot;
struct client_list* icurr=itemp2;
struct client_list* inext;
while(icurr !=NULL){
inext=icurr->next;
free(icurr);
icurr=inext;
}
croot=NULL;
}