-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathF4Server.c
More file actions
383 lines (303 loc) · 10.7 KB
/
Copy pathF4Server.c
File metadata and controls
383 lines (303 loc) · 10.7 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
#include "F4.h"
/* Global declarations. */
int shm_matrix_id;
int shm_names;
int shm_info;
int sem_sync;
int sem_mutex;
int another_server = 0;
int * matrix_pointer;
int * shm_info_attach;
int count_sigint = 0;
char symbol[2] = {MATRIX_DEFAULT_CHAR, MATRIX_DEFAULT_CHAR};
void delete_all(){
if (!another_server){
shmctl(shm_matrix_id, IPC_RMID, NULL);
shmctl(shm_info, IPC_RMID, NULL);
semctl(sem_sync, 0, IPC_RMID, NULL);
semctl(sem_mutex, 0, IPC_RMID, NULL);
}
}
/// @brief To handle the errors easier
/// @param string The string to put in perror
void perror_exit_server(char * string){
perror(string);
//delete_all();
exit(-1);
}
/// @brief Handler for the SIGUSR1
/// @param sig The value of the signal
void sigusr1_handler(int sig){
if (shm_info_attach[0]==2){
if (shm_info_attach[9] == symbol[0]){
printf("%c is out of the game!\n", symbol[0]);
printf("%c is the WINNER!\n", symbol[1]);
kill(shm_info_attach[5], SIGUSR2);
}
else{
printf("%c is out of the game!\n", symbol[1]);
printf("%c is the WINNER!\n", symbol[0]);
kill(shm_info_attach[4], SIGUSR2);
}
}
else{
printf("The player closed the game, please restart the server...\n");
}
exit(0);
}
/// @brief Handler for the SIGINTs
/// @param sig The value of the signal
void sigint_handler(int sig){
if (++count_sigint == MAX_SIGINT){ /* End of game. */
printf("End of Game\n");
if (shm_info_attach != NULL){
shm_info_attach[9] = SECOND_SIGINT;
if (shm_info_attach[4] != 0 && shm_info_attach[5]!=0){
kill(shm_info_attach[4], SIGUSR1);
kill(shm_info_attach[5], SIGUSR1);
}
}
exit(-1);
}else if (count_sigint == 1){ /* Warning. */
printf("Next CTRL+C will kill the game!\n");
}
}
/// @brief Handler for the SIGHUP
/// @param sig The value of the signal
void sighup_handler(int sig){
count_sigint = MAX_SIGINT - 1;
kill(getpid(), SIGINT);
exit(-1);
}
/// @brief Initializes the info matrix
/// @param shm_info_attach the pointer to the matrix
/// @param N the number of rows
/// @param M the number of columns
/// @param timer the value of the timer
void shm_info_init(int * shm_info_attach, int N, int M, int timer){
shm_info_attach[0] = 0;
shm_info_attach[1] = symbol[0];
shm_info_attach[2] = symbol[1];
shm_info_attach[3] = getpid();
shm_info_attach[4] = 0;
shm_info_attach[5] = 0;
shm_info_attach[6] = shm_matrix_id;
shm_info_attach[7] = N;
shm_info_attach[8] = M;
shm_info_attach[9] = RIS_DEFAULT_VALUE;
shm_info_attach[10] = timer;
shm_info_attach[11] = 0;
}
int main(int argc, char *argv[])
{
/* Cheking if the number of argument is correct. */
if (argc != 5){
printf("Parameters Error.\n");
printf("The correct sintax should be: ./F4Server n_rows n_colums symbol1 symbol2 \n");
return -1;
}
/* Getting the info from the command line and checking if they are acceptable. */
if(!isNumeric(argv[1])){
printf("The number of Rows is not a number...\n");
exit(-1);
}
const int N = string_to_int(argv[1]);
if (N < 5){
printf("Number of Rows not acceptable...\n");
exit(-1);
}
if(!isNumeric(argv[2])){
printf("The number of Columns is not a number...\n");
exit(-1);
}
const int M = string_to_int(argv[2]);
if (M < 5){
printf("Number of Columns not acceptable...\n");
exit(-1);
}
/* Checking if the symbols are valid. */
if((strlen(argv[3])==1 && strlen(argv[4])==1) && (argv[3][0]!=argv[4][0])){
symbol[0] = argv[3][0];
symbol[1] = argv[4][0];
}
else{
printf("The symbols must be 1 char each and different!\n");
exit(-1);
}
/* Forza4 Logo. */
print_banner();
/* Setting the exit function to delete all the ipcs */
if (atexit(delete_all) == -1){
perror("Error setting delete_all up...");
exit(-1);
}
/* Handling all the signals. */
if (signal(SIGINT, sigint_handler) == SIG_ERR){
perror("Error Handling CTRL+C!\n");
exit(-1);
}
if (signal(SIGHUP, sighup_handler) == SIG_ERR){
perror("Error Handling SIGHUP signal!\n");
exit(-1);
}
if (signal(SIGUSR1, sigusr1_handler) == SIG_ERR){
perror("Error Handling SIGUSR1!\n");
exit(-1);
}
/* Setting the timer for the turns. */
int timer;
int checkValidTimer;
do{
checkValidTimer = 1;
printf("Insert the number of seconds for each turn (0 for no timer): ");
if(!scanf(" %d", &timer)){ /*If the user inserts a string*/
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
checkValidTimer = 0;
printf("The timer should be a number...\n");
}
if (timer<0){
printf("The timer cannot be negative...\n");
checkValidTimer = 0;
}
}while(!checkValidTimer);
/* Sops for the sync semaphores. */
struct sembuf sops[3];
/* Used for locking operations on myself. */
sops[0].sem_num = 2;
sops[0].sem_op = -2; /* Subtract 1 from server semaphore. */
sops[0].sem_flg = 0;
/* Used for unlocking operations on the first client. */
sops[1].sem_num = 0;
sops[1].sem_op = +1; /* Add 1 from client1 semaphore. */
sops[1].sem_flg = 0;
/* Used for unlocking operations on the second client. */
sops[2].sem_num = 1;
sops[2].sem_op = +1; /* Add 1 from client2 semaphore. */
sops[2].sem_flg = 0;
int shm_info_key = ftok("key.txt", SHMINFO_KEY);
/* Creating the shm that store various information. */
if ((shm_info = shmget(shm_info_key, sizeof(int) * 12, IPC_CREAT | 0777 | IPC_EXCL)) == -1){
if (errno == EEXIST){
printf("Another server already exists!\n");
another_server = 1;
exit(-1);
}
perror_exit_server("Creating the Info Shared Memory...");
}
/* Creating the shm that store the matrix. */
if ((shm_matrix_id = shmget(IPC_PRIVATE, sizeof(int) * N * M, IPC_CREAT | 0777 | IPC_EXCL)) == -1){
perror_exit_server("Matrix Shared Memory...");
}
/* Attaching to the shm that store the matrix. */
if ((matrix_pointer = (int *) shmat(shm_matrix_id, NULL, 0)) == NULL){
perror_exit_server("Matrix Shared Memory Attach...");
}
/* Initializing the matrix. */
matrix_init(matrix_pointer, N, M);
printf("Initializing matrix...\n");
/* Attaching to the previous shm. */
if ((shm_info_attach = (int *) shmat(shm_info, NULL, 0)) == NULL){
perror_exit_server("Info Shared Memory Attach...");
}
/* Putting info into the shm. */
shm_info_init(shm_info_attach, N, M, timer);
printf("Initializing infos...\n");
int sem_sync_key = ftok("key.txt", SEMSYNC_KEY);
/* Creating the sem sync semaphore. */
if ((sem_sync = semget(sem_sync_key, 3, IPC_CREAT | IPC_EXCL | 0777))== -1){
perror_exit_server("Semaphore Sync...");
}
/* Union for setting all the semaphores previosly created. */
union semun arg;
unsigned short values[3] = {0, 0, 0};
arg.array = values;
/* Setting all the semsync semaphores. */
if ((semctl(sem_sync, 0, SETALL, arg)) == -1){
perror_exit_server("Assigning Sem Sync...");
}
printf("Setting semsync...\n");
int sem_mutex_key = ftok("key.txt", SEMMUTEX_KEY);
/* Setting the mutex semaphore. */
if ((sem_mutex = semget(sem_mutex_key, 1, IPC_CREAT | IPC_EXCL | 0777))== -1)
perror_exit_server("Semaphore Mutex...");
arg.val = 1;
if ((semctl(sem_mutex, 0, SETVAL, arg)) == -1){
perror_exit_server("Assigning Sem Mutex...");
}
printf("Setting semmutex...\n");
printf("Waiting for players...\n");
if ((semop(sem_sync, &sops[0], 1)) == -1){
if (errno == EINTR){
if (semop(sem_sync, &sops[0], 1) == -1)
perror_exit_server("Error locking myself...");
}else perror_exit_server("Error locking myself...");
}
/* Declarations. */
int turn;
int win;
while(1){
turn = 0;
sops[0].sem_op = -1;
shm_info_attach[11] = 0;
while(1){
/* Unlocking the client corresponding to the current turn. */
if (semop(sem_sync, &sops[(turn % 2) + 1], 1) == -1)
perror_exit_server("Error waking the current client...");
/* Locking myself and handling if a signal make the semop fails. */
if (semop(sem_sync, &sops[0], 1) == -1){
if (errno == EINTR){
if (semop(sem_sync, &sops[0], 1) == -1)
break;
}
else
perror_exit_server("Error locking myself...");
}
/* Checking if either client has won. */
win = check_winner(matrix_pointer, N, M, symbol[turn%2]);
if ( win == 1){
printf("Game over! The winner is: %c\n", symbol[turn%2]);
break;
}else if(win == -1){
printf("The matrix is full!\n");
break;
}
turn ++;
}
if (win == -1)
shm_info_attach[9] = MATRIX_IS_FULL;
else
shm_info_attach[9] = symbol[turn%2];
kill(shm_info_attach[4], SIGUSR1);
kill(shm_info_attach[5], SIGUSR1);
printf("Waiting for clients' decision...\n");
sops[0].sem_op = -2;
/* Locking myself. */
if ((semop(sem_sync, &sops[0], 1)) == -1){
if (errno == EINTR){
if (semop(sem_sync, &sops[0], 1) == -1){
perror_exit_server("Locking Server...\n");
}
}else{
perror_exit_server("Locking Server...");
}
}
/* Handling if one player doesn't wanna play anymore. */
if(shm_info_attach[11] != 2){
shm_info_attach[9] = CLIENT_QUIT;
kill(shm_info_attach[4], SIGUSR1);
kill(shm_info_attach[5], SIGUSR1);
break;
}
/* Initializing the matrix. */
matrix_init(matrix_pointer, N, M);
}
printf("Game Over!\n");
//delete_all();
return 0;
}
/************************************
*VR471346, VR471414, VR471404
*Aldegheri Alessandro, Venturi Davide, Zerman Nicolò
*15/04/2023
*************************************/