-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
397 lines (282 loc) · 6.58 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
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
#include "head.h"
#define LOG_FILE_NAME "log.txt"
#define MAX_BLOBSZ BUFFSZ-HEADSZ
struct client{
int sfd;
struct msg_head last_hdr;
};
struct srv_connection{
int srv_sfd;
int cli_ct;
int active_ct;
struct client *cli;
};
struct select{
fd_set readfds;
int max_d;
struct timeval t;
};
//
// main
//
int init_connection(struct srv_connection *conn, int cli_ct);
int make_daemon(char* logfile);
int create_workers(int n);
int process_file(FILE* file, struct srv_connection *conn);
int close_connection(struct srv_connection *conn);
int main(int argc, char *argv[]){
struct srv_connection conn;
int cli_ct;
FILE *fin;
if (argc!=3) {
printf("Bad param\n");
return 1;
}
cli_ct = atoi(argv[2]);
fin = fopen(argv[1], "r");
if (fin == NULL) {
printf("err file");
return 1;
}
make_daemon(argv[1]);
create_workers(cli_ct);
init_connection(&conn, cli_ct);
process_file(fin, &conn);
fclose(fin);
close_connection(&conn);
return 0;
}
//
// init connection
//
int get_srv_socket();
int connect_clients(struct srv_connection *conn);
int init_connection(struct srv_connection *conn, int cli_ct){
int i;
conn->cli_ct = cli_ct;
conn->cli = (struct client*) malloc(conn->cli_ct*sizeof(struct client));
conn->srv_sfd = get_srv_socket();
connect_clients(conn);
conn->active_ct = conn->cli_ct;
for(i=0; i<conn->cli_ct; i++) {
conn->cli[i].last_hdr.code=MSG_NULL;
}
}
// get server socket
int get_srv_socket(){
int len, sfd;
struct sockaddr_un addr;
if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
printf("s_open_err");
return 0;
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SERV_PATH);
unlink(addr.sun_path);
len = sizeof(addr.sun_family) + strlen(addr.sun_path);
if (bind(sfd, (struct sockaddr *)&addr, len) == -1) {
printf("bind err");
return 0;
}
if (listen(sfd, 5) == -1) {
printf("listen err");
return 0;
}
return sfd;
}
// connect clients
int connect_clients(struct srv_connection *conn){
int len, i;
struct sockaddr_un addr;
len = sizeof(addr);
for(i=0; i<conn->cli_ct; i++){
if ((conn->cli[i].sfd = accept(conn->srv_sfd, (struct sockaddr *)&addr, &len)) == -1) {
printf("accept err");
return 0;
}
printf("Connected: %d\n", i);
}
printf("\nall connected\n");
return 0;
}
//
// make daemon
//
int make_daemon(char* logfile){
int logfd;
if ((logfd = open(LOG_FILE_NAME, O_CREAT|O_WRONLY|O_TRUNC)) != -1) {
close(0);
close(1);
close(2);
open("/dev/null", O_RDONLY); // block in
dup2(logfd, 1);
dup2(logfd, 2);
close(logfd);
setsid(); // change session
//chdir("/");
} else {
printf("Error, cannot open log, process not will start\n");
}
return 0;
}
//
// create workers
//
int create_workers(int n){
int i;
for (i=0; i<n; i++){
if (fork() == 0) {
execlp("./client", "./client", (char *)NULL);
// will be executed only if error
printf("CLIENT BIG ERROR\n");
}
}
}
//
// process file
//
int init_count(int *count);
int prepare_select(struct srv_connection *conn, struct select* sel);
int process_select(struct srv_connection *conn, struct select s_val, FILE* file, int* count);
int process_msg(struct client* cli, FILE* file, int* count);
int print_result(int *arr, int ct);
int process_file(FILE* file, struct srv_connection *conn){
struct select s_val;
int count[CHAR_CT];
int res;
init_count(count);
while(1){
if (!prepare_select(conn, &s_val)){
printf("File has processed, All clients ended");
break;
}
res = select(s_val.max_d+1, &(s_val.readfds), NULL, NULL, &(s_val.t));
if (res<0) {
printf("err select\n");
continue;
} else if (res==0){
printf("\ns timeout s\n");
continue;
}
process_select(conn, s_val, file, count);
}
print_result(count, CHAR_CT);
return 0;
}
// init count
int init_count(int *count){
int i;
for (i=0; i<CHAR_CT; i++){
count[i]=0;
}
}
// prepare select
int set_trans_block_sz(int sfd, int size);
int prepare_select(struct srv_connection *conn, struct select* sel){
int i;
FD_ZERO(&(sel->readfds));
sel->max_d = 0;
for(i=0; i<conn->cli_ct; i++) {
if (conn->cli[i].last_hdr.code!=TERM) {
FD_SET(conn->cli[i].sfd, &(sel->readfds));
set_trans_block_sz(conn->cli[i].sfd, conn->cli[i].last_hdr.blob_size);
if (conn->cli[i].sfd > sel->max_d)
sel->max_d = conn->cli[i].sfd;
}
}
sel->t.tv_sec = WAIT_TIME;
return sel->max_d;
}
int set_trans_block_sz(int sfd, int size){
int res;
res = setsockopt(sfd, SOL_SOCKET, SO_RCVLOWAT, (const char *) &size, sizeof(int));
if (res!=0) {
printf("Error: set_trans_block_sz\n");
return 1;
}
return 0;
}
// process select
int process_select(struct srv_connection *conn, struct select s_val, FILE* file, int* count){
int i;
for (i=0; i<conn->cli_ct; i++){
if (!FD_ISSET(conn->cli[i].sfd, &(s_val.readfds)))
continue;
process_msg(&(conn->cli[i]), file, count);
}
}
// process message
int read_msg(struct client* cli, char* buff);
int store_count(int *count_to, int *count_from);
int process_msg(struct client* cli, FILE* file, int* count){
char buff[BUFFSZ];
int rc;
struct msg_head m_msg;
rc = read_msg(cli, buff);
switch(cli->last_hdr.code){
case MSG_BLOB:
store_count(count, (int*) buff);
cli->last_hdr.code=MSG_NULL;
break;
default:
cli->last_hdr=*((struct msg_head*)buff);
if (cli->last_hdr.code==MSG_BLOB)
break;
if (feof(file)){
cli->last_hdr.code=TERM;
break;
}
rc = fread(buff, sizeof(char), MAX_BLOBSZ, file);
rc = send_new_blob(cli, buff, rc);
}
}
int read_msg(struct client* cli, char* buff){
int msg_sz;
if (cli->last_hdr.code==MSG_BLOB)
msg_sz = cli->last_hdr.blob_size;
else msg_sz = HEADSZ;
return read(cli->sfd, buff, msg_sz);
}
int store_count(int *count_to, int *count_from){
int i;
for(i=0; i<CHAR_CT; i++){
count_to[i] += count_from[i];
}
return 0;
}
int send_new_blob(struct client* cli, char* buff, int sz){
int rc;
struct msg_head m_msg;
m_msg.code = MSG_BLOB;
m_msg.blob_size = sz;
rc = write(cli->sfd, &m_msg, HEADSZ);
printf("s send msg\n");
rc = write(cli->sfd, buff, sz);
printf("s send blob %d\n", rc);
return rc;
}
// print result
int print_result(int *arr, int ct){
int i;
printf("\n-- RESULTS --\n");
for(i=0; i<ct; i++) {
printf("Char: %c | Num: %3d | Count: %10d\n", i, i, arr[i]);
}
printf("\n-- END RESULTS --\n");
}
//
// close connection
//
int close_connection(struct srv_connection *conn){
int i;
for(i=0; i<conn->cli_ct; i++){
send_term(conn->cli[i].sfd);
close(conn->cli[i].sfd);
}
close(conn->srv_sfd);
}
int send_term(int sfd){
struct msg_head m_msg;
m_msg.code = TERM;
return write(sfd, &m_msg, HEADSZ);
}