Exemple #1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(main_process, ev, data)
{
    PROCESS_BEGIN();
    INIT_NETWORK_DEBUG();
    {
        // Due to the way Contiki protothreads work this needs to be static,
        // otherwise the data will be lost when switching to a different thread
        static struct uip_udp_conn * conn;
        static char buffer[BUFFER_SIZE];
        static int i = 0;
        PRINTF("===START===\n");

        conn = udp_new_connection(CLIENT_PORT, SERVER_PORT, SERVER_IP_ADDR);

        while(1)
        {
            i++;
            sprintf(buffer, "Hello number %i from client", i);

            PRINTF("Sending data: %s\n", buffer);

            udp_packet_send(conn, buffer, strlen(buffer));
            PROCESS_WAIT_UDP_SENT();
        }

    }

    PROCESS_END();
}
Exemple #2
0
/*
 * Decode UDP packet according to the given instructions!
 */
void udp_packet_decode(char * packet, char * fromIP){
	struct dir_files_status_list *currTmp, *watchedTmp, *result;
	char pak[MAXBUF];
	char fileSHA[SHA1_BYTES_LEN];
	off_t file_len;
	int64_t clk, mod_time;
	char FromClient[255], file_name[255];
	uint16_t tcp_port;
	int count =3 , i=0;
	char tmp[2], *file_full_path;
	
	
	/* First Decode each field and then print */
	memcpy(pak, packet,MAXBUF);
	tmp[0] = pak[0];
	tmp[1] = pak[1];
	
	if(pak[2] != 0)
		perror("Not a Valid Message Field Client_name\n");
	while(pak[count] != 0){
		FromClient[i++] = pak[count++];
	}FromClient[i] = '\0';
	count++;
	
	memcpy(&tcp_port, &pak[count], 2);
	count+=2;
	
	memcpy(&clk, &pak[count],8);
	count+=8;
	
	if( (tmp[0] >= 3) && (tmp[0] <= 7)){
		memcpy(&mod_time, &pak[count], 8);
		count += 9;
		i=0;
		while(pak[count] != 0){
			file_name[i++] = pak[count++];
		}file_name[i] = '\0';
		count++;
		memcpy(fileSHA, &pak[count], SHA1_BYTES_LEN);
		count+=SHA1_BYTES_LEN;
		memcpy(&file_len, &pak[count], 8);
		count+=8;
		
	}
	/* Get the Lock and start printing!*/
	
	pthread_mutex_lock(&print_mutex);
	switch(tmp[0]){
		case(1):
			printf("\n\tSTATUS_MSG \n");
			break;
		case(2):
			printf("\n\tNO_CHANGES_MSG \n");
			break;
		case(3):
			printf("\n\tNEW_FILE_MSG \n");
			pthread_mutex_lock(&file_list_mutex);
			watchedTmp = watched_files;
			currTmp = (struct dir_files_status_list * ) malloc( sizeof (struct dir_files_status_list));
			if (!currTmp) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			currTmp->filename = (char *) malloc(strlen(file_name));
			if (!currTmp->filename) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			strcpy(currTmp->filename, file_name);
			
			SGLIB_LIST_FIND_MEMBER(struct dir_files_status_list, watchedTmp, currTmp, ILIST_COMPARATOR, next, result);
			/* Case 1: the client does not have the file */
			if(result == NULL){
			
				/* Add file to the list and wait until its received to compare the SHA */
				currTmp->size_in_bytes = file_len;
				currTmp->modifictation_time_from_epoch = mod_time;
				/* deep copy */
				for(i = 0; i < SHA1_BYTES_LEN; i++)
					currTmp->sha1sum[i] = fileSHA[i];
				
				
				SGLIB_SORTED_LIST_ADD(struct dir_files_status_list, watched_files, currTmp, ILIST_COMPARATOR, next);
				
				
				/* Ask for Transfer! */ 
				i = udp_file_packet_encode(FILE_TRANSFER_REQUEST,client_name,TCP_PORT,&clk,&mod_time, file_name,fileSHA,file_len);
				currTmp->processed = TRUE;
				udp_packet_send(i);
				currTmp->processed = FALSE;
				
			}
			/* Case 2: the client DOES have the file listed */
			else{
				
				if(result->modifictation_time_from_epoch < mod_time){
					/* Ask for Transfer! */ 
					i = udp_file_packet_encode(FILE_TRANSFER_REQUEST,client_name,TCP_PORT,&clk,&mod_time, file_name,fileSHA,file_len);
					currTmp->processed = TRUE;
					udp_packet_send(i);
					currTmp->processed = FALSE;
				}
				free(currTmp);
			}
			pthread_mutex_unlock(&file_list_mutex);
			break;
		case(4):
			printf("\n\tFILE_CHANGED_MSG \n");
			pthread_mutex_lock(&file_list_mutex);
			watchedTmp = watched_files;
			currTmp = (struct dir_files_status_list * ) malloc( sizeof (struct dir_files_status_list));
			if (!currTmp) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			currTmp->filename = (char *) malloc(strlen(file_name));
			if (!currTmp->filename) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			strcpy(currTmp->filename, file_name);
			SGLIB_LIST_FIND_MEMBER(struct dir_files_status_list, watchedTmp, currTmp, ILIST_COMPARATOR, next, result);
			/* Case 1: the client does not have the file, so add and transfer */
			if(result == NULL){
			
				/* Add file to the list and wait until its received to compare the SHA */
				currTmp->size_in_bytes = file_len;
				currTmp->modifictation_time_from_epoch = mod_time;
				/* deep copy */
				for(i = 0; i < SHA1_BYTES_LEN; i++)
					currTmp->sha1sum[i] = fileSHA[i];
				
				
				SGLIB_SORTED_LIST_ADD(struct dir_files_status_list, watched_files, currTmp, ILIST_COMPARATOR, next);
				
				
				/* Ask for Transfer! */ 
				i = udp_file_packet_encode(FILE_TRANSFER_REQUEST,client_name,TCP_PORT,&clk,&mod_time, file_name,fileSHA,file_len);
				/* file does not exist so its ok to set processed without checking */
				currTmp->processed = TRUE;
				udp_packet_send(i);
				currTmp->processed = FALSE;
				
			}
			/* Case 2: the client DOES have the file listed so update it!*/
			else{
				/*Check timestamp */
				if(result->modifictation_time_from_epoch < mod_time){
					
					/* Add file to the list and wait until its received to compare the SHA */
					result->size_in_bytes = file_len;
					result->modifictation_time_from_epoch = mod_time;
					/* deep copy */
					for(i = 0; i < SHA1_BYTES_LEN; i++)
						result->sha1sum[i] = fileSHA[i];
					
					/* Ask for Transfer! */ 
					i = udp_file_packet_encode(FILE_TRANSFER_REQUEST,client_name,TCP_PORT,&clk,&mod_time, file_name,fileSHA,file_len);
					if (result->processed == FALSE){
						result->processed = TRUE;
						udp_packet_send(i);
						result->processed = FALSE;
					}
				}
				free(currTmp);
			}
			pthread_mutex_unlock(&file_list_mutex);
			break;
		case(5):
			printf("\n\tFILE_DELETED_MSG \n");
			pthread_mutex_lock(&file_list_mutex);
			watchedTmp = watched_files;
			currTmp = (struct dir_files_status_list * ) malloc( sizeof (struct dir_files_status_list));
			if (!currTmp) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			currTmp->filename = (char *) malloc(strlen(file_name));
			if (!currTmp->filename) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			strcpy(currTmp->filename, file_name);
			SGLIB_LIST_FIND_MEMBER(struct dir_files_status_list, watchedTmp, currTmp, ILIST_COMPARATOR, next, result);
			/* Case 1: the client does have the file */
			if(result != NULL){
			
				/* Check file similarity! */
				if((file_len == result->size_in_bytes) && (compare_sha1(result->sha1sum,fileSHA) == 0))
				{
					/* remove from dir */
					file_full_path = (char * )malloc(strlen(file_name) + strlen(watched_dir)+1);
					strcpy(file_full_path,watched_dir);
					strcat(file_full_path,file_name);
					
					/*now remove from list*/
					
					if (result->processed == FALSE){
						result->processed = TRUE;
						if(remove(file_full_path) != 0 ){
							fprintf(stderr,"[Cloudbox] Error deleting file %s\n",file_full_path);
							exit(EXIT_FAILURE);
						}
						SGLIB_LIST_DELETE(struct dir_files_status_list, watchedTmp, result, next);
						result->processed = FALSE;
					}
					
					
					free(file_full_path);
					
				}
				
			}
			
			/* Case 2: the client DOES NOT have the file Deleted */
			else{
				free(currTmp);
			}
			pthread_mutex_unlock(&file_list_mutex);
			break;
		case(6):
			printf("\n\tFILE_TRANSFER_REQUEST \n");
			pthread_mutex_lock(&file_list_mutex);
			watchedTmp = watched_files;
			currTmp = (struct dir_files_status_list * ) malloc( sizeof (struct dir_files_status_list));
			if (!currTmp) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			currTmp->filename = (char *) malloc(strlen(file_name));
			if (!currTmp->filename) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			strcpy(currTmp->filename, file_name);
			SGLIB_LIST_FIND_MEMBER(struct dir_files_status_list, watchedTmp, currTmp, ILIST_COMPARATOR, next, result);
			if((result != NULL) && (compare_sha1(result->sha1sum,fileSHA) == 0) ){
				/* its my file the other client is looking for! 
				 */
				if (result->processed == FALSE){
					result->processed = TRUE;
					send_file(fromIP, tcp_port, file_name);
					result->processed = FALSE;
				}
			}
			pthread_mutex_unlock(&file_list_mutex);
			free(currTmp->filename);
			free(currTmp);
			break;
		case(7):
			printf("\n\tFILE_TRANSFER_OFFER \n");
			pthread_mutex_lock(&file_list_mutex);
			watchedTmp = watched_files;
			currTmp = (struct dir_files_status_list * ) malloc( sizeof (struct dir_files_status_list));
			if (!currTmp) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			currTmp->filename = (char *) malloc(strlen(file_name));
			if (!currTmp->filename) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			strcpy(currTmp->filename, file_name);
			
			SGLIB_LIST_FIND_MEMBER(struct dir_files_status_list, watchedTmp, currTmp, ILIST_COMPARATOR, next, result);
			/* Case 1: the client does not have the file */
			if(result == NULL){
			
				/* Add file to the list and wait until its received to compare the SHA */
				currTmp->size_in_bytes = file_len;
				currTmp->modifictation_time_from_epoch = mod_time;
				/* deep copy */
				for(i = 0; i < SHA1_BYTES_LEN; i++)
					currTmp->sha1sum[i] = fileSHA[i];
				
				
				SGLIB_SORTED_LIST_ADD(struct dir_files_status_list, watched_files, currTmp, ILIST_COMPARATOR, next);
				
				/* Ask for Transfer! */ 
				i = udp_file_packet_encode(FILE_TRANSFER_REQUEST,client_name,TCP_PORT,&clk,&mod_time, file_name,fileSHA,file_len);
				currTmp->processed = TRUE;
				udp_packet_send(i);
				currTmp->processed = FALSE;
				
			}
			/* Case 2: the client DOES have the file listed */
			else{
				free(currTmp);
			}
			pthread_mutex_unlock(&file_list_mutex);
			break;
		case(8):
			printf("\n\tDIR_EMPTY \n");
			pthread_mutex_unlock(&print_mutex);
			/* sleep is useful in case we remove all files from directory and we have both DELETE FILE and EMPTY DIR messages */
			sleep(2);
			watchedTmp = listWatchedDir(watched_dir);
			watched_files = watchedTmp;
			pthread_mutex_lock(&print_mutex);
			pthread_mutex_lock(&file_list_mutex);
			currTmp = (struct dir_files_status_list * ) malloc( sizeof (struct dir_files_status_list));
			if (!currTmp) {
				fprintf(stderr, "malloc() failed: insufficient memory!\n");
				exit(EXIT_FAILURE);
			}
			int listlen=0;
			SGLIB_LIST_LEN(struct dir_files_status_list,watchedTmp,next, listlen);
			UNUSED(currTmp);
			/* If other client is empty start updating him */
			if(listlen > 0){
				SGLIB_LIST_MAP_ON_ELEMENTS(struct dir_files_status_list, watchedTmp, currTmp, next, {
					
					/* Offer file! */ 
					i = udp_file_packet_encode(FILE_TRANSFER_OFFER,client_name,TCP_PORT,&clk,&currTmp->modifictation_time_from_epoch, currTmp->filename,currTmp->sha1sum,currTmp->size_in_bytes);
					if (currTmp->processed == FALSE){
						currTmp->processed = TRUE;
						udp_packet_send(i);
						currTmp->processed = FALSE;
					}
					
				});