static void do_server_request(struct server *sv, int connfd) { int ret; struct request *rq; struct file_data *data; data = file_data_init(); rq = request_init(connfd, data); if (!rq) { file_data_free(data); return; } if(cache_active ==1){ pthread_mutex_lock( &conditionMutex); struct hash_node *cache_stored; cache_stored= cache_lookup(data->file_name); if(cache_stored!=NULL){ // check if it populated in the cache{ data->file_buf = Malloc(cache_stored->data->file_size); strncpy(data->file_buf, cache_stored->data->file_buf, cache_stored->data->file_size); data->file_size =cache_stored->data->file_size; } else{ /* reads file, * fills data->file_buf with the file contents, * data->file_size with file size. */ pthread_mutex_unlock( &conditionMutex ); ret = request_readfile(rq); pthread_mutex_lock( &conditionMutex ); // need to renew the cache if(data->file_size<max_cache){ if(data->file_size <=(max_cache-cache_current_size) ){ cache_insert(data); } else{ cache_evict(data->file_size); cache_insert(data); } } } pthread_mutex_unlock( &conditionMutex ); } else{ ret = request_readfile(rq); if (!ret) goto out; } /* sends file to client */ request_sendfile(rq); out: request_destroy(rq); file_data_free(data); }
static void do_server_request(struct server *sv, int connfd) { //printf("Starting cache_size:%d\n",cache_size); int ret; struct request *rq; struct file_data *data; data = file_data_init(); /* fills data->file_name with name of the file being requested */ rq = request_init(connfd, data); if (!rq) { file_data_free(data); return; } cache* temp; int hash_index = hash(data->file_name); pthread_mutex_lock(&lock2); temp = cache_lookup(data->file_name, hash_index); pthread_mutex_unlock(&lock2); if (temp != NULL) { request_set_data(rq, temp->data); } else { ret = request_readfile(rq); //printf("Request file size is %d\n", data->file_size); if ((data->file_size)>(sv->max_cache_size)) { request_sendfile(rq); goto out; } pthread_mutex_lock(&lock2); if (cache_lookup(data->file_name, hash_index)==NULL) // if it won the race, then cache { if (((sv->max_cache_size)-cache_size) < (data->file_size)) // check available cache size { //printf("Evicting for file size(minimum) of : %d\n",data->file_size); int amount = (data->file_size)-((sv->max_cache_size)-cache_size); cache_evict(amount); } cache_insert(data, hash_index); } pthread_mutex_unlock(&lock2); } if (!ret) goto out; /* sends file to client */ request_sendfile(rq); out: //printf("cache_size:%d\n",cache_size); //printf("END OF REQUEST----------------\n"); request_destroy(rq); //file_data_free(data); }