Skip to content
Snippets Groups Projects
Commit 22b800d8 authored by Abhisha Nirmalathas's avatar Abhisha Nirmalathas
Browse files

cooklio

parent e7a73d37
No related branches found
No related tags found
No related merge requests found
......@@ -139,7 +139,8 @@ void move_to_front(Bucket *bucket, int index){
/************************************************************************/
char* hash_table_get(HashTable *table, char *key) {
/*Checks if key in hash table and returns value if present*/
char* freq = malloc(60*sizeof(char));
char* freq = calloc(60,sizeof(char));
assert(freq);
assert(table != NULL);
assert(key != NULL);
......@@ -149,7 +150,7 @@ char* hash_table_get(HashTable *table, char *key) {
Bucket *bucket = &table->buckets[hash_value];
for (int i=0; i < bucket->n_elems; i++){
if (equal(bucket->keys[i], key)){
freq = bucket->values[i];
memcpy(freq, bucket->values[i], strlen(bucket->values[i]));
move_to_front(bucket, i);
break;
}
......@@ -175,7 +176,8 @@ bool hash_table_has(HashTable *table, char *key) {
return false;
}
char* print_hash_map(HashTable *table){
char* headers = malloc(100*sizeof(char));
char* headers = calloc(100,sizeof(char));
assert(headers);
for (int i = 0; i < table->size; i++) {
if (table->buckets[i].n_elems > 0){
for (int j=0; j< table->buckets[i].n_elems; j++){
......@@ -187,3 +189,19 @@ char* print_hash_map(HashTable *table){
}
return headers;
}
void free_cookie(HashTable *headers) {
/*Checks if key in hash table and returns value if present*/
assert(headers != NULL);
int hash_value = xor_hash("Set-cookie: ", headers->size);
// look for existing key
Bucket *bucket = &headers->buckets[hash_value];
for (int i=0; i < bucket->n_elems; i++){
if (equal(bucket->keys[i], "Set-cookie: ")){
free(bucket->values[i]);
}
}
}
\ No newline at end of file
......@@ -24,3 +24,5 @@ char* hash_table_get(HashTable *table, char *key);
bool hash_table_has(HashTable *table, char *key);
char* print_hash_map(HashTable *table);
void free_cookie(HashTable *headers);
\ No newline at end of file
No preview for this file type
No preview for this file type
......@@ -27,8 +27,8 @@ Response* initialise_session(Request* request){
resp->version = request->version;
resp->phrase = "OK";
resp->header = new_hash_table(2);
char *cook = cookie_generator();
hash_table_put(resp->header, "Set-cookie: ", cook);
char *cookie = cookie_generator();
hash_table_put(resp->header, "Set-cookie: ", cookie);
resp->body="";
return resp;
}
......@@ -44,14 +44,17 @@ char* parse_response(Response* response){
strcat(response_string, " ");
strcat(response_string, response->phrase);
strcat(response_string, "\r\n");
strcat(response_string, print_hash_map(response->header));
char* headers = print_hash_map(response->header);
memcpy(response_string, headers, strlen(headers));
strcat(response_string, "\r\n");
strcat(response_string, response->body);
free(headers);
return response_string;
}
void free_response(Response* resp){
free_cookie(resp->header);
free_hash_table(resp->header);
free(resp);
}
......@@ -17,5 +17,6 @@ char* parse_response(Response* response);
char* cookie_generator();
void free_response(Response* resp);
Response* initialise_session(Request* request);
\ No newline at end of file
No preview for this file type
......@@ -22,8 +22,14 @@
#include <unistd.h>
#include "http-parser.h"
#include "http-response.h"
#include "hashtable.h"
#include "user.h"
#define ROUND_1 3
#define ROUND_2 4
#define ROUND_3 1
#define ROUND_4 2
// constants
static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\
Content-Type: text/html\r\n\
......@@ -33,6 +39,8 @@ static int const HTTP_400_LENGTH = 47;
static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
static int const HTTP_404_LENGTH = 45;
char *substring(char *string, int position, int length)
{
char *p;
......@@ -287,8 +295,13 @@ static bool handle_http_request(int sockfd, User_list *users)
// char* resp_string = parse_response(resp);
// printf("COOKIE CREATING RESP %s\n", resp_string);
// player_session(buff, sockfd, "1_welcome.html", resp_string);
// User* new_player = new_user(sockfd);
// add_user(new_player, users);
// char* cookie = hash_table_get(resp->header, "Set-cookie: ")+13;
// cookie = strtok(cookie, ";");
// printf("the cookie token is %s*****\n", cookie);
// free(resp_string);
// free(resp);
// free_response(resp);
User* new_player = new_user(sockfd);
add_user(new_player, users);
get_request(buff,sockfd, "1_welcome.html");
......
No preview for this file type
No preview for this file type
......@@ -18,6 +18,7 @@ User* new_user(int id){
user->n_keywords = 0;
user->status = WAIT;
user->keywords = calloc(user->n_capacity,sizeof(char*));
user->round = 0;
assert(user->keywords);
return user;
}
......@@ -108,6 +109,20 @@ void change_player_status(int user_id, User_list* users, STATUS status){
}
}
void change_player_round(int user_id, User_list* users){
for(int i=0; i < users->n_users; i++){
if (users->users[i]->id == user_id){
if (users->users[i]->round < 4){
users->users[i]->round++;
}
else{
users->users[i]->round = 1;
}
}
}
}
bool keyword_match(User* user, char* keyword){
for(int i=0; i <user->n_keywords;i++){
printf("words being matched is with %s and %s\n", keyword, user->keywords[i]);
......
......@@ -15,6 +15,7 @@ typedef struct User {
int n_keywords;
STATUS status;
int n_capacity;
int round;
}User;
typedef struct User_list{
......@@ -42,6 +43,8 @@ bool players_ready(User_list* users);
void change_player_status(int user_id, User_list* users, STATUS status);
void change_player_round(int user_id, User_list* users);
bool keyword_match(User* user, char* keyword);
void reset_players(User_list *users);
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment