-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrystal.cpp
More file actions
186 lines (163 loc) · 5.8 KB
/
crystal.cpp
File metadata and controls
186 lines (163 loc) · 5.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <cstring>
#include <map>
#include <winsock2.h>
#include <fstream>
#include <string>
#pragma comment(lib, "ws2_32.lib")
const int PORT = 479;
const size_t CACHE_SIZE = 1024 * 1024; // 1MB
std::map<std::string, std::string> cache;
const std::string FILENAME = "cache_data.txt";
//void printCache() {
// std::cout << "\x1B[2J\x1B[H";
//std::cout << "Current Cache Contents:" << std::endl;
// std::cout << "-----------------------" << std::endl;
// for (const auto& entry : cache) {
// std::cout << "Key: " << entry.first << ", Value: " << entry.second << std::endl;
// }
// std::cout << "-----------------------" << std::endl;
//}
void saveCacheToFile() {
std::ofstream outFile(FILENAME);
if (outFile.is_open()) {
for (const auto& entry : cache) {
outFile << entry.first << ":" << entry.second << std::endl;
}
outFile.close();
}
}
void loadCacheFromFile() {
std::ifstream inFile(FILENAME);
if (inFile.is_open()) {
cache.clear();
std::string line;
while (std::getline(inFile, line)) {
size_t pos = line.find(':');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
cache[key] = value;
}
}
inFile.close();
}
}
const int GRAPH_WIDTH = 60;
int totalTraffic = 0;
void drawTotalTrafficGraph() {
system("cls");
std::cout << "Total Traffic" << std::endl;
std::cout << "-------------" << std::endl;
int barWidth = static_cast<int>(static_cast<double>(totalTraffic) / GRAPH_WIDTH);
std::string bar(barWidth, '#');
std::cout << "Total: " << bar << " " << totalTraffic << std::endl;
}
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed" << std::endl;
return 1;
}
loadCacheFromFile();
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET) {
std::cerr << "Error creating socket: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cerr << "Error binding: " << WSAGetLastError() << std::endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
if (listen(serverSocket, 10) == SOCKET_ERROR) {
std::cerr << "Error listening: " << WSAGetLastError() << std::endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
std::cout << "Cache service started on port " << PORT << std::endl;
while (true) {
sockaddr_in clientAddr;
int clientLen = sizeof(clientAddr);
SOCKET clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientLen);
totalTraffic += 1;
drawTotalTrafficGraph();
if (clientSocket == INVALID_SOCKET) {
std::cerr << "Error accepting client connection: " << WSAGetLastError() << std::endl;
continue;
}
char request[256];
memset(request, 0, sizeof(request));
recv(clientSocket, request, sizeof(request), 0);
std::string response;
if (strncmp(request, "GET", 3) == 0) {
std::string key = request + 4;
if (cache.find(key) != cache.end()) {
response = cache[key];
send(clientSocket, response.c_str(), response.size(), 0);
}
else {
response = "Key not found in cache";
send(clientSocket, response.c_str(), response.size(), 0);
}
// No need to close client socket here
}
else if (strncmp(request, "REWRITE", 7) == 0) {
std::string data = request + 8;
size_t pos = data.find(':');
if (pos != std::string::npos) {
std::string key = data.substr(0, pos);
std::string value = data.substr(pos + 1);
if (cache.find(key) != cache.end()) {
cache[key] = value;
response = "Key " + key + " rewritten with value " + value;
//printCache();
saveCacheToFile();
}
else {
response = "Key not found in cache";
}
}
else {
response = "Invalid data format";
}
send(clientSocket, response.c_str(), response.size(), 0);
}
else if (strncmp(request, "SET", 3) == 0) {
std::string data = request + 4;
size_t pos = data.find(':');
if (pos != std::string::npos) {
std::string key = data.substr(0, pos);
std::string value = data.substr(pos + 1);
cache[key] = value;
response = key + ":" + value + " pair stored in cache";
//printCache();
saveCacheToFile();
}
else {
response = "Invalid data format";
}
send(clientSocket, response.c_str(), response.size(), 0);
}
else if (strncmp(request, "PRINT", 5) == 0) {
//printCache();
response = "Printed cache contents";
send(clientSocket, response.c_str(), response.size(), 0);
}
else {
response = "Invalid command";
send(clientSocket, response.c_str(), response.size(), 0);
}
closesocket(clientSocket);
}
closesocket(serverSocket);
WSACleanup();
return 0;
}