void list_push_back_o(list_t *l, list_base_t x) { l->buffer[ l->end ] = x; l->end++; if ( l->end >= l->sz ) l->end = 0; if (!list_full(l)) ++(l->ct); return; }
void list_push_front_o(list_t *l, list_base_t x) { if ( !list_full(l) ) { l->ct++; } l->first--; if( l->first >= l->sz ) l->first = l->sz - 1; l->buffer[ l->first ] = x; }
list_error_t list_push_back(list_t *l, list_base_t x) { if ( unlikely( list_full(l) ) ){ LIST_ERROR(); return -1; } l->ct++; l->buffer[ l->end ] = x; l->end++; if ( l->end >= l->sz ) l->end = 0; return 0; }
list_error_t list_push_front(list_t *l, list_base_t x) { if ( unlikely( list_full(l) ) ){ LIST_ERROR(); return -1; } l->ct++; l->first--; if ( l->first >= l->sz ) // if overflowed l->first = l->sz - 1; l->buffer[ l->first ] = x; return 0; }
/*Recieves the client request and addes it to the queue * Input: * int client_fd : specifies the connection identifier * */ void server_response (int client_fd, thread_pool_t *pool_obj, C_Request* c_request) { char* buffer; char* data; int len = 0; int ret = 0; long res = 0; int buf_ptr = 0; buffer = (char*) calloc (1,1024); ret = read (client_fd, buffer, 1024); printf ("Recieved bytes %d\n", ret); if(ret > 0) { /* Read the footer. */ memcpy (&res, (buffer + (ret - sizeof (int)) ), sizeof (int)); /* Checking the message is valid or not .*/ if (res != 0xbaadf00d) { goto out; } /* Read the length of the message */ memcpy (&len, buffer, sizeof (int)); buf_ptr += sizeof (int); /* If valid message read the type of the request. */ memcpy (&res, buffer + buf_ptr, sizeof (int)); buf_ptr += sizeof (int); switch (res) { case file_request: /* Process the file request */ c_request -> type = file_request; c_request -> buf = (char*) calloc (1, ret - buf_ptr); memcpy (c_request -> buf, buffer + buf_ptr, ret - buf_ptr) ; c_request -> client_fd = client_fd; c_request -> operation = (void*) file_handler; break; case write_request: /* Process the write request */ c_request -> type = write_request; c_request -> buf = buffer; c_request -> client_fd = client_fd; c_request -> operation = (void*) write_handler; break; case open_file_request: /* Process the open_file_request */ c_request -> type = open_file_request; c_request -> buf = (char*) calloc (1, ret - buf_ptr); memcpy (c_request -> buf, buffer + buf_ptr, ret - buf_ptr) ; c_request -> client_fd = client_fd; c_request -> operation = (void*) open_file_handler; break; case close_file_request: /* Process the close file request */ c_request -> type = close_file_request; c_request -> buf = (char*) calloc (1, ret - buf_ptr); memcpy (c_request -> buf, buffer + buf_ptr, ret - buf_ptr); c_request -> client_fd = client_fd; c_request -> operation = (void*) close_file_handler; break; case read_in_chunks: /* Process the read in chunks from the file * and send it back to the client */ c_request -> type = read_in_chunks; c_request -> buf = (char*) calloc (1, ret - buf_ptr); memcpy (c_request -> buf, buffer + buf_ptr, ret - buf_ptr); c_request -> client_fd = client_fd; c_request -> operation = (void*) read_chunks_handler; break; } /* Check whether the list is full or not * if list is full then send feedback to client that * client is full otherwise the request is added to the * queue * */ data_read: if (list_full (pool -> list) == 1) { /* Clears the buffer */ memset (buffer, 0, 1024); res = 0; /* Gives the length of the response. */ len = calc_response_queue_full (); /* Allocate the memory */ buffer = (char*) calloc (1, len + 4); /* Copy the length of the request. */ memcpy (buffer, &len, sizeof (int)); res += sizeof (int); /*Copy the type of the response */ len = fail_response; memcpy (buffer + res, &len, sizeof (int)); res += sizeof (int); /* Copy the response */ len = queue_ful; memcpy (buffer + res, &len, sizeof (int)); res +=sizeof (int); /* Send the response to the client */ ret = write (client_fd, data, res); if (ret < 0) { printf ("\tSome error ocurred\n"); goto data_read; } goto out; } /*Calls the function to add the client_request to the * list */ pool_obj -> list -> first_node = insert_into_list (pool_obj -> list, c_request); printf ("\tClient : %d's request is inserted\n", client_fd); pthread_mutex_lock (& (pool_obj -> q_lock)); pthread_cond_broadcast (& pool_obj -> cond_t); pthread_mutex_unlock (& (pool_obj -> q_lock)); } out: return ; }