-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.h
More file actions
170 lines (148 loc) · 4.68 KB
/
server.h
File metadata and controls
170 lines (148 loc) · 4.68 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2024, <>
*/
#ifndef SERVER_H
#define SERVER_H
#include "utils.h"
#include "constants.h"
#include "lru_cache.h"
#include "queue.h"
#define TASK_QUEUE_SIZE 1000
#define MAX_LOG_LENGTH 100
#define MAX_RESPONSE_LENGTH 4096
#define REPLICA_OFFSET 100000
#define MAX_REPLICAS 3
typedef struct server
{
lru_cache *cache;
queue_t *task_queue;
doubly_linked_list_t *local_database;
unsigned int server_id;
unsigned int *server_hash;
unsigned int no_replicas;
unsigned int handler_replica;
unsigned int (*hash_function_docs)(void *);
} server_t;
typedef struct server_data
{
char *name;
char *content;
unsigned int data_hash;
unsigned int associated_replica_index;
} server_data_t;
typedef struct request
{
request_type type;
unsigned int replica_index;
char *doc_name;
char *doc_content;
} request;
typedef struct response
{
char *server_log;
char *server_response;
int server_id;
} response;
server_t *init_server(unsigned int cache_size,
unsigned int server_id,
unsigned int (*hash_function_servers)(void *),
unsigned int (*hash_function_docs)(void *),
unsigned int replicas);
/**
* @brief Should deallocate completely the memory used by server,
* taking care of deallocating the elements in the queue, if any,
* without executing the tasks
*/
void free_server(server_t **s);
/**
* server_handle_request() - Receives a request from the load balancer
* and processes it according to the request type
*
* @param s: Server which processes the request.
* @param req: Request to be processed.
*
* @return response*: Response of the requested operation, which will
* then be printed in main.
*
* @brief Based on the type of request, should call the appropriate
* solver, and should execute the tasks from queue if needed (in
* this case, after executing each task, PRINT_RESPONSE should
* be called).
*/
response *server_handle_request(server_t *s, request *req);
/**
* get_server_data_local_database_node() - Gets the data stored in the
* local database node of the server.
*
* @param local_database_node: The local database node.
* @returns server_data_t* - Data from the local database node.
*/
server_data_t *
get_server_data_local_database_node(dll_node_t *local_database_node);
void server_data_free(server_data_t *server_data);
/**
* push_task_queue() - Pushes request to task queue until limit
* is reached.
*
* @param s: Server on which queue will be executed.
* @param data: Data pushed to the task queue.
*/
void push_task_queue(server_t *s, void *data);
void request_free(request *req);
void print_server_data(server_data_t *server_data);
void print_server(server_t *server);
/**
* copy_request() - Copies the reques.
*
* @param req: Request to be copied.
* @return request - New request which contains
* the same information with the one sent as
* parameter.
*/
request copy_request(request *req);
/**
* get_server_data_by_name() - Gets the data of the document with
* a specific name.
*
* @param server: Server on which the search will be done.
* @param name: The name of the document.
* @return server_data_t* - The data of the document found.
*/
server_data_t *get_server_data_by_name(server_t *server, char *name);
/**
* execute_server_task_queue() - Executes the whole task queue and
* empties it.
*
* @param s: Server whose task queue will be executed.
*/
void execute_server_task_queue(server_t *s);
/**
* calculate_replica_label() - Calculates the ID of the replica
* of a specific server.
*
* @param server_id: The server ID.
* @param replica_number: Index of the replica.
* @return unsigned int - ID of the replica.
*/
unsigned int calculate_replica_label(unsigned int server_id,
unsigned int replica_number);
/**
* get_server_replica_executor() - Gets the index of the replica which
* will execute a specific task.
*
* @param s: The server which will execute the task.
* @param data_hash: The hash of the data on which the task will be performed.
* @return unsigned int - The index of the replica.
*/
unsigned int get_server_replica_executor(server_t *s, unsigned int data_hash);
/**
* get_associated_label_index_for_data() - Gets the index of the replica which
* is associated with a specific server document.
*
* @param server: The server on which search will be performed..
* @param server_data: The server document.
* @return unsigned int - The index of the replica.
*/
unsigned int get_associated_label_index_for_data(server_t *server,
server_data_t *server_data);
#endif /* SERVER_H */