-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller.cpp
285 lines (227 loc) · 6.7 KB
/
poller.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
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
// PANAGIOTIS KONTOEIDIS
// 1115201900266
#include "libs_poller.hpp"
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf(" Please give port number \n ");
exit(1);
}
int port, serversock, clientsock;
int buff_size, num_threads;
int status;
struct sockaddr_in server, client;
socklen_t clientlen;
struct sockaddr *serverptr = (struct sockaddr *)&server;
struct sockaddr *clientptr = (struct sockaddr *)&client;
struct hostent *rem;
static struct sigaction sa;
sa.sa_handler = signal_handler;
sigemptyset(&(sa.sa_mask));
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
port = atoi(argv[1]);
num_threads = atoi(argv[2]);
buff_size = atoi(argv[3]);
const char *poll_log_name = argv[4];
poll_stat_name = argv[5];
if (num_threads > THR_LIMIT)
{
cout << "Too many threads" <<endl;
exit(-1);
}
poll_log_file = open(poll_log_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); // create poll log file
if (poll_log_file == -1)
{
perror(" failed to open file");
}
if ((serversock = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* Create socket */
perror(" failed to create socket ");
server.sin_family = AF_INET; /* Internet domain */
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port); /* The given port */
if (bind(serversock, serverptr, sizeof(server)) < 0) /* Bind socket to address */
perror(" failed to bind ");
if (listen(serversock, 128) < 0) /* Listen for connections */
perror(" failed to listen ");
printf(" Listening for connections to port % d \n ", port);
// Initialise mutexes
pthread_mutex_init(&buffer_lock, 0);
pthread_mutex_init(&names_lock, 0);
pthread_mutex_init(&parties_lock, 0);
pthread_mutex_init(&votes_lock, 0);
pthread_mutex_init(&poll_lock, 0);
pthread_cond_init(&buffer_nonempty, 0);
pthread_cond_init(&buffer_nonfull, 0);
pthread_t *threads = new pthread_t[num_threads]; // create matrix of threads
for (int i = 0; i < num_threads; i++) // create threads recursively
{
if (pthread_create(&threads[i], NULL, get_vote, NULL) < 0)
perror(" failed to create thread ");
}
while ((clientsock = accept(serversock, clientptr, &clientlen)) > 0)
{
int *client_ptr = new int[sizeof(int)];
*client_ptr = clientsock;
pthread_mutex_lock(&buffer_lock);
while (buffer.size() == buff_size) // wait for worker to empty buffer
{
pthread_cond_wait(&buffer_nonfull, &buffer_lock);
}
buffer.push(client_ptr); // then push new connection to buffer
pthread_cond_signal(&buffer_nonempty);
pthread_mutex_unlock(&buffer_lock);
}
for (int i = 0; i < num_threads; i++) // wait for threads to join
{
if (pthread_join(threads[i], (void **)&status) < 0)
perror(" failed to join");
}
pthread_exit(NULL);
close(serversock);
delete[] threads;
return 1;
}
void *get_vote(void *arg)
{
while (1)
{
pthread_mutex_lock(&buffer_lock);
while (buffer.size() == 0) // wait if buffer is empty
{
pthread_cond_wait(&buffer_nonempty, &buffer_lock);
}
int *client_sock = buffer.front();
buffer.pop();
pthread_cond_signal(&buffer_nonfull);
pthread_mutex_unlock(&buffer_lock);
char buf[1];
char buf2[1];
int *newsock = client_sock;
string s4 = "SEND NAME PLEASE\n";
if (send(*newsock, s4.c_str(), s4.length(), 0) < 0)
perror("write problem");
string temp_party;
string temp_name;
while (read(*newsock, buf, 1) > 0)
{ /* Receive 1 char */
if (buf[0] != '\n' && buf[0] != '\r')
temp_name.push_back(buf[0]);
if (buf[0] == '\n')
{
temp_name.push_back('\0');
break;
}
}
pthread_mutex_lock(&names_lock);
if (!names.empty())
{
if (find(names.begin(), names.end(), temp_name) != names.end())
{
string s5 = "ALREADY VOTED\n";
write(*newsock, s5.c_str(), s5.size());
printf("Closing connection .\n ");
pthread_mutex_unlock(&names_lock); // unlock names lock
close(*newsock); /* Close socket */
continue;
}
else
{
names.push_back(temp_name);
}
}
else
{
names.push_back(temp_name);
}
pthread_mutex_unlock(&names_lock);
write(*newsock, "SEND VOTE PLEASE\n", 18);
while (read(*newsock, buf2, 1) > 0)
{
putchar(buf2[0]);
if (buf2[0] != '\n' && buf2[0] != '\r')
temp_party.push_back(buf2[0]);
if (buf2[0] == '\n')
{
temp_party.push_back('\0');
break;
}
}
pthread_mutex_lock(&parties_lock);
if (!political_parties.empty()) // political parties vector gets all political parties without duplicates
{
if (find(political_parties.begin(), political_parties.end(), temp_party) == political_parties.end())
{
political_parties.push_back(temp_party);
}
}
else
{
political_parties.push_back(temp_party); // add to political parties vector
}
pthread_mutex_unlock(&parties_lock);
pthread_mutex_lock(&votes_lock);
votes.push_back(make_pair(temp_name, temp_party)); // add to votes
pthread_mutex_unlock(&votes_lock);
pthread_mutex_lock(&poll_lock);
char s[temp_name.size() + temp_party.size() + 1];
strcpy(s, temp_name.c_str());
strcat(s, " ");
strcat(s, temp_party.c_str());
strcat(s, "\n");
write(poll_log_file, s, strlen(s));
pthread_mutex_unlock(&poll_lock);
string s2 = "VOTE for Party " + temp_party + " RECORDED\n";
write(*newsock, s2.c_str(), s2.size());
temp_name.clear();
temp_party.clear();
printf("Closing connection .\n ");
close(*newsock); /* Close socket */
}
pthread_exit(NULL);
return NULL;
}
void signal_handler(int signalnum)
{
poll_stat_file = open(poll_stat_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); // create poll log file
if (!votes.empty()) // if there are votes
{
if (political_parties.empty())
{
string s = "TOTAL 0";
write(poll_stat_file, s.c_str(), s.size());
exit(1);
}
else
{
vector<pair<string, int>> res; // res contains party name and how many times counted
int total = 0; //total votes
for (int i = 0; i < political_parties.size(); i++) // for every political party find its number of occurances in the votes vector
{
int count = 0;
for (int k = 0; k < votes.size(); k++)
{
if (political_parties[i] == votes[k].second)
count++;
}
res.push_back(make_pair(political_parties[i], count));
total += count;
}
sort(res.begin(), res.end(), compare);
for (int i = 0; i < res.size(); i++)
{
string s = res[i].first + " " + to_string(res[i].second) + "\n"; // write the number of votes for every party in poll-stat
write(poll_stat_file, s.c_str(), s.size());
}
string s2 = "TOTAL " + to_string(total) + "\n";
write(poll_stat_file, s2.c_str(), s2.size());
}
}
cout << "\n\nServer is Terminated successfully" << endl;
exit(1);
}
bool compare(pair<string,int> &a, pair<string,int> &b) // for sorting vector
{
return a.second > b.second;
}