示例#1
0
文件: spellcheck.c 项目: causes/bloom
/**
 * Ad-hoc command-line spell checker
 */
int main(int argc, char *argv[])
{
    // Open the dictionary file
    FILE *fp; 
    if (!(fp = fopen("dictionary", "r"))) {
        fprintf(stderr, "E: Couldn't open words file\n");
        fflush (stderr);
        return 1;
    }

    // Create a bloom filter
    bloom_t *filter = bloom_filter_new(2500000);

    // Add all dictionary words to the filter
    char *p;
    char line[1024];
    while (fgets(line, 1024, fp)) {
        strip(line);
        bloom_filter_add(filter, line);
    }
    fclose(fp);
    printf("bloom filter count : %u\n", bloom_filter_count(filter));
    printf("bloom filter size  : %u\n", bloom_filter_size(filter));

    // Read words from stdin and print those words not in the bloom filter
    while (fgets(line, 1024, stdin)) {
        strip(line);
        p = strtok(line, " \t,.;:\r\n?!-/()");
        while (p) {
            if (!bloom_filter_contains(filter, p)) {
                printf("%s\n", p);
            }
            p = strtok(NULL, " \t,.;:\r\n?!-/()");
        }
    }

    // Cleanup
    bloom_filter_free(filter);
    return 0;
}
示例#2
0
// core routine: recv the chunk and act by type
void recv_chunk(int sockfd){
	ssize_t		n, rlen;
	char		buf[MAXLINE];
	char *res = "data recv done";
	FILE *fs = NULL; // for store
	FILE *configfile = NULL;
	char fp[FP_LEN+1];
	char filename[MAXLINE] = {0};
	fp[FP_LEN] = '\0';
	struct sockaddr_in	cliaddr;
	socklen_t	cliaddrlen = sizeof(cliaddr);

	memset(buf, 0, sizeof(buf));
	TransferUnit *header = (TransferUnit *)malloc(sizeof(TransferUnit));
	int headerlen = sizeof(TransferUnit);
	
	while(1){
		memset(header, 0, headerlen);
		rlen = Readn(sockfd, header, headerlen);
		// printf("header->type = %d, header->len = %d\n", header->type ,header->len);

		if(rlen == 0 ){// No data now  continue
			// printf("%d===========\n", headerlen);
			printf("Client Closed!\n");
			break;
		}

		if(rlen != headerlen){
			printf("Read Chunk Header Wrong!\n");
			exit(-1);
		}
		//FIXME: use switch
		if(header->type ==  FILE_FIRST_PACKET){ // file first pakcet (is fp), Definitely new File

			// struct timeval tv1, tv2;
			// gettimeofday(&tv1, NULL);

			rlen = Readn(sockfd, buf, header->len);
			char *fp = buf;
			// printf("File FP: %s\n", buf);

			//--------------	update index -----------//
			// NO cache!!!
			update_fp(fp); // persist to disk
			// printf("bloom_filter_add %s\n", fp);
			if(bloom_on)
				bloom_filter_add(filter, fp); // 

			//--------------	create file for store -----------//
			char path[MAXLINE];
			path[0] = '\0';
			strcat(path, "store/");
			strcat(path, fp);
			// printf("%s\n", path);
			make_storage_dir("store");
			fs = fopen(path, "wb"); // open for write store this file 
			if(!fs){
				printf("fopen error, %s\n", strerror(errno));
				exit(-1);
			}

			// gettimeofday(&tv2, NULL);
			// double time_ms = (tv2.tv_sec-tv1.tv_sec)*1000.0+(tv2.tv_usec-tv1.tv_usec)/1000.0;
			// printf("time of first packet handle: %f", time_ms);

		}else if(header->type == FILE_DATA_PACKET){  // file data chunk
			rlen = Readn(sockfd, buf, header->len);
			fwrite(buf, sizeof(char), rlen, fs);

		}else if(header->type == FILE_EOF_PACKET){ // file EOF
			rlen = Readn(sockfd, buf, header->len);
			fflush(fs);
			fclose(fs);

		}else if(header->type == FILE_FP_QUERY){ // fp query
			// struct timeval tv1, tv2;
			// gettimeofday(&tv1, NULL);

			rlen = Readn(sockfd, buf, header->len);
			// If this file exists by fingerprint
			// Now use a hash table TODO
			int exists = isExistsFile(buf);

			// gettimeofday(&tv2, NULL);
			// double time_ms = (tv2.tv_sec-tv1.tv_sec)*1000.0+(tv2.tv_usec-tv1.tv_usec)/1000.0;
			// printf("time of fp query handle: %f", time_ms);

			char *msg = "new";
			if(exists)
				msg = "duplicated";
			Write(sockfd, msg, strlen(msg)); // Reply to Client
		}else if(header->type == START_BACKUP){ // start backup
			char store_dir[MAXLINE];
			rlen = Readn(sockfd, buf, header->len);
			if (!(fpfile_latest = fopen("/tmp/fp.latest.out", "w"))) {
        		fprintf(stderr, "E: Couldn't open file for write latest fp\n");
        		fflush (stderr);
        		exit(-1);
    		}
    		/*
    		// FIXME: mkdir -p XX, 
    		strcpy(store_dir, "mkdir -p ");
    		strcat(store_dir, STORE_PREFIX);
    		strcat(store_dir, buf);
    		printf("Backup(%s) starting...\n", store_dir);
    		system(store_dir);
    		*/
		}else if(header->type == END_BACKUP){ //end backup
			rlen = Readn(sockfd, buf, header->len);
			printf("Backup Complete!\n");
			if(sdna_on){
				sync_bloom_to_pox();
			}
		}else if(header->type == BACKUP_CONFIG_FILE_START){
			rlen = Readn(sockfd, buf, header->len);
			char *dir = buf;

			char path[MAXLINE];
			path[0] = '\0';
			strcat(path, "store/");
			strcat(path, dir); //FIXME: dir not contain '/'
			strcat(path, "_config_file");

			configfile = fopen(path, "wb"); // open for write store this file 
			if(!configfile){
				printf("fopen error, %s\n", strerror(errno));
				exit(-1);
			}
		}else if(header->type == BACKUP_CONFIG_FILE){
			rlen = Readn(sockfd, buf, header->len);
			fwrite(buf, sizeof(char), rlen, configfile);

		}else if(header->type == BACKUP_CONFIG_FILE_END){
			rlen = Readn(sockfd, buf, header->len);
			fflush(configfile);
			fclose(configfile);
		}
		memset(buf, 0, sizeof(buf));
	}

	close(sockfd);
}