-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
245 lines (217 loc) · 8.8 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/time.h>
#define NUM_OF_ROOM_MEMBERS 3
#define MAX_NUM_OF_CLIENTS 1000
#define DELAY 0.01
#define COMPUTER_REQUEST "computer\n"
#define ELECTRICAL_REQUEST "electrical\n"
#define CIVIL_REQUEST "civil\n"
#define MECHANIC_REQUEST "mechanic\n"
#define COMP_FILE "comp.txt"
#define ELEC_FILE "elec.txt"
#define CIVI_FILE "civi.txt"
#define MECH_FILE "mech.txt"
int setupServer(int port) {
struct sockaddr_in address;
int server_fd;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(port);
bind(server_fd, (struct sockaddr *)&address, sizeof(address));
listen(server_fd, 4);
return server_fd;
}
int acceptClient(int server_fd) {
int client_fd;
struct sockaddr_in client_address;
int address_len = sizeof(client_address);
client_fd = accept(server_fd, (struct sockaddr *)&client_address, (socklen_t*) &address_len);
return client_fd;
}
int make_room(int *members, int* num_of_rooms, int server_port) {
char buff[10] = {0};
(*num_of_rooms)++;
for(int i = 0; i < NUM_OF_ROOM_MEMBERS; i++){
memset(buff, 0, 10);
sprintf(buff, "%d", server_port + *num_of_rooms);
send(members[i], buff, strlen(buff), 0);
sleep(0.01);
memset(buff, 0, 10);
sprintf(buff, "%d", i);
send(members[i], buff, strlen(buff), 0);
sleep(0.01);
}
return *num_of_rooms;
}
void save_to_proper_file(char *buffer, int i, int comp_set[MAX_NUM_OF_CLIENTS], int elec_set[MAX_NUM_OF_CLIENTS], int civi_set[MAX_NUM_OF_CLIENTS], int mech_set[MAX_NUM_OF_CLIENTS], int comp_set_ind, int elec_set_ind, int civi_set_ind, int mech_set_ind){
char file_name[100];
for(int j = 0; j < comp_set_ind; j++){
if(comp_set[j] == i){
strcpy(file_name, COMP_FILE);
}
}
for(int j = 0; j < elec_set_ind; j++){
if(elec_set[j] == i){
strcpy(file_name, ELEC_FILE);
}
}
for(int j = 0; j < civi_set_ind; j++){
if(civi_set[j] == i){
strcpy(file_name, CIVI_FILE);
}
}
for(int j = 0; j < mech_set_ind; j++){
if(mech_set[j] == i){
strcpy(file_name, MECH_FILE);
}
}
int file = open(file_name, O_CREAT | O_APPEND | O_WRONLY);
write(file, buffer, strlen(buffer));
close(file);
}
void clear_from_sets(int i, int *comp_set, int *elec_set, int *civi_set, int *mech_set, int comp_set_ind, int elec_set_ind, int civi_set_ind, int mech_set_ind){
for(int j = 0; j < comp_set_ind; j++){
if(comp_set[j] == i){
comp_set[j] = -1;
}
}
for(int j = 0; j < elec_set_ind; j++){
if(elec_set[j] == i){
elec_set[j] = -1;
}
}
for(int j = 0; j < civi_set_ind; j++){
if(civi_set[j] == i){
civi_set[j] = -1;
}
}
for(int j = 0; j < mech_set_ind; j++){
if(mech_set[j] == i){
mech_set[j] = -1;
}
}
}
int main(int argc, char const *argv[]) {
int server_fd, new_socket, max_sd, server_port;
char buffer[NUM_OF_ROOM_MEMBERS*1024] = {0};
fd_set master_set, working_set;
int comp_set_ind, elec_set_ind, civi_set_ind, mech_set_ind, num_of_rooms;
comp_set_ind = elec_set_ind = civi_set_ind = mech_set_ind = num_of_rooms = 0;
int comp_set[MAX_NUM_OF_CLIENTS], elec_set[MAX_NUM_OF_CLIENTS], civi_set[MAX_NUM_OF_CLIENTS], mech_set[MAX_NUM_OF_CLIENTS];
server_port = atoi(argv[1]);
server_fd = setupServer(server_port);
FD_ZERO(&master_set);
max_sd = server_fd;
FD_SET(server_fd, &master_set);
write(1, "Server is running\n", 18);
int comp[NUM_OF_ROOM_MEMBERS];
int comp_index = 0;
int elec[NUM_OF_ROOM_MEMBERS];
int elec_index = 0;
int civi[NUM_OF_ROOM_MEMBERS];
int civi_index = 0;
int mech[NUM_OF_ROOM_MEMBERS];
int mech_index = 0;
while (1) {
working_set = master_set;
select(max_sd + 1, &working_set, NULL, NULL, NULL);
for (int i = 0; i <= max_sd; i++) {
if (FD_ISSET(i, &working_set)) {
if (i == server_fd) { // new clinet
new_socket = acceptClient(server_fd);
FD_SET(new_socket, &master_set);
if (new_socket > max_sd)
max_sd = new_socket;
printf("New client connected. fd = %d\n", new_socket);
}
else { // client sending msg
int bytes_received;
bytes_received = recv(i , buffer, 1024*NUM_OF_ROOM_MEMBERS, 0);
if (bytes_received == 0) { // EOF
printf("client fd = %d closed\n", i);
close(i);
FD_CLR(i, &master_set);
clear_from_sets(i, comp_set, elec_set, civi_set, mech_set, comp_set_ind, elec_set_ind, civi_set_ind, mech_set_ind);
continue;
}
printf("client %d: %s\n", i, buffer);
if (strcmp(buffer, COMPUTER_REQUEST) == 0){ // computer request
comp[comp_index] = i;
comp_index++;
comp_set[comp_set_ind] = i;
comp_set_ind++;
if (comp_index == NUM_OF_ROOM_MEMBERS){
make_room(comp, &num_of_rooms, server_port);
printf("create computer room with id : %d\n" , comp_set_ind);
comp_index = 0;
}
else{
printf("add a client to computer waiting queue.\n");
printf("in this queue exist %d client.\n", comp_index);
}
}
else if (strcmp(buffer, ELECTRICAL_REQUEST) == 0){ // electrical request
elec[elec_index] = i;
elec_index++;
elec_set[elec_set_ind] = i;
elec_set_ind++;
if (elec_index == NUM_OF_ROOM_MEMBERS){
make_room(elec, &num_of_rooms, server_port);
printf("create electrical room with id : %d\n" , elec_set_ind);
elec_index = 0;
}
else{
printf("add a client to electrical waiting queue.\n");
printf("in this queue exist %d client.\n", elec_index);
}
}
else if (strcmp(buffer, CIVIL_REQUEST) == 0){ // civil request
civi[civi_index] = i;
civi_index++;
civi_set[civi_set_ind] = i;
civi_set_ind++;
if (civi_index == NUM_OF_ROOM_MEMBERS){
make_room(civi, &num_of_rooms, server_port);
printf("create civil room with id : %d\n" , civi_set_ind);
civi_index = 0;
}
else{
printf("add a client to civil waiting queue.\n");
printf("in this queue exist %d client.\n", civi_index);
}
}
else if (strcmp(buffer, MECHANIC_REQUEST) == 0){ // mechanic request
mech[mech_index] = i;
mech_index++;
mech_set[mech_set_ind] = i;
mech_set_ind++;
if (mech_index == NUM_OF_ROOM_MEMBERS){
make_room(mech, &num_of_rooms, server_port);
printf("create mechanic room with id : %d\n" , mech_set_ind);
mech_index = 0;
}
else{
printf("add a client to mechanic waiting queue.\n");
printf("in this queue exist %d client.\n", mech_index);
}
}
else{
save_to_proper_file(buffer, i, comp_set, elec_set, civi_set, mech_set, comp_set_ind, elec_set_ind, civi_set_ind, mech_set_ind);
}
memset(buffer, 0, 1024*NUM_OF_ROOM_MEMBERS);
}
}
}
}
return 0;
}