-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfclient.c
337 lines (278 loc) · 8.8 KB
/
dfclient.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for fgets */
#include <strings.h> /* for bzero, bcopy */
#include <unistd.h> /* for read, write */
#include <sys/socket.h> /* for socket use */
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include "ClientHelper.h"
#define MAXLINE 8192 /* max text line length */
#define MAXBUF 8192 /* max I/O buffer size */
#define LISTENQ 1024 /* second argument to listen() */
#define MAXFILEBUF 60000
#define MD5_LEN 32
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
bzero(config_path, LISTENQ);
strcpy(config_path, argv[1]);
//printf("Config file: %s\n", config_path);
display_and_handle_menu();
}
void do_list(struct cmdlineinfo cmdline){
// create message linked list
create_linked_list();
// create socket and connect to servers
parse_config_and_connect(cmdline, 1);
// parse message linked list and populate file and chunk linked lists
struct thread_message * crawl = thread_head;
char * buf;
if(crawl == NULL){
printf("Failed to create thread ll?\n");
}
while(crawl != NULL){
if(crawl->msg == NULL){
crawl = crawl->next;
continue;
}
//printf("Message found in LL: %d\n", crawl->server_num);
buf = strdup(crawl->msg);
char * temp = strtok_r(buf, "\n", &buf);
while(temp != NULL){
int chunk_num = split_fn_and_chunk(temp);
insert_file_and_chunk_node(temp, crawl->msg, 1, chunk_num);
temp = strtok_r(buf, "\n", &buf);
}
crawl = crawl->next;
}
compute_if_files_are_complete();
// iterate through file linked list and display list to user
struct file_node * fcrawl = file_head;
if(file_head == NULL){
printf("PROGRAM: Empty directory\n");
return;
}
fcrawl = file_head;
printf("\n---Listing files---\n");
while(fcrawl != NULL){
printf("%s", fcrawl->filename);
if(fcrawl->is_complete != 1)
{
printf(" [incomplete]");
}
printf("\n");
fcrawl = fcrawl->next;
}
// delete message linked list
delete();
}
void do_put(struct cmdlineinfo cmdline){
// create message linked list
create_linked_list();
int sum = md5sum(cmdline.fn);
if (sum < 0) {
puts("Error occurred!");
}
int x = sum % 4;
printf("x value is %d\n", x);
// calculate length of the 4 chunks
FILE * fp = fopen(cmdline.fn, "rb");
int len = get_file_length(fp);
int floor = (int)len / 4;
// separate file into chunks and store in linkedlist node corresponding to destination server
struct client_info cinfo = parse_config_and_connect(cmdline, 0);
separate_file_to_chunks_and_store(floor, fp, x, cinfo);
//thread_linked_list();
// finally, connect to servers
parse_config_and_connect(cmdline, 1);
// iterate thru connections
//print_thread_linked_list();
struct thread_message *crawl = thread_head;
size_t n;
int connfd;
char buf[MAXBUF];
bzero(buf, MAXBUF);
while(crawl != NULL){
connfd = crawl->connfd;
if(connfd < 0){ // if we did not connect to this server
crawl = crawl->next;
continue;
}
n = write(connfd, crawl->send_chunks0, crawl->chunk0_len);
printf("Send bytes: %d\n", n);
n = read(crawl->connfd, buf, MAXBUF);
n = write(connfd, crawl->send_chunks1, crawl->chunk1_len);
printf("Send bytes: %d\n", n);
n = read(crawl->connfd, buf, MAXBUF);
crawl = crawl->next;
}
// delete message linked list
delete();
}
void do_get(struct cmdlineinfo cmdline){
// create message linked list
create_linked_list();
// create socket and connect to servers
struct client_info cinfo = parse_config_and_connect(cmdline, 1);
// parse message linked list and populate file and chunk linked lists
print_linked_list_get();
printf("-----------------\n");
struct thread_message * crawl = thread_head;
char * buf;
if(crawl == NULL){
printf("Failed to create thread ll?\n");
}
while(crawl != NULL){
if(crawl->msg == NULL){
crawl = crawl->next;
continue;
}
buf = strdup(crawl->msg);
char * temp1 = strtok_r(buf, "\r\n", &buf);
temp1 = strdup(temp1);
int chunk1 = split_getfile_and_chunk(temp1);
temp1 = temp1+1;
char * temp2 = strtok_r(buf, "\r\n", &buf);
if(temp2 != NULL){
temp2 = strdup(temp2);
}
int chunk2 = split_getfile_and_chunk(temp2);
temp2 = temp2+1;
insert_file_and_chunk_node(cmdline.fn, temp1, 0, chunk1);
insert_file_and_chunk_node(cmdline.fn, temp2, 0, chunk2);
crawl = crawl->next;
}
printf("-----------------\n");
compute_if_files_are_complete();
struct file_node * fcrawl = file_head;
if(file_head == NULL){
printf("PROGRAM: File not found\n");
return;
}
if(file_head->is_complete != 1){
printf("Printing file --\n%s [incomplete]\n", cmdline.fn);
return;
}
// reconstruct file and save. Order 0, 1, 2, 3
char *save_buf;
file_head->filename = cmdline.fn;
struct chunk_node * ptr = file_head->head;
int i = 0;
int iter = 0;
char * temp;
int n;
while(1){
ptr = file_head->head;
while(ptr != NULL){
if(ptr->chunknum == i){
FILE * fp;
fp = fopen(file_head->filename, "ab");
if(fp == NULL){
printf("Failed to open file\n");
}
if(ptr->msg == NULL){
printf("Msg is null?\n");
ptr = ptr->next;
}
n = strlen(ptr->msg);
fwrite(ptr->msg, 1, n, fp);
fclose(fp);
i++;
// if we collected all 4 chunks then exit
iter++;
if(iter == 4){
break;;
}
}
ptr = ptr->next;
}
if(iter == 4){
break;
}
}
// delete message linked list
//printf("Deleting\n");
delete();
}
void * thread(void * argument)
{
/*
Copy over arguments
*/
struct arg_struct args = *(struct arg_struct*) argument;
int connfd = args.connfdp;
int server_num = args.server_num;
//char * cmd = args.cmd;
char msg[MAXBUF];
bzero(msg, MAXBUF);
strcpy(msg, args.msg);
char cmd[MAXBUF];
bzero(cmd, MAXBUF);
strcpy(cmd, args.cmd);
//printf("THREAD - Connected to Server %d\n", server_num);
write(connfd, msg, strlen(msg));
// TODO clean this up
if(strcasecmp("PUT", cmd) == 0){
// save the received message in the linked list node dedicated to this thread
if(thread_head == NULL){
printf("THREAD - Error = thread message/ head of LL is null\n");
pthread_exit(NULL);
return NULL;
}
struct thread_message * crawl = thread_head;
while(crawl != NULL){
if(crawl->server_num == server_num){
// store connection id in dedicated node
crawl->connfd = connfd;
break;
}
crawl = crawl->next;
}
pthread_exit(NULL);
return NULL;
}
// receive msg
char buf[MAXBUF];
bzero(buf, MAXBUF);
size_t n;
char *cache_buf = calloc(MAXFILEBUF,1);
int size = 0;
while((n = read(connfd, buf, MAXBUF)) > 0){
// make sure allocated memory is large enough
if((cache_buf = realloc(cache_buf, size + n * sizeof(char))) == NULL){
pthread_exit(NULL);
return NULL;
}
memcpy(cache_buf + size, buf, n);
size+=n;
}
if(size == 0){
free(cache_buf);
pthread_exit(NULL);
return NULL;
}
//printf("Received:--\n%s\n", cache_buf);
printf("\n\nBytes received: %d\n", size);
struct thread_message * target_server_node = find_server_node(server_num);
if(target_server_node == NULL){
printf("THREAD - Error = thread message/ head of LL is null\n");
pthread_exit(NULL);
return NULL;
}
target_server_node->msg = calloc(1, strlen(cache_buf));
strcpy(target_server_node->msg, cache_buf);
target_server_node->connfd = connfd;
free(cache_buf);
//printf("THREAD - Connection closed\n");
pthread_exit(NULL);
return NULL;
}