-
Notifications
You must be signed in to change notification settings - Fork 1
/
sorter_server.c
441 lines (355 loc) · 13.4 KB
/
sorter_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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "sorter_server.h"
#include "mergesort.c"
#include <pthread.h>
#include <arpa/inet.h>
#define MAX_FILES 10000
// Global Variables
movie *Globalarray;
int colnum;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int Globalarraysize;
char *sortpath; //Path leading to the file being sorted
char *category;
char *output_dir;
int hasoutput;
int hasinput;
int loadArray(char * data, movie * array) {
int size = 128;
char *line = strsep(&data, "\n"); // remove column headings
//printf("token: %s\n", token);
char * token=0;
line = strsep(&data, "\n");
int i=0;
// Tokenize all data from given line (obtained from buffer) and load into an array
while(line != NULL){
//printf("line %i: %s\n", i, token);
//char *token;
if(strlen(line)==0){
//printf("End of input, breaking\n");
break;
}
if(strlen(line) < 27){
break;
}
// 1) STRING - Color
//,,,,,,,,,,,,,,,,,,,,,,,,,,\n\0
token = strsep(&line, ",");
//printf("*****%s*****\n",token);
trim(token); //remove trailing and leading spaces
array[i].color = malloc(strlen(token) + 1);
strcpy(array[i].color, token);
//printf("color: %s\n", array[i].color);
// 2) STRING - Director Name
token = strsep(&line, ",");
array[i].directorName=malloc(strlen(token) + 1);
strcpy(array[i].directorName, token);
//printf("director name: %s\n", array[i].directorName);
// 3) INT - Number of critic review
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].numCriticReviews = -1;
} else {
array[i].numCriticReviews = atoi(token);
}
// 4) INT - Duration
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].duration = -1;
} else {
array[i].duration = atoi(token);
}
// 5) INT - Director facebook likes
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].directorFBlikes = -1;
} else {
array[i].directorFBlikes = atoi(token);
}
// 6) INT - Actor 3 facebook likes
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].actor3FBlikes = -1;
} else {
array[i].actor3FBlikes = atoi(token);
}
// 7) STRING - Actor 2 name
token = strsep(&line, ",");
array[i].actor2name = malloc(strlen(token) + 1);
strcpy(array[i].actor2name, token);
// 8) INT - Actor 1 facebook likes
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].actor1FBlikes = -1;
} else {
array[i].actor1FBlikes = atoi(token);
}
// 9) INT - Gross
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].gross = -1;
} else {
array[i].gross = atoi(token);
}
// 10) STRING - Genres
token = strsep(&line, ",");
array[i].genres=malloc(strlen(token) + 1);
strcpy(array[i].genres, token);
// 11) STRING - Actor 1 name
token = strsep(&line, ",");
array[i].actor1name = malloc(strlen(token) + 1);
strcpy(array[i].actor1name, token);
// 12) STRING - Movie title
if(line[0]=='\"'){
line = line + 1;
token = strsep(&line, "\"");
trim(token); //remove trailing and leading spaces
array[i].movieTitle = malloc(strlen(token));
strcpy(array[i].movieTitle, token);
strsep(&line, ",");
} else {
token = strsep(&line, ",");
trim(token); //remove trailing and leading spaces
array[i].movieTitle = malloc(strlen(token) + 1);
strcpy(array[i].movieTitle, token);
}
// 13) INT - Num_voted_users
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].numVotedUsers = -1;
} else {
array[i].numVotedUsers = atoi(token);
}
// 14) INT - Cast total facebook likes
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].castTotalFBlikes = -1;
} else {
array[i].castTotalFBlikes = atoi(token);
}
// 15) STRING - Actor 3 name
token = strsep(&line, ",");
array[i].actor3name = malloc(strlen(token) + 1);
strcpy(array[i].actor3name,token);
// 16) INT - Facenumber_in_poster
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].faceNumberPoster = -1;
} else {
array[i].faceNumberPoster = atoi(token);
}
// 17) STRING - plot keywords
token = strsep(&line, ",");
array[i].plotKeywords = malloc(strlen(token) + 1);
strcpy(array[i].plotKeywords,token);
// 18) STRING - Movie imdb link
token = strsep(&line, ",");
array[i].link = malloc(strlen(token) + 1);
strcpy(array[i].link,token);
// 19) INT - Num user for reviews
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].numUserReviews = -1;
} else {
array[i].numUserReviews = atoi(token);
}
// 20) STRING - Language
token = strsep(&line, ",");
array[i].language = malloc(strlen(token) + 1);
strcpy(array[i].language, token);
// 21) STRING - Country
token = strsep(&line, ",");
array[i].country = malloc(strlen(token) + 1);
strcpy(array[i].country, token);
// 22) STRING - Content Rating
token = strsep(&line, ",");
array[i].contentRating = malloc(strlen(token) + 1);
strcpy(array[i].contentRating,token);
// 23) INT - Budget
token = strsep(&line, ",");
//printf("%s\n", token);
//printf("%i\n",atoi(token));
if(token[0] == '\0'){
array[i].budget = -1;
} else {
array[i].budget = atoi(token);
}
// 24) INT - Title Year
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].titleYear = -1;
} else {
array[i].titleYear = atoi(token);
}
//25) INT - Actor 2 Facebook Likes
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].actor2FBlikes = -1;
} else {
array[i].actor2FBlikes = atoi(token);
}
// 26) DOUBLE - IMDB Score Num
char* temp;
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].imdbScore = -1;
} else {
array[i].imdbScore = strtod(token, &temp);
}
// 27) DOUBLE - Aspect Ratio
token = strsep(&line, ",");
if(token[0] == '\0'){
array[i].aspectRatio = -1;
} else {
array[i].aspectRatio = strtod(token, &temp);
}
// 28) INT - Movie Facebook Likes
token = strsep(&line, ",");
//printf("token: %s\n", token);
if(token[0] == '\0'){
array[i].movieFBlikes = -1;
} else {
array[i].movieFBlikes = atoi(token);
}
line = strsep(&data, "\n");
i++;
} //end of while loop
free(data);
return i;
} //end of 'loadArray' function
void *clientHandler(void *fd) {
int comm_fd = *(int*) fd;
int rc;
char buff[20];
char coln[20];
int buffSize = 0;
int colnum = 0;
char signal[20];
recv(comm_fd,signal,20,0);
if(strcmp(signal,"Sending_File")==0){
//printf("Received SF signal, now receiving buffsize...\n");
rc = recv(comm_fd, buff, 20, 0); //gets size of incoming csv file **(in the form of a CHAR ARRAY)
buffSize = atoi(buff); //puts size of incoming csv file into an integer
//printf("buffSize = %d\n", buffSize);
char * buffer = malloc(sizeof(char) * buffSize); //inializes the buffer in which we will put all of the new csv files
char * data = calloc(sizeof(char), buffSize); //initializes the
movie * array = malloc(buffSize * 1.5);
int bytesrec=0;
while(1){
bzero(buffer, buffSize); //this nulls out "buffer" so theres no leftover characters each time it reads from client
rc = recv(comm_fd, buffer, buffSize, 0); //reads from client and puts it into str
bytesrec+=rc;
//printf("rc: %d\n", rc);
strcat(data, buffer);
if(bytesrec >= buffSize){
break;
}
}
//printf("bytesrec =%d",bytesrec);
//printf("%d",data);
int i = loadArray(data, array);
//mergesort(array, ,i);
pthread_mutex_lock(&m);
// (Load stuff into the global array)
int currsize = Globalarraysize;
Globalarraysize+=i;
//printf("Globalarraysize:%d\n",Globalarraysize);
Globalarray=realloc(Globalarray,Globalarraysize*sizeof(movie));
int k;
int l = 0;
for(k=currsize; k<Globalarraysize; k++){
Globalarray[k] = array[l];
l++;
}
free(array);
pthread_mutex_unlock(&m);
send(comm_fd, "FUF", 20, 0);
} else if(strcmp(signal, "Dump_Request") == 0){
char colchar[20];
int colnum;
//printf("Recieved dump request\n");
recv(comm_fd, colchar, 20, 0);
colnum = atoi(colchar);
//printf("Column number is: %d\n", colnum);
//printf("Global array size is: %d\n",G lobalarraysize);
pthread_mutex_lock(&m);
mergesort(Globalarray, colnum, Globalarraysize);
pthread_mutex_unlock(&m);
movie *array = Globalarray;
char finalline[3000];
char num[20];
snprintf(num, 20, "%d", Globalarraysize);
send(comm_fd, num,20,0);
int k;
for(k=0; k<Globalarraysize; k++){
if(strchr(array[k].movieTitle,',')){ //this if statement takes each title with a "," and puts quotations around it -> detection works fine
char qm1[100] = "";
char qm2[100] = "";
qm1[0] = '\"';
qm2[0] = '\"';
strcat(qm1,array[k].movieTitle);
strcat(qm1,qm2);
array[k].movieTitle = qm1;
}
//printf("%s,%s,%d,%d,%d,%d,%s,%d,%f,%s,%s,%s,%d,%d,%s,%d,%s,%s,%d,%s,%s,%s,%d,%d,%d,%f,%f,%d\n", array[k].color,array[k].directorName,array[k].numCriticReviews,array[k].duration, array[k].directorFBlikes, array[k].actor3FBlikes, array[k].actor2name,array[k].actor1FBlikes,array[k].gross, array[k].genres, array[k].actor1name, array[k].movieTitle, array[k].numVotedUsers,array[k].castTotalFBlikes, array[k].actor3name,array[k].faceNumberPoster, array[k].plotKeywords, array[k].link, array[k].numUserReviews,array[k].language,array[k].country,array[k].contentRating,array[k].budget,array[k].titleYear,array[k].actor2FBlikes,array[k].imdbScore,array[k].aspectRatio,array[k].movieFBlikes);
snprintf(finalline, 3000, "%s,%s,%i,%i,%i,%i,%s,%i,%i,%s,%s,%s,%i,%i,%s,%i,%s,%s,%i,%s,%s,%s,%i,%i,%i,%f,%f,%i\n", array[k].color,array[k].directorName,array[k].numCriticReviews,array[k].duration, array[k].directorFBlikes, array[k].actor3FBlikes, array[k].actor2name,array[k].actor1FBlikes,array[k].gross, array[k].genres, array[k].actor1name, array[k].movieTitle, array[k].numVotedUsers,array[k].castTotalFBlikes, array[k].actor3name,array[k].faceNumberPoster, array[k].plotKeywords, array[k].link, array[k].numUserReviews,array[k].language,array[k].country,array[k].contentRating,array[k].budget,array[k].titleYear,array[k].actor2FBlikes,array[k].imdbScore,array[k].aspectRatio,array[k].movieFBlikes);
//printf("%d\n", array[k].movieFBlikes);
send(comm_fd,finalline,3000,0);
bzero(finalline,3000);
}
} else {
printf("Received unknown input, breaking.\n");
return;
}
//send FINISHED signal back to client.
} //end of 'clientHandler' function
int main(int argc, char ** argv){
long port = -1;
if(argc != 3){
printf("ERROR: Incorrect number of arguments.\n");
return -1;
}
if (strcmp(argv[1], "-p") != 0){
printf("ERROR: Expected '-p' as first argument.\n");
return -1;
}
if(atol(argv[2]) < 0 || atol(argv[2]) > 65565){
printf("ERROR: Port is out of range.\n");
return -1;
} else {
port = atol(argv[2]);
}
Globalarraysize = 0;
int listen_fd, comm_fd[MAX_FILES];
struct sockaddr_in servaddr;
//int addrlen = sizeof(address);
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);
int len = sizeof(servaddr);
bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listen_fd, 10);
printf("Received connections from: ");
int threadcount = 0;
pthread_t tids[4096];
while(1){
comm_fd[threadcount] = accept(listen_fd, (struct sockaddr*) &servaddr, (socklen_t *) &len);
//printf("Accepted a client \n");
printf("%s, ", inet_ntoa(servaddr.sin_addr));
pthread_create(&tids[threadcount], NULL, clientHandler,&comm_fd[threadcount]);
threadcount++;
fflush(stdout);
}
return 0;
} //end of 'main' function