-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
777 lines (712 loc) · 23.4 KB
/
server.cpp
File metadata and controls
777 lines (712 loc) · 23.4 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <pthread.h>
#include <iterator>
using namespace std;
#define NUM 3
typedef struct
{
int sock;
int client_id;
} connection_holder;
typedef struct
{
int total_clients;
}server_holder;
void initialize_questions(vector <string> & questions)
{
questions[0] = "Which was the first Pokemon to be created by the Pokemon team?";
questions[1] = "What Pokemon does Pikachu evolve into?";
questions[2] = "What type of Pokemon is Mewtwo?";
questions[3] = "The Pokemon Eevee can have multiple evolutions. How many such evolutions exist?";
questions[4] = "If you need to revive your fainted Pokemon to full health, where do you go?";
questions[5] = "What is the full form of the word 'Pokemon'?";
questions[6] = "How many Mega Evolutions does Charizard have?";
questions[7] = "Which is the only Pokemon that can change its gender?";
questions[8] = "Which is the first Pokemon that was able to talk in the anime?";
questions[9] = "Which is the only Pokemon that is able to devolve?";
questions[10] = "Who was the creator of Pokemon?";
questions[11] = "Who is the God of all Pokemon?";
questions[12] = "The Pokemon Hitmonchan got its name from which famous martial artist?";
questions[13] = "Which Pokemon is considered to be a failed attempt of cloning Mew?";
questions[14] = "In the anime, which food item does Ash's Pikachu love?";
questions[15] = "Which Pokemon was originally supposed to be the Mascot for Pokemon Series?";
questions[16] = "Where is Ash's hometown located?";
questions[17] = "Which is the most effective ball in the Pokemon game?";
questions[18] = "In which season of Pokemon Anime does Ash finally win the Pokemon League?";
questions[19] = "What does Ash dream of becoming one day?";
}
void initialize_answers(vector <vector <string> > & answer)
{
answer[0].push_back("rhydon");
answer[0].push_back("rydon");
answer[1].push_back("raichu");
answer[2].push_back("psychic");
answer[3].push_back("8");
answer[3].push_back("eight");
answer[4].push_back("pokemon center");
answer[4].push_back("pokemon centre");
answer[4].push_back("pokecenter");
answer[4].push_back("pokecentre");
answer[4].push_back("pokemoncentre");
answer[4].push_back("pokemoncenter");
answer[4].push_back("pokemon_center");
answer[4].push_back("pokemon_centre");
answer[5].push_back("pocket monster");
answer[5].push_back("pocket monsters");
answer[5].push_back("pocket_monster");
answer[5].push_back("pocket_monsters");
answer[5].push_back("pocketmonster");
answer[5].push_back("pocketmonsters");
answer[6].push_back("2");
answer[6].push_back("two");
answer[7].push_back("azurill");
answer[7].push_back("azuril");
answer[8].push_back("meowth");
answer[9].push_back("slowbro");
answer[10].push_back("satoshi tajiri");
answer[10].push_back("satoshi_tajiri");
answer[11].push_back("arceus");
answer[12].push_back("jackie chan");
answer[12].push_back("jackie_chan");
answer[13].push_back("ditto");
answer[13].push_back("dito");
answer[14].push_back("ketchup");
answer[15].push_back("clefairy");
answer[15].push_back("clifairy");
answer[15].push_back("klefairy");
answer[15].push_back("klifairy");
answer[16].push_back("kanto");
answer[16].push_back("pallet");
answer[16].push_back("pallet town");
answer[16].push_back("kanto town");
answer[16].push_back("pallet_town");
answer[16].push_back("kanto_town");
answer[16].push_back("town pallet");
answer[16].push_back("town kanto");
answer[16].push_back("town_pallet");
answer[16].push_back("town_kanto");
answer[16].push_back("townpallet");
answer[16].push_back("townkanto");
answer[16].push_back("pallettown");
answer[16].push_back("kantotown");
answer[17].push_back("master ball");
answer[17].push_back("master_ball");
answer[17].push_back("masterball");
answer[17].push_back("master");
answer[18].push_back("20");
answer[18].push_back("twenty");
answer[19].push_back("pokemon master");
answer[19].push_back("pokemon_master");
answer[19].push_back("pokemonmaster");
answer[19].push_back("master");
}
void lower_it(string &to_lower)
{
for(int i = 0 ; i < to_lower.size(); i++)
{
int k = to_lower[i];
if(k >= 65 && k <= 90)
{
k = k + 32;
to_lower[i] = k;
}
else
{
continue;
}
}
}
string number_to_string(int num)
{
int is_negative = 0;
string to_return;
if(num < 0)
{
is_negative = 1;
num = num * (-1);
}
while(num / 10 != 0)
{
int k = num % 10;
num = num/10;
to_return.push_back(k + 48);
}
to_return.push_back(num + 48);
if(is_negative == 1)
{
fflush(stdout);
to_return.push_back('-');
}
reverse(to_return.begin(), to_return.end());
return to_return;
}
int is_corrects(int question_id, vector <vector <string> > &answer, string audience_answer)
{
int is_ok = 0;
lower_it(audience_answer);
for(int j = 0 ; j < answer[question_id].size(); j++)
{
if(audience_answer == answer[question_id][j])
{
is_ok = 1;
break;
}
}
return is_ok;
}
int end_signal = 0 ; //0 is quiz is to continue, else 1
int answer_got = 0;
string what_answer;
int signal_client = 0;
string to_send;
int correct_signal = 0; //1 if correct, -1 if wrong and 0 by default (no answer received)
string end_send_loss = ""; //this will be send when the quiz is ended to the non-winning clients
string rand_ans = "";
int buzz_press = 0; //tell the server_process if someone has pressed the buzzer or not (0 if no one has pressed the buzzer, else 1)
vector <int> answer_tracker;
int t_end_signal = 0;
pthread_mutex_t mtx;
void print(string s)
{
pthread_mutex_lock(&mtx);
cout<<s<<"\n";
fflush(stdout);
pthread_mutex_unlock(&mtx);
}
int is_corrects_rand(string audience_answer)
{
int is_ok = 0;
lower_it(audience_answer);
if(audience_answer == rand_ans)
{
is_ok = 1;
}
return is_ok;
}
string num_to_string(int num)
{
int is_negative = 0;
string to_return;
if(num < 0)
{
is_negative = 1;
num = num * (-1);
}
while(num / 10 != 0)
{
int k = num % 10;
num = num/10;
to_return.push_back(k + 48);
}
to_return.push_back(num + 48);
if(is_negative == 1)
{
fflush(stdout);
to_return.push_back('-');
}
reverse(to_return.begin(), to_return.end());
return to_return;
}
string double_to_string(double x)
{
string to_return = "";
if(x < 0)
{
to_return = to_return + "-";
x = x * (-1.0);
}
int k = x;
to_return = to_return + num_to_string(k);
to_return = to_return + ".";
x = (x - k)*10;
to_return = to_return + num_to_string(x);
return to_return;
}
string generator(int number_of_digits, string char_list)
{
vector <int> indices;
while(indices.size() != number_of_digits)
{
int x = rand() % (char_list.size());
indices.push_back(x);
}
string to_return;
vector <int>::iterator it;
for(it = indices.begin(); it != indices.end(); it++)
{
to_return.push_back(char_list[*it]);
}
return to_return;
}
int string_to_num(string s)
{
int x = 0;
for(int i = 0 ; i < s.size(); i++)
{
x = x * 10;
x = x + s[i] - 48;
}
return x;
}
int apply_operator(int left, int right, char op)
{
if(op == '*')
{
return (left * right);
}
else if(op == '-')
{
return (left - right);
}
else
{
return (left + right);
}
}
void generate_expression()
{
rand_ans = "";
srand(time(NULL));
int dig_1 = rand() % 3;
dig_1 = dig_1 + 1;
int dig_2 = rand() % 3;
dig_2 = dig_2 + 1;
int dig_3 = rand() % 3;
dig_3 = dig_3 + 1;
//cout<<dig_1<<" "<<dig_2<<" "<<dig_3<<"\n";
string num_1 = generator(dig_1, "123456789");
string num_2 = generator(dig_2, "123456789");
string num_3 = generator(dig_3, "123456789");
string op_1 = generator(1, "+-*");
string op_2 = generator(1, "+-*");
string final_string = num_1 + op_1 + num_2 + op_2 + num_3;
to_send = to_send + final_string;
//cout<<final_string<<"\n";
//cout<<string_to_num(num_1)<<" "<<string_to_num(num_2)<<" "<<string_to_num(num_3)<<" "<<op_1[0]<<" "<<op_2[0]<<"\n";
int result = 0;
if(op_1[0] == '*')
{
result = apply_operator(string_to_num(num_1), string_to_num(num_2), op_1[0]);
if(op_2[0] == '*')
{
result = apply_operator(result, string_to_num(num_3), op_2[0]);
}
else
{
result = apply_operator(result, string_to_num(num_3), op_2[0]);
}
}
else
{
if(op_2[0] == '*')
{
result = apply_operator(string_to_num(num_2), string_to_num(num_3), op_2[0]);
result = apply_operator(string_to_num(num_1), result, op_1[0]);
}
else
{
result = apply_operator(string_to_num(num_1), string_to_num(num_2), op_1[0]);
result = apply_operator(result, string_to_num(num_3), op_2[0]);
}
}
rand_ans = num_to_string(result);
//cout<<result<<"\n";
}
void * server_process(void * data_server)
{
fflush(stdout);
server_holder * data;
data = (server_holder *) data_server;
srand(time(NULL));
unordered_set <int> question_order;
fflush(stdout);
while(question_order.size() != 20)
{
int x = rand() % 20;
question_order.insert(x);
}
vector <int> question_order_dup;
unordered_set <int>::iterator it;
for(it = question_order.begin(); it != question_order.end(); it++)
{
question_order_dup.push_back(*it);
}
vector <double> scores(3);
vector <string> questions(20);
vector <vector <string> > answer(20);
initialize_questions(questions);
initialize_answers(answer);
int total_clients = data->total_clients;
int i = 0 ;
while(!end_signal)
{
correct_signal = 0;
answer_got = 0;
what_answer = "";
signal_client = 0;
to_send = "";
rand_ans = "";
buzz_press = 0;
usleep(21000000);
correct_signal = 0;
answer_got = 0;
what_answer = "";
signal_client = 0;
to_send = "";
rand_ans = "";
buzz_press = 0;
answer_tracker.push_back(0);
if(i < 20)
{
int question_to_send = question_order_dup[i];
to_send = number_to_string(i + 1);
to_send = to_send + ". ";
to_send = to_send + questions[question_to_send];
signal_client = 1;
print(to_send);
fflush(stdout);
}
else
{
to_send = number_to_string(i + 1);
to_send = to_send + ". Evaluate the expression : ";
generate_expression();
signal_client = 1;
print(to_send);
}
while(1)
{
if(answer_got == 0) //no answer received by the client
{
continue;
}
else if(answer_got == -1) //the buzzer was pressed, but the participant was unable to answer in the time
{
correct_signal = -1;
break;
}
else if(answer_got == -2 && !buzz_press) //timer of 10 seconds is over, nobody was able to answer the question (and no one even pressed the buzzer !)
{
break;
}
else //the buzzer was pressed, and the answer was recieved;
{
if(i <= 19)
{
//print("Client sent answer : " + what_answer);
//print(questions[question_order_dup[i]]);//print()
//print(what_answer);
int is_correct = is_corrects(question_order_dup[i], answer, what_answer);
if(is_correct == 1)
{
correct_signal = 1;
while(correct_signal != 0)
{
continue;
}
}
else
{
correct_signal = -1;
while(correct_signal != 0)
{
continue;
}
}
}
else
{
int is_correct = is_corrects_rand(what_answer); //;
if(is_correct == 1)
{
correct_signal = 1;
while(correct_signal != 0)
{
continue;
}
}
else
{
correct_signal = -1;
while(correct_signal != 0)
{
continue;
}
}
}
break;
}
}
i++;
}
pthread_exit(0);
}
/*
int answer_got = 0;
string what_answer;
int signal_client = 0;
string to_send;
int correct_signal = 0; //1 if correct, -1 if wrong and 0 by default (no answer received)
*/
void * process(void * data_client)
{
int has_answered;
float score = 0;
connection_holder * data;
data = (connection_holder *) data_client;
start:
has_answered = 0;
if(t_end_signal == 1) //close this thread and end program
{
end_send_loss = "### YOU LOST THE QUIZ ###\n" + end_send_loss;
char * end_send = (char *) malloc(sizeof(char) * (end_send_loss.size() + 1));
for(int phi = 0 ; phi < end_send_loss.size() ; phi++)
{
end_send[phi] = end_send_loss[phi];
}
end_send[end_send_loss.size()] = '\0';
send(data->sock, end_send, end_send_loss.size(), 0);
end_signal += 1;
pthread_exit(0);
}
while(signal_client == 0)
{
continue;
}
char * final_send;
final_send = (char*)malloc(sizeof(char) * (to_send.size() + 1));
for(int i = 0 ; i < to_send.size(); i++)
{
final_send[i] = to_send[i];
}
final_send[to_send.size()] = '\0';
send(data->sock, final_send, to_send.size(), 0);
char buzzer_pressed = '1';
recv(data->sock, &buzzer_pressed, sizeof(buzzer_pressed), 0); //received 'p' if pressed
if(buzzer_pressed == '1')
{
has_answered = -2; //no answer by this client
if(buzz_press != 1)
{
answer_got = -2;
}
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " DIDN'T ATTEMPT THE QUESTION !";
print(to_print);
string score_print = "SCORE OF CLIENT ID " + num_to_string(data->client_id) + " IS " + double_to_string(score);
print(score_print);
}
else //the buzzer has been pressed
{
buzz_press = 1;
if(answer_got == 0) //nobody has answered this question
{
char client_response_nstl[2000] = "\0"; //stores response in non stl way
string client_response = "\0";
int bytesReceived = 0;
bytesReceived = recv(data->sock, client_response_nstl, sizeof(client_response_nstl), 0);
if(bytesReceived == -1) //timer of 10 seconds is reached, buzzer pressed but not answered the question
{
has_answered = -1;
answer_got = -1;
}
else
{
for(int j = 0; j < strlen(client_response_nstl) ; j++)
{
client_response.push_back(client_response_nstl[j]);
}
what_answer = client_response;
has_answered = 1;
answer_got = 1;
}
}
else //suppose someone has already pressed the buzzer, then this participant presses buzzer, we need to clear this buffer of data sent by him
{
char client_response_waste[2000] = "\0";
int bytesReceived = 0;
bytesReceived = recv(data->sock, client_response_waste, sizeof(client_response_waste), 0);
}
if(has_answered == -1)
{
if(answer_tracker[answer_tracker.size() - 1] == 1)
{
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " RESPONDED LATE ";
print(to_print);
string score_print = "SCORE OF CLIENT ID " + num_to_string(data->client_id) + " IS " + double_to_string(score);
print(score_print);
goto start;
}
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " DIDN'T ANSWER THE QUESTION IN TIME !";
print(to_print);
score = score - 0.5;
correct_signal = 0;
string score_print = "SCORE OF CLIENT ID " + num_to_string(data->client_id) + " IS " + double_to_string(score);
print(score_print);
answer_tracker[answer_tracker.size() - 1] = 1;
}
else //if(has_answered == 1)
{
if(answer_tracker[answer_tracker.size() - 1] == 1)
{
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " RESPONDED LATE";
print(to_print);
string score_print = "SCORE OF CLIENT ID " + num_to_string(data->client_id) + " IS " + double_to_string(score);
print(score_print);
goto start;
}
while(correct_signal == 0)
{
continue;
}
if(correct_signal == 1)
{
answer_tracker[answer_tracker.size() - 1] = 1;
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " GAVE CORRECT ANSWER !";
print(to_print);
score = score + 1.0;
signal_client = 0;
correct_signal = 0;
string score_print = "SCORE OF CLIENT ID " + num_to_string(data->client_id) + " IS " + double_to_string(score);
print(score_print);
}
else if(correct_signal == -1)
{
answer_tracker[answer_tracker.size() - 1] = 1;
//print(what_answer);
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " GAVE WRONG ANSWER !";
print(to_print);
score = score - 0.5;
correct_signal = 0;
string score_print = "SCORE OF CLIENT ID " + num_to_string(data->client_id) + " IS " + double_to_string(score);
print(score_print);
}
}
/*
else
{
string to_print = "CLIENT ID " + num_to_string(data->client_id) + " DIDN'T ATTEMPT THE QUESTION !";
print(to_print);
}
*/
}
if(score <= 4.9 && end_signal != 1)
{
goto start;
}
else
{
t_end_signal = 1;
if(score >= 5.0)
{
string end_send_stl = "### CLIENT WITH ID " + num_to_string(data->client_id) + " HAS WON THE QUIZ ! ###";
end_send_loss = end_send_stl;
char * end_send = (char *)malloc(sizeof(char) * (end_send_stl.size() + 1));
for(int phi = 0 ; phi < end_send_stl.size(); phi++)
{
end_send[phi] = end_send_stl[phi];
}
end_send[end_send_stl.size()] = '\0';
send(data->sock, end_send, end_send_stl.size(), 0);
end_signal += 1;
}
else
{
end_send_loss = "### YOU LOST THE QUIZ ###\n" + end_send_loss;
char * end_send = (char *) malloc(sizeof(char) * (end_send_loss.size() + 1));
for(int phi = 0 ; phi < end_send_loss.size() ; phi++)
{
end_send[phi] = end_send_loss[phi];
}
end_send[end_send_loss.size()] = '\0';
send(data->sock, end_send, end_send_loss.size(), 0);
end_signal += 1;
}
print("\n-----------THE QUIZ HAS ENDED-------------");
fflush(stdout);
pthread_exit(0);
}
}
int main()
{
pthread_mutex_init(&mtx, 0);
pthread_t thread;
pthread_t server_thread;
int clients_to_connect;
cout<<"Please enter the number of participants in this contest : ";
fflush(stdout);
cin>>clients_to_connect;
server_holder * server_data;
server_data = (server_holder *)malloc(1 * sizeof(server_holder));
server_data->total_clients = clients_to_connect;
//now we start socket programming
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0); //using TCP - reliable
if(server_socket == -1)
{
perror("Could not create socket \n");
}
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(8080); //connecting to port number 8080
server_address.sin_addr.s_addr = INADDR_ANY;
int addrlen = sizeof(server_address);
if(bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
{
perror("Binding failed");
return -1;
}
listen(server_socket, 50); //maximum 50 simultaneous connections allowed
int total_clients = 0;
while(total_clients < clients_to_connect)
{
connection_holder * connection;
connection = (connection_holder *) malloc (1 * sizeof(connection_holder));
connection->client_id = total_clients + 1;
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;
connection->sock = accept(server_socket, (struct sockaddr *) &server_address, (socklen_t * ) &addrlen);
if(connection->sock <= 0)
{
free(connection);
}
else
{
setsockopt(connection->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
//send the client id to the client
string cid_send_stl = num_to_string(connection->client_id);
char * cid_send = (char *) malloc(sizeof(char) * (cid_send_stl.size() + 1));
for(int phi = 0 ; phi < cid_send_stl.size(); phi++)
{
cid_send[phi] = cid_send_stl[phi];
}
cid_send[cid_send_stl.size()] = '\0';
send(connection->sock, cid_send, cid_send_stl.size(), 0);
//start the thread for this client
pthread_create(&thread, 0, process, (void *) connection);
total_clients = total_clients + 1;
}
}
pthread_create(&server_thread, 0, server_process, (void *) server_data);
while(!t_end_signal || (end_signal != clients_to_connect))
{
continue;
}
close(server_socket);
pthread_mutex_destroy(&mtx);
return 0;
}