main.c

Go to the documentation of this file.
00001 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <sys/types.h>
00028 #include <sys/stat.h>
00029 #include <unistd.h>
00030 #include <fcntl.h>
00031 
00032 #include "../../alclib/blocking_alg.h"
00033 #include "../../alclib/transport.h"
00034 #include "../../alclib/null_fec.h"
00035 #include "../../alclib/xor_fec.h"
00036 #include "../../alclib/rs_fec.h"
00037 
00038 #include "../../flutelib/fdt.h"
00039 #include "../../flutelib/uri.h"
00040 
00041 #include "decode_query_string.h"
00042 #include "parse_query_string.h"
00043 #include "add_fec_plid.h"
00044 #include "helpers.h"
00045 
00053 int main(void) {
00054 
00055   char *request_method = NULL;
00056   char *size_of_query_string = NULL;
00057   char *query_string = NULL;
00058   char *decoded_query_string = NULL;
00059   blocking_struct_t *bs = NULL;
00060   query_str_t *qs = NULL;
00061   char filepath[MAX_PATH_LENGTH];
00062   struct stat64 file_stats;
00063   struct stat fdtfile_stats;
00064   char *fdt_buf = NULL;
00065   unsigned int fdt_length;
00066   unsigned fdt_nbytes;
00067   FILE *fdt_fp;
00068   fdt_t *fdt = NULL;
00069   file_t *file = NULL;
00070 
00071   int fec_ratio = 0;
00072   
00073   char *buf = NULL;
00074   unsigned long long nbytes;
00075   unsigned long long pos;
00076   int fp;
00077 
00078   trans_block_t *tr_block = NULL;
00079   trans_unit_t *tr_unit = NULL;
00080 
00081   qs_missing_block_t *ms_block = NULL;
00082   qs_missing_symbol_t *ms_symbol = NULL;
00083 
00084   int i;
00085   char *uri_path = NULL;
00086 
00087   char *repair_conf_file = NULL;
00088   repair_arguments_t ra;
00089   int retval;
00090   FILE *ptm_repair_file;
00091 
00092   unsigned int sbn = 0;
00093 
00094   repair_conf_file = getenv("RepairConfFile");
00095 
00096   if(repair_conf_file == NULL) {
00097     return -1;
00098   }
00099 
00100   if(repair_conf_file[strlen(repair_conf_file)-1] == '\r') {
00101     repair_conf_file[strlen(repair_conf_file)-1] = '\0';
00102   }
00103   
00104   retval = parse_repair_conf_file(repair_conf_file, &ra);
00105     
00106   if(retval == -1) {
00107     return -1;
00108   }
00109 
00110   retval = parse_flute_conf_file(&ra);
00111     
00112   if(retval == -1) {
00113     return -1;
00114   }
00115 
00116   if(stat(ra.fdt_file, &fdtfile_stats) == -1) {
00117     return -1;
00118   }
00119 
00120   fdt_length = fdtfile_stats.st_size;
00121 
00122   /* Allocate memory for buf, to read fdt file to it */
00123   if(!(fdt_buf = (char*)calloc((fdt_length + 1), sizeof(char)))) {
00124     return -1;
00125   }
00126 
00127   if((fdt_fp = fopen(ra.fdt_file, "rb")) == NULL) {
00128     free(fdt_buf);
00129     return -1;
00130   }
00131 
00132   fdt_nbytes = fread(fdt_buf, 1, fdt_length, fdt_fp);
00133 
00134   if(fdt_nbytes <= 0) {
00135     fclose(fdt_fp);
00136     free(fdt_buf);
00137     return -1;
00138   }
00139 
00140   fdt = decode_fdt_payload(fdt_buf);
00141   free(fdt_buf);
00142   fclose(fdt_fp);
00143 
00144   request_method = getenv("REQUEST_METHOD");
00145 
00146   if(strcmp(request_method, "GET") == 0) {
00147     query_string = getenv("QUERY_STRING");
00148 
00149     if(query_string == NULL) {
00150       FreeFDT(fdt);
00151       return -1;
00152     }
00153   }
00154   else {
00155       size_of_query_string = getenv("CONTENT_LENGTH");
00156 
00157       if(!(query_string = (char*)calloc((atoi(size_of_query_string) + 1), sizeof(char)))) {
00158         FreeFDT(fdt);
00159         return -1;
00160       }
00161 
00162       read(0, query_string, atoi(size_of_query_string)); /* 0 is stdin */
00163   }
00164 
00165   decoded_query_string = decode_query_string(query_string);
00166   
00167   if(decoded_query_string == NULL) {
00168 
00169     if(strcmp(request_method, "POST") == 0) {
00170       free(query_string);
00171     }
00172     FreeFDT(fdt);
00173     return -1;
00174   }
00175 
00176   /* parse query string */
00177   qs = parse_query_string(decoded_query_string);
00178   
00179   if(qs == NULL) {
00180     free(decoded_query_string);
00181 
00182     if(strcmp(request_method, "POST") == 0) {
00183       free(query_string);
00184     }
00185 
00186     FreeFDT(fdt);
00187     return -1;
00188   }
00189 
00190   /* Find file struct */
00191 
00192   file = fdt->file_list;
00193 
00194   while(file != NULL) {
00195     uri_path = get_uri_host_and_path(qs->fileURI);
00196 
00197     if(strstr(file->location, uri_path) != NULL) {
00198       break;
00199     }
00200 
00201     file = file->next;
00202   }
00203 
00204   if(file == NULL) {
00205 
00206     free(decoded_query_string);
00207 
00208     if(strcmp(request_method, "POST") == 0) {
00209       free(query_string);
00210     }
00211 
00212     FreeFDT(fdt);
00213     free_query_str(qs);
00214     return -1;
00215   }
00216 
00217   if(strcmp(ra.repair_method, "PTM") == 0){
00218 
00219     if((ptm_repair_file = fopen(ra.requested_blocks_file, "a")) == NULL) {
00220       free(decoded_query_string);
00221 
00222       if(strcmp(request_method, "POST") == 0) {
00223         free(query_string);
00224       }
00225 
00226       free_query_str(qs);
00227       FreeFDT(fdt);
00228       return -1;
00229     }
00230 
00231     ms_block = qs->block_list;
00232 
00233     if(ms_block == NULL) {
00234       fprintf(ptm_repair_file, "%llu:*\n", file->toi);
00235     }
00236     else {
00237       while(ms_block != NULL) {
00238         fprintf(ptm_repair_file, "%llu:%i\n", file->toi, ms_block->sbn);
00239         ms_block = ms_block->next;
00240       }
00241     }
00242     
00243     free(decoded_query_string);
00244 
00245     if(strcmp(request_method, "POST") == 0) {
00246       free(query_string);
00247     }
00248 
00249     free_query_str(qs);
00250     FreeFDT(fdt);
00251     fclose(ptm_repair_file);
00252     
00253     printf("%s%c%c", "Location:", 13, 10);
00254     printf("%c%c", 13, 10);
00255   }
00256   else {
00257     memset(filepath, 0, MAX_PATH_LENGTH);
00258     
00259     if(ra.base_dir != NULL) {
00260       if(!(strcmp(ra.base_dir, "") == 0)) {
00261         strcpy(filepath, ra.base_dir);
00262         strcat(filepath, "/");
00263       }
00264     } 
00265     strcat(filepath, get_uri_host_and_path(qs->fileURI));
00266 
00267     if(file->encoding != NULL) {
00268 #ifdef USE_ZLIB
00269       if(strcmp(file->encoding, "gzip") == 0) {
00270         strcat(filepath, GZ_SUFFIX);
00271       }
00272 #endif
00273     }
00274 
00275     /* use stat to get file size */
00276     
00277     if(stat64(filepath, &file_stats) == -1) {
00278       free(decoded_query_string);
00279 
00280       if(strcmp(request_method, "POST") == 0) {
00281         free(query_string);
00282       }
00283 
00284       free_query_str(qs);
00285       FreeFDT(fdt);
00286       return -1;
00287 
00288     }
00289     
00290     if(file_stats.st_size == 0) {
00291       free(decoded_query_string);
00292 
00293       if(strcmp(request_method, "POST") == 0) {
00294         free(query_string);
00295       }
00296 
00297       free_query_str(qs);
00298       FreeFDT(fdt);
00299       return -1;
00300     }
00301     
00302     /* calculate blocking structure */
00303 
00304     bs = compute_blocking_structure((unsigned long long)file_stats.st_size, file->max_sb_len, file->es_len);
00305        
00306     /* Allocate memory for buf */
00307     if(!(buf = (char*)calloc((unsigned int)(file->es_len * file->max_sb_len), sizeof(char)))) {
00308       free(decoded_query_string);
00309 
00310       if(strcmp(request_method, "POST") == 0) {
00311         free(query_string);
00312       }
00313 
00314       free_query_str(qs);
00315       free(bs);
00316       FreeFDT(fdt);
00317       return -1;
00318     }
00319     
00320     /* File to repair */
00321     if((fp = open64(filepath, 0, 0)) < 0) {
00322       free(decoded_query_string);
00323 
00324       if(strcmp(request_method, "POST") == 0) {
00325         free(query_string);
00326       }
00327 
00328       free_query_str(qs);
00329       free(bs);
00330       free(buf);
00331       FreeFDT(fdt);
00332       return -1;
00333     }
00334         
00335     /* http response message */    
00336     /* http response message headers*/
00337 
00338     printf("%s%c%c", "Content-Type: application/simpleSymbolContainer", 13, 10);
00339     printf("%s%c%c", "Content-Transfer-Encoding: binary", 13, 10);
00340     printf("%c%c", 13, 10);
00341     
00342     ms_block = qs->block_list;
00343 
00344     if(ms_block == NULL) {
00345      
00346       while(sbn < bs->N) {
00347         
00348         if(sbn < bs->I) {
00349           nbytes = file->es_len * (bs->A_large);
00350         }
00351         else {
00352           nbytes = file->es_len * (bs->A_small);
00353         }
00354         
00355         memset(buf, 0, (file->es_len * file->max_sb_len));
00356         nbytes = read(fp, buf, (unsigned int)nbytes);
00357         
00358         if(nbytes < 0) {
00359           free(decoded_query_string);
00360 
00361           if(strcmp(request_method, "POST") == 0) {
00362             free(query_string);
00363           }
00364 
00365           free_query_str(qs);
00366           free(buf);
00367           free(bs);
00368           close(fp);
00369           FreeFDT(fdt);
00370           return -1;
00371         }
00372         
00373         /* all could use null_fec_encode_src_block() functions, because FEC symbols are not transmitted */
00374         
00375         if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
00376           tr_block = null_fec_encode_src_block(buf, nbytes, sbn, file->es_len);
00377         }
00378         else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
00379           tr_block = xor_fec_encode_src_block(buf, nbytes, sbn, file->es_len);
00380         }
00381         else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
00382           tr_block = rs_fec_encode_src_block(buf, nbytes, sbn, file->es_len, fec_ratio, file->max_sb_len);
00383         }
00384         else {
00385           free(decoded_query_string);
00386 
00387           if(strcmp(request_method, "POST") == 0) {
00388             free(query_string);
00389           }
00390 
00391           free_query_str(qs);
00392           free(buf);
00393           free(bs);
00394           close(fp);
00395           FreeFDT(fdt);
00396           return -1;
00397         }
00398         
00399         if(tr_block == NULL) {
00400           free(decoded_query_string);
00401 
00402           if(strcmp(request_method, "POST") == 0) {
00403             free(query_string);
00404           }
00405 
00406           free_query_str(qs);
00407           free(buf);
00408           free(bs);
00409           close(fp);
00410           FreeFDT(fdt);
00411           return -1;
00412         }
00413         
00414         tr_unit = tr_block->unit_list;
00415         
00416         add_length_indicator(tr_block->k);
00417 
00418         if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
00419           add_fec_plid_0_130((unsigned short)tr_block->sbn, (unsigned short)tr_unit->esi);
00420         }
00421         else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
00422           add_fec_plid_128(tr_block->sbn, tr_unit->esi);
00423         }
00424         else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
00425           add_fec_plid_129(tr_block->sbn, (unsigned int)tr_block->k, (unsigned int)tr_unit->esi);
00426         }
00427         
00428         while(1) {
00429           
00430           for(i = 0; i < tr_unit->len; i++) {
00431             printf("%c", tr_unit->data[i]);
00432           }
00433           
00434           if(tr_unit->esi == (tr_block->k - 1)) { /* no FEC symbols */
00435             break;
00436           }
00437           
00438           tr_unit++;
00439         }
00440 
00441         tr_unit = tr_block->unit_list;
00442 
00443         while(1) {
00444           free(tr_unit->data);
00445 
00446           if(tr_unit->esi == (tr_block->n - 1)) {
00447             break;
00448           }
00449 
00450           tr_unit++;
00451         }
00452 
00453         free(tr_block->unit_list);
00454         free(tr_block);
00455 
00456         sbn++;
00457       }
00458     }
00459     else {
00460     
00461       while(ms_block != NULL) {
00462         
00463         /* Set place where to read */
00464         
00465         if(ms_block->sbn < bs->I) {
00466           pos = (unsigned long long)ms_block->sbn * (unsigned long long)bs->A_large * (unsigned long long)file->es_len;
00467         }
00468         else {
00469           pos = ( ( ( (unsigned long long)bs->I * (unsigned long long)bs->A_large ) + 
00470                     ( (unsigned long long)ms_block->sbn - (unsigned long long)bs->I ) * (unsigned long long)bs->A_small ) * (unsigned long long)file->es_len );
00471         }
00472 
00473         /* set correct position */
00474         
00475         if(lseek64(fp, pos, SEEK_SET) == -1) {
00476           free(decoded_query_string);
00477 
00478           if(strcmp(request_method, "POST") == 0) {
00479             free(query_string);
00480           }
00481 
00482           free_query_str(qs);
00483           free(buf);
00484           free(bs);
00485           close(fp);
00486           FreeFDT(fdt);
00487           return -1;
00488         }
00489         
00490         if(ms_block->sbn < bs->I) {
00491           nbytes = file->es_len * (bs->A_large);
00492         }
00493         else {
00494           nbytes = file->es_len * (bs->A_small);
00495         }
00496         
00497         memset(buf, 0, (file->es_len * file->max_sb_len));
00498         nbytes = read(fp, buf, (unsigned int)nbytes);
00499         
00500         /* all could use null_fec_encode_src_block() functions, because FEC symbols are not transmitted */
00501         
00502         if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
00503           tr_block = null_fec_encode_src_block(buf, nbytes, ms_block->sbn, file->es_len);
00504         }
00505         else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
00506           tr_block = xor_fec_encode_src_block(buf, nbytes, ms_block->sbn, file->es_len);
00507         }
00508         else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
00509           tr_block = rs_fec_encode_src_block(buf, nbytes, ms_block->sbn, file->es_len, fec_ratio, file->max_sb_len);
00510         }
00511         else {
00512           free(decoded_query_string);
00513 
00514           if(strcmp(request_method, "POST") == 0) {
00515             free(query_string);
00516           }
00517 
00518           free_query_str(qs);
00519           free(buf);
00520           free(bs);
00521           close(fp);
00522           FreeFDT(fdt);
00523           return -1; 
00524         }
00525         
00526         if(tr_block == NULL) {
00527           free(decoded_query_string);
00528 
00529           if(strcmp(request_method, "POST") == 0) {
00530             free(query_string);
00531           }
00532 
00533           free_query_str(qs);
00534           free(buf);
00535           free(bs);
00536           close(fp);
00537           FreeFDT(fdt);
00538           return -1;
00539         }
00540         
00541         ms_symbol = ms_block->es_list;
00542         
00543         if(ms_symbol == NULL) {
00544           tr_unit = tr_block->unit_list;
00545           
00546           add_length_indicator(tr_block->k);
00547           
00548           if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
00549             add_fec_plid_0_130((unsigned short)ms_block->sbn, (unsigned short)tr_unit->esi);
00550           }
00551           else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
00552             add_fec_plid_128(ms_block->sbn, tr_unit->esi);
00553           }
00554           else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
00555             add_fec_plid_129(ms_block->sbn, (unsigned int)tr_block->k, (unsigned int)tr_unit->esi);
00556           }
00557           
00558           while(1) {
00559             
00560             for(i = 0; i < tr_unit->len; i++) {
00561               printf("%c", tr_unit->data[i]);
00562             }
00563             
00564             if(tr_unit->esi == (tr_block->k - 1)) { /* no FEC symbols */
00565               break;
00566             }
00567             
00568             tr_unit++;
00569           }
00570         }
00571         else {
00572           
00573           while(ms_symbol != NULL) {
00574             
00575             tr_unit = tr_block->unit_list;
00576             
00577             while(1) {
00578               if(tr_unit->esi == ms_symbol->esi) {
00579                 add_length_indicator(1);
00580                 
00581                 if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
00582                   add_fec_plid_0_130((unsigned short)ms_block->sbn, (unsigned short)ms_symbol->esi);
00583                 }
00584                 else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
00585                   add_fec_plid_128(ms_block->sbn, ms_symbol->esi);
00586                 }
00587                 else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
00588                   add_fec_plid_129(ms_block->sbn, (unsigned short)tr_block->k,
00589                                    (unsigned short)ms_symbol->esi);
00590                 }
00591                 
00592                 for(i = 0; i < tr_unit->len; i++) {
00593                   printf("%c", tr_unit->data[i]);
00594                 }
00595                 
00596                 break;
00597               }
00598               
00599               if(tr_unit->esi == (tr_block->k - 1)) { /* no FEC symbols */
00600                 break;
00601               }
00602               
00603               tr_unit++;
00604             }
00605             ms_symbol = ms_symbol->next;
00606           }
00607         }
00608         
00609         tr_unit = tr_block->unit_list;
00610         
00611         while(1) {
00612           free(tr_unit->data);
00613           
00614           if(tr_unit->esi == (tr_block->n - 1)) {
00615             break;
00616           }
00617           
00618           tr_unit++;
00619         } 
00620         
00621         free(tr_block->unit_list);
00622         free(tr_block);
00623         
00624         ms_block = ms_block->next;
00625       }
00626     }
00627 
00628     free(decoded_query_string);
00629 
00630     if(strcmp(request_method, "POST") == 0) {
00631       free(query_string);
00632     }
00633 
00634     free_query_str(qs);
00635     free(bs);
00636     free(buf);
00637     close(fp);        
00638     FreeFDT(fdt);
00639   }
00640 
00641   return 0;
00642 }

Generated on Fri Mar 9 19:59:52 2007 for MAD-FCL by  doxygen 1.5.0