├── .gitattributes ├── LT_code.cpp ├── LT_code.h ├── LT_function.c ├── LT_function.h ├── LT_matlab ├── BP.m ├── Gussian.m ├── LT_decode_BP.m ├── LT_decode_Guassian.m ├── LT_encode.m ├── LT_full_link.m ├── LT_link_simulate.m ├── compute_packet_loss.m ├── find_rank.m └── robust_solition.m └── main.c /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /LT_code.cpp: -------------------------------------------------------------------------------- 1 | #include "LT_code.h" 2 | 3 | LT_code::~LT_code() 4 | { 5 | delete _filename; 6 | //message matrix dtor 7 | if(_buf) 8 | { 9 | delete []_buf; 10 | } 11 | if(_matrix_message_row) 12 | { 13 | for(int i = 0;i < _matrix_message_row;i++) 14 | { 15 | delete []_matrix_message[i]; 16 | } 17 | delete []_matrix_message; 18 | } 19 | 20 | //encode matrix dtor 21 | if(_matrix_encode) 22 | { 23 | for(int i = 0;i < _matrix_encode_row;i++) 24 | { 25 | delete []_matrix_encode[i]; 26 | } 27 | delete []_matrix_encode; 28 | } 29 | //H 30 | if(_H) 31 | { 32 | for(int i = 0;i < _matrix_encode_row;i++) 33 | { 34 | delete []_H[i]; 35 | } 36 | delete []_H; 37 | } 38 | //robust distribution 39 | if(_matrix_dgree_distribution) 40 | { 41 | delete[] _matrix_dgree_distribution; 42 | } 43 | if(_matrix_H_decode) 44 | { 45 | 46 | } 47 | 48 | } 49 | 50 | void LT_code::LT_XOR(int* arr1,int* arr2,int len,int* result) 51 | { 52 | for(int i = 0;i < len;i++) 53 | { 54 | result[i] = arr1[i] ^ arr2[i]; 55 | } 56 | } 57 | 58 | void LT_code::LT_XOR(char* arr1,char* arr2,int len,char* result) 59 | { 60 | for(int i = 0;i < len;i++) 61 | { 62 | result[i] = arr1[i] ^ arr2[i]; 63 | } 64 | } 65 | 66 | 67 | 68 | /************************************************************************/ 69 | /* fun:get the size of file */ 70 | /* Entrance:nothing 71 | /* Return:the size of file 72 | /************************************************************************/ 73 | int LT_code::get_filesize() 74 | { 75 | struct stat f_stat; 76 | if (stat(_filename, &f_stat) == -1) 77 | { 78 | return -1; 79 | } 80 | return f_stat.st_size; 81 | } 82 | 83 | /************************************************************************/ 84 | /* fun:Read the files to be processed into the matrix */ 85 | /* Entrance:nothing 86 | /* Return:nothing 87 | /************************************************************************/ 88 | void LT_code::ReadFile() 89 | { 90 | ifstream infile; 91 | int filesize = get_filesize(); 92 | infile.open(_filename,ios::in|ios::binary); 93 | if(!infile) 94 | { 95 | cout<<"fail to open the file"< packet_encode_num) 326 | { 327 | if(Gussian_decode(row_max,packet_encode_num,_matrix_encode_col)) 328 | { 329 | WriteFile("../test1.jpg",packet_encode_num); 330 | return 1; 331 | } 332 | } 333 | if(insert_index == row_max) 334 | { 335 | insert_index = 0; 336 | } 337 | 338 | return 0; 339 | 340 | } 341 | 342 | /************************************************************************/ 343 | /* fun:Gauss elimination function */ 344 | /* Entrance:row_max:the max row num of matrix H and matrix message 345 | col_H_max:the max col num of matrix H 346 | col_message_max:the max col num of matrix message 347 | /* Return:1 represent decode success,0 represent fail 348 | /************************************************************************/ 349 | int LT_code::Gussian_decode(int row_max,int col_H_max,int col_message_max) 350 | { 351 | for(int col_index = 0;col_index < col_H_max;col_index++) 352 | { 353 | //If the diagonal element is not 1,then find found a row where this col is 1, then swap 354 | if(_matrix_H_decode[col_index][col_index] != 1) 355 | { 356 | int row_index = col_index + 1; 357 | for(;row_index < row_max;row_index++) 358 | { 359 | if(_matrix_H_decode[row_index][col_index] == 1) 360 | { 361 | swap(_matrix_H_decode[col_index],_matrix_H_decode[row_index]); 362 | swap(_matrix_message_decode[col_index],_matrix_message_decode[row_index]); 363 | break; 364 | } 365 | } 366 | if(row_index == row_max) 367 | { 368 | return 0; 369 | } 370 | } 371 | for(int row_index = 0;row_index < row_max;row_index++) 372 | { 373 | if(row_index != col_index) 374 | { 375 | if(_matrix_H_decode[row_index][col_index] == 1) 376 | { 377 | LT_XOR(_matrix_H_decode[row_index],_matrix_H_decode[col_index],col_H_max,_matrix_H_decode[row_index]); 378 | LT_XOR(_matrix_message_decode[row_index],_matrix_message_decode[col_index],col_message_max,_matrix_message_decode[row_index]); 379 | } 380 | } 381 | } 382 | 383 | } 384 | int H_rank; 385 | if((H_rank = find_rank(col_H_max)) == col_H_max) 386 | { 387 | return 1; 388 | } 389 | return 0; 390 | } 391 | 392 | 393 | /************************************************************************/ 394 | /* fun:find the rank of matrix */ 395 | /* Entrance:the max num of col and row 396 | /* Return:the rank of matrix 397 | /************************************************************************/ 398 | int LT_code::find_rank(int rank_max) 399 | { 400 | int H_rank = 0; 401 | for(int i = 0; i < rank_max; i++) 402 | { 403 | if(_matrix_H_decode[i][i] == 1) 404 | { 405 | H_rank++; 406 | } 407 | } 408 | return H_rank; 409 | } 410 | 411 | /************************************************************************/ 412 | /* fun:recover the file from decode matrix */ 413 | /* Entrance:nothing 414 | /* Return:nothing 415 | /************************************************************************/ 416 | void LT_code::WriteFile(const char* filename,int line_num) 417 | { 418 | ofstream outfile; 419 | outfile.open(filename,ios::binary); 420 | for(int i = 0;i < line_num;i++) 421 | { 422 | outfile.write(_matrix_message_decode[i],_matrix_encode_col); 423 | } 424 | outfile.close(); 425 | } 426 | -------------------------------------------------------------------------------- /LT_code.h: -------------------------------------------------------------------------------- 1 | #ifndef LT_CODE_H 2 | #define LT_CODE_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | typedef char* (*send_callback_fun)(char*,int); 13 | 14 | class LT_code 15 | { 16 | public: 17 | LT_code(const char* filename,double redundancy,send_callback_fun send): 18 | _filename(filename),_buf(NULL),_redundancy(redundancy),_send_buf(NULL),_send(send), 19 | _matrix_message(NULL),_matrix_message_row(0), 20 | _H(NULL), 21 | _matrix_encode(NULL),_matrix_encode_row(0),_matrix_dgree_distribution(NULL), 22 | _matrix_H_decode(NULL),_matrix_message_decode(NULL){}; 23 | ~LT_code(); 24 | //process fun 25 | void LT_XOR(int* arr1,int* arr2,int len,int* result); 26 | void LT_XOR(char* arr1,char* arr2,int len,char* result); 27 | //sender 28 | //read file record as binary 29 | void ReadFile(); 30 | int get_filesize(); 31 | void encodeAndsend(); 32 | void init_sender(); 33 | int* robust_solition(int packet_num,double redundancy); 34 | //receiver 35 | void init_receiver(int row_max,int col_H_max,int col_message_max); 36 | int receiveAndDecode(int row_max,char* buf,int& current_session_num,int& insert_index); 37 | int Gussian_decode(int row_max,int col_H_max,int col_message_max); 38 | int find_rank(int rank_max); 39 | void WriteFile(const char* filename,int line_num); 40 | protected: 41 | private: 42 | //sender 43 | const char* _filename; 44 | char* _buf; 45 | double _redundancy; 46 | char* _send_buf; 47 | send_callback_fun _send; 48 | //message 49 | char** _matrix_message; 50 | int _matrix_message_row; 51 | //H 52 | int** _H; 53 | //encode 54 | char** _matrix_encode; 55 | int _matrix_encode_row;//matrix_row is the number of packages generated 56 | int* _matrix_dgree_distribution; 57 | //receiver 58 | int** _matrix_H_decode; 59 | char** _matrix_message_decode; 60 | //constraint of sender and receiver 61 | static const int _session_num_len = 4; //the first 4 byte of the package indicate the session num 62 | static const int _packet_encode_num_len = 4; //the second 4 byte of the package indicate the num of of packages involved in mixing 63 | static const int _matrix_encode_col = 1000;// this define that the data of one package is 1KB(Type char data requires 1 bytes) 64 | }; 65 | #endif // LT_CODE_H 66 | -------------------------------------------------------------------------------- /LT_function.c: -------------------------------------------------------------------------------- 1 | //LT function begin 2 | #include "LT_function.h" 3 | void show_fun() 4 | { 5 | printf("OK"); 6 | } 7 | 8 | void LT_XOR_int(int* arr1,int* arr2,int len,int* result) 9 | { 10 | int i; 11 | for(i = 0;i < len;i++) 12 | { 13 | result[i] = arr1[i] ^ arr2[i]; 14 | } 15 | } 16 | 17 | void LT_XOR_char(char* arr1,char* arr2,int len,char* result) 18 | { 19 | int i; 20 | for(i = 0;i < len;i++) 21 | { 22 | result[i] = arr1[i] ^ arr2[i]; 23 | } 24 | } 25 | 26 | void row_swap_int(int** arr1,int** arr2) 27 | { 28 | int* temp; 29 | temp = *arr1; 30 | *arr1 = *arr2; 31 | *arr2 = temp; 32 | } 33 | 34 | void row_swap_char(char** arr1,char** arr2) 35 | { 36 | char* temp; 37 | temp = *arr1; 38 | *arr1 = *arr2; 39 | *arr2 = temp; 40 | } 41 | 42 | /*fun:encode the message data according to the redundancy,then send to destAddr */ 43 | /*entrance:send_data:the message data 44 | len:the length of data 45 | redundancy:coding redundancy 46 | linksocket:link socket 47 | destAddr:the sockaddr_in of destAddr 48 | session_num:Custom session signs, packages have same session_num indicate that they belong to same source package */ 49 | /*return:No practical significance*/ 50 | int encodeAndSend(char* send_data,int len,double redundancy,int linkSocket,struct sockaddr_in *destAddr,int session_num) 51 | { 52 | char** matrix_message = NULL; 53 | int packetNum = (len/blocksize_data_Len) + 1; 54 | int* matrix_degreeDistribution = NULL; 55 | int matrix_encode_row = packetNum * (1 + redundancy); 56 | int packetTotalSend = 1; 57 | 58 | initSender(&matrix_message,send_data,len,packetNum); 59 | 60 | 61 | srand(time(NULL)); 62 | //encode and send 63 | { 64 | //inital send_buf 65 | char* temp_sendBuf = (char*)malloc(blocksize_sessionNum+blocksize_packetNum+blocksize_data_Len+packetNum*sizeof(int)); 66 | int* temp_H = (int*)malloc(packetNum*sizeof(int)); 67 | char* temp_code =(char*)malloc(blocksize_data_Len*sizeof(char)); 68 | int col_index; 69 | int row_index; 70 | int degree_num; 71 | int packet_index; 72 | int length = blocksize_sessionNum+blocksize_packetNum+blocksize_data_Len+packetNum*sizeof(int); 73 | int i; 74 | int byteswritten; 75 | matrix_degreeDistribution = robustSolition(packetNum,redundancy); 76 | for(row_index = 0;row_index < matrix_encode_row;row_index++) 77 | { 78 | memset(temp_sendBuf,0,length); 79 | memset(temp_H,0,packetNum*sizeof(int)); 80 | memset(temp_code,0,blocksize_data_Len); 81 | degree_num = matrix_degreeDistribution[row_index]; 82 | for(col_index = 0;col_index < degree_num;col_index++) 83 | { 84 | packet_index = rand()%packetNum; 85 | if (temp_H[packet_index] == 0) 86 | { 87 | temp_H[packet_index] = 1; 88 | LT_XOR_char(temp_code,matrix_message[packet_index],blocksize_data_Len,temp_code); 89 | } 90 | else 91 | { 92 | col_index--; 93 | } 94 | } 95 | memcpy(temp_sendBuf,&session_num,blocksize_sessionNum); 96 | memcpy(temp_sendBuf+blocksize_sessionNum,&packetNum,blocksize_packetNum); 97 | memcpy(temp_sendBuf+blocksize_sessionNum+blocksize_packetNum,temp_code,blocksize_data_Len); 98 | memcpy(temp_sendBuf+blocksize_sessionNum+blocksize_packetNum+blocksize_data_Len,temp_H,packetNum*sizeof(int)); 99 | 100 | 101 | byteswritten = isendto(linkSocket, temp_sendBuf, length, 0, 102 | (struct sockaddr *) destAddr, 103 | sizeof(struct sockaddr)); 104 | if (byteswritten < 0) 105 | { 106 | if (errno == EINTR) 107 | { 108 | continue; 109 | } 110 | 111 | } 112 | packetTotalSend++; 113 | } 114 | free(temp_sendBuf); 115 | temp_sendBuf = NULL; 116 | free(temp_H); 117 | temp_H = NULL; 118 | free(temp_code); 119 | temp_code = NULL; 120 | if(matrix_degreeDistribution) 121 | { 122 | free(matrix_degreeDistribution); 123 | matrix_degreeDistribution = NULL; 124 | } 125 | for (i = 0;i < packetNum;i++) 126 | { 127 | free(matrix_message[i]); 128 | matrix_message[i] = NULL; 129 | } 130 | free(matrix_message); 131 | matrix_message = NULL; 132 | } 133 | 134 | return 0; 135 | } 136 | 137 | void initSender(char*** matrix_message,char* send_data,int send_data_len,int packetNum) 138 | { 139 | int i; 140 | *matrix_message = (char**)malloc(packetNum*sizeof(char*)); 141 | for (i = 0;i < packetNum;i++) 142 | { 143 | (*matrix_message)[i] = (char*)malloc(blocksize_data_Len*sizeof(char)); 144 | memset((*matrix_message)[i],0,blocksize_data_Len*sizeof(char)); 145 | if (i != packetNum-1) 146 | { 147 | memcpy((*matrix_message)[i],send_data+i*blocksize_data_Len,blocksize_data_Len); 148 | } 149 | else 150 | { 151 | memcpy((*matrix_message)[i],send_data+i*blocksize_data_Len,send_data_len-i*blocksize_data_Len); 152 | } 153 | } 154 | } 155 | 156 | /*fun:gennerate distribution matrix according to robust soliton,return the distribution matrix In the case of REDUNDANCY of the parameter redundancy*/ 157 | int* robustSolition(int packet_num,double redundancy) 158 | { 159 | int i; 160 | double c = 0.05; 161 | double delta = 0.05; //The probability of successful decoding is guaranteed as 1- delta 162 | double R = c * log((double)packet_num/delta) * (double)sqrt((double)packet_num); 163 | int degree_max = (int)(packet_num/R); 164 | double* p_robust; 165 | int p_robust_len; 166 | p_robust_len = (degree_max>packet_num)?degree_max:packet_num; 167 | p_robust = (double*)malloc(p_robust_len*sizeof(double)); 168 | memset(p_robust,0,p_robust_len*sizeof(double)); 169 | p_robust[0] = (double)1/packet_num; 170 | //ideal solition 171 | for(i = 1;i < packet_num;i++) 172 | { 173 | p_robust[i] = (double)1/(i*(i+1)); 174 | } 175 | //robust solition 176 | for(i = 0;i < degree_max;i++) 177 | { 178 | double p = R/((i+1)*packet_num); 179 | p_robust[i] = p_robust[i] + p; 180 | } 181 | //normalization 182 | { 183 | double sum = 0; 184 | double temp_min = 1; 185 | int min_index = 0; 186 | int que_num; 187 | int* distribution; 188 | int random_matrix_index; 189 | int j; 190 | int matrix_distribution_len = packet_num * (1 + redundancy); 191 | int* matrix_distribution = (int*)malloc(sizeof(int) * matrix_distribution_len); 192 | for(i = 0;i < p_robust_len;i++) 193 | { 194 | sum += p_robust[i]; 195 | } 196 | 197 | for(i = 0;i < p_robust_len;i++) 198 | { 199 | p_robust[i] = p_robust[i] / sum; 200 | if(p_robust[i] <(0.1/packet_num)) 201 | { 202 | p_robust[i] = 0; 203 | } 204 | else if(p_robust[i] < temp_min) 205 | { 206 | temp_min = p_robust[i]; 207 | min_index = i; 208 | } 209 | } 210 | que_num = 1 / temp_min; 211 | distribution = (int*)malloc(que_num*sizeof(int)); 212 | memset(distribution,0,que_num*sizeof(int)); 213 | 214 | random_matrix_index = 0; 215 | j = 0; 216 | for(i = 0; i < min_index;i++) 217 | { 218 | int temp_num = p_robust[i] * que_num; 219 | random_matrix_index += temp_num; 220 | //printf("i is %d",i); 221 | for(;j < random_matrix_index;j++) 222 | { 223 | //printf("j is %d",j); 224 | distribution[j] = i+1; 225 | } 226 | } 227 | 228 | //create robust solition matrix 229 | srand(time(NULL)); 230 | for(i = 0;i < matrix_distribution_len;i++) 231 | { 232 | int rand_num = rand()%random_matrix_index; 233 | matrix_distribution[i] = distribution[rand_num]; 234 | } 235 | 236 | free(distribution); 237 | distribution = NULL; 238 | free(p_robust); 239 | p_robust = NULL; 240 | return matrix_distribution; 241 | } 242 | } 243 | 244 | 245 | /*fun:decode the data received */ 246 | /*entrance:row_max:the max number of row, define in platform.h 247 | buf:the data received 248 | current_session_num:current session num that is decoding 249 | insert_index:The position of data that should be inserted in the decoding matrix 250 | matrix_message_decode:decoding matrix of message 251 | matrix_H_decode:decoding matrix of H 252 | success_recover_que:when a source packet has been successed recover,it will be record into the success_recover_que 253 | que_insert_pos: The position that the success recover packet should be record */ 254 | /*return:0 means decoding faild then wait next block 255 | any value >0 means decoding success(packet_encode_num means the number of source packets is divided into small pieces) */ 256 | int receiveAndDecode(int row_max,char* buf,int* current_session_num,int* insert_index,char*** matrix_message_decode,int*** matrix_H_decode,int* success_recover_que,int* que_insert_pos) 257 | { 258 | int session_num; 259 | int packet_encode_num; 260 | int i; 261 | memcpy(&session_num,buf,blocksize_sessionNum); 262 | memcpy(&packet_encode_num,buf+blocksize_sessionNum,blocksize_packetNum); 263 | //if there is no session,then set up the session,or the session num of the packet is not current session,then throw this package 264 | for (i = 0;i < MAX_SUCCESS_QUENUM;i++) 265 | { 266 | if (session_num == success_recover_que[i]) 267 | { 268 | return -1; 269 | } 270 | } 271 | if(*current_session_num == 0) 272 | { 273 | *current_session_num = session_num; 274 | *insert_index = 0; 275 | init_receiver(row_max,packet_encode_num,blocksize_data_Len,matrix_message_decode,matrix_H_decode); 276 | } 277 | else if(session_num != *current_session_num) 278 | { 279 | return -1; 280 | } 281 | 282 | memcpy((*matrix_message_decode)[*insert_index],buf+blocksize_sessionNum+blocksize_packetNum,blocksize_data_Len); 283 | memcpy((*matrix_H_decode)[*insert_index],buf+blocksize_sessionNum+blocksize_packetNum+blocksize_data_Len,packet_encode_num*sizeof(int)); 284 | *insert_index = *insert_index + 1; 285 | if(*insert_index > packet_encode_num) 286 | { 287 | if(*insert_index == 24) 288 | { 289 | printf("receive 24"); 290 | } 291 | if(Gussian_decode(row_max,packet_encode_num,blocksize_data_Len,*matrix_message_decode,*matrix_H_decode)) 292 | { 293 | success_recover_que[*que_insert_pos] = *current_session_num; 294 | *que_insert_pos = *que_insert_pos + 1; 295 | if (*que_insert_pos == MAX_SUCCESS_QUENUM) 296 | { 297 | *que_insert_pos = 0; 298 | } 299 | *current_session_num = 0; 300 | *insert_index = 0; 301 | return packet_encode_num; 302 | } 303 | } 304 | if(*insert_index == row_max) 305 | { 306 | *insert_index = 0; 307 | } 308 | return 0; 309 | } 310 | 311 | void init_receiver(int row_max,int col_H_max,int col_message_max,char*** matrix_message_decode,int*** matrix_H_decode) 312 | { 313 | int i; 314 | if(!(*matrix_H_decode)) 315 | { 316 | *matrix_H_decode = (int**)malloc(sizeof(int*)*row_max); 317 | } 318 | for(i = 0;i < row_max;i++) 319 | { 320 | (*matrix_H_decode)[i] =(int*)malloc(sizeof(int)*col_H_max); 321 | memset((*matrix_H_decode)[i],0,sizeof(int)*col_H_max); 322 | } 323 | *matrix_message_decode = (char**)malloc(sizeof(char*)*row_max); 324 | for(i = 0;i < row_max;i++) 325 | { 326 | (*matrix_message_decode)[i] = (char*)malloc(sizeof(char)*col_message_max); 327 | memset((*matrix_message_decode)[i],0,sizeof(char)*col_message_max); 328 | } 329 | } 330 | 331 | void free_receiver(int row_max,char*** matrix_message_decode,int*** matrix_H_decode) 332 | { 333 | int i; 334 | for(i = 0;i < row_max;i++) 335 | { 336 | free((*matrix_H_decode)[i]) ; 337 | (*matrix_H_decode)[i] = NULL; 338 | } 339 | free((*matrix_H_decode)); 340 | (*matrix_H_decode) = NULL; 341 | for(i = 0;i < row_max;i++) 342 | { 343 | free((*matrix_message_decode)[i]); 344 | (*matrix_message_decode)[i] = NULL; 345 | } 346 | free((*matrix_message_decode)); 347 | *matrix_message_decode = NULL; 348 | } 349 | 350 | int Gussian_decode(int row_max,int col_H_max,int col_message_max,char** matrix_message_decode,int** matrix_H_decode) 351 | { 352 | int col_index; 353 | int row_index; 354 | int H_rank; 355 | for(col_index = 0;col_index < col_H_max;col_index++) 356 | { 357 | //If the diagonal element is not 1,then find found a row where this col is 1, then swap 358 | if(matrix_H_decode[col_index][col_index] != 1) 359 | { 360 | row_index = col_index + 1; 361 | for(;row_index < row_max;row_index++) 362 | { 363 | if(matrix_H_decode[row_index][col_index] == 1) 364 | { 365 | row_swap_int(&matrix_H_decode[col_index],&matrix_H_decode[row_index]); 366 | row_swap_char(&matrix_message_decode[col_index],&matrix_message_decode[row_index]); 367 | break; 368 | } 369 | } 370 | if(row_index == row_max) 371 | { 372 | return 0; 373 | } 374 | } 375 | for(row_index = 0;row_index < row_max;row_index++) 376 | { 377 | if(row_index != col_index) 378 | { 379 | if(matrix_H_decode[row_index][col_index] == 1) 380 | { 381 | LT_XOR_int(matrix_H_decode[row_index],matrix_H_decode[col_index],col_H_max,matrix_H_decode[row_index]); 382 | LT_XOR_char(matrix_message_decode[row_index],matrix_message_decode[col_index],col_message_max,matrix_message_decode[row_index]); 383 | } 384 | } 385 | } 386 | 387 | } 388 | if((H_rank = find_rank(col_H_max,matrix_H_decode)) == col_H_max) 389 | { 390 | return 1; 391 | } 392 | return 0; 393 | } 394 | 395 | 396 | int find_rank(int rank_max,int** matrix_H_decode) 397 | { 398 | int H_rank = 0; 399 | int i; 400 | for(i = 0; i < rank_max; i++) 401 | { 402 | if(matrix_H_decode[i][i] == 1) 403 | { 404 | H_rank++; 405 | } 406 | } 407 | return H_rank; 408 | } 409 | 410 | //LT function end -------------------------------------------------------------------------------- /LT_function.h: -------------------------------------------------------------------------------- 1 | #ifndef _LT_FUNCTION_H 2 | #define _LT_FUNCTION_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | //sender 10 | #define blocksize_sessionNum 4 //This section in the block represents the session number,the size of session number in a block is 4byte 11 | #define blocksize_packetNum 4 //This section in the block represents the number of packages involved in the mixture, with a total of 4 bytes 12 | #define blocksize_data_Len 1000 //This section in the block represents the data, with total of 1000byte 13 | 14 | #define REDUNDANCY 0.2 15 | 16 | #define MAX_RETRANSMISSION_TIMES 10 17 | 18 | //receiver 19 | #define MAX_ROWNUM 200 20 | #define MAX_SUCCESS_QUENUM 200 21 | 22 | //sender 23 | /*typedef struct send_buf 24 | { 25 | int sessionNum; 26 | int packetNum; 27 | char data[1000]; 28 | int H; 29 | }send_buf; 30 | */ 31 | 32 | int encodeAndSend(char* send_data,int len,double redundancy,int linkSocket,struct sockaddr_in *destAddr,int session_num); 33 | 34 | void initSender(char*** matrix_message,char* send_data,int send_data_len,int packetNum); 35 | 36 | int* robustSolition(int packet_num,double redundancy); 37 | 38 | 39 | 40 | //receiver 41 | int receiveAndDecode(int row_max,char* buf,int* current_session_num,int* insert_index,char*** matrix_message_decode,int*** matrix_H_decode,int* success_recover_que,int* que_insert_pos); 42 | 43 | void init_receiver(int row_max,int col_H_max,int col_message_max,char*** matrix_message_decode,int*** matrix_H_decode); 44 | 45 | void free_receiver(int row_max,char*** matrix_message_decode,int*** matrix_H_decode); 46 | 47 | int Gussian_decode(int row_max,int col_H_max,int col_message_max,char** matrix_message_decode,int** matrix_H_decode); 48 | 49 | int find_rank(int rank_max,int** matrix_H_decode); 50 | 51 | //common 52 | void LT_XOR_int(int* arr1,int* arr2,int len,int* result); 53 | 54 | void show_fun(); 55 | 56 | void LT_XOR_char(char* arr1,char* arr2,int len,char* result); 57 | 58 | void row_swap_int(int** arr1,int** arr2); 59 | 60 | void row_swap_char(char** arr1,char** arr2); 61 | 62 | 63 | //end LT function 64 | 65 | 66 | #endif//_LT_FUNCTION_H -------------------------------------------------------------------------------- /LT_matlab/BP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Neo-ZK/LT_code/d416d90a1acebc9f33697b483b27dc06e66b983c/LT_matlab/BP.m -------------------------------------------------------------------------------- /LT_matlab/Gussian.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Neo-ZK/LT_code/d416d90a1acebc9f33697b483b27dc06e66b983c/LT_matlab/Gussian.m -------------------------------------------------------------------------------- /LT_matlab/LT_decode_BP.m: -------------------------------------------------------------------------------- 1 | function [H_decode_after,code_decode_after,tag_decode] = LT_decode_BP(H_receive,code_receive,H_decode_before,code_decode_before) 2 | tag_decode = 0; 3 | H_decode_after = [H_decode_before; 4 | H_receive]; 5 | code_decode_after = [code_decode_before; 6 | code_receive]; 7 | 8 | if size(find(H_receive == 1),2) == 1 9 | [H_decode_after,code_decode_after] = BP(H_decode_after,code_decode_after,size(H_decode_after,1)); 10 | rank_H = find_rank(H_decode_after); 11 | if rank_H == size(H_decode_after,2) 12 | tag_decode = 1; 13 | end 14 | end 15 | 16 | end 17 | 18 | -------------------------------------------------------------------------------- /LT_matlab/LT_decode_Guassian.m: -------------------------------------------------------------------------------- 1 | function [ H_decode_after,code_decode_after,tag_decode,rank_statistic ] = LT_decode_Guassian( H_receive,code_receive,H_decode_before,code_decode_before,rank_statistic) 2 | tag_decode = 0; 3 | H_decode_after = [H_decode_before; 4 | H_receive]; 5 | code_decode_after = [code_decode_before; 6 | code_receive]; 7 | 8 | [H_decode_after,code_decode_after] = Gussian(H_decode_after,code_decode_after); 9 | rank_H = find_rank(H_decode_after); 10 | rank_statistic = [rank_statistic rank_H]; 11 | if rank_H == size(H_decode_after,2) 12 | tag_decode = 1; 13 | end 14 | 15 | 16 | end 17 | 18 | -------------------------------------------------------------------------------- /LT_matlab/LT_encode.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Neo-ZK/LT_code/d416d90a1acebc9f33697b483b27dc06e66b983c/LT_matlab/LT_encode.m -------------------------------------------------------------------------------- /LT_matlab/LT_full_link.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Neo-ZK/LT_code/d416d90a1acebc9f33697b483b27dc06e66b983c/LT_matlab/LT_full_link.m -------------------------------------------------------------------------------- /LT_matlab/LT_link_simulate.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Neo-ZK/LT_code/d416d90a1acebc9f33697b483b27dc06e66b983c/LT_matlab/LT_link_simulate.m -------------------------------------------------------------------------------- /LT_matlab/compute_packet_loss.m: -------------------------------------------------------------------------------- 1 | function [ packet_loss ] = compute_packet_loss( ser,length ) 2 | packet_loss =1 - (1-ser)^length; 3 | end 4 | 5 | -------------------------------------------------------------------------------- /LT_matlab/find_rank.m: -------------------------------------------------------------------------------- 1 | function [ rank_value ] = find_rank( H ) 2 | rank_value = 0; 3 | for i = 1:min(size(H)) 4 | if H(i,i) == 1 5 | rank_value = rank_value + 1; 6 | end 7 | end 8 | end 9 | 10 | -------------------------------------------------------------------------------- /LT_matlab/robust_solition.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Neo-ZK/LT_code/d416d90a1acebc9f33697b483b27dc06e66b983c/LT_matlab/robust_solition.m -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "LT_function.h" 5 | 6 | 7 | char* send(char* arr,int len) 8 | { 9 | //char buf[000]; 10 | //memcpy(buf,arr,len); 11 | return arr; 12 | } 13 | 14 | int main() 15 | { 16 | int i; 17 | //LT_code* m_LT_code = new LT_code("../lena.jpg",0.5,&send); 18 | //m_LT_code->encodeAndsend(); 19 | char* a = (char*)malloc(2); 20 | char* b = (char*)malloc(2); 21 | for (i = 0;i < 2;i++) 22 | { 23 | a[i] = 'a'; 24 | b[i] = 'b'; 25 | } 26 | LT_XOR_char(a,b,2,a); 27 | LT_XOR_char(a,b,2,a); 28 | return 0; 29 | } 30 | --------------------------------------------------------------------------------