-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_ops.c
190 lines (175 loc) · 5.02 KB
/
sys_ops.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
#include "sys_ops.h"
void malloc_users_passwords(char ***user_names, char ***passwords){
int name_len = 10;
int pass_len = 10;
*(user_names) = (char **)malloc(sizeof(char *) * user_count);
*(passwords) = (char **)malloc(sizeof(char *) * user_count);
// # pragma omp parallel
for(int i=0; i < user_count; i++){
*(*(user_names)+i) = (char *)malloc(sizeof(char) * name_len);
*(*(passwords)+i) = (char *)malloc(sizeof(char) * pass_len);
}
}
void malloc_combinations(char ***combinations){
int phrase_len = 20;
*combinations = (char **)malloc(sizeof(char *) * comb_count);
for(int i=0; i < comb_count; i++){
*(*(combinations)+i) = (char *)malloc(sizeof(char) * phrase_len);
}
}
void malloc_leaderboard(lb **leaderboard){
*(leaderboard) = (lb *)malloc(sizeof(lb) * user_count);
for(int i=0; i < user_count; i++){
((*leaderboard)+i)->user = (char *)(malloc(sizeof(char) * 10));
}
}
void free_memory_users_passwords(char ***user_names, char ***passwords){
for(int i=0; i < user_count; i++){
free(*(*(user_names)+i));
free(*(*(passwords)+i));
}
free(*(user_names));
free(*(passwords));
}
void free_memory_combinations(char ***combinations){
for(int i=0; i < comb_count; i++){
free(*(*(combinations)+i));
}
free(*(combinations));
}
void setup_threadpool(pthread_t *t_pool, int *thread_id, int numThreads){
for(int i=0; i < numThreads; i++){
*(thread_id+i) = i;
if(pthread_create(&t_pool[i], NULL, game_requests_loop, (thread_id+i)) !=0){
printf("Error in creating thread number %d\n", i);
exit(-1);
}
}
}
void destroy_threadpool(pthread_t *t_pool, int *thread_id, int numThreads){
// for(int i=0; i < numThreads; i++){
// *(thread_id+i) = i;
// pthread_kill(t_pool[i], SIGSTOP);
// }
}
void* game_requests_loop(void *args){
int thread_id = *(int *)args;
int rc = pthread_mutex_lock(&request_mutex);
while(1) {
if(num_requests > 0) {
request *req = get_request();
if(req != NULL){
rc = pthread_mutex_unlock(&request_mutex);
int *status = (int *)play_game(req);
if(*status == 0){
printf("Something went wrong with a client session.\n");
}
else if(*status == 1){
printf("Client quit the session.\n");
}
free(req);
rc = pthread_mutex_lock(&request_mutex);
}
}
else{
rc = pthread_cond_wait(&got_request, &request_mutex);
}
}
}
void add_request(request *new){
int rc = pthread_mutex_lock(&request_mutex);
if(num_requests == 0) {
requests = new;
}
else if( num_requests > 0) {
new->next = requests;
requests = new;
}
num_requests++;
printf("Request queued ....\n");
rc = pthread_mutex_unlock(&request_mutex);
rc = pthread_cond_signal(&got_request);
}
request* get_request(){
request *pop;
int rc = pthread_mutex_lock(&request_mutex);
if(num_requests > 0){
pop = requests;
if(requests->next != NULL){
requests = requests->next;
}
num_requests--;
}
else{
pop = NULL;
}
rc = pthread_mutex_unlock(&request_mutex);
return pop;
}
char * read_segment(int *connfd){
int n, i = 0;
char *ch;
ch = (char *)malloc(sizeof(char));
memset(ch, '0', 1);
char *buffer;
buffer = (char *)malloc(sizeof(char) * 256);
memset(buffer, '0', 256);
while(1){
n = recv(*connfd, ch, 1, 0);
if (n == 0){
return NULL;
}
else if(n == -1){
printf("Receiving failed ....\n");
return NULL;
}
if(*ch != '#'){
*(buffer+i) = *ch;
}
else{
*(buffer+i) = '\0';
break;
}
i++;
}
return buffer;
}
int send_segment(int *connfd, char *msg, int msg_len){
char terminate = '#';
int n = 0;
for(int i=0; i < msg_len; i++){
if ((n = send(*connfd, (msg+i), 1, 0)) <= 0){
printf("Sending failed ....\n");
return 0;
}
}
if ((n = send(*connfd, &terminate, 1, 0)) <=0 ){
printf("Segment termination failed ....\n");
return 0;
}
return 1;
}
void initialise_leaderboard(char ***usernames){
for(int i =0; i < user_count; i++){
strcpy(((leaderboard+i)->user), (*(*(usernames)+i)));
(leaderboard+i)->won = 0;
(leaderboard+i)->played = 0;
}
}
void close_connections() {
for(int i=0; i<CONN_LIMIT; i++){
// close(connfd[i]);
// shutdown(connfd[i], SHUT_RDWR);
int sent = 0;
char *flag = "5";
if (*(connfd+i) != 0){
send_segment((connfd+i), flag, strlen(flag));
}
}
}
void clear_pending_requests(){
while(num_requests > 0){
request *req = get_request();
free(req);
}
}