-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_tcp.c
184 lines (145 loc) · 3.92 KB
/
server_tcp.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
#include "conn.h"
#define MAX_FILENAME 100
#define TOKEN 65536
#define MAX_PROC 10
/* Number of active processes */
int activeProc = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;
/* Serve the connection */
void serveConn(Connection conn) {
int bytes, sended_bytes, fd, count;
uint32_t file_dim, timestamp;
uint32_t file_dim_net, timestamp_net;
float percent, seconds, bytes_per_sec;
time_t start_send, end_send;
char request[MAX_FILENAME+10], op[10], *response;
char filename[MAX_FILENAME], path[MAX_FILENAME+6] = "files/";
struct stat file_info;
bytes = conn_setTimeout(conn, 120);
if (bytes == -1) {
printf("Terminating service.\n");
return;
}
while(TRUE) {
printf("\tWaiting file request..\n");
bytes = conn_recvs(conn, request, sizeof(request), "\r\n");
if (bytes == -1) break;
sscanf(request, "%s", op);
if (strcmp(op, "GET") == 0) {
strcpy(filename, request+4);
strcpy(path, "files/");
strcat(path, filename);
fd = open(path, O_RDONLY);
if (fd == -1) {
report_err("Cannot open the file");
bytes = conn_sends(conn, "-ERR\r\n");
break;
}
if (fstat(fd, &file_info)) {
report_err("Cannot retreive file information");
continue;
}
file_dim = file_info.st_size;
timestamp = file_info.st_mtime;
printf("\tSELECTED FILE: %s\n\t\tFile size:\t[%d B]\n\t\tTimestamp:\t[%d]\n", filename, file_dim, timestamp);
/* Send the header */
file_dim_net = htonl(file_dim);
timestamp_net = htonl(timestamp);
bytes = conn_sends(conn, "+OK\r\n");
if (bytes == -1)
break;
bytes = conn_sendn(conn, &file_dim_net, sizeof(file_dim));
if (bytes == -1)
break;
bytes = conn_sendn(conn, ×tamp_net, sizeof(timestamp));
if (bytes == -1)
break;
/* Send the file */
if (file_dim <= TOKEN) {
bytes = conn_sendfile(conn, fd, file_dim);
if (bytes == -1)
break;
} else {
bytes = conn_sendfile_tokenized(conn, fd, file_dim, TOKEN);
if (bytes == -1)
break;
}
} else if (strcmp(op, "QUIT") == 0) {
printf("\tQUIT request\n");
break;
} else {
printf("\tINVALID request\n");
bytes = conn_sends(conn, "-ERR\r\n");
if (bytes == -1)
break;
}
}
printf("Terminating service.\n");
}
void signalHandler(int sig) {
if (sig == SIGCHLD) {
/* One of the children is died, but can be that more than one are died */
pthread_mutex_lock(&mutex);
while (waitpid(-1, NULL, WNOHANG) > 0) {
activeProc--;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
if (sig == SIGINT) {
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
/* Some termination before die */
printf("\n");
exit(0);
}
}
int main(int argc, char *argv[]) {
int maxServerProcesses = 2;
Connection conn;
Host thisServer;
int port, servNumber, ctrl;
if (argc != 3) {
if (argc != 2) {
fprintf(stderr, "syntax: %s <port> [nproc]\n", argv[0]);
exit(-1);
}
} else {
servNumber = atoi(argv[2]);
if (servNumber <= 0 || servNumber > MAX_PROC)
fprintf(stderr, "Wrong number of processes: set default [%d].\n", maxServerProcesses);
else
maxServerProcesses = servNumber;
}
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
signal(SIGCHLD, signalHandler);
signal(SIGINT, signalHandler);
port = checkport(argv[1]);
if (port == -1) return -1;
thisServer = prepareServer(port, TCP); // Dies on failure
while(TRUE) {
pthread_mutex_lock(&mutex);
while (activeProc == maxServerProcesses) {
pthread_cond_wait(&cond, &mutex);
}
activeProc++;
pthread_mutex_unlock(&mutex);
printf("Waiting a new connection...\n");
conn = acceptConn(thisServer);
if (conn.sock == -1) continue;
if (!fork()) {
/* Child process close the server connection */
closeHost(thisServer);
/* Serve the new connection, than terminate */
serveConn(conn);
conn_close(conn);
return 0;
} else {
/* Main process, keep going listen */
conn_close(conn);
}
}
return 0;
}