int ManageLogger::svc()
{
	int wait_index = 0;
	ACE_Time_Value sleep_time(0, 1 * 1000);
	LoggerCtn_t logger_ctn;
	LoggerSet_t logger_flush_set;

	while (!m_stop)
	{
		logger_ctn.clear();

		// get the copy of logger content
		{
			ACE_GUARD_RETURN(ACE_Thread_Mutex, guard, m_logger_mutex, -1);
			if (!m_logger_ctn.empty())
			{
				std::copy(m_logger_ctn.begin(), m_logger_ctn.end(), std::back_inserter(logger_ctn));
				m_logger_ctn.clear();
			}
		}

		// if logger is empty, continue
		if (logger_ctn.empty())
		{
			{
				ACE_GUARD_RETURN(ACE_Thread_Mutex, guard, m_logger_flush_set_mutex, -1);
				if (m_logger_flush_set.size() > 0)
				{
					for (LoggerSet_t::iterator it = m_logger_flush_set.begin(); it != m_logger_flush_set.end(); ++it)
					{
						logger_flush_set.insert(*it);
					}
				}
			}

			if (m_need_flush)
			{
				m_need_flush = false;
				flushLogger(logger_flush_set);
			}
			else
			{
				ACE_OS::sleep(sleep_time);
			}
			continue;
		}

		m_need_flush = true;

		for (LoggerCtn_t::iterator it = logger_ctn.begin(); it != logger_ctn.end(); ++it)
		{
			doLogging(it->first, it->second);
		}
	}

	m_wait = false;

	return 0;
}
Beispiel #2
0
void *processRequest(void *thrID){
	int *id = (int *) thrID;
	pthread_setspecific(tName, (void *) id);

	int cSockfd;
/*	char *cWDirectory= getcwd(NULL,0);//current working directory
	char *fpath = (char *) malloc(len * sizeof(char));
	strcpy(fpath, cWDirectory);
	strcat(fpath, );*/
	int e;
	char line[1024];
	FILE *fpntr;

	while (1) {
		struct requestData *req = removeElementFromQueue(schedQueue);
		cSockfd= req->socketID;
		int rStatus = req->reqStatus;
		if (rStatus == 404){
				write(cSockfd,"HTTP/1.0 404 Not Found\r\n",25);
				write(cSockfd,"Content-Type: text/html\r\n",25);
				write(cSockfd,"\n\n",3);				
				write(cSockfd,"Resource Not Found 404",22);
				close(cSockfd);
				
		}
		else if(rStatus==200){
		if(strcmp(req->methodType,"GET")==0){
		if(strcmp(req->fileType,"directory")!=0){

		//printf("%s\n",req->absPath);
		fpntr=fopen(req->absPath,"r");
		char *contentType="text/plain";
		char *cTime;
		if(strcmp(req->fileType,"html")==0)
		contentType="text/html";
		else if(strcmp(req->fileType,"jpeg")==0)
		contentType="image/jpeg";
		else if(strcmp(req->fileType,"gif")==0)
		contentType="image/gif";
		fseek(fpntr, 0, SEEK_END); // seek to end of file
		char si[20];
		int size = ftell(fpntr);
		rewind(fpntr);
		sprintf(si,"%d",size);	
		write(cSockfd, "HTTP/1.0 200 OK\n",16);
		write(cSockfd,"Connection: close\n",19);
		write(cSockfd, "Date: ",7);
		cTime=getGMTTime();
		write(cSockfd, cTime,strlen(cTime));
		write(cSockfd, "Server: ",9);
		write(cSockfd,"myhttpd\n",8);
		write(cSockfd,"Last-Modified: ",14);
		write(cSockfd,req->lastModified,strlen(req->lastModified));
		write(cSockfd,"Content-Length: ",17);
		write(cSockfd,si,strlen(si));
		write(cSockfd, "\nContent-Type: ",16);
		write(cSockfd, contentType,strlen(contentType));
		write(cSockfd, "\n\n",3);

		while (fgets(line,1024,fpntr)!=NULL)
		write(cSockfd,line,strlen(line));
		close(cSockfd);
		}
		else if(strcmp(req->fileType,"directory")==0){

		int pathLength = strlen(req->absPath) + strlen("index.html") + 1;
		char *indexFPath = (char *) malloc(pathLength * sizeof(char));
		strcpy(indexFPath,req->absPath);
		strcat(indexFPath,"index.html");
		FILE *fp= fopen(indexFPath,"r");

		//struct stat fS= fstat(fp,&fS);
		if(fp==NULL){

		chdir(req->absPath);
		FILE *fpntr= popen("ls -lrt","r");
		chdir(cwDirectory);
				
		//fseek(fpntr, 0, SEEK_END); // seek to end of file
		int size=0 ;//= ftell(fpntr);

		//rewind(fpntr);
		
		while (fgets(line,1024,fpntr)){
		size+=strlen(line);
		write(cSockfd,line,strlen(line));
		}
		req->fileSize=size;
		close(cSockfd);

		}
		else{
		fseek(fp, 0, SEEK_END); // seek to end of file
		char si[20];
		int size = ftell(fp);
//		printf("size=%d\n",size);
		rewind(fp);
		sprintf(si,"%d",size);	
		req->fileSize=size;
		char *contentType="text/html";
		char *cTime;
		write(cSockfd, "HTTP/1.0 200 OK\n",16);
		write(cSockfd,"Connection: close\n",19);
		write(cSockfd, "Date: ",7);
		cTime=getGMTTime();
		write(cSockfd, cTime,strlen(cTime));
		write(cSockfd, "Server: ",9);
		write(cSockfd,"myhttpd\n",8);
		write(cSockfd,"Last-Modified: ",15);
		write(cSockfd,req->lastModified,strlen(req->lastModified));
//		fseek(fp, 0, SEEK_END); // seek to end of file
//		char si[20];
//		int size = ftell(fp);
//		rewind(fp);
//		sprintf(si,"%d",size);	
		write(cSockfd,"Content-Length: ",17);
		write(cSockfd,si,strlen(si));
		write(cSockfd, "\nContent-type: ",16);
		write(cSockfd, contentType,strlen(contentType));
		write(cSockfd, "\n\n",3);
		while (fgets(line,1024,fp))
		write(cSockfd,line,strlen(line));
		close(cSockfd);
	}
		}
		}
	}
		if(strcmp(req->methodType,"HEAD")==0){
		
		char *contentType="text/plain";
		char *cTime;
		if(strcmp(req->fileType,"html")==0)
		contentType="text/html";
		else if(strcmp(req->fileType,"jpeg")==0)
		contentType="image/jpeg";
		else if(strcmp(req->fileType,"gif")==0)
		contentType="image/gif";

		write(cSockfd, "HTTP/1.0 200 OK\n",16);
		write(cSockfd, "Date: ",7);
		cTime=getGMTTime();
		write(cSockfd, cTime,strlen(cTime));
		write(cSockfd, "Server: ",8);
		write(cSockfd,"myhttpd",7);
		write(cSockfd,"\n",2);
		write(cSockfd,"Last-Modified:",14);
		write(cSockfd,req->lastModified,strlen(req->lastModified));
		write(cSockfd, "Content-type:",13);
		write(cSockfd, contentType,strlen(contentType));
		write(cSockfd, "\n\n",3);
		close(cSockfd);

		}

		doLogging(req);

	}

}