-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyftpclient.c
1096 lines (1021 loc) · 26.3 KB
/
myftpclient.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> // "struct sockaddr_in"
#include <arpa/inet.h> // "in_addr_t"
#include <malloc.h>
#include <dirent.h>
#include <getopt.h>
#include <isa-l.h>
#include <ctype.h>
#include "myftp.h"
#include <sys/select.h>
int N,SERVERNUM,K,BLOCKSIZE=0;
void client_recv_file_data(int fd[], char filename[],char path[],int work_node[]);
void client_send_file_data(int fd[], Stripe *stripe, char filename[]);
void send_encode_data(int fd[],Stripe* stripe);
void client_send_file_data(int fd[], Stripe *stripe, char filename[]){
fd_set sendfds;
int i;
int j, m;
int max_sd=0;
int len=0;
unsigned char payload[PAYLEN];
memset(payload,0,PAYLEN);
int *have_send=(int*)malloc(sizeof(int)*N);
for(i=0;i<N;i++){
have_send[i]=0;
}
for(i=0;i<N;i++){
char blockcode[1026];
construct_blockcode(blockcode,filename, stripe->sid, i);
if((len=send(fd[i],blockcode,sizeof(blockcode),0))!=sizeof(blockcode)){
perror("fail to send blockcode\n");
exit(1);
}
}
while(1){
FD_ZERO(&sendfds);
for(i=0;i<N;i++){
FD_SET(fd[i],&sendfds);
max_sd=(max_sd>fd[i])?max_sd:fd[i];
}
if(select(max_sd+1,NULL,&sendfds,NULL,NULL)<0){
printf("select failed\n");
exit(-1);
}
for(i=0;i<N;i++){
if(FD_ISSET(fd[i],&sendfds)){
int min=(BLOCKSIZE-have_send[i]>sizeof(payload))?sizeof(payload):(BLOCKSIZE-have_send[i]);
if(have_send[i]!=BLOCKSIZE){
for(m=0;m<min;m++){
payload[m]=stripe->data_block[i][have_send[i]+m];
}
if((len=send(fd[i],payload,min,0))<0){
printf("Can not send it to the %d server\n",i);
exit(1);
}
// printf("len: %d min: %d data_block: %d\n",len,min,BLOCKSIZE);
// print_payload(payload,len);
have_send[i]+=len;
memset(payload,0,sizeof(payload));
}
// for(j=0;j<=sizeof(stripe->data_block[i])/(sizeof(payload)-1);j++){
// for(m=0;m<sizeof(payload)-1;m++){
// payload[m]=stripe->data_block[i][j*(sizeof(payload)-1)+m];
// }
// if((len=send(fd[i],payload,sizeof(payload)-1,0))<0){
// printf("Can not send it to the %d server\n",i);
// exit(1);
// }
// have_send[i]+=len;
// memset(payload,0,sizeof(payload));
// }
}
}
int sendAll=0;
for(i=0;i<N;i++){
if(have_send[i]==BLOCKSIZE){
sendAll+=1;
}
}
if(sendAll==N){
break;
}
}
}
// void send_encode_data(int fd[N],Stripe*stripe){
// int i;
// for(i=0;i<N;i++){
// }
// }
void encode_data(int fd[N],Stripe* stripe,char filename[]){
int i,j,m,c,e,ret;
int k=K,p=N-K,len=BLOCKSIZE,n=N;
// uint8_t *frag_ptrs[BLOCKSIZE];
// uint8_t *encode_matrix, errors_matrix;
// uint8_t *invert_matrix, *temp_matrix;
// uint8_t *g_tbls;
// Stripe *stripe;
// encode_matrix=(uint8_t *)malloc(sizeof(uint8_t)*((k+p)*k));
// errors_matrix=(uint8_t *)malloc(sizeof(uint8_t)*(k*k));
// invert_matrix=(uint8_t *)malloc(sizeof(uint8_t)*(k*k));
// g_tbls=(uint8_t *)malloc(sizeof(uint8_t)*(k*p*32));
// printf("can i reach here 6\n");
stripe->encode_matrix=(uint8_t*)malloc(sizeof(uint8_t)*(n*k));
stripe->table=(uint8_t*)malloc(sizeof(uint8_t)*(32*p*k));
// printf("can i reach here 5\n");
gf_gen_rs_matrix(stripe->encode_matrix,k+p,k);
// printf("can i reach here 7\n");
ec_init_tables(k,p,&stripe->encode_matrix[k*k],stripe->table);
// printf("can i reach here 8\n");
ec_encode_data(BLOCKSIZE,k,p,stripe->table,stripe->data_block,stripe->parity_block);
// printf("can i reach here 9\n");
// for(i=0;i<N;i++){
// // printf("%dth row:",i);
// for(j=0;j<len;j++){
// //printf("%d ",stripe->data_block[i][j]);
// }
// // printf("\n");
// }
client_send_file_data(fd,stripe,filename);
}
void divide_file_into_blocks(int fd[],char filename[],char path[]){
int i;
char filepath[1025]="";
strcat(filepath,path);
strcat(filepath,filename);
// printf("can i reach here\n");
FILE * file=NULL;
// printf("can i reach here\n");
if((file=fopen(filepath,"rb"))<0){
perror("fail to open the file\n");
}
// printf("can i reach here\n");
unsigned int filelength=0;
char c;
c=fgetc(file);
while(!feof(file)){
filelength+=1;
// printf("char is %X\n",c);
c=fgetc(file);
}
// printf("file length is %d\n",filelength);
fclose(file);
struct message_s header;
int len;
char payload[PAYLEN];
header.protocol[0]='m';
header.protocol[1]='y';
header.protocol[2]='f';
header.protocol[3]='t';
header.protocol[4]='p';
header.type=0xFF;
header.length=BLOCKSIZE*ceiling((float)filelength/(BLOCKSIZE*K));
header.file_length= htonl(filelength);
header.length = htonl(header.length);
if((file=fopen(filepath,"rb"))<0){
perror("fail to open the file\n");
}
for( i=0;i<N;i++){
if((len=send(fd[i],&header,sizeof(header),0))<0){
perror("Not all servers connecting to the client\n");
exit(1);
}
}
// printf("can i reach here1\n");
Stripe* stripe=(Stripe *)malloc(sizeof(Stripe));
// printf("can i reach here6\n");
stripe->data_block=(unsigned char**)malloc(sizeof(unsigned char * )*N);
// unsigned char ** data_block=(unsigned char**)malloc(sizeof(unsigned char * )*N);
// printf("can i reach here4\n");
stripe->parity_block=(unsigned char**)&(stripe->data_block[K]);
for(i=0;i<N;i++){
// printf("can i reach here3\n");
stripe->data_block[i]=(unsigned char*)malloc(sizeof(unsigned char )*BLOCKSIZE);
// printf("ith data_block: %d\n",BLOCKSIZE);
memset(stripe->data_block[i],0,BLOCKSIZE);
}
//
// memset(payload,0,PAYLEN);
int if_finish=0;
unsigned int file_length=0;
// int stripe_num=0;
header.length = ntohl(header.length);
stripe->sid=0;
for(i=0;i<=(filelength-1)/(BLOCKSIZE*K);i++){
int j;
for(j=0;j<K;j++){
int y;
for(y=0;y<BLOCKSIZE;y++){
stripe->data_block[j][y]=fgetc(file);
if(!feof(file)){
file_length+=1;
}
else{//finish reading the file
if_finish=1;
encode_data(fd,stripe,filename);
stripe->sid+=1;
// printf("can i reach here 10\n");
int x;
for(x=0;x<N;x++){
memset(stripe->data_block[x],0,BLOCKSIZE);
}
break;
}
}
if(if_finish==1){
break;
}
}
if(if_finish==0){
encode_data(fd,stripe,filename);
stripe->sid+=1;
int x;
for(x=0;x<N;x++){
memset(stripe->data_block[x],0,BLOCKSIZE);
}
}
}
// printf("Finish dividing the file\n");
fclose(file);
}
void list_file(int fd[], struct message_s header){
// memset(command,0,5);
int len;
int i;
header.protocol[0]='m';
header.protocol[1]='y';
header.protocol[2]='f';
header.protocol[3]='t';
header.protocol[4]='p';
header.type=0xA1;
// buf.type=0xA1;
header.length=0;
header.length = htonl(header.length);
// buf=htonl(buf);
for(i=0;i<N;i++)
{
if(fd[i]!=-1){
// printf("fd: %d\n",fd[i]);
if((len=(send(fd[i],&header,sizeof(header),0)))<0){
perror("can not send request for list\n");
}
// printf("send the command to server\n");
struct message_s message_from_server;
// printf("size : %d %d\n",sizeof(message_from_server),sizeof(message_from_server.payload));
if((len=(recv(fd[i],&message_from_server,sizeof(message_from_server),0)))<0){
perror("can not recv the message from server\n");
}
// printf("recv the message\n");
else{
char payload[PAYLEN];
memset(payload,0,sizeof(payload));
message_from_server.length = ntohl(message_from_server.length);
// printf("size:%d\n",message_from_server.length);
printf("list: \n");
int j;
for( j=0;j<=(int)(message_from_server.length-1)/sizeof(payload);j++){
int lenleft=sizeof(payload);
while(lenleft!=0){
if((len=(recv(fd[i],payload,sizeof(payload),0)))<0){
perror("can not receive the file list from server\n");
exit(1);
}
lenleft-=len;
if(lenleft<0){
printf("lenleft < 0\n");
}
}
printf("%s\n",payload);
}
}
break;
}
}
for(i=0;i<SERVERNUM;i++){
if(fd[i]!=-1){
close(fd[i]);
}
}
printf("Done\n");
}
void select_recv_payload(int fd[],int work_node[], Stripe *stripe,int length){
int i,j;
int len;
fd_set recvfds;
int max_sd;
int recvAll=0;
int *have_recv=(int*)malloc(sizeof(int)*N);
unsigned char payload[PAYLEN];
memset(payload,0,sizeof(payload));
for(i=0;i<N;i++){
have_recv[i]=0;
}
while(1){
FD_ZERO(&recvfds);
for(i=0;i<N;i++){
if( work_node[i]==1){
FD_SET(fd[i],&recvfds);
max_sd=(max_sd>fd[i])?max_sd:fd[i];
}
}
if(select(max_sd+1,&recvfds,NULL,NULL,NULL)<0){
printf("select failed\n");
exit(-1);
}
for(i=0;i<N;i++){
if(FD_ISSET(fd[i],&recvfds)){
// int min=(BLOCKSIZE-have_send[i]>sizeof(payload))?sizeof(payload):(BLOCKSIZE-have_send[i]);
// struct message_s header;
memset(payload,0,sizeof(payload));
if(work_node[i]==1 && have_recv[i]!=length){
if(sizeof(payload)<length-have_recv[i]){
if((len=recv(fd[i],payload,sizeof(payload),0))<0){
printf("Can not recv the payload from the %d server\n",i);
exit(1);
}
}
else{
if((len=recv(fd[i],payload,length-have_recv[i],0))<0){
printf("Can not recv the payload from the %d server\n",i);
exit(1);
}
}
// work_node[i]=1;
// printf("can i reach here \n");
for(j=0;j<len;j++){
// printf("have recv i: %d, j:%d, have_recv:%d, length: %d\n",i,j,have_recv[i],length);
stripe->data_block[i][j+have_recv[i]]=payload[j];
}
// printf("can i reach here 6\n");
have_recv[i]+=len;
if(have_recv[i]==length){
recvAll+=1;
}
// printf(" have_recv:%d, length: %d i:%i\n",have_recv[i],length,i);
if(have_recv[i]>length){
printf("recv extra data\n");
exit(1);
}
}
if(recvAll==K){
// return header;
break;
}
}
}
if(recvAll==K){
break;
}
}
}
struct message_s select_recv_header(int fd[],int work_node[]){
int i;
int len;
fd_set recvfds;
int max_sd;
int recvAll=0;
int *have_recv=(int*)malloc(sizeof(int)*N);
for(i=0;i<N;i++){
have_recv[i]=0;
}
while(1){
FD_ZERO(&recvfds);
for(i=0;i<N;i++){
if( work_node[i]==1){
FD_SET(fd[i],&recvfds);
max_sd=(max_sd>fd[i])?max_sd:fd[i];
}
}
if(select(max_sd+1,&recvfds,NULL,NULL,NULL)<0){
printf("select failed\n");
exit(-1);
}
for(i=0;i<N;i++){
if(FD_ISSET(fd[i],&recvfds)){
// int min=(BLOCKSIZE-have_send[i]>sizeof(payload))?sizeof(payload):(BLOCKSIZE-have_send[i]);
struct message_s header;
if(work_node[i]==1 && have_recv[i]==0){
// printf("severid: %d\n",i);
memset(&header,0,sizeof(header));
if((len=recv(fd[i],&header,sizeof(header),0))!=sizeof(header)){
// printf("Can not recv the header from the %d server\n",i);
// printf("recv %d\n",len);
exit(1);
}
have_recv[i]=1;
// work_node[i]=1;
recvAll+=1;
}
if(recvAll==K){
return header;
break;
}
}
}
if(recvAll==K){
break;
}
}
}
void select_send_content(int fd[],int work_node[],void * content,int size_to_send,int if_firsttime){
int i,j;
int len;
fd_set sendfds;
int max_sd;
int sendAll=0;
int *have_send=(int*)malloc(sizeof(int)*N);
for(i=0;i<N;i++){
have_send[i]=0;
}
while(1){
FD_ZERO(&sendfds);
// FD_SET(2,&sendfds);
for(i=0;i<N;i++){
if(if_firsttime==0 ){
if( work_node[i]==1){
FD_SET(fd[i],&sendfds);
max_sd=(max_sd>fd[i])?max_sd:fd[i];
}
}
else{
FD_SET(fd[i],&sendfds);
max_sd=(max_sd>fd[i])?max_sd:fd[i];
}
}
struct timeval timelimit;
timelimit.tv_sec=20;
if(select(max_sd+1,NULL,&sendfds,NULL,&timelimit)<0){
// printf("exception: %s",explain_select(max_sd+1,NULL,&sendfds,NULL,&timelimit));
printf("select failed\n");
exit(-1);
}
if(if_firsttime==0)
{
for(i=0;i<N;i++){
if(FD_ISSET(fd[i],&sendfds)){
// int min=(BLOCKSIZE-have_send[i]>sizeof(payload))?sizeof(payload):(BLOCKSIZE-have_send[i]);
if(work_node[i]==1 && have_send[i]!=1){
if((len=send(fd[i],content,size_to_send,0))!=size_to_send){
printf("Can not send the header to the %d server\n",i);
exit(1);
}
// printf("i have send %d size_to_send: %d i:%d\n",len,size_to_send,i);
sendAll+=1;
have_send[i]=1;
}
if(sendAll==K){
break;
}
}
}
}
else{
// timeout=0
// if(FD_ISSET(2,&sendfds)){
// printf("i can connect to an unexisted socket\n");
// exit(1);
// }
for(i=0;i<N;i++){
if(FD_ISSET(fd[i],&sendfds)){
if(work_node[i]!=1){
// printf("%dth server can be connected fd: %d\n",i,fd[i]);
if((len=send(fd[i],content,size_to_send,0))!=size_to_send){
printf("Can not send the header to the %d server\n",i);
exit(1);
}
work_node[i]=1;
sendAll+=1;
// timeout=1;
}
if(sendAll==K){
for(j=0;j<N;j++){
if(work_node[j]==0){
close(fd[j]);
}
}
break;
}
}
}
// printf("time is out\n");
if(sendAll<K){
printf("no enough servers are online\n");
for(i=0;i<N;i++){
close(fd[i]);
}
exit(1);
}
}
if(sendAll==K){
break;
}
}
}
void select_send_serverid(int fd[],int work_node[]){
int i;
int len;
fd_set sendfds;
int max_sd;
int sendAll=0;
int *have_send=(int*)malloc(sizeof(int)*N);
for(i=0;i<N;i++){
have_send[i]=0;
}
while(1){
FD_ZERO(&sendfds);
for(i=0;i<N;i++){
if( work_node[i]==1){
FD_SET(fd[i],&sendfds);
max_sd=(max_sd>fd[i])?max_sd:fd[i];
}
}
if(select(max_sd+1,NULL,&sendfds,NULL,NULL)<0){
printf("select failed\n");
exit(-1);
}
for(i=0;i<N;i++){
if(FD_ISSET(fd[i],&sendfds)){
// int min=(BLOCKSIZE-work_node[i]>sizeof(payload))?sizeof(payload):(BLOCKSIZE-work_node[i]);
struct message_s header;
header.server_id=i;
if(work_node[i]==1 && have_send[i]!=1){
if((len=send(fd[i],&header,sizeof(header),0))!=sizeof(header)){
printf("Can not send the header to the %d server\n",i);
exit(1);
}
sendAll+=1;
have_send[i]==1;
}
if(sendAll==K){
break;
}
}
}
if(sendAll==K){
break;
}
}
}
void stripe_decode(Stripe * stripe,int work_node[],FILE* file,int lenleft){
int i,j,h=0;
uint8_t *encode_matrix=malloc(sizeof(uint8_t)*(N*K));
uint8_t *errors_matrix=malloc(sizeof(uint8_t)*(K*K));
uint8_t *invert_matrix=malloc(sizeof(uint8_t)*(K*K));
uint8_t *decode_matrix=malloc(sizeof(uint8_t)*(N*K));
uint8_t *table=(uint8_t*)malloc(sizeof(uint8_t)*(32*(N-K)*K));
gf_gen_rs_matrix(encode_matrix,N,K);
for(i=0;i<N;i++){
if(work_node[i]==1){
for(j=0;j<K;j++){
errors_matrix[K*h+j]=encode_matrix[K*i+j];
}
h+=1;
}
}
gf_invert_matrix(errors_matrix,invert_matrix,K);
h=0;
// int failed_block[K];
// for(i=0;i<K;i++){
// failed_block[i]=0;
// }
int failed_blocks_num=0;
for(i=0;i<K;i++){
if(work_node[i]!=1){
for(j=0;j<K;j++){
decode_matrix[K*h+j]=invert_matrix[K*i+j];
}
h+=1;
failed_blocks_num+=1;
}
}
// printf("in decode: can i reach here\n");
ec_init_tables(K,N-K,decode_matrix,table);
// printf("in decode: can i reach here2\n");
unsigned char** normal_datablock=(unsigned char**)malloc(sizeof(unsigned char*)*(K-failed_blocks_num));
unsigned char** failed_datablock=(unsigned char**)malloc(sizeof(unsigned char*)*(failed_blocks_num));
int g=0;
// h=0;
// printf("in decode: can i reach here4\n");
// for(i=0;i<K;i++){
// if(work_node[i]==1){
// normal_datablock[g]=(unsigned char*)malloc(sizeof(unsigned char)*BLOCKSIZE);
// for(j=0;j<BLOCKSIZE;j++){
// normal_datablock[g][j]=stripe->data_block[i][j];
// }
// g++;
// }
// else{
// failed_datablock[h]=(unsigned char*)malloc(sizeof(unsigned char)*BLOCKSIZE);
// for(j=0;j<BLOCKSIZE;j++){
// // failed_datablock[h][j]=stripe->data_block[i][j];
// failed_datablock[h][j]=0;
// }
// h++;
// }
// }
h=0;
for(i=0;i<N;i++){
if(work_node[i]==1){
normal_datablock[h]=stripe->data_block[i];
h++;
}
}
for(i=0;i<failed_blocks_num;i++){
failed_datablock[i]=(unsigned char*)malloc(sizeof(unsigned char)*BLOCKSIZE);
}
// printf("in decode: can i reach here3,failed_datablock: %d\n",failed_blocks_num);
ec_encode_data(BLOCKSIZE,K,failed_blocks_num,table,normal_datablock,failed_datablock);
// printf("in decode: can i reach here5\n");
int len_to_write=0;
if(lenleft>BLOCKSIZE*K){
len_to_write=BLOCKSIZE*K;
}
else{
len_to_write=lenleft;
}
g=0;
h=0;
// printf("len_to_write is %d\n",len_to_write);
for(i=0;i<K;i++){
if(len_to_write==0){
break;
}
if(work_node[i]==1){
if(len_to_write>BLOCKSIZE){
if((fwrite(normal_datablock[g],BLOCKSIZE,1,file))<0){
perror("fail to write the file\n");
}
len_to_write-=BLOCKSIZE;
}
else{
if((fwrite(normal_datablock[g],len_to_write,1,file))<0){
perror("fail to write the file\n");
}
len_to_write-=len_to_write;
}
g++;
}
else{
if(len_to_write>BLOCKSIZE){
if((fwrite(failed_datablock[h],BLOCKSIZE,1,file))<0){
perror("fail to write the file\n");
}
len_to_write-=BLOCKSIZE;
}
else{
if((fwrite(failed_datablock[h],len_to_write,1,file))<0){
perror("fail to write the file\n");
}
len_to_write-=len_to_write;
}
h++;
}
}
fflush(file);
return;
}
void client_recv_file_data(int fd[],char filename[],char path[],int work_node[]){
int stripenum=0;
int i,j;
unsigned char payload[PAYLEN];
fd_set sendfds;
int max_sd=0;
// int *have_send=(int*)malloc((int)*N);
// for(i=0;i<N;i++){
// have_send[i]=0;
// }
// printf("can i reach here 1\n");
select_send_serverid(fd,work_node);
int filelength=0;
struct message_s header=select_recv_header(fd,work_node);
// printf("can i reach here 2\n");
if(header.type!=0xFF){
printf("wrong type!\n");
exit(1);
}
header.file_length=ntohl(header.file_length);
header.length=ntohl(header.length);
filelength=header.length;
int lenleft=header.file_length;
Stripe* stripe=(Stripe*)malloc(sizeof(Stripe));
stripe->data_block=(unsigned char**)malloc(sizeof(unsigned char*)*N);
for(i=0;i<N;i++){
stripe->data_block[i]=(char*)malloc(sizeof(unsigned char)*BLOCKSIZE);
}
FILE * file=NULL;
char filepath[1024]="";
strcat(filepath,filename);
if((file=fopen(filepath,"wb"))==NULL){
printf("faile to open file: %s\n",filepath);
}
for(i=0;i<ceiling((float)filelength/(BLOCKSIZE*N));i++){
// printf("can i reach here 3\n");
select_recv_payload(fd,work_node,stripe,BLOCKSIZE);
// printf("can i reach here4\n");
// printf("lenleft: %d\n",lenleft);
stripe_decode(stripe,work_node,file,lenleft);
if(i<ceiling((float)filelength/(BLOCKSIZE*N))-1){
lenleft-=BLOCKSIZE*K;
}
struct message_s header;
header.type=0xD1;
// printf("i have done collect a stripe\n");
select_send_content(fd,work_node,&header,sizeof(header),0);
}
fclose(file);
printf("Done\n");
}
void get_file(int fd[SERVERNUM], char * command,char filename[]){
struct message_s header;
int i;
int len;
int mr_fd=0;
int ms_fd=0;
fd_set recv_fds;
fd_set send_fds;
// if(command[3]!=' '){
// printf("c:%s\n",command);
// perror("wrong format\n");
// exit(1);
// }
// for (i=3;i<strlen(command);i++){
// if(command[i]!=' '){
// break;
// }
// }
// const char *tmp=&command[i];
// char filename[32];
char payload[PAYLEN]="";
// strcpy(filename,tmp);
header.protocol[0]='m';
header.protocol[1]='y';
header.protocol[2]='f';
header.protocol[3]='t';
header.protocol[4]='p';
header.type=0xB1;
strcpy(payload,filename);
header.length=strlen(filename);
header.length = htonl(header.length);
int ava_server=0;
int *work_node=(int*)malloc(N*sizeof(int));
for(i=0;i<N;i++){
work_node[i]=0;
}
select_send_content(fd,work_node,&header,sizeof(header),1);
for(i=0;i<N;i++){
if(work_node[i]==1){
// printf("the %d server is on\n",i);
}
}
select_send_content(fd,work_node,payload,sizeof(payload),0);
struct message_s result_of_get=select_recv_header(fd,work_node);
if(result_of_get.type==0xB2){
printf("servers successfully find the file\nplease wait for a while\n");
// recv_file_data(fd, filename,"");
}
else{
printf("fail to find the file\n");
exit(1);
}
char path[2]="";
client_recv_file_data(fd, filename,path,work_node);
}
void put_file(int fd[SERVERNUM], char command[],char filename[]){
struct message_s header;
int i;
int len;
// if(command[3]!=' '){
// printf("c:%s\n",command);
// perror("wrong format\n");
// exit(1);
// }
// for (i=3;i<strlen(command);i++){
// if(command[i]!=' '){
// break;
// }
// }
// const char *tmp=&command[i];
// char filename[32];
char payload[PAYLEN]="";
int f_exist=0;
// strcpy(filename,tmp);
strcpy(payload,filename);
header.protocol[0]='m';
header.protocol[1]='y';
header.protocol[2]='f';
header.protocol[3]='t';
header.protocol[4]='p';
header.type=0xC1;
header.length = strlen(filename);
header.length = htonl(header.length);
DIR * dir;
struct dirent * ptr;
if ((dir=opendir("."))==NULL){
perror("can not find the responding data\n");
}
while((ptr=readdir(dir))!=NULL){
if(strcmp(payload,ptr->d_name)==0){
f_exist=1;
break;
}
}
if(f_exist==0){
// printf("can not find the file locally\n" );
perror("can not find the file locally\n");
exit(1);
}
else{
printf("find the file\nplease wait for a while\n");
for(i=0;i<N;i++){
if((len=(send(fd[i],&header,sizeof(header),0)))<0){
perror("can not send request to server");
}
}
//printf("send the header\n");
for(i=0;i<N;i++){
if((len=(send(fd[i],payload,sizeof(payload),0)))<0){
perror("can not send request to server");
}
}
//printf("send the file name\n");
struct message_s message_from_server;
for(i=0;i<N;i++){
if((len=(recv(fd[i],&message_from_server,sizeof(message_from_server),0)))<0){
perror("can not recv request to client");
}
if(message_from_server.type!=0xC2){
perror("can't receive header from all servers\n");
}
}
divide_file_into_blocks(fd,filename,"");
}
printf("Done\n");
}
void main_task(in_addr_t *ip, unsigned short *port,char command[],char filename[])
{
int i;
int j;
struct message_s header;
// struct all_data buf;
int *fd=(int*)malloc(sizeof(int)*SERVERNUM);
// char command[100];
// memset(command,0,100);
struct sockaddr_in addr;
unsigned int addrlen=sizeof (struct sockaddr_in);
int len;
// scanf("%s", command);
// printf("command: %s",command);
for( i=0;i<SERVERNUM;i++)
{
fd[i]=socket(AF_INET, SOCK_STREAM,0);
if(fd[i]== -1){
perror("socket()");
exit(1);
}
// printf("fd[%d]: %d\n",i,fd[i]);
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=ip[i];
addr.sin_port=htons(port[i]);
// printf("connect: %d\n",port[i]);
if(connect(fd[i], (struct sockaddr *) &addr, addrlen)==-1){
printf("connection to the %dth server is failed\n",i);
close(fd[i]);
fd[i]=-1;
// exit(1);
}
}
// while(strcmp(command,"list")!=0 && strncmp(command,"get",3)!=0&& strncmp(command,"put",3)!=0){
// printf("command: (1) list, (2) get filename, or (3) put filename\n");
// // scanf("%s", command);
// // gets(command);
// fgets(command,100,stdin);
// command[strlen(command)-1]='\0';
// printf("command: %s\n",command);
// // scanf("%s", command);
// }
printf("command: %s\n",command);
if (strcmp(command,"list")==0){
// printf("what is wrong\n");
list_file(fd,header);
}
if (strcmp(command,"get")==0){
get_file(fd,command,filename);
}
if (strcmp(command,"put")==0){
for(i=0;i<N;i++){
if(fd[i]<0){
printf("Not all servers are connected\n");
for(j=0;j<N;j++){
if(fd[j]<0){
close(fd[i]);
}
}
exit(1);
}
}
put_file(fd,command,filename);
// printf("Sent nothing\n");
}
for(j=0;j<N;j++){
if(fd[i]<0){
close(fd[i]);
}
}
}
int main(int argc, char **argv){
unsigned short *port;
if (argc !=4 && argc!=3){
fprintf(stderr,"Usage: %s [configeration file] <list|get|input> <file>\n", argv[0]);
exit(1);
}
in_addr_t *ip;
// if ((ip=inet_addr(argv[1]))==-1){
// perror("inet_addr()");
// exit(1);
// }
FILE *config_file;
if((config_file=fopen(argv[1],"r"))<0){
printf("No such file\n");
exit(1);
}
char line[100];