-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataServer.c
More file actions
382 lines (307 loc) · 11.6 KB
/
dataServer.c
File metadata and controls
382 lines (307 loc) · 11.6 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
#include <stdio.h>
#include <sys/stat.h>
#include <sys/wait.h> /* sockets */
#include <sys/types.h> /* sockets */
#include <sys/socket.h> /* sockets */
#include <netinet/in.h> /* internet sockets */
#include <netdb.h> /* gethostbyaddr */
#include <unistd.h> /* fork */
#include <stdlib.h> /* exit */
#include <ctype.h> /* toupper */
#include <signal.h> /* signal */
#include <string.h>
#include <pthread.h>
#include <dirent.h>
#include "include/MyQueue.h"
Queue execution_queue;
/* Mutex and condition variable for file queue */
pthread_mutex_t mutexQueue;
pthread_cond_t condQueue;
/* Mutex for writing in socket */
pthread_mutex_t mutexWrite;
void main(int argc, char* argv[]){
int port, sock, newsock, queue_size, thread_pool_size, block_size;
if (argc != 9) {
printf("Please give all arguments ./dataServer -p <port> -s <thread_pool_size> -q <queue_size> -b <vlock_size>\n");
exit(1);
}
for(int i=1; i<argc; i++){
if(strcmp(argv[i], "-p") == 0){
port = atoi(argv[i+1]);
}
else if(strcmp(argv[i], "-s") == 0){
thread_pool_size = atoi(argv[i+1]);
}
else if(strcmp(argv[i], "-q") == 0){
queue_size = atoi(argv[i+1]);
}
else if(strcmp(argv[i], "-b") == 0){
block_size = atoi(argv[i+1]);
}
}
printf("Server's parameters are:\n");
printf("port: %d\nthread_pool_size: %d\nqueue_size: %d\nblock_size: %d\n", port, thread_pool_size, queue_size, block_size);
printf("\nServer was succesfully initialized\n\n");
struct sockaddr_in server, client;
socklen_t clientlen;
struct sockaddr *serverptr=(struct sockaddr *)&server;
struct sockaddr *clientptr=(struct sockaddr *)&client;
struct hostent *rem;
/* Create mutex */
pthread_mutex_init(&mutexQueue, 0);
pthread_mutex_init(&mutexWrite, 0);
/* Create condition variables */
pthread_cond_init(&condQueue, 0);
execution_queue = q_create(free, queue_size);
/* Create <thread_pool_size> worker threads */
pthread_t worker_threads[thread_pool_size];
for(int i=0; i<thread_pool_size; i++){
if(pthread_create(&worker_threads[i], NULL, workerThread, &block_size) != 0){
perror("Worker Thread Create");
exit(2);
}
}
/* Create socket */
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror_exit("socket");
}
server.sin_family = AF_INET; /* Internet domain */
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port); /* The given port */
/* Bind socket to address */
if(bind(sock, serverptr, sizeof(server)) < 0){
perror_exit("bind");
}
/* Listen for connections */
if(listen(sock, 1000) < 0){
perror_exit("listen");
}
printf("\nListening for connections to port %d\n\n", port);
while(1){
clientlen = sizeof(client);
/* Accept connection - Blocks until connection is made */
if((newsock = accept(sock, clientptr, &clientlen))< 0){
perror_exit("accept");
}
/* Find client's name after we got a connection */
if((rem = gethostbyaddr((char *) &client.sin_addr.s_addr,sizeof(client.sin_addr.s_addr), client.sin_family)) == NULL){
herror("gethostbyaddr");
exit(1);
}
printf("Accepted connection from %s\n", rem->h_name);
/* Create Communication Thread */
pthread_t comms_thread;
int* pclient = malloc(sizeof(int));
pclient = &newsock;
pthread_create(&comms_thread, NULL, child_server, pclient);
}
}
/* Server communication thread function that reads directory name */
void* child_server(void* newsock){
char buf[256];
/* Perhaps a mutex ? */
int tmp=0;
int file_counter = 0;
while(read(*(int*)newsock, buf, 256) > 0) {
if(strlen(buf) > 0){
printf("[Thread: %ld]: About to scan directory %s \n", pthread_self(), buf);
}
get_number_of_files(buf, newsock, &file_counter);
/* Pass number of files to be read */
tmp = htonl(file_counter);
if(write(*(int*)newsock, &tmp, sizeof(tmp)) < 0){
perror_exit("Write");
}
get_files(buf, newsock);
}
}
void get_number_of_files(const char* directory, const void* newsock, int* file_counter){
DIR* dir;
dir = opendir(directory);
/* Check if directory exists */
if(dir == NULL){
return;
}
/* We are going to use this pointer to iterate over all the
elements in directory */
struct dirent* entity;
char* apath = malloc(512);
entity = readdir(dir);
while(entity != NULL){
/* Create path recursively */
if(entity->d_type == DT_DIR && strcmp(entity->d_name, ".") != 0 && strcmp(entity->d_name, "..") != 0){
char path[200] = { 0 };
strcat(path,directory);
strcat(path, "/");
strcat(path, entity->d_name);
strcat(apath, path);
get_number_of_files(path, newsock, file_counter);
}
if(entity->d_type == DT_REG){
(*file_counter)++;
}
entity = readdir(dir);
}
closedir(dir);
free(apath);
}
/* Function that reads directory recursively placing files in queue */
void get_files(const char* directory, const void* sock){
DIR* dir;
/* Now we should print directory contents recursively */
dir = opendir(directory);
/* Check if directory exists */
if(dir == NULL){
return;
}
/* We are going to use this pointer to iterate over all the
elements in directory */
struct dirent* entity;
struct stat st; /* To get size of file */
AddVars av;
char* apath = malloc(512);
entity = readdir(dir);
while(entity != NULL){
/* Create path recursively */
if(entity->d_type == DT_DIR && strcmp(entity->d_name, ".") != 0 && strcmp(entity->d_name, "..") != 0){
char path[200] = { 0 };
strcat(path,directory);
strcat(path, "/");
strcat(path, entity->d_name);
strcat(apath, path);
get_files(path, sock);
}
if(strcmp((char*)entity->d_name, ".") != 0 && strcmp((char*)entity->d_name, "..") != 0){
/* Add file path to queue */
pthread_mutex_lock(&mutexQueue);
while(q_size(execution_queue) == q_maxsize(execution_queue)){
printf(">> Queue is max size %d. Waiting...\n", q_size(execution_queue));
pthread_cond_wait(&condQueue, &mutexQueue);
}
/* Queue is not full so we add file path */
apath[0] = '\0';
strcat(apath, directory);
if(entity->d_type == DT_REG){
strcat(apath, "/");
strcat(apath, (char*)entity->d_name);
}
stat(apath, &st);
av = malloc(sizeof(*av));
av->file_size = st.st_size;
av->path = malloc(strlen(apath)+1);
strcpy(av->path, apath);
av->socket_id = *(int*)sock;
/* I only pass file paths to queue */
if(entity->d_type == DT_REG){
printf("[Thread: %ld]: Adding file %s to queue\n", pthread_self(), av->path);
q_insert(execution_queue, av);
}
/* Since we copy the file we dont need the apath */
memset(apath, '\0', strlen(apath));
pthread_cond_signal(&condQueue);
pthread_mutex_unlock(&mutexQueue);
}
entity = readdir(dir);
}
closedir(dir);
free(apath);
}
/* Worker thread function that is going to wait until it gets a filename
from queue to read */
void* workerThread(void* args){
int length, block_length = *(int*)args, size, received = ntohl(0);
char* filename, *metadata, *block, *p_path;
char* buf;
AddVars buff;
char file_read[block_length];
FILE* fptr; /* To open and read file */
while(1){
/* If queue is empty, wait */
pthread_mutex_lock(&mutexQueue);
while(q_size(execution_queue) == 0){
printf(">> Queue is empty. Waiting...\n");
pthread_cond_wait(&condQueue, &mutexQueue);
}
/* Now that the queue has at least one element we get the first struct object */
buff = q_first_value(execution_queue);
printf("\n\n[Thread: %ld]: Received task %s with socketId : %d and size : %d our block size is: %d\n", pthread_self(), buff->path, buff->socket_id, buff->file_size, *(int*)args);
/* Only one thread should be writting in socket */
pthread_mutex_lock(&mutexWrite);
/* Pass file metadata (file size) */
size = htonl(buff->file_size); // file size + path + path-EOF + \n
if(write(buff->socket_id, &size, sizeof(size)) < 0){
perror_exit("Write");
}
/* Pass file path */
p_path = malloc(strlen(buff->path) + 2);
strcpy(p_path, buff->path);
strcat(p_path, "\n");
if(write(buff->socket_id, p_path, strlen(p_path)) < 0){
perror_exit("Write");
}
/* Get message from client */
if(read(buff->socket_id, &received, sizeof(received)) < 0){
perror_exit("Read");
}
if(ntohl(received) == 1){
received = 0;
}
/* Worker opens file and sends it to client block by block */
struct stat path_stat;
stat(buff->path, &path_stat); /* To find out if its a regfile */
/* To find the end of file */
char* eof = malloc(strlen(buff->path) + 5); // "-EOF"
eof[0] = '\0';
strcpy(eof, buff->path);
strcat(eof, "-EOF");
/* If it is a regular file we open and send it block by block */
if(S_ISREG(path_stat.st_mode)){
fptr = fopen(buff->path, "r");
if(fptr == NULL){
perror_exit("Can't open file");
}
while(fgets(file_read, block_length, fptr) != NULL){
/* Write in client */
if(write(buff->socket_id, file_read, block_length) < 0){
perror_exit("Write");
}
/* Get message from client */
if(read(buff->socket_id, &received, sizeof(received)) < 0){
perror_exit("Read");
}
if(ntohl(received) == 1){
received = 0;
}
}
/* Pass the end of file */
if(write(buff->socket_id, eof, block_length) < 0){
perror_exit("Write");
}
/* Get message from client */
if(read(buff->socket_id, &received, sizeof(received)) < 0){
perror_exit("Read");
}
if(ntohl(received) == 1){
received = 0;
}
else if(ntohl(received) == 2){
received = 0;
printf("Closing socket \n");
/* Close socket */
close(buff->socket_id);
}
fclose(fptr);
}
pthread_mutex_unlock(&mutexWrite);
free(eof);
free(p_path);
q_remove_first(execution_queue);
/* Send signal to communication thread */
pthread_cond_signal(&condQueue);
pthread_mutex_unlock(&mutexQueue);
}
}
void perror_exit(char *message) {
perror(message);
exit(EXIT_FAILURE);
}