s32 menu_cb_choose_file (s32 idx, void *param) {
	s32 curidx = idx;
	s32 loaded = 0;

	while (aptMainLoop()) {
		gspWaitForVBlank();

		curidx = print_file_list(curidx, &g_file_list);	           
		u32 kDown = wait_key(); 

		if (kDown & KEY_B) {
			break;
		}
		else if (kDown & KEY_A) {
			consoleClear();
			loaded = menu_execute_function(curidx, &g_file_list, &curidx);
			printf("%s\n", loaded? "[+] Success":"[!] Failure");
			wait_any_key();
			if (loaded)
				break;
		}
		else if (kDown & KEY_UP) {
			curidx--;
		}
		else if (kDown & KEY_DOWN) {
			curidx++;
		}
		gfxFlushBuffers();
		gfxSwapBuffers();
	}
	return 0;
}
Exemple #2
0
int clnt_rm(int index){
	int i,id;
	/*remove from file list*/
	id=MyClntList[index].id;
	rm_from_filelist(id);
	/*remove from client list*/
	pthread_mutex_lock(&lock);
	for(i=index+1;i<clnt_num;i++){
		MyClntList[i-1].id=MyClntList[i].id;
		MyClntList[i-1].no_response=MyClntList[i].no_response;
		strcpy(MyClntList[i-1].IPaddr,MyClntList[i].IPaddr);
		strcpy(MyClntList[i-1].port_d,MyClntList[i].port_d);
	}
	clnt_num--;
	pthread_mutex_unlock(&lock);
	print_clnt();
	print_file_list();
}
Exemple #3
0
int
main(int argc, char *argv[])
{
	char *src_hostfile = NULL, *dst_hostfile = NULL;
	gfarm_stringlist paths;
	gfs_glob_t types;
	int mode_src_ch = 0, mode_dst_ch = 0, parallel = -1;
	int i, ch;
	gfarm_error_t e;
	const char *errmsg, *errmsg2 = NULL;
	struct gfrep_arg gfrep_arg;
	struct flist flist;

	if (argc >= 1)
		program_name = basename(argv[0]);
	memset(&gfrep_arg, 0, sizeof(gfrep_arg));
	memset(&flist, 0, sizeof(flist));
	flist.src_domain = "";
	flist.dst_domain = "";

	e = gfarm_initialize(&argc, &argv);
	error_check(e);

#ifdef _OPENMP
	while ((ch = getopt(argc, argv, "h:j:mnqvxS:D:H:N:?")) != -1) {
#else
	while ((ch = getopt(argc, argv, "h:mnqvxS:D:H:N:?")) != -1) {
#endif
		switch (ch) {
		case 'h':
			src_hostfile = optarg;
			conflict_check(&mode_src_ch, ch);
			break;
#ifdef _OPENMP
		case 'j':
			parallel = strtol(optarg, NULL, 0);
			break;
#endif
		case 'm':
			act = &migrate_mode;
			break;
		case 'n':
			opt_noexec = 1;
			break;
		case 'q':
			opt_quiet = 1;
			break;
		case 'v':
			opt_verbose = 1;
			break;
		case 'x':
			opt_remove = 1;
			break;
		case 'S':
			flist.src_domain = optarg;
			conflict_check(&mode_src_ch, ch);
			break;
		case 'D':
			flist.dst_domain = optarg;
			conflict_check(&mode_dst_ch, ch);
			break;
		case 'H':
			dst_hostfile = optarg;
			conflict_check(&mode_dst_ch, ch);
			break;
		case 'N':
			opt_nrep = strtol(optarg, NULL, 0);
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* make writing-to-stderr atomic, for GfarmFS-FUSE log output */
	setvbuf(stderr, NULL, _IOLBF, 0);

	if (!opt_quiet) {
		printf("constructing file list...");
		fflush(stdout);
	}

	e = gfarm_stringlist_init(&paths);
	if (e == GFARM_ERR_NO_ERROR) {
		e = gfs_glob_init(&types);
		if (e == GFARM_ERR_NO_ERROR) {
			for (i = 0; i < argc; i++)
				gfs_glob(argv[i], &paths, &types);
			gfs_glob_free(&types);
		}
	}
	error_check(e);

	e = gfarm_list_init(&flist.slist);
	error_check(e);
	e = gfarm_list_init(&flist.dlist);
	error_check(e);
	flist.srchash = gfarm_hash_table_alloc(HOSTHASH_SIZE,
		gfarm_hash_casefold, gfarm_hash_key_equal_casefold);
	if (flist.srchash == NULL)
		error_check(GFARM_ERR_NO_MEMORY);

	e = create_hosthash_from_file(src_hostfile,
		HOSTHASH_SIZE, &flist.src_hosthash);
	error_check(e);
	e = create_hosthash_from_file(dst_hostfile,
		HOSTHASH_SIZE, &flist.dst_hosthash);
	error_check(e);

	for (i = 0; i < gfarm_stringlist_length(&paths); i++) {
		char *file = gfarm_stringlist_elem(&paths, i), *realpath = NULL;

		e = gfarm_realpath_by_gfarm2fs(file, &realpath);
		if (e == GFARM_ERR_NO_ERROR)
			file = realpath;
		e = gfarm_foreach_directory_hierarchy(
			create_filelist, NULL, NULL, file, &flist);
		free(realpath);
		if (e != GFARM_ERR_NO_ERROR)
			break;
	}
	gfarm_stringlist_free_deeply(&paths);
	error_check(e);

	if (!opt_quiet)
		printf(" done\n");
	if (opt_verbose) {
		printf("files to be replicated\n");
		print_file_list(&flist.slist);
	}
	if (opt_verbose && opt_remove) {
		printf("files having too many replicas\n");
		print_file_list(&flist.dlist);
	}
	if (gfarm_list_length(&flist.slist) <= 0
	    && (!opt_remove || gfarm_list_length(&flist.dlist) <= 0))
		exit(0); /* no file */

	/* replicate files */
	e = gfarm_hash_to_string_array(
		flist.srchash, &gfrep_arg.nsrc, &gfrep_arg.src);
	error_check(e);
	gfarm_hash_table_free(flist.srchash);

	if (!opt_quiet) {
		printf("investigating hosts...");
		fflush(stdout);
	}
	e = create_hostlist_by_domain_and_hash(
		gfarm_list_length(&flist.slist) > 0 ?
		gfarm_list_elem(&flist.slist, 0) :
		gfarm_list_elem(&flist.dlist, 0),
		flist.dst_domain, flist.dst_hosthash,
		&gfrep_arg.ndst, &gfrep_arg.dst, &gfrep_arg.dst_port);
	error_check(e);
	if (!opt_quiet)
		printf(" done\n");

	errmsg = pfor_list(act, &flist.slist, parallel, &gfrep_arg);
	gfarm_list_free(&flist.slist);

	/* remove file replicas */
	if (opt_remove)
		errmsg2 = pfor_list(
			&remove_mode, &flist.dlist, parallel, &gfrep_arg);
	gfarm_list_free(&flist.dlist);
	if (errmsg == NULL)
		errmsg = errmsg2;
	if (errmsg != NULL)
		fprintf(stderr, "%s\n", errmsg), exit(EXIT_FAILURE);

	gfarm_strings_free_deeply(gfrep_arg.nsrc, gfrep_arg.src);
	gfarm_strings_free_deeply(gfrep_arg.ndst, gfrep_arg.dst);
	free(gfrep_arg.dst_port);
	e = gfarm_terminate();
	error_check(e);

	return (0);
}
Exemple #4
0
void *listen_thread(void *ptr){
	/*Binding*/
	int sfd,slen,recv_len,index,i;
	char *pch;
	char buf[BUFLEN];
	char cmd[BUFLEN];
    struct sockaddr_in server,dst;
	sfd=socket(AF_INET,SOCK_DGRAM,0);
	bzero(&server,sizeof(server));
	server.sin_family=AF_INET;
	server.sin_port=htons(My_port);
	inet_aton(MyIP,&server.sin_addr);
	if( bind(sfd,(struct sockaddr *)&server,sizeof(server)) == -1)
	{
		die("bind");
	}
	slen=sizeof(server);
	//printf("in Ping_thread\n");
	while(1)
	{
		fflush(stdout);

		memset(buf,'\0', BUFLEN);
		if ((recv_len = recvfrom(sfd, buf, BUFLEN, 0, (struct sockaddr *) &dst, (socklen_t *)&slen)) == -1)
		{
			die("recvfrom()");
		}
		printf("Received packet from %s:%d\n", inet_ntoa(dst.sin_addr), ntohs(dst.sin_port));
		printf("Data: %s\n" , buf);

		char *p;
		p=buf;
		p++;
		strcpy(cmd,p);

		/*Deal with received cmd*/
		if(buf[0]==REGISTER){
			/*Add new clnt to MyClntList*/
			printf("REGISTER\n");
			char IP[INET_ADDRSTRLEN],port_d[PORTLEN];
			char s[PIDLEN];

			int id;
			//printf("%s\n",cmd);
			pch = strtok (cmd,";");
			id = atoi(pch);
			pch = strtok (NULL,";");
			//printf("%d\n",id);
			if(pch!=NULL){
				strcpy(IP,pch);
				pch = strtok (NULL,";");
				if(pch!=NULL){
					strcpy(port_d,pch);
					index=search_id(id);
					if(index!=-1){
						/*This peer has crashed*/
						/*deal with crash*/
						/*1. remove old information*/
						printf("Peer crashed\n");
						clnt_rm(index);
						/*2. Add new info*/
						add_clnt(IP, port_d, id);
					}else{
						printf("Add new Peer\n");
						add_clnt(IP, port_d, id);
					}
					/*then reply with pid*/
					sprintf(s,"%d",My_pid);
					if (sendto(sfd, s, strlen(s) , 0 , (struct sockaddr *) &dst, slen)==-1)
					{
						die("sendto()");
					}
				}
			}
		}else if(buf[0]==UPDATELIST){
			/*update*/
			printf("UPDATELIST\n");
			int count, id, checksum,index;
			char filename[FILENAME];
			char *pch;
			pch = strtok (cmd,";");
			id=atoi(pch);
			/*check id*/
			index=search_id(id);
			if(index!=-1){
				pch = strtok (NULL,";");
				if(pch!=NULL){
					count = atoi(pch);
					for(i=0;i<count;i++){
						pch = strtok (NULL,";");
						if(pch!=NULL){
							strcpy(filename,pch);
							pch = strtok (NULL,";");
							if(pch!=NULL){
								checksum=atoi(pch);

								/*Add or update file*/
								add_file(filename, checksum, id);
							}
						}
					}
					print_file_list();
				}
			}else{
				printf("received UPDATELIST request from peer %d, which is not on my list\n",id);
			}

		}else if(buf[0]==FIND){
			/*find file*/
			printf("FIND\n");
			int id, index_c;
			char filename[FILENAME],msg[BUFLEN],loc[LOCLEN+3];
			char *pch;
			pch = strtok (cmd,";");
			id=atoi(pch);
			/*chech id*/
			index=search_id(id);
			if(index!=-1){
				pch = strtok (NULL,";");
				if(pch!=NULL){
					strcpy(filename,pch);

					/*Build a message to reply*/
					index = search_file(filename);
					if(index!=-1){
						memset(msg,'\0', BUFLEN);
						sprintf(msg,"%d;%d;",MyFileList[index].checksum,MyFileList[index].copy_num);
						for(i=0;i<MyFileList[index].copy_num;i++){
							index_c =search_id(MyFileList[index].loc_list[i]);
							sprintf(loc,"%d;%s;%s;",MyClntList[index_c].id,MyClntList[index_c].IPaddr,MyClntList[index_c].port_d);
							strcat(msg,loc);
						}
						printf("Sending msg : %s\n",msg);
						if (sendto(sfd, msg, strlen(msg) , 0 , (struct sockaddr *) &dst, slen)==-1)
						{
							die("sendto()");
						}
					}else{
						printf("Cannot find the file\n");
						memset(msg,'\0', BUFLEN);
						msg[0]=FIND;
						printf("%s(%d)\n",msg,strlen(msg));
						if (sendto(sfd, msg, strlen(msg) , 0 , (struct sockaddr *) &dst, slen)==-1)
						{
							die("sendto()");
						}
					}
				}
			}else{
				printf("received FIND request from peer %d, which is not on my list\n",id);
			}


		}else if(buf[0]==REPORTPEERDOWN){
			printf("REPORTPEERDOWN\n");
			int id;
			char *pch;
			pch = strtok (cmd,";");
			id=atoi(pch);
			/*check id*/
			index=search_id(id);
			if(index!=-1){
				printf("received crash report of peer %d, which is not on my list\n",id);
			}else{
				/*kick its ass*/
				clnt_rm(index);
				printf("Peer %d removed\n",MyClntList[i].id);
			}



		}

	}

}