Esempio n. 1
0
static void producer_operation(int sfd)
{
	while(status_on)
	{	
		int peer_sfd = accept(sfd,NULL,NULL);                           
		if(peer_sfd<0) 
		{
			print_log(WARNING,"\nACCEPT FAILED MAY BE BECAUSE OF INTERRUPT");
			continue;
		}
		if(pthread_mutex_lock(&buffer_lock_mtx)!=0)
			print_log(WARNING,"\nthere is error in lock prod");
		while(is_buffer_full()) 
		{
			if(pthread_cond_wait(&buffer_not_full_cond,&buffer_lock_mtx)!=0)
				print_log(WARNING,"\nthere is error in wait prod");
		}

		buffer_add(peer_sfd);				

		if(pthread_cond_broadcast(&buffer_not_empty_cond)!=0)
			print_log(WARNING,"\nthere is error in broadcast");
		if(pthread_mutex_unlock(&buffer_lock_mtx)!=0)
			print_log(WARNING,"\nthere is erorr in unlock");
	}
	destroy_buffer();
}
Esempio n. 2
0
static int write_data(char *in, buffer_stream_t *bs, int size)
{
	if(bs->bInited==0)
		return -1;//not inited
	int ret=is_buffer_full(bs);
	if(ret==1)
	{

		printf("=====buffer full \n");
		return 0;//buffer full
	}
	//start write data
	int len= MIN(bs->buf_length-bs->buf_level,size);
	if(bs->wr_ptr<bs->rd_ptr)
	{
		memcpy(bs->wr_ptr,in,len);
		bs->wr_ptr+=len;
		bs->buf_level+=len;
		if(bs->wr_ptr==(bs->data+bs->buf_length))
		     bs->wr_ptr=bs->data;
		//printf("=====write ok: condition 1 write :%d byte \n",len);
		return len;
	}
	else if(len<(bs->data+bs->buf_length-bs->wr_ptr))
	{
		memcpy(bs->wr_ptr,in,len);
		bs->wr_ptr+=len;
		bs->buf_level+=len;
		if(bs->wr_ptr==(bs->data+bs->buf_length))
		    bs->wr_ptr=bs->data;
		//printf("=====write ok: condition 2 write :%d byte \n",len);
		return len;

	}
	else
	{
		int tail_len=(bs->data+bs->buf_length-bs->wr_ptr);
		memcpy(bs->wr_ptr,in,tail_len);
		memcpy(bs->data,in+tail_len,len-tail_len);
		bs->wr_ptr=bs->data+len-tail_len;
		bs->buf_level+=len;
		if(bs->wr_ptr==(bs->data+bs->buf_length))
		    bs->wr_ptr=bs->data;
		//printf("=====write ok: condition 3 write :%d byte \n",len);
		return len;
	}

}
Esempio n. 3
0
data_packet_list_t *re_generateWhohas(bt_config_t *config){
	int i;
	data_packet_list_t *ret = NULL;	
	if( file_manager.init == 0){
		return NULL;
	}

	for(i = 0;i < file_manager.chunk_count; i ++){
		int offset = file_manager.offset[i];
		if( is_buffer_full(offset) == 1){		
		  continue;
		}
		/* check if chunk timeout */
		time_t current_time = time(NULL);
		double diff_t = difftime(current_time, file_manager.timer[i]);

		if( diff_t > 10){
			// set timeout to 10 seconds
			/* re-generate whohas for this chunk */

			//first set the current recv buffer(if exists) to invalid
		//	printf("RE-generate: offset %d time out\n", file_manager.offset[i]);
			printf("start = %lld , now = %lld diff = %f\n", (long long int)file_manager.timer[i], (long long int)current_time, diff_t);

			release_buffer(offset);

			// get hash value for this offset
			char *hash = get_hash_from_master_chunkfile(offset,config);
			data_packet_t *packet = init_packet(0,  hash, 20);
			data_packet_list_t* new_element = (data_packet_list_t*)malloc( sizeof(data_packet_list_t));
			new_element->packet = packet;
			if( ret == NULL ){
				new_element->next = NULL;
				ret = new_element;
			}
			else{
				new_element->next = ret;
				ret = new_element;
			}

			file_manager.timer[i] = time(NULL);

		} 
	}
	return ret;
}
Esempio n. 4
0
int check_file_manager(bt_config_t* config){
	if( file_manager.init == 0){
		return -2;
	}

	printf("DEBUG: GO into file_manager, chunkcount = %d", file_manager.chunk_count);
	int i;

	/*for test*/

	int full_count = 0;
	for(i = 0;i < file_manager.chunk_count; i ++){

		if( -1 == is_buffer_full(file_manager.offset[i])){
			printf("DEBUG : chunk %d NOTFULL\n", file_manager.offset[i]);
		}
		else{
			full_count ++;
			printf("DEBUG: chunk %d is FULL\n", file_manager.offset[i]);
		}
	}
	printf("DEBUG : full chunk = %d\n", full_count);


	for(i = 0;i < file_manager.chunk_count; i ++){
		printf("Offset[%d] = %d\n", i, file_manager.offset[i]);
		int buffer_full = is_buffer_full(file_manager.offset[i]);
		if( buffer_full == 1){
			/* release it from the recv list, free the node it connect to and may prepare to grab a waiting chunk */
			release_buffer(file_manager.offset[i]);
		}
		if( -1 == buffer_full ){
			printf("In check_file_manager, file is not full, first unfull offset = %d\n",file_manager.offset[i]);
			return -1;
		}
	}

	/* if get here means the file is full*/
	// here write back and return 1
	//printf("DEBUG: File is full, writing back to disk location = %s\n", output_location);
	printf("strleng = %s\n" ,config->output_file);
	FILE *fp = fopen(config->output_file, "wb+");
	int count = 0;
	for(i = 0;i < file_manager.chunk_count; i ++){
		printf("Writing chunk offset = %d\n", file_manager.offset[i]);
		int j;
		char temp[1500];
		memset(temp, 0 , 1500);
		for(j = 0;j < 512; j ++){
			int length = copy_chunk_data(temp, file_manager.offset[i], j);
			fwrite(temp, 1, length,fp);
			count += length;

		}

		/*
		for(j = 0;j < 512 ; j ++){
			copy_chunk_data(temp, file_manager.offset[i], j);
			fwrite(temp,1, 1024,fp);
			memset(temp, 0 , 1024);
		}*/
		
	}
	printf("GET FILE length = %d\n",count);

	/* close the file_manager */
	file_manager.init = 0;
	free(file_manager.timer);
	free(file_manager.output_location);
	free(file_manager.offset);
	free(file_manager.already_used);

	fclose(fp);

	/*TODO : delete after finish */
	
	return 1;
}