-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
335 lines (268 loc) · 7.85 KB
/
server.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <semaphore.h>
#include "server.h"
sem_t mapLock;
static inline int makeRandomInt(int max, int min) {
return min + rand() % (max - min + 1);
}
// Thread entry function to handle each client
void* handleClient(void *arg) {
tmpDGIST* tmpDG = (tmpDGIST*)arg;
DGIST* dgist = tmpDG->dgist;
client_info* client;
int client_socket;
pthread_t tid;
int pId = tmpDG->cIndex;
client = &(dgist->players[pId]);
client_socket = client->socket;
ClientAction cAction;
free(tmpDG);
int row, col, valRead;
while (1) {
if ((valRead = read(client_socket, &cAction, sizeof(ClientAction))) == 0) {
printf("Client disconnected, socket fd is %d\n", client_socket);
close(client_socket);
pthread_exit(NULL);
}
row = cAction.row;
col = cAction.col;
if(row != client->row || col != client->col){
sem_wait(&mapLock);
if(row >= 0 && row < MAP_ROW && col >= 0 && col < MAP_COL){
switch ((dgist->map[row][col]).item.status) {
case nothing:
printf("Player%d reach (%d, %d)\n", pId+1, row, col);
client->row = row;
client->col = col;
break;
case item:
printf("Player%d reach (%d, %d) and get %dpoints\n", pId+1, row, col,(dgist->map[row][col]).item.score);
client->score += (dgist->map[row][col]).item.score;
(dgist->map[row][col]).item.status = nothing;
client->row = row;
client->col = col;
break;
case trap:
printf("Player%d reach (%d, %d) and get trap\n", pId+1, row, col);
client->score -= SCORE_DEDUCTION;
(dgist->map[row][col]).item.status = nothing;
client->row = row;
client->col = col;
break;
}
if (client->bomb > 0 && cAction.action == setBomb){
(dgist->map[row][col]).item.status = trap;
client->bomb -= 1;
}
}
sem_post(&mapLock);
pthread_create(&tid, NULL, broadcastInformation, (void *)dgist);
pthread_join(tid, NULL);
printMap(dgist);
printPlayer(dgist);
}
}
return NULL;
}
int setItem(DGIST* dPtr) {
int row, col, score;
sem_wait(&mapLock);
do {
row = makeRandomInt(MAP_ROW - 1, 0);
col = makeRandomInt(MAP_COL - 1, 0);
} while (dPtr->map[row][col].item.status != nothing);
score = makeRandomInt(MAX_SCORE, 1);
if(((row-col == 0) && (row == 4 || row == 0))){
sem_post(&mapLock);
return 0;
}
dPtr->map[row][col].item.status = item;
dPtr->map[row][col].item.score = score;
sem_post(&mapLock);
printMap(dPtr);
return 1;
}
// Broadcast map information to all clients
void* broadcastInformation(void* arg) {
DGIST* dgist = (DGIST*)arg;
client_info client;
int client_socket;
sem_wait(&mapLock);
for (int i = 0; i < MAX_CLIENTS; i++) {
client = (dgist->players)[i];
if(client.socket != -1){
client_socket = client.socket;
send(client_socket, dgist, sizeof(DGIST), 0);
}
}
sem_post(&mapLock);
return NULL;
}
void* handleItem(void* arg) {
DGIST* dgist = (DGIST*)arg;
pthread_t tid;
while (1) {
sleep(SETTING_PERIOD);
setItem(dgist);
pthread_create(&tid, NULL, broadcastInformation, (void *)dgist);
pthread_join(tid, NULL);
}
}
// Print the map
void* printMap(void* arg) {
DGIST* dgist = (DGIST*)arg;
Item tmpItem;
printf("==========PRINT MAP==========\n");
sem_wait(&mapLock);
for (int i = 0; i < MAP_ROW; i++) {
for (int j = 0; j < MAP_COL; j++) {
tmpItem = (dgist->map[i][j]).item;
switch (tmpItem.status) {
case nothing:
printf("- ");
break;
case item:
printf("%d ", tmpItem.score);
break;
case trap:
printf("x ");
break;
}
}
printf("\n");
}
sem_post(&mapLock);
printf("==========PRINT DONE==========\n");
return NULL;
}
void printPlayer(void* arg){
DGIST* dgist = (DGIST*)arg;
client_info client;
printf("==========PRINT PLYAERS==========\n");
for(int i=0; i < MAX_CLIENTS; i++){
client = dgist->players[i];
printf("++++++++++Player %d++++++++++\n",i+1);
printf("Location: (%d,%d)\n",client.row, client.col);
printf("Score: %d\n",client.score);
printf("Bomb: %d\n",client.bomb);
}
printf("==========PRINT DONE==========\n");
}
int main(int argc, char *argv[]) {
int server_fd;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
pthread_t tid;
int numClient = 0;
srand(time(NULL));
if (argc != 2) {
fprintf(stderr, "Usage: %s <number>\n", argv[0]);
return 1;
}
const int PORT = atoi(argv[1]);
// Initialize semaphore
sem_init(&mapLock, 0, 1);
// Racing map + player information
DGIST dgist;
// Initialize map
sem_wait(&mapLock);
for (int i = 0; i < MAP_ROW; i++) {
for (int j = 0; j < MAP_COL; j++) {
(dgist.map[i][j]).row = i;
(dgist.map[i][j]).col = j;
(dgist.map[i][j]).item.status = nothing;
}
}
sem_post(&mapLock);
for (int i = 0; i < MAX_CLIENTS; i++) {
dgist.players[i].socket = -1;
}
// Place items in the map
int k = 0;
while(k < INITIAL_ITEM){
k += setItem(&dgist);
}
printf("SERVER DATA INITIALIZING COMPLETE\n");
// Setup socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Server IP address:%s\n", inet_ntoa(address.sin_addr));
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
// Start item handling thread
pthread_create(&tid, NULL, handleItem, (void *)&dgist);
Dictionary* clientDict = create_dictionary();
// Get clients
while (1) {
int new_socket;
int ipLast;
int cIndex;
client_info newClient;
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("New connection, socket fd is %d, ip is : %s, port : %d\n",
new_socket,
inet_ntoa(address.sin_addr),
ntohs(address.sin_port)
);
ipLast = get_last_part_as_int(inet_ntoa(address.sin_addr));
cIndex = dictionary_get(clientDict, ipLast);
if(cIndex != -1){ //client exist
dgist.players[cIndex].socket = new_socket;;
newClient.address = address;
}else{//client does not exist
cIndex = numClient;
dictionary_add(clientDict, ipLast, cIndex);
newClient.socket = new_socket;
newClient.address = address;
newClient.score = 0;
if(cIndex == 0){
newClient.row = 0;
newClient.col = 0;
}
else{
newClient.row = 4;
newClient.col = 4;
}
newClient.bomb = INITIAL_BOMB;
dgist.players[cIndex] = newClient;
numClient++;
}
if(numClient > MAX_CLIENTS){
perror("MAX CLIENT EXCCEDED");
exit(EXIT_FAILURE);
}
printPlayer(&dgist);
tmpDGIST* tDG = (tmpDGIST*)malloc(sizeof(tmpDGIST));;
tDG->dgist = &dgist;
tDG->cIndex = cIndex;
pthread_create(&tid, NULL, handleClient, (void *)tDG);
pthread_create(&tid, NULL, broadcastInformation, (void *)&dgist);
pthread_join(tid, NULL);
}
return 0;
}