Пример #1
0
std::string basic_warning::what() const {
	if (is_active()) {
		return prepare_msg();
	} else {
		return std::string();
	}
}
Пример #2
0
basic_exception::basic_exception(const basic_exception& e) :
	std::runtime_error((std::runtime_error) e), my_cause(0) {
	if (e.my_cause) {
		my_cause = new basic_exception(*e.my_cause);
	}
	prepare_msg();
}
Пример #3
0
void		private_msg(t_list **list, int cs1, char *nick, char *str)
{
    t_list	*tmp;
    char		*name;
    char		*msg;
    char		result[4096];
    int		i;

    i = 0;
    tmp = *list;
    while (str[i] != ' ')
        i++;
    name = strndup(str, i);
    msg = strdup(str + i + 1);
    while (strcmp(tmp->nick, name) != 0 && tmp->next != NULL)
        tmp= tmp->next;
    if (strcmp(tmp->nick, name) == 0)
    {
        sprintf(result, "To %s: %s", name, msg);
        write(cs1, result, strlen(result));
        prepare_msg(nick, msg, tmp->value, list);
    }
    else
        write(cs1, "This users doesn't exist\n", 25);
}
Пример #4
0
void* listenClient(void* ptr)
{
	int soc;
        struct msghdr *msg;
        char * data = (char *) malloc(MAX_BUF_SZ);
        prepare_msg(0, &msg, data, MAX_BUF_SZ);

	#ifdef DEBUG
		printf("this thread listens connection requests from clients\n");
	#endif

	while(1){
		soc = acceptConnection(client_listen_socket);
		if (soc == -1 ) {
			printf("Accept failed");
			exit(0);
		}
		#ifdef DEBUG
			printf("connected to client\n");
		#endif
                if (thr_id == MAX_THR) {
                        thr_id = 0;
                }
                if((pthread_create(&threads[thr_id++], NULL, handle_client_request, (void*)soc)) != 0) {
                        printf("%s: Failed to create thread to handle client requests %d\n", __func__, thr_id);
                }
	}
}
Пример #5
0
static void catch_in_unit_test(print_fn_t fn, const loc_t *loc,
                               const char *fmt, va_list ap)
{
   if (opt_get_int("unit-test")) {
      char *strp LOCAL = prepare_msg(fmt, ap, error_force_plain);
      error_fn(strp, loc != NULL ? loc : &LOC_INVALID);
   }
Пример #6
0
basic_exception& basic_exception::operator=(const basic_exception& e) {
	std::runtime_error::operator=(e);
	if (e.my_cause) {
		my_cause = new basic_exception(*e.my_cause);
	}
	prepare_msg();
	return *this;
}
Пример #7
0
static void msg_at(print_fn_t fn, const loc_t *loc, const char *fmt, va_list ap)
{
   char *strp = prepare_msg(fmt, ap, false);
   if (message_style == MESSAGE_COMPACT)
      fmt_loc(stderr, loc);
   (*fn)("%s", strp);
   if (message_style == MESSAGE_FULL)
      fmt_loc(stderr, loc);
   free(strp);
}
Пример #8
0
void error_at(const loc_t *loc, const char *fmt, ...)
{
   va_list ap;
   va_start(ap, fmt);

   char *strp LOCAL = prepare_msg(fmt, ap, error_force_plain);
   error_fn(strp, loc != NULL ? loc : &LOC_INVALID);

   va_end(ap);
}
Пример #9
0
basic_exception::basic_exception(const std::string& msg,
		const std::exception& cause) :
	std::runtime_error(msg) {
	if (const basic_exception* b = dynamic_cast<const basic_exception*>(&cause)) {
		my_cause = new basic_exception(*b);
	} else {
		my_cause = new basic_exception(cause.what());
	}
	prepare_msg();
}
Пример #10
0
static int color_vfprintf(FILE *f, const char *fmt, va_list ap)
{
   char *strp LOCAL = prepare_msg(fmt, ap, false);

   bool escape = false;
   int len = 0;
   for (const char *p = strp; *p != '\0'; p++) {
      if (*p == '\033')
         escape = true;
      if (escape)
         escape = (*p != 'm');
      else
         len += 1;
   }

   fputs(strp, f);
   return len;
}
Пример #11
0
static int gfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
	struct msghdr *msg;
	int master_soc;
        int ret;
	open_req data_ptr;
	create_open_req(&data_ptr,path,fi->flags | O_CREAT);
	prepare_msg(OPEN_REQ, &msg, &data_ptr, sizeof(open_req));

        if((master_soc = createSocket())==-1){
                printf("%s: Error creating socket\n",__func__);
                return -1;
        }

        if(createConnection(master,master_soc) == -1){
                printf("%s: can not connect to the master server\n",__func__);
                return -1;
        }
	#ifdef DEBUG
	print_msg(msg->msg_iov[0].iov_base);
	printf("flags are ------ %d\n", ((open_req*)msg->msg_iov[1].iov_base)->flags);
	#endif
	
	/* send a message to master */
        if((sendmsg(master_soc,msg,0))==-1){
                printf("%s: message sending failed\n",__func__);
                return -1;
        }

        /* reply from master */
        if((recvmsg(master_soc,msg,0))==-1){
                printf("%s: message receipt failed\n",__func__);
                return -1;
        }
	close(master_soc);
        
	/* if failure return -errno */
        dfs_msg *dfsmsg =  msg->msg_iov[0].iov_base;
        if(dfsmsg->status!=0){
                return dfsmsg->status;
        }
        return 0;
}
Пример #12
0
static int gfs_mkdir(const char *path, mode_t mode)
{
	struct msghdr *msg;
	int master_soc;
       	int ret;
	mkdir_req data_ptr;
	create_mkdir_req(&data_ptr,path,mode);
        prepare_msg(MAKE_DIR_REQ, &msg, &data_ptr, sizeof(mkdir_req)); 

        if((master_soc = createSocket())==-1){
                printf("%s: Error creating socket\n",__func__);
                return -1;
        }


        if(createConnection(master,master_soc) == -1){
		printf("%s: can not connect to the master server\n",__func__);
		return -1;
	}
	print_msg(msg->msg_iov[0].iov_base);

	/* send a message to master */
        if((sendmsg(master_soc,msg,0))==-1){
                printf("%s: message sending failed\n",__func__);
                return -1;
        }

        /* reply from master */
        if((recvmsg(master_soc,msg,0))==-1){
                printf("%s: message receipt failed\n",__func__);
                return -1;
        }
	close(master_soc);

        /* if failure return -errno */
        dfs_msg *dfsmsg =  msg->msg_iov[0].iov_base;
        if(dfsmsg->status != 0){
                return dfsmsg->status;
	}
        return 0;
}
Пример #13
0
int color_printf(const char *fmt, ...)
{
   va_list ap;
   va_start(ap, fmt);
   char *strp LOCAL = prepare_msg(fmt, ap, false);
   va_end(ap);

   bool escape = false;
   int len = 0;
   for (const char *p = strp; *p != '\0'; p++) {
      if (*p == '\033')
         escape = true;
      if (escape)
         escape = (*p != 'm');
      else
         len += 1;
   }

   printf("%s", strp);
   return len;
}
Пример #14
0
basic_exception::basic_exception(const std::string& msg) :
	std::runtime_error(msg), my_cause(0) {
	prepare_msg();
}
Пример #15
0
/* draw the the AR objects */
static int draw(ObjectData_T *object, int objectnum)
{
	int     i;
	double  gl_para[16];

	glClearDepth(1.0);
	glClear(GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_LIGHTING);

	/* calculate the viewing parameters - gl_para */

	int count = 0;
	
	float x1, x2,x3;
	float y1, y2,y3;
	float z1, z2,z3;


	for (i = 0; i < objectnum; i++) {
		if (object[i].visible == 0)continue;
		count++;
		argConvGlpara(object[i].trans, gl_para);
		draw_object(object[i].id, gl_para);
	}	


	x1 = object[0].trans[0][3], x2 = object[1].trans[0][3],x3 = object[2].trans[0][3];
	y1 = object[0].trans[1][3], y2 = object[1].trans[1][3], y3 = object[2].trans[1][3];
	z1 = object[0].trans[2][3], z2 = object[1].trans[2][3], z3 = object[2].trans[2][3];

	int grid = object[0].visible && object[1].visible;

	if (grid == 1){

		distX = fabs(x2 - x1);
		distY = fabs(y2 - y1);
		distZ = fabs(z2 - z1);

		//printf("grid distance %f %f %f \n ", distX, distY, distZ);
	}
	int controler =  object[2].visible;
	if (object[0].visible && controler){
		distX1 = fabs(x3 - x1);
		distY1 = fabs(y3 - y1);

		distX0 = fabs(x3 - x2);
		 distY0 = fabs(y3 - y2);
	}

	if (nFrame++ == 50) {
		nFrame = 0;

		if (controler){

			//calculate distance
			//printf("object id = %d ||  x = %f    || y =  %f     ||  z =  %f    ||\n", object[0].id, x1, y1, z1);
			//printf("object id = %d ||  x = %f    || y =  %f     ||  z =  %f    ||\n", object[1].id, x2, y2, z2);
			//printf("object id = %d ||  x = %f    || y =  %f     ||  z =  %f    ||\n", object[2].id, x3, y3, z3);

			printf(" distancia  0 - - 1    x = %f    || y =  %f     ||  z =  %f    ||\n", distX, distY, distZ);
			printf(" distancia  0-2 0-1 ||  x = %f  - - %f   || y =  %f - - %f       ||\n", distX1,distX0 ,  distY1, distY0);

			double x_send = distX1 / distX;
			double y_send = distY1 / distY;

			printf(" distancia norma   x = %f    || y =  %f    \n", x_send, y_send);
			
			/*
			// treat overflow
			if (x_send < 0.0)
				x_send = 0.0;
			else{
				if (y_send < 0.0)
					y_send = 0.0;
				else{
					if (x_send > 1.0)
						x_send = 1.0;
					else{
						if (y_send > 1.0)
							y_send = 1.0;
					}
				}
			}
			*/
			//printf(" distancia  normalizada   x = %f    || y =  %f    ||\n", x_send, y_send);

          	prepare_msg(x_send, y_send);

			udpSend(buf2);

			// if relevante change in distance then udpSend
		}
	}



	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);
	return(0);
}
Пример #16
0
static int gfs_write(const char *path, const char *buf, size_t size,off_t offset, struct fuse_file_info *fi)
{
	#ifdef DEBUG
		printf("Inside Write\n");
	#endif
	struct msghdr *msg;
	int master_soc;
        int ret;
	int i;
	int chunk_soc;
	char write_ptr[SMALL_BUF];
	char *resp = (char*)malloc(SMALL_BUF*sizeof(char));
	host chunk_server[2];
	char chunk_handle[64];
	size_t write_size = 0;	
	int start_block = offset/CHUNK_SIZE;
	int last_block = (offset+size-1)/CHUNK_SIZE;
	int chunk_size, chunk_offset;

	for(i=start_block;i<=last_block;i++){
		if((master_soc = createSocket())==-1){
			printf("%s: Error creating socket\n",__func__);
			return -1;
		}

		if(createConnection(master,master_soc) == -1){
			printf("%s: can not connect to the master server\n",__func__);
			return -1;
		}

		/* Send metadat request to master */
		if ( i == start_block) {
			chunk_offset = offset % CHUNK_SIZE;
		} else {
			chunk_offset = 0;
		}
		if ((size - write_size + chunk_offset) > CHUNK_SIZE) {
			chunk_size = CHUNK_SIZE - chunk_offset;
		} else {
			chunk_size = size - write_size;
		}
		create_write_req(write_ptr, path, i, chunk_offset, chunk_size);
		prepare_msg(WRITE_REQ, &msg, write_ptr, SMALL_BUF);
//		print_msg(msg->msg_iov[0].iov_base);
		if((sendmsg(master_soc,msg,0))==-1){
                	printf("%s: message sending failed\n",__func__);
	                return -1;
        	}

        	/* Matadata reply from master */
        	if((recvmsg(master_soc,msg,0))==-1){
                	printf("%s: message receipt failed\n",__func__);
	                return -1;
        	}
		close(master_soc);
		/* Extract primary and secondary chunkserver details */
	 	dfs_msg *dfsmsg =  msg->msg_iov[0].iov_base;
		if (dfsmsg->status != 0) {
                	printf("%s: Error creating chunk on master\n",__func__);
	                return -1;
        	}
		printf("before response\n");
		strcpy(resp,msg->msg_iov[1].iov_base);	
		#ifdef DEBUG
			printf("response is %s\n",resp); 
		#endif
		strcpy(chunk_server[0].ip_addr,strtok(resp,":"));
		chunk_server[0].port = atoi(strtok(NULL,":"));
		strcpy(chunk_server[1].ip_addr,strtok(NULL,":"));
		chunk_server[1].port = atoi(strtok(NULL,":"));
		strcpy(chunk_handle, strtok(NULL,":"));
		#ifdef DEBUG	
			printf("Write response - primary-%s:%d secondary-%s:%d chunkhandle-%s\n", 
			chunk_server[0].ip_addr, chunk_server[0].port, chunk_server[1].ip_addr, chunk_server[1].port, chunk_handle);	
		#endif

		/* Create connection with secondary chunkserver */
	        if((chunk_soc = createSocket())==-1){
                	printf("%s: Error creating socket\n",__func__);
	                return -1;
        	}

		if(createConnection(chunk_server[1],chunk_soc) == -1){
			printf("%s: can not connect to the chunk server\n",__func__);
			return -1;
		}

		/* Prepare write data request */
		//write_data_req *data_ptr = (write_data_req*) malloc (sizeof(write_data_req));
		char *data_ptr = (char*)malloc(sizeof(char)*MAX_BUF_SZ);
		create_write_data_req(data_ptr, chunk_handle, buf+ write_size, chunk_offset, chunk_size);
		
		#ifdef DEBUG
		int j;
		printf("Data to be written is - \n");
		for (j = 0; j < strlen(data_ptr); j++) 
			printf("%c", data_ptr[j]);
//		printf("\nhandle - %s\n", data_ptr->chunk+CHUNK_SIZE);
		printf("\n");
		#endif
		free_msg(msg);
		prepare_msg(WRITE_DATA_REQ, &msg, data_ptr, strlen(data_ptr)+1);
		print_msg(msg->msg_iov[0].iov_base);


		/* Send write-data request to secondary chunkserver */
		if((sendmsg(chunk_soc,msg,0))==-1){
                	printf("%s: message sending failed\n",__func__);
			free_msg(msg);
			free(data_ptr);
	                return -1;
        	} else {
			printf("%s: Sent write request to secondary chunkserver\n",__func__);
		}
		
        	/* Reply from secondary chunkserver */
        	if((recvmsg(chunk_soc,msg,0))==-1){
                	printf("%s: message receipt failed\n",__func__);
			free_msg(msg);
			free(data_ptr);
	                return -1;
        	} else {
			printf("%s: Received write reply from secondary chunkserver\n",__func__);
		}
		close(chunk_soc);

		/* If secondary chunkserver failed to write data - report failure to client */
	 	dfsmsg =  msg->msg_iov[0].iov_base;
		if (dfsmsg->status != 0) {
                	printf("%s: Error writing data to secondary chunserver\n",__func__);
			free_msg(msg);
			free(data_ptr);
	                return -1;
		}

		/* create connection with primary chunk server */
	        if((chunk_soc = createSocket())==-1){
                	printf("%s: Error creating socket\n",__func__);
			free_msg(msg);
			free(data_ptr);
                	return -1;
		}
		if(createConnection(chunk_server[0],chunk_soc) == -1){
			printf("%s: can not connect to the chunk server\n",__func__);
			free_msg(msg);
			free(data_ptr);
			return -1;
		}

		/* Send write-data to primary chunkserver */
		create_write_data_req(data_ptr, chunk_handle, buf+ write_size, chunk_offset, chunk_size);
		free_msg(msg);
		prepare_msg(WRITE_DATA_REQ, &msg, data_ptr, strlen(data_ptr)+1);
		if((sendmsg(chunk_soc,msg,0))==-1){
                	printf("%s: message sending failed\n",__func__);
			free_msg(msg);
			free(data_ptr);
	                return -1;
        	} else {
			printf("%s: Sent write request to primary chunkserver\n",__func__);
		}

        	/* Reply from primary chunkserver */
        	if((recvmsg(chunk_soc,msg,0))==-1) {
                	printf("%s: message receipt failed\n",__func__);
			free_msg(msg);
			free(data_ptr);
	                return -1;
        	} else {
			printf("%s: Received write reply from primary chunkserver\n",__func__);
		}
		close(chunk_soc);

		/* If Primary chunkserver failed to write data - report failure to client and secondary chunkserver */
	 	dfsmsg =  msg->msg_iov[0].iov_base;
		if (dfsmsg->status != 0) {
                	printf("%s: Error writing data to primary chunserver\n",__func__);
			free_msg(msg);
			/* TODO : send rollback to secondary chunkserver */
			if((chunk_soc = createSocket())==-1){
				printf("%s: Error creating socket\n",__func__);
				return -1;
			}
			if(createConnection(chunk_server[1],chunk_soc) == -1){
				printf("%s: can not connect to the chunk server\n",__func__);
				return -1;
			}
			prepare_msg(ROLLBACK_REQ, &msg, data_ptr, sizeof(write_data_req));
			if((sendmsg(chunk_soc,msg,0))==-1){
				printf("%s: message sending failed\n",__func__);
				free_msg(msg);
				free(data_ptr);
				return -1;
			} else {
				printf("%s: Sent write request to secondary chunkserver\n",__func__);
			}
			/* Reply from secondary chunkserver */
			if((recvmsg(chunk_soc,msg,0))==-1) {
				printf("%s: message receipt failed\n",__func__);
				free_msg(msg);
				free(data_ptr);
				return -1;
			} else {
				printf("%s: Received write reply from secondary chunkserver\n",__func__);
			}
			free(data_ptr);
			close(chunk_soc);
			return -1;
		}
		if((master_soc = createSocket())==-1){
			printf("%s: Error creating socket\n",__func__);
			return -1;
		}

		if(createConnection(master,master_soc) == -1){
			printf("%s: can not connect to the master server\n",__func__);
			return -1;
		}
		create_write_req(write_ptr, path, i, chunk_offset, chunk_size);
		prepare_msg(WRITE_COMMIT_REQ, &msg, write_ptr, strlen(write_ptr));
		print_msg(msg->msg_iov[0].iov_base);
		if((sendmsg(master_soc,msg,0))==-1){
                	printf("%s: message sending failed\n",__func__);
	                return -1;
        	}

        	/* Matadata reply from master */
        	if((recvmsg(master_soc,msg,0))==-1){
                	printf("%s: message receipt failed\n",__func__);
	                return -1;
        	}
		close(master_soc);
	 	dfsmsg =  msg->msg_iov[0].iov_base;
		if (dfsmsg->status != 0) {
                	printf("%s: Error commiting write on master\n",__func__);
	                return -1;
        	}
		write_size += chunk_size;
		free_msg(msg);
		free(data_ptr);
	}
	/* Return number of bytes written */
#ifdef DEBUG
	printf("No. of bytes written successfully - %d\n", write_size);
	#endif
	return write_size;
}
Пример #17
0
//#define DEBUG
static int gfs_getattr(const char *path, struct stat *stbuf)
{
	struct msghdr *msg;
	int master_soc;
	int ret;
	char * buf = (char*) malloc (1000);
	strcpy(buf, path);
	prepare_msg(GETATTR_REQ, &msg, buf, 1000);
	print_msg(msg->msg_iov[0].iov_base);

        if((master_soc = createSocket())==-1){
                printf("%s: Error creating socket\n",__func__);
		free(buf);
                return -1;
        }

        if(createConnection(master,master_soc) == -1){
                printf("%s: can not connect to the master server - error-%d\n",__func__, errno);
		free(buf);
                return -1;
        }

	/* send a message to master */
	if((sendmsg(master_soc,msg,0))==-1){
		printf("%s: message sending failed - %d\n",__func__, errno);
		free(buf);
		return -1;
	} else {
	#ifdef DEBUG
		printf("%s: Getattr request sent\n",__func__);
	#endif
	}
	free(msg);
	prepare_msg(GETATTR_REQ, &msg, buf, 1000);
	/* reply from master */
	if((recvmsg(master_soc,msg,0))==-1){
		printf("%s: message receipt failed - %d\n",__func__, errno);
		free(buf);
		return -1;
	} else {
	#ifdef DEBUG
		printf("%s: Getattr response received from server\n",__func__);
	#endif
	}
	close(master_soc);

	/* if failure return -errno */
	dfs_msg *dfsmsg =  msg->msg_iov[0].iov_base;
	if(dfsmsg->status != 0){
	#ifdef DEBUG
		printf("%s: Getattr status is - %d\n",__func__, dfsmsg->status);
	#endif
		free(buf);
		return dfsmsg->status;
	}

	/* TODO : Only size and inode nummer is received from master right now */
	char *str = msg->msg_iov[1].iov_base;
	#ifdef DEBUG
		printf("%s: Getattr str - %s\n",__func__, str);
	#endif
	char ino[10], size[10];
	int i = 0, j = 0;
	while (str[i] != ' ') {
		ino[j++] = str[i++];
	}
	ino[j] = '\0';
	i++;
	j = 0;
	while (str[i] != ' ') {
		size[j++] = str[i++];
	}
	size[j] = '\0';
	lstat("./client.c", stbuf);
	stbuf->st_ino = atol(ino);	
	stbuf->st_size = atol(size);	
	
	#ifdef DEBUG
		printf("%s: Getattr status is - %d ino = %llu file size = %llu\n",__func__
			, dfsmsg->status, stbuf->st_ino, stbuf->st_size);
	#endif
	free(buf);
	return 0;
}
Пример #18
0
static int gfs_read(const char *path, char *buf, size_t size, off_t offset,struct fuse_file_info *fi)
{
	#ifdef DEBUG
		printf("Inside Read\n");
	#endif
	struct msghdr *msg;
	int master_soc;
	int chunk_soc;
	host chunk_server;
	int ret;
	int i;
	int curr_offset;
	char read_ptr[SMALL_BUF];
	char *data_ptr = (char*)malloc(sizeof(char)*SMALL_BUF);
	char chunk_handle[64];
	dfs_msg *dfsmsg;
	size_t size_read = 0;
	int chunk_size, chunk_offset;
	char *resp = (char*) malloc(sizeof(char)*MAX_BUF_SZ);

	int start_block = offset/CHUNK_SIZE;
	int last_block = (offset+size-1)/CHUNK_SIZE;
	
	#ifdef DEBUG
		printf("start block = %d last block = %d size = %u offset = %llu\n", start_block, last_block, size, offset);
	#endif

	for(i=start_block;i<=last_block;i++) {
		#ifdef DEBUG
		printf("Chunk - %d\n", i);
		#endif

		if((master_soc = createSocket())==-1){
			printf("%s: Error creating socket\n",__func__);
			return -1;
		}

		if(createConnection(master,master_soc) == -1){
			printf("%s: can not connect to the master server\n",__func__);
			return -1;
		}

		/* Send metadata request to master */
		if ( i == start_block) {
			chunk_offset = offset % CHUNK_SIZE;
		} else {
			chunk_offset = 0;
		}
		if ((size - size_read + chunk_offset) > CHUNK_SIZE) {
			chunk_size = CHUNK_SIZE - chunk_offset;
		} else {
			chunk_size = size - size_read;
		}
		create_read_req(read_ptr,path,i, chunk_offset, chunk_size);
		prepare_msg(READ_REQ, &msg, read_ptr, SMALL_BUF);
		print_msg(msg->msg_iov[0].iov_base);
		if((sendmsg(master_soc,msg,0))==-1){
                	printf("%s: message sending failed\n",__func__);
	                return -1;
        	}

        	/* reply from master - chunk metadata and location info */
        	if((recvmsg(master_soc,msg,0))==-1){
                	printf("%s: message receipt failed\n",__func__);
			return -1;
        	} else {
			dfsmsg =  msg->msg_iov[0].iov_base;
			/* No metadata for this chunk */
			if (dfsmsg->status != 0)
				break;
		}
		close(master_soc);

		/* extract chunkserver details */
		strcpy(resp,msg->msg_iov[1].iov_base);
		printf("Read response is %s\n",resp);
		strcpy(chunk_server.ip_addr,strtok(resp,":"));	
		chunk_server.port = atoi(strtok(NULL,":"));
		strcpy(chunk_handle,strtok(NULL,":"));
		/*strcpy(chunk_server.ip_addr,((read_resp*)dfsmsg -> data) ->ip_address);
		chunk_server.port = ((read_resp*)dfsmsg->data) ->port;
		strcpy(chunk_handle,((read_resp*)dfsmsg->data) ->chunk_handle);*/

		/* conect to the chunk server */
	        if((chunk_soc = createSocket())==-1){
                	printf("%s: Error creating socket\n",__func__);
                	return -1;
        	}
		if(createConnection(chunk_server,chunk_soc) == -1){
			printf("%s: can not connect to the chunk server\n",__func__);
			return -1;
		}
		/* Preapare read-data request */
		create_read_data_req(data_ptr,chunk_handle, chunk_offset, chunk_size);
		free_msg(msg);
		prepare_msg(READ_DATA_REQ, &msg, data_ptr, strlen(data_ptr));
		print_msg(msg->msg_iov[0].iov_base);

		/* Send read-data request to chunkserver */
		if((sendmsg(chunk_soc,msg,0))==-1){
                	printf("%s: read request sending to chunkserver failed\n",__func__);
	                return -1;
        	} else {
                	printf("%s: Success: Sent read request\n",__func__);
		}

        	/* Receive read-data reply from chunkserver */
		free_msg(msg);
		prepare_msg(READ_DATA_RESP, &msg, resp, MAX_BUF_SZ);
        	if((recvmsg(chunk_soc,msg,0))==-1){
                	printf("%s: read reply from chunkserver failed\n",__func__);
			free(resp);
	                return -1;
        	} else {
                	printf("%s: Success: Received read reply from chunkserver\n",__func__);
		}

		/*TODO: process received data */
        	dfsmsg =  msg->msg_iov[0].iov_base;
		if(dfsmsg->status == 0) {
			char *q;
			int size; 
			/* Update number of bytes read */
			resp = msg->msg_iov[1].iov_base;
			size = atoi(strtok_r(resp,":",&q));
			memcpy(buf+size_read, q, size); 
			#ifdef DEBUG
				int j;
				printf("Data read is - \n");
				for (j = 0; j < CHUNK_SIZE; j++) 
					printf("%c", buf[j+size_read]);
				printf("\n");
			#endif
			size_read += size;
			if (size < chunk_size) {
				break;
			}
		}
		free_msg(msg);
		close(chunk_soc);
	}

	free(resp);
	/* Return number of bytes written */
	#ifdef DEBUG
		printf("No. of bytes Read successfully - %d\n", size_read);
	#endif
	return size_read;

}
Пример #19
0
basic_exception::basic_exception(const std::string& msg,
		const basic_exception& cause) :
	std::runtime_error(msg), my_cause(new basic_exception(cause)) {
	prepare_msg();
}
Пример #20
0
static int gfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,off_t offset, struct fuse_file_info *fi)
{
	struct msghdr *msg;
	int master_soc;
	struct stat st;
	int ret;
	char * tempbuf = (char*) malloc (1000);
	char *file_name = (char*) malloc(1000);
	strcpy(tempbuf, path);
	prepare_msg(READDIR_REQ, &msg, tempbuf, 1000);
	print_msg(msg->msg_iov[0].iov_base);

        if((master_soc = createSocket())==-1){
		printf("%s: Error creating socket\n",__func__);
		return -1;
        }

        if(createConnection(master,master_soc) == -1){
                printf("%s: can not connect to the master server - error-%d\n",__func__, errno);
                return -1;
        }

	/* send a message to master */
	if((sendmsg(master_soc,msg,0))==-1){
		printf("%s: message sending failed - %d\n",__func__, errno);
		return -1;
	} else {
	#ifdef DEBUG
		printf("%s: readdir request sent\n",__func__);
	#endif
	}

	/* reply from master */
	while(1){
		//free_msg(msg);
		//prepare_msg(READDIR_RESP, &msg, &st, sizeof(struct stat));
		if((recv(master_soc,file_name,1000,0))==-1){
			printf("%s: message receipt failed - %d\n",__func__, errno);
			return -1;
		} else {
		#ifdef DEBUG
			printf("%s: received filename %s\n",__func__,file_name);
		#endif
		}	
//		if(((dfs_msg*)(msg->msg_iov[0]).iov_base)->status == -1)
		if(strcmp(file_name,"END")==0)
			break;
		//dptr = &d;
		free_msg(msg);
		prepare_msg(READDIR_REQ, &msg, tempbuf, 1000);
		if((sendmsg(master_soc,msg,0))==-1){
		printf("%s: message sending failed - %d\n",__func__, errno);
			return -1;
		} else {
		#ifdef DEBUG
			printf("%s: request sent for stat\n",__func__);
		#endif
		}
		//memcpy(&st,msg->msg_iov[1].iov_base, sizeof(struct stat));
		//free_msg(msg);
		//memset(file_name,0,MAX_BUF_SZ);
		//prepare_msg(READDIR_RESP, &msg, file_name, sizeof(struct stat));
		if((recv(master_soc,&st,sizeof(struct stat),0))==-1){
			printf("%s: message receipt failed - %d\n",__func__, errno);
			return -1;
		} 
		//memcpy(file_name,msg->msg_iov[1].iov_base, MAX_BUF_SZ);
		#ifdef DEBUG
			printf("%s: received stat\n",__func__);
		//	printf("status is %d\n",((dfs_msg*)(msg->msg_iov[0]).iov_base)->status);
		#endif
	
		if (filler(buf, file_name+1, &st, 0)){	}

		free_msg(msg);
		prepare_msg(READDIR_REQ, &msg, tempbuf, 1000);
		if((sendmsg(master_soc,msg,0))==-1){
		printf("%s: message sending failed - %d\n",__func__, errno);
			return -1;
		} else {
			#ifdef DEBUG
				printf("%s: readdir request sent for next record\n",__func__);
			#endif
		}
		

	}
	close(master_soc);
	return 0;
}
Пример #21
0
void* handle_client_request(void *arg)
{	
        struct msghdr *msg;
        int soc = (int)arg;
        char * data = (char *) malloc(MAX_BUF_SZ);
        prepare_msg(0, &msg, data, MAX_BUF_SZ);
	char * resp;
	dfs_msg *dfsmsg;

        int retval = recvmsg(soc, msg, 0);
	if (retval == -1) {
		printf("failed to receive message from client - errno-%d\n", errno);
	} else {
		dfsmsg = (dfs_msg*)msg->msg_iov[0].iov_base;
		#ifdef DEBUG
        	printf("received message from client - %d bytes = %d\n", dfsmsg->msg_type, retval);
		#endif
	}


        //extract the message type
        print_msg(dfsmsg);

        switch (dfsmsg->msg_type) {

                case HEARTBEAT:
                        break;

                case READ_DATA_REQ:
			resp = (char*) malloc(sizeof(char)*MAX_BUF_SZ);
			dfsmsg->status = chunk_read(msg->msg_iov[1].iov_base, resp);
			msg->msg_iov[1].iov_base = resp;
			msg->msg_iov[1].iov_len = strlen(resp)+1;
			dfsmsg->msg_type = READ_DATA_RESP;
			retval = sendmsg(soc, msg, 0); 
			if (retval == -1) {
				printf("failed to send read reply to client - errno-%d\n", errno);
			} else {
				#ifdef DEBUG
				printf("sent read reply to client\n");
				#endif
			}
			break;

                case WRITE_DATA_REQ:
			dfsmsg->status = chunk_write(msg->msg_iov[1].iov_base);
			dfsmsg->msg_type = WRITE_DATA_RESP; 
			retval = sendmsg(soc, msg, 0); 
			if (retval == -1) {
				printf("failed to send write reply to client - errno-%d\n", errno);
			} else {
				#ifdef DEBUG
				printf("sent write reply to client\n");
				#endif
			}
			break;

                case ROLLBACK_REQ:
			dfsmsg->status = chunk_truncate(msg->msg_iov[1].iov_base);
			dfsmsg->msg_type = ROLLBACK_RESP; 
			retval = sendmsg(soc, msg, 0); 
			if (retval == -1) {
				printf("failed to send rollback reply to client - errno-%d\n", errno);
			} else {
				#ifdef DEBUG
				printf("sent rollback reply to client\n");
				#endif
			}
			break;
	}
}