-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
52 lines (45 loc) · 1.63 KB
/
server.c
File metadata and controls
52 lines (45 loc) · 1.63 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
47
48
49
50
51
52
/* Copyright 2021 <Tulpan Andrei> */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "/home/student/server.h"
// Initiez memoria pentru replica
server_t* init_server(server_memory *real_server, unsigned int tag) {
server_t *server = malloc(sizeof(server_t));
DIE(!server, "malloc failed!\n");
server->hash_server = real_server; // referinta pentru serverul real
server->tag = tag; // id-ul replicii
server->hash = hash_function_servers(&tag); // hash-ul replicii
return server;
}
// Initiez memoria pentru server
server_memory* init_server_memory() {
server_memory *servers = malloc(sizeof(server_memory));
DIE(!servers, "malloc failed!\n");
// Creez hashtable-ul corespunzator serverului
servers->server_ht =
ht_create(997, hash_function_key, compare_function_strings);
return servers;
}
// Functia pentru stocarea produselor pe server
void server_store(server_memory* server, char* key, char* value) {
// Apelez functia de adaugare in hashtable
ht_put(server->server_ht, key, strlen(key) + 1, value, strlen(value) + 1);
}
// Functia pentru stergerea produselor din hashtable
void server_remove(server_memory* server, char* key) {
// Apelez functia de sterge din hashtable
ht_remove_entry(server->server_ht, key);
}
// Functia de gasirea de returnare a valorii corespunzatoare cheii
char* server_retrieve(server_memory* server, char* key) {
// Apelez functia de gasire in hashtable
return ht_get(server->server_ht, key);
}
// Functia pentru eliberarea memoriei serverului
void free_server_memory(server_memory* server) {
// Eliberez hashtable-ul
ht_free(server->server_ht);
// Eliberez serverul
free(server);
}