blob: 465e79f8baa6de912d2d9c03c9293ebde478caf7 (
plain) (
blame)
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
|
#pragma once
#include <pthread.h>
#include <sstream>
#define SHM_NAME "/hashtable_queue"
#define QUEUE_SIZE 10
#define MAX_KEY_SIZE 64
#define MAX_VALUE_SIZE 128
/**
* @brief Possible operations on the hashtable.
*/
enum Operations { INSERT, DELETE, GET, PRINT };
/**
* @brief Possible statuses of a request.
*/
enum Status { FREE, SENT, PROCESSED };
/**
* @brief One request constists out of the operation, the arguments and the response.
*/
struct Request {
Operations type;
Status status = FREE;
char key[MAX_KEY_SIZE];
char value[MAX_VALUE_SIZE];
char response[MAX_VALUE_SIZE];
};
/**
* @brief The shared memory between client and server.
*
* @details The shared memory consists out of:
* - A circular-buffer, to de- and enqueue multiple request at once.
* - A mutex with a conditional variable, to ensure concurrency of the buffer.
*/
struct SharedMemory {
Request request[QUEUE_SIZE];
pthread_mutex_t mutex;
pthread_cond_t cond_var;
int tail;
int head;
};
/**
* @brief Converts a generic type to a stringstream, so that it can be saved in the shared memory.
*/
template <typename T>
std::string serialize(const T& data)
{
std::ostringstream oss;
oss << data;
return oss.str();
}
/**
* @brief Converts a stringstream to a generic type, so that value in the shared memory can be read.
*/
template <typename T>
T deserialize(const std::string& str)
{
std::istringstream iss(str);
T data;
iss >> data;
return data;
}
|