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
|
#include "client.h"
#include "shared_memory.h"
#include <optional>
Client::Client()
{
shm_fd = shm_open(SHM_NAME, O_RDWR, 0666);
if (shm_fd == -1) {
std::cout << "Server not running" << '\n';
return;
}
shared_memory =
(SharedMemory*)mmap(0, sizeof(SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
}
Client::~Client()
{
munmap(shared_memory, sizeof(SharedMemory));
close(shm_fd);
}
void Client::start_client()
{
while (true) {
char operation;
int k, v;
std::cout << "Choose the operation (i: Insert, g: Get, r: Remove, p: Print, e: Exit)"
<< '\n';
std::cin >> operation;
switch (operation) {
case 'e':
return;
case 'i':
std::cout << "Insert: Enter the k-v pair(<int> <int>):" << '\n';
if (!(std::cin >> k >> v)) {
std::cout << "Invalid input" << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
};
send_request(
shared_memory, INSERT, std::optional(serialize(k)), std::optional(serialize(v)));
break;
case 'g': {
std::cout << "Get: Enter the key(<int>):" << '\n';
if (!(std::cin >> k)) {
std::cout << "Invalid input" << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
};
int index = send_request(shared_memory, GET, std::optional(serialize(k)), std::nullopt);
std::string response = process_result(shared_memory, index);
std::cout << "Got: " << response << '\n';
break;
}
case 'r':
std::cout << "Remove: Enter key(<int>):" << '\n';
if (!(std::cin >> k)) {
std::cout << "Invalid input" << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
};
send_request(shared_memory, DELETE, std::optional(serialize(k)), std::nullopt);
break;
case 'p':
send_request(shared_memory, PRINT, std::nullopt, std::nullopt);
break;
default:
break;
}
std::cout << '\n';
}
}
bool Client::request_processed(SharedMemory* shared_memory, int index)
{
if (shared_memory->full) {
return false;
}
if (shared_memory->tail == shared_memory->head && !shared_memory->full) {
return true;
}
for (int i = shared_memory->head - 1; i != shared_memory->tail; i = (i - 1) % QUEUE_SIZE) {
if (i == index) {
return false;
}
}
return shared_memory->tail != index;
}
int Client::send_request(
SharedMemory* shared_memory,
Operations type,
std::optional<const std::string> k,
std::optional<const std::string> v)
{
int index;
pthread_mutex_lock(&shared_memory->mutex);
while (shared_memory->full) {
pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex);
}
index = shared_memory->head;
Request* request = &shared_memory->request[index];
request->type = type;
strncpy(request->key, k.value_or("null").c_str(), MAX_KEY_SIZE);
strncpy(request->value, v.value_or("null").c_str(), MAX_VALUE_SIZE);
shared_memory->head = (1 + shared_memory->head) % QUEUE_SIZE;
shared_memory->full = shared_memory->head == shared_memory->tail;
pthread_cond_signal(&shared_memory->cond_var);
pthread_mutex_unlock(&shared_memory->mutex);
return index;
}
std::string Client::process_result(SharedMemory* shared_memory, int index)
{
pthread_mutex_lock(&shared_memory->mutex);
while (!request_processed(shared_memory, index)) {
pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex);
}
std::string result(shared_memory->request[index].response);
pthread_mutex_unlock(&shared_memory->mutex);
return result;
}
|