Example #1
0
// Generate a header event
bool
WriteUserLogHeader::GenerateEvent( GenericEvent &event )
{
	int len = snprintf( event.info, COUNTOF(event.info),
			  "Global JobLog:"
			  " ctime=%d"
			  " id=%s"
			  " sequence=%d"
			  " size=" FILESIZE_T_FORMAT""
			  " events=%" PRId64""
			  " offset=" FILESIZE_T_FORMAT""
			  " event_off=%" PRId64""
			  " max_rotation=%d"
			  " creator_name=<%s>",
			  (int) getCtime(),
			  getId().Value(),
			  getSequence(),
			  getSize(),
			  getNumEvents(),
			  getFileOffset(),
			  getEventOffset(),
			  getMaxRotation(),
			  getCreatorName().Value()
			  );
	if (len < 0 || len == sizeof(event.info)) {
		// not enough room in the buffer
		len = (int)COUNTOF(event.info)-1;
		event.info[len] = 0; // make sure it's null terminated.
		::dprintf( D_FULLDEBUG, "Generated (truncated) log header: '%s'\n", event.info );
	}  else {
		::dprintf( D_FULLDEBUG, "Generated log header: '%s'\n", event.info );
		while( len < 256 ) {
			event.info[len++] = ' ';
			event.info[len] = 0;
		}
	}

	return true;
}
Example #2
0
//thread routine function
void* proxyroutine(void* arg)
{
	int fd;
	int nfds;
	int sockfd = (int)arg;	
	int n=0;	
	struct sockaddr_in cli_addr;
	
	socklen_t clilen;// return addressstructure size

	fd_set rfds;
	fd_set afds;
	struct timeval tv;
	FD_ZERO(&afds);
	FD_SET(sockfd,&afds);
	nfds = getdtablesize();
	char buffer[CLIBUFF_SIZE]; //receive buffer a

while(1){
	tv.tv_sec = 10;
	tv.tv_usec = 0;
	monitorCache(head);
	memcpy(&rfds,&afds,sizeof(rfds));
	if(select(nfds,&rfds,(fd_set*)0,(fd_set*)0,&tv) <= 0)
		die("selecot()",errno);
	
	if(FD_ISSET(sockfd,&rfds)){	
		int newsockfd;
		clilen = sizeof(cli_addr);
		pthread_mutex_lock(&acceptlock);
		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);//accept a new connection
		pthread_mutex_unlock(&acceptlock);
		if(newsockfd < 0)//if accept is blocked, then return error message
			die("accept()",errno);
		FD_SET(newsockfd,&afds);
	}
	
for(fd=0; fd<nfds; ++fd){
	if(fd != sockfd && FD_ISSET(fd,&rfds)){	
//	printf("server: got connection from %s\n",iota(cli_addr.sin_addr));
		//printf("Thread '%ld' responding request..\n",pthread_self());
	bzero(buffer,CLIBUFF_SIZE);// initialize buffer
	n = read(fd,buffer,CLIBUFF_SIZE-1);//from nesockfd read 255byte and store the buffer

	
	if(n<=0) error("can't recv");// if can't read error message
	//printf("Here is the message : %s\n", buffer);

	char* cp_buffer;


	char temp_init[CLIBUFF_SIZE];
	bzero(temp_init,CLIBUFF_SIZE);
	strcpy(temp_init,buffer);	
	//initial method url version
	char * init_request = strtok_r(temp_init,"\r\n",&cp_buffer);
	char* commandp = NULL;
	char* serverp = NULL;
	char* pathp = NULL;
	char * protocolp = NULL;
	char * portp = NULL;


	parse(init_request, &commandp,&serverp,&pathp,&protocolp, &portp);
	
//initial parsed storage;


	int s_portno = 80;
	char* server_host=NULL;
	 
	if(portp != NULL){
		server_host = (char*)malloc(strlen(serverp)+1+strlen(portp)+strlen(pathp)+1); 
		sprintf(server_host,"%s:%s%s",serverp,portp,pathp);
		s_portno = atoi(portp);
	}else{
		server_host = (char*)malloc(strlen(serverp)+strlen(pathp)+1); 
		sprintf(server_host,"%s%s",serverp,pathp);
	}

	char* out1 = replaceAll(cp_buffer,"keep-alive","close");

	char* http_host= malloc(strlen(commandp)+1+strlen(pathp)+strlen(" HTTP/1.0")+strlen(out1)+1);
	sprintf(http_host,"%s %s%s%s",commandp,pathp," HTTP/1.0",out1);
	
	//printf("changed:%s\n",http_host);

	//printf("hostnport:%s\n",server_host);
 
	char *ip = (char*)inet_ntoa((cli_addr.sin_addr));
// send the object
	int transsize = 0;

	DATA* send_object = getByKey(head,server_host);
	if(send_object){
		//printf("sedn:%s\n",send_object->key);
		char* metadata = NULL;//parse_type(pathp);
		char remess[512];
		bzero(remess,512);
	sprintf(remess,"%s%s%d%s%s%s","HTTP/1.1 200 OK\r\nServer: \r\nAccept-Ranges: bytes\r\n","Content-Length: ",send_object->size,"\r\nContent-Type: ",metadata,"; charset=utf-8\r\nConnection: close\r\n\r\n");
		write(fd,remess,strlen(remess));
	//	printf("remes%d\n",strlen(remess));
		transsize +=	write(fd,send_object->object,send_object->size);

	//	printf("====================cache key:%s send===================\n",send_object->key);
		DATA tempD = delBykey(head,server_host);
		add(head,tempD.key,tempD.object,tempD.size);
	}else{
	//printf("server_host:%s\n",server_host);
	transsize = makeCli(s_portno, serverp,pathp,fd,server_host,http_host);
	}
	close(fd);
	printf("tt:%d\n",transsize);

	if(transsize != 0){

/***** ciritical section start ********/
		pthread_mutex_lock(&mylock);
		makelog(getCtime(),ip,server_host,transsize);
		pthread_mutex_unlock(&mylock);
/**** critical section stop ********/
	}
	free(server_host);
	free(out1);
	free(http_host);
	FD_CLR(fd,&afds);
			}
		}
	}
}