-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
192 lines (177 loc) · 5.63 KB
/
main.cpp
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
187
188
189
190
191
192
#include "main.h"
void parseCommands(std::map<std::string, std::vector<std::string>> &clientCommands)
{
for (auto &i : inputs::commands)
{
clientCommands[i[0]].emplace_back(i[1]);
}
}
void processCommand(const std::string clientName, const std::string command,
Service &service, std::mutex &outputMutex, std::ofstream &outputFile, std::vector<std::string> &args)
{
// get the arguments of the command
std::vector<std::string> output;
if (args[0] == "GET")
{
if (args.size() < 2)
{
output.emplace_back("WRONG COMMAND!");
return;
}
get(service, clientName, args, output);
}
else if (args[0] == "SET")
{
if (args.size() < 3)
{
output.emplace_back("WRONG COMMAND!");
return;
}
set(service, clientName, args, output);
}
else if (args[0] == "MGET")
{
if (args.size() < 2)
{
output.emplace_back("WRONG COMMAND!");
return;
}
mget(service, clientName, args, output);
}
else if (args[0] == "MSET")
{
if (args.size() < 3 || args.size() % 2 == 0)
{
output.emplace_back("WRONG COMMAND!");
return;
}
mset(service, clientName, args, output);
}
else if (args[0] == "INC")
{
if (args.size() < 2)
{
output.emplace_back("WRONG COMMAND!");
return;
}
incDec(service, clientName, args, output, true);
}
else if (args[0] == "DEC")
{
if (args.size() < 2)
{
output.emplace_back("WRONG COMMAND!");
return;
}
incDec(service, clientName, args, output, false);
}
else
{
output.emplace_back("WRONG COMMAND!");
}
// output the result to the log file
outputMutex.try_lock();
outputFile << clientName << " " << command << ":\n";
if (output.empty())
{
outputFile << "NULL" << std::endl;
}
for (auto &result : output)
{
if (result.empty())
{
result = "NULL";
}
outputFile << "> " << result << std::endl;
}
outputMutex.unlock();
}
void run(const std::string clientName, const std::vector<std::string> commands,
Service &service, std::mutex &outputMutex, std::ofstream &outputFile)
{
std::vector<std::string> queuedCommands;
std::vector<std::vector<std::string>> queuedArgs;
bool isTransaction = false;
unsigned int i;
for (auto &command : commands)
{
std::vector<std::string> args = helpers::splitBySpace(command);
if (args.empty())
{
outputFile << clientName << " " << command << std::endl;
outputFile << "> WRONG COMMAND!" << std::endl;
continue;
}
if (isTransaction)
{
if (args[0] == "EXEC")
{
service.lockMtx();
outputMutex.try_lock();
outputFile << "EXEC: " << std::endl;
for (i = 0; i < queuedCommands.size(); ++i)
{
outputMutex.try_lock();
outputFile << i + 1 << ") ";
outputMutex.unlock();
processCommand(clientName, queuedCommands[i], service, outputMutex, outputFile, queuedArgs[i]);
}
outputMutex.unlock(); // outputMutex is unlocked here to ensure other outputs do not interrupt the transaction's output.
service.unlockMtx();
}
else
{
outputMutex.try_lock();
outputFile << clientName << " " << command << std::endl;
outputFile << "> Queued" << std::endl;
outputMutex.unlock();
queuedCommands.emplace_back(command);
queuedArgs.emplace_back(args);
}
}
else
{
if (args[0] == "MULTI")
{
isTransaction = true;
outputMutex.try_lock();
outputFile << clientName << " " << command << std::endl;
outputMutex.unlock();
continue;
}
else
{
processCommand(clientName, command, service, outputMutex, outputFile, args);
}
}
}
}
int main(int argc, char **argv)
{
std::map<std::string, std::vector<std::string>> clientCommands;
std::vector<std::thread> clients;
// lock for outputing the result
std::mutex outputMtx;
// log file
std::ofstream outputFile(inputs::outputFileName, std::ofstream::out);
Service service(inputs::cacheSize);
parseCommands(clientCommands);
for (auto &p : clientCommands)
{
outputMtx.lock();
std::cout << "Started connection with: " << p.first << "...\n";
outputMtx.unlock();
clients.emplace_back(std::thread(run, std::ref(p.first),
std::ref(p.second), std::ref(service),
std::ref(outputMtx), std::ref(outputFile)));
// sleep for some time to approximate the delay between the requests.
std::this_thread::sleep_for(std::chrono::milliseconds(inputs::delayBetweenRequests));
}
for (auto &c : clients)
{
c.join();
}
outputFile.close();
std::cout << "All requests have been handled." << std::endl;
return 0;
}