-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
46 lines (40 loc) · 1.2 KB
/
server.c
File metadata and controls
46 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* Copyright 2023 Razvan-Constantin Rus */
#include "server.h"
server_memory *init_server_memory()
{
// Server allocation
server_memory *s = (server_memory *)malloc(sizeof(server_memory));
DIE(s == NULL, "Server allocation failed\n");
// Server hashtable allocation
s->memory = ht_create(SERVER_H, hash_function_string,
compare_function_strings,
key_val_free_function);
DIE(s->memory == NULL, "Server memory allocation failed\n");
return s;
}
void server_store(server_memory *server, char *key, char *value) {
// In order to store the data we are using hashtable a function
if (server)
ht_put(server->memory, key, KEY_LENGTH, value, VALUE_LENGTH);
}
char *server_retrieve(server_memory *server, char *key) {
// In order to store the data we are using hashtable a function
if (server)
return ht_get(server->memory, key);
return NULL;
}
void server_remove(server_memory *server, char *key) {
// In order to obtain the data we are using hashtable a function
if (server)
ht_remove_entry(server->memory, key);
}
void free_server_memory(server_memory *server) {
if (server) {
// Frees the hashtable
ht_free(server->memory);
// Frees the server
free(server);
}
// Update value
server = NULL;
}