Beispiel #1
0
void
app_shutdown(void)
{
    run_application_hook(AH_SHUTDOWN);
    log_tags_deinit();
    stats_destroy();
    dns_cache_destroy();
    child_manager_deinit();
    g_list_foreach(application_hooks, (GFunc) g_free, NULL);
    g_list_free(application_hooks);
    msg_deinit();
}
int main()
{
	char url[100] = {0};
	dns_cache_init();
	
	sprintf(url, "abc.com");
	dns_check_cache(url, strlen(url));
	sprintf(url, "def.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "ghi.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "jkl.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "mno.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "pqr.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "stu.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "wxy.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "google.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "itm.com");
        dns_check_cache(url, strlen(url));
	sprintf(url, "microsoft.com");
        dns_check_cache(url, strlen(url));			
	
	dns_dump_cache();

	dns_check_cache(url, strlen(url));
	sprintf(url, "mno.com");
        dns_check_cache(url, strlen(url));
	dns_dump_cache();
	
	dns_cache_destroy();
}
/*
Description: 	Main entry of the program. Calculate each thread's share. 
		Spawn thread and in end, consolidate all the file in one out put file.
Input: 		Standard inputs;
OutPut: 	0 for error, 1 for success.
*/
int main(int argc, char* argv[])
{
	/*local variables*/
        unsigned int numThread=0;
        FILE *fOutFile = NULL, *fThreadFile = NULL;
        struct stat stStat={0};
        int fqdnFileSize=0;
        int threadCount=0, rval=0;
	char data[65535] = {0}, threadFile[MAX_FILE_NAME]={0};
        

	//check if we have got all the required parameters.
	if (argc != 4) {
		//User is playing with us, lets talk to him :)
                printf("Help: ./DNSClient.o \"number-of-thread\" \"input-FQDN-filename\" \"output-filename\"\n");
                goto error;
        }

	//get the number of thread, and check if it is under limit.
        numThread = (int) atoi(argv[1]);
        if (numThread > 100) {
                printf("Warning: Maximum 100 threads are allowed. Number reduced to 100\n");
		numThread = 100;
        }
	//get the input fqdn file name.
	if (strlen(argv[2]) > MAX_FILE_NAME) {
		printf("Error: File name legth exceeded the maximum allowed limit %d:%d\n", strlen(argv[2]), MAX_FILE_NAME);
		goto error;
	}
	//get the output file name.
	if (strlen(argv[3]) > MAX_FILE_NAME) {
                printf("Error: File name legth exceeded the maximum allowed limit %d:%d\n", strlen(argv[3]), MAX_FILE_NAME);
                goto error;
        }


	sprintf(dnsFileName, "%s", argv[2]);
	//get the stat of the input file, to get the size.
        stat(argv[2], &stStat);
        fqdnFileSize = stStat.st_size;
        if (fqdnFileSize == 0) {
                printf("Error: Empty file\n");
                goto error;
        }

	//if number of thread are more than number of bytes available in input FQDN file.
	//then reinit the number of threads to one less than the size. so that each thread will
	//get atleast 1 byte to read.
	if (numThread >= fqdnFileSize)
		numThread = fqdnFileSize-1;

	//calculate each thread's segment.
        eachThreadShare = fqdnFileSize/numThread;
	//add one more thread of the remainder of the file.
        numThread += (fqdnFileSize%numThread)? 1:0;

	if (DNS_CLIENT_DEBUG) {
		printf("DNS_DEBUG: number of threads:%d, thread share %d bytes\n", numThread, eachThreadShare); 
	}
	//init base to fire the rocket.
        dns_init_server();
	dns_cache_init();
	pthread_mutex_init(&dns_mutex, NULL);

	//fire the threads.
        for(threadCount=0; threadCount<numThread; threadCount++) {
                rval = pthread_create(&allThread[threadCount], NULL, (void *) &DNSClientThreadFucntion, (void*)(threadCount+1));
                if (rval) {
                        printf("Error: Could not spawn all the thread(s). Return value %d\n", rval);
                        goto error;
                }
        }
	//join, wait till all the thead finishes.
        for(threadCount=0; threadCount<numThread; threadCount++) {
                rval = pthread_join(allThread[threadCount], NULL);
                if (rval) {
                        printf("Error: Could not join all the thread(s). Return value %d\n", rval);
                        goto error;
                }

        }
        printf("\nDone.\n");
	dns_cache_dump();
	//create output file.
	fOutFile = fopen(argv[3], "w+");
	if (NULL==fOutFile) {
		printf("Error:Not able to open %s\n", argv[3]);
		goto error;
	}
	
	//lets go through all the thread's file and read them write in one single output file.
	for(threadCount=1; threadCount<=numThread; threadCount++) {	
		//create thread's file name and open it, 
		sprintf(threadFile, "thread%d", threadCount);
		stat(threadFile, &stStat);
		fThreadFile = fopen(threadFile, "r");
		if (DNS_CLIENT_DEBUG) {
                	printf("DNS_DEBUG: Reading %s\n", threadFile);
        	}
		//start reading and witing chenk by chenk.
		do{
			//first read big chenks of 65000 bytes
			if(stStat.st_size > 65000) {
				fread(data, 65000, 1 , fThreadFile);
				fwrite(data,65000, 1,  fOutFile);
				stStat.st_size -= 65000;
			} else {
				//read the last leftover chunk which is less than 65000
				fread(data, stStat.st_size, 1 , fThreadFile);
                                fwrite(data,stStat.st_size, 1,  fOutFile);
                                stStat.st_size -= stStat.st_size;
			}
		}while(stStat.st_size);

		//flush all the file pointer and close it.
		fflush(fThreadFile);
		fclose(fThreadFile);		
		//unlink the inode of thread file, so that it would be deleted form file system.
		unlink(threadFile);
	}	
	
	//This function has only one return point, i.e. this section.
	error:
	pthread_mutex_destroy(&dns_mutex);
	dns_cache_destroy();
	if (fOutFile){
		fflush(fOutFile);
		fclose(fOutFile);
	}
	return 1;
}