Пример #1
0
int main(int argc, char **argv) {
	clock_t start, finish;

	read_data *reads;

	start = clock();
	int reads_cnt = parse_reads_readsim(&reads, argv[1]);
	finish = clock(); 
	printf("Parsing reads: %.4f sec\n", (double) (finish - start) / (double) CLOCKS_PER_SEC);

	reads_sequence reads_seq;

	start = clock();
	int reads_seq_len = generate_reads_sequence(&reads_seq, reads, reads_cnt);
	finish = clock();
	printf("Generating reads sequence: %.4f sec\n", (double) (finish - start) / (double) CLOCKS_PER_SEC);

	int *SA = (int*) malloc(reads_seq_len * sizeof(int));

	start = clock();
	sa_is(SA, reads_seq.sequence, reads_seq_len, reads_cnt + 4);
	finish = clock(); 
	printf("Generating SA: %.4f sec\n", (double) (finish - start) / (double) CLOCKS_PER_SEC);

	int *LCP = (int*) malloc(reads_seq_len * sizeof(int));

	start = clock();
	lcp(LCP, SA, reads_seq.sequence, reads_seq_len);
	finish = clock();
	printf("Generating LCP: %.4f sec\n", (double) (finish - start) / (double) CLOCKS_PER_SEC);

	printf("Checking SA and LCP...\n");

	for (int i = 1; i < reads_seq_len; i++) {
		for (int j = 0; j < LCP[i]; j++) {
			if (reads_seq.sequence[SA[i-1]+j] != reads_seq.sequence[SA[i]+j]) {
				printf("LCP error at position %d.\n", i);
			}
		}

		if (SA[i-1] + LCP[i] < reads_seq_len && SA[i] + LCP[i] < reads_seq_len) {
			if (reads_seq.sequence[SA[i-1]+LCP[i]] >= reads_seq.sequence[SA[i]+LCP[i]]) {
				printf("SA error at positions %d-%d.\n", i - 1, i);
			}
		}
	}

	printf("Done!\n");

	free_read_data(&reads, reads_cnt);

	free_reads_sequence(&reads_seq);

	free(SA);

	free(LCP);

	return 0;
}
Пример #2
0
static void *response_thread(void *args)
{
    pthread_t		thread_id;
    int				has_method, sockfd;
    unsigned int	n;
    ghd_task_t*		task;
    ghd_method		method;
    ghd_request_fields_t	Connection_field;
    volatile p_read_data	received_data = NULL;


	// 注册线程意外退出回调函数
	// 强制类型转换,make gcc happy.
    pthread_cleanup_push((void (*)(void *))pthread_mutex_unlock,
			(void *)&mutex);

    thread_id = pthread_self();
    syslog(LOG_INFO, "Thread ID: %ld", thread_id);
    /* 处理线程就在这个while 里循环, 
     * 等待信号, 有信号就处理,处理完毕继续等待
     */
    while ( 1 ) {
		/* 在使用前一定要确保mutex已初始化 */
		pthread_mutex_lock(&mutex);

		while(task_queue_head == NULL)
			pthread_cond_wait(&cond, &mutex);

		sockfd = task_queue_head->sockfd;
		task = task_queue_head;
		task_queue_head = task_queue_head->next;
		if (task_queue_head == NULL)
			task_queue_tail = NULL;
		geekhttpd_task_free(task);

		pthread_mutex_unlock(&mutex);

		received_data = alloc_read_data();
		if (received_data == NULL) {
			syslog(LOG_INFO, "Cannot alloc memory for received_data");
			goto closefd;
		}

		has_method = 0;
		/* 读取从客户端发来的清求 */
		while (1) {
			/* 重置errno */
			errno = 0;
			if ( received_data->len < MAXHEADERLEN)
				n = read(sockfd, received_data->data + received_data->len,
						MAXHEADERLEN - received_data->len);
			else
				break;

			if (n < 0) {
				if ( errno == EINTR) {
					continue;
				}
				/* 处理有问题的链接 */
				syslog(LOG_INFO, "brower closed connection");
				goto closefd;
			} else if (n == 0) {
				break;
			} else {
				if ( errno == EAGAIN ) 
					break;
				received_data->len += n;
				if (!has_method && received_data->len > 7){
					get_request_method(received_data->data, &method);
					has_method = 1;
				}
			}
		} 

		syslog(LOG_INFO, "[Request] Size %d, Content: %s",  received_data->len, \
			(char *)received_data->data);

		/* 
		 * 根据方法响应请求,向浏览器发送消息和数据
		 */
		switch (method) {
			case GET:
				do_get(sockfd, received_data);
				break;
			case POST:
				do_post(sockfd, received_data);
				break;
			case HEAD:
				do_head(sockfd, received_data);
				break;
			case PUT:
			case DELETE:
			case TRACE:
			case CONNECT:
			case OPTIONS:
				do_not_implement(sockfd);
				break;
			case UNKNOWN:
				/* send 405 */
				do_unknown_method(sockfd);
				break;
			default:
				break;
		}
		/* TODO:
		 * Connection: Keep-Alive 
		 * 没有处理
		 */
		
closefd:
		close(sockfd);
		/* free read_data */
		free_read_data(received_data);
		received_data = NULL;
    }
    pthread_cleanup_pop(0);
}