-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasterServer.cpp
216 lines (202 loc) · 7.29 KB
/
masterServer.cpp
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
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/time.h>
#include<unistd.h>
#include<thread>
#include<mutex>
#include<libgen.h>
#define PORT 9122
#define MAXCL 100
using namespace std;
std::mutex mtx;
int clientList[MAXCL];
int clientsocks[MAXCL];
int sendsocks[MAXCL];
int leaders[MAXCL];
int no_of_leaders;
int n, k, initial, a, b, nc;
int flag = 1;
struct socmsg{
int id;
int soc;
};
struct nclient{
int pid;
int csock;
};
void mserver(){
int opt = 1;
int serverSocket, addrl, tempSocket, maxClients = MAXCL, activity, i, valread, socdescp, temp;
int maxsd;
struct sockaddr_in ipaddr;
fd_set readfds;
for (i = 0; i < maxClients; i++)clientList[i] = 0, clientsocks[i] = 0;
serverSocket = socket(AF_INET , SOCK_STREAM, 0);
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
ipaddr.sin_family = AF_INET;
ipaddr.sin_addr.s_addr = INADDR_ANY;
ipaddr.sin_port = htons( PORT );
if(bind(serverSocket, (struct sockaddr *)&ipaddr, sizeof(ipaddr))<0){
perror("bind failed");
exit(EXIT_FAILURE);
}
// cout << "Listener on port :" << PORT << endl;
listen(serverSocket, 20);
addrl = sizeof(ipaddr);
// cout << "Waiting for connections ..." << endl;
while(flag){
FD_ZERO(&readfds);
FD_SET(serverSocket, &readfds);
maxsd = serverSocket;
for(i=0;i<maxClients;i++){
socdescp = clientList[i];
if(socdescp > 0)FD_SET(socdescp, &readfds);
if(socdescp > maxsd)maxsd = socdescp;
}
activity = select( maxsd + 1 , &readfds , NULL , NULL , NULL);
if(FD_ISSET(serverSocket, &readfds))
{
tempSocket = accept(serverSocket, (struct sockaddr *)&ipaddr, (socklen_t*)&addrl);
// cout << "New connection, socket fd is :"<< tempSocket <<", ip is :" << inet_ntoa(ipaddr.sin_addr) <<", port is :" << ntohs(ipaddr.sin_port) << endl;
for(i=0;i<maxClients;i++){
if(clientList[i] == 0){
struct nclient nc;
struct socmsg sm;
nc.pid = i;
clientList[i] = tempSocket;
// cout << "New process added with pid:" << i << endl;
send(tempSocket, &i, sizeof(int), 0);
read(tempSocket, &clientsocks[i], sizeof(int));
nc.csock = clientsocks[i];
send(tempSocket, &clientsocks, sizeof(int)*MAXCL, 0);
send(tempSocket, &n, sizeof(int), 0);
send(tempSocket, &n, sizeof(int), 0);
send(tempSocket, &initial, sizeof(int), 0);
send(tempSocket, &k, sizeof(int), 0);
send(tempSocket, &a, sizeof(int), 0);
send(tempSocket, &b, sizeof(int), 0);
// send(tempSocket, &no_of_leaders, sizeof(no_of_leaders), 0);
send(tempSocket, &leaders, sizeof(leaders), 0);
for(int j=0;j<maxClients;j++){
if(clientList[j]>0 && j!= i){
send(clientList[j], &nc, sizeof(struct nclient), 0);
}
}
// cout << "All proceess intimidated of new Process " << i << "." << endl;
break;
}
}
}
for(i=0;i<maxClients;i++){
socdescp = clientList[i];
if(FD_ISSET(socdescp, &readfds)){
if((valread = read(socdescp, &temp, sizeof(int))) == 0){
getpeername(socdescp, (struct sockaddr*)&ipaddr, (socklen_t*)&addrl);
//cout << "Process " << i <<" disconnected." << endl;
close(socdescp);
clientList[i] = 0;
clientsocks[i] = 0;
struct nclient dc;
dc.pid = i;
dc.csock = 0;
for(int j=0;j<maxClients;j++){
if(clientsocks[j]>0 && j!=i)send(clientList[j], &dc, sizeof(struct nclient), 0);
}
//cout << "All proceess intimidated of process " << i << " terminating!" << endl;
}
else if(temp == 999){
nc--;
if(nc<=0){
struct nclient dc;
dc.pid = 999;
dc.csock = 0;
for(int j=0;j<maxClients;j++){
if(clientsocks[j]>0)send(clientList[j], &dc, sizeof(struct nclient), 0);
}
}
}
}
}
}
}
void logPrint(){
int opt = 1;
int serverSocket, addrl, tempSocket, clientList[MAXCL], maxClients = MAXCL, activity, i, valread, socdescp;
int maxsd;
int msgd[MAXCL];
double resp_time[MAXCL];
struct sockaddr_in ipaddr;
char buffer[65536];
fd_set readfds;
char msg[1024];
for (i = 0; i < maxClients; i++){
clientList[i] = 0;
}
serverSocket = socket(AF_INET , SOCK_STREAM, 0);
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
ipaddr.sin_family = AF_INET;
ipaddr.sin_addr.s_addr = INADDR_ANY;
ipaddr.sin_port = htons( 9123 );
if(bind(serverSocket, (struct sockaddr *)&ipaddr, sizeof(ipaddr))<0){
perror("bind failed");
exit(EXIT_FAILURE);
}
listen(serverSocket, 20);
addrl = sizeof(ipaddr);
while(flag){
FD_ZERO(&readfds);
FD_SET(serverSocket, &readfds);
maxsd = serverSocket;
for(i=0;i<maxClients;i++){
socdescp = clientList[i];
if(socdescp > 0)FD_SET(socdescp, &readfds);
if(socdescp > maxsd)maxsd = socdescp;
}
activity = select( maxsd + 1 , &readfds , NULL , NULL , NULL);
if(FD_ISSET(serverSocket, &readfds)){
tempSocket = accept(serverSocket, (struct sockaddr *)&ipaddr, (socklen_t*)&addrl);
sprintf(msg, "Connection Established!\n");
send(tempSocket, msg, strlen(msg), 0);
read(tempSocket, &msgd[--nc], sizeof(msgd[0]));
read(tempSocket, &resp_time[nc], sizeof(resp_time[0]));
valread = read(tempSocket, buffer, 65536);
buffer[valread] = '\0';
cout << buffer << endl;
if(nc<=0){
double sum = 0;
for(int x=0;x<n;x++){
sum += msgd[x];
}
cout << "Average Message Complexity in System is " << sum/n << endl;
sum = 0;
for(int x=0;x<n;x++){
sum += resp_time[x];
}
cout << "Average Response Time in System is " << sum/n << "secs." << endl;
flag = 0;
}
}
}
}
int main(){
scanf("%d%d%d%d%d", &n, &k, &initial, &a, &b);
scanf("%d", &no_of_leaders);
for(int i = 0; i < MAXCL; i++)
leaders[i] = 0;
for(int i = 0; i < n; i++){
int temp;
scanf("%d", &temp);
leaders[temp] = 1;
}
nc = n;
std::thread t1(mserver);
std::thread t2(logPrint);
t1.detach();
t2.join();
return 0;
}