-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_main.cpp
288 lines (260 loc) · 7.49 KB
/
server_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// Created by ilya on 12.05.18.
//
#include "dict_socket.h"
#include "utils.h"
#include "fill_db.h"
#include <stdexcept>
#include <vector>
#include <iostream>
#include <signal.h>
#include <algorithm>
#include <sstream>
#include <map>
using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::cerr;
using std::pair;
using std::endl;
using std::map;
// dicts: dictionary name -> dictionary
// dictionary: word -> word
map<string, map<string, string>> dicts;
dict_socket listener{};
bool need_to_terminate = false;
void sigint_handler(int signum)
{
cout << "SIGINT catched, exiting...";
listener.close();
need_to_terminate = true;
}
string get_connection_message()
{
return "220 connected successfully\r\n";
}
void process_show_db(dict_socket &socket)
{
if (dicts.empty())
{
socket.send("554 No databases present\r\n");
return;
}
string res = "110 ";
res += (std::to_string(dicts.size()) + string(" databases present -"));
for (auto const& p: dicts)
{
res += " " + p.first + ",";
}
res.pop_back();
res += "\r\n";
socket.send(res);
}
void process_show_info(dict_socket& socket, string const& name)
{
if (dicts.count(name) == 0)
{
socket.send("550 Invalid database, use \"SHOW DB\" for list of databases\r\n");
return;
}
string res = "112 name: " + name + ", size: " +
std::to_string(dicts[name].size()) + "\r\n";
socket.send(res);
}
void process_define_request(dict_socket& socket, vector<string>& parts)
{
string database = parts[1];
string word = parts[2];
if (database != "*" && database != "!")
{
if (dicts.count(database) == 0)
{
socket.send("550 Invalid database, use \"SHOW DB\" for list of databases\r\n");
return;
}
map<string, string> cur_dict = dicts[database];
if (cur_dict.count(word) == 0)
{
socket.send("552 No match\r\n");
return;
}
socket.send("150 1 definitions retrieved - definitions follow\r\n");
socket.send("151 " + cur_dict[word] + " " + database + "\r\n");
socket.send("250 ok\r\n");
return;
}
if (database == "!")
{
vector <pair<string, string>> answers;
for (auto const& dict: dicts)
{
if (dict.second.count(word) > 0)
{
answers.emplace_back(dict.second.at(word), dict.first);
}
}
if (answers.empty())
{
socket.send("552 No match\r\n");
return;
}
socket.send("150 " +
std::to_string(answers.size()) +
" definitions retrieved - definitions follow\r\n");
for (auto const& answer: answers)
{
socket.send("151 " + answer.first + " " + answer.second + "\r\n");
}
socket.send("250 ok\r\n");
return;
}
if (database == "*")
{
for (auto const& dict: dicts)
{
if (dict.second.count(word) > 0)
{
socket.send("150 1 definitions retrieved - definitions follow\r\n");
socket.send("151 " + dict.second.at(word) + " " + dict.first + "\r\n");
socket.send("250 ok\r\n");
return;
}
}
socket.send("552 No match\r\n");
return;
}
}
bool process_request(dict_socket& socket, string const& request, bool first)
{
cout << "Request received: " + request << endl;
vector<string> parts = parse_querry(request);
for (size_t i = 0; i < parts.size(); ++i)
{
cout << "Part № " << i << " - " << parts[i] << endl;
}
if (first)
{
if (!parts.empty() && parts[0] == "client")
{
cout << "CLIENT request: " << endl;
socket.send("250 ok client information received\r\n");
return false;
}
else
{
socket.send("500 Error, CLIENT request should be first\r\n");
return true;
}
}
if (parts.size() == 2 && parts[0] == "show" &&
(parts[1] == "db" || parts[1] == "databases"))
{
cout << "SHOW DB request: " << endl;
process_show_db(socket);
}
else if (parts.size() == 2 && parts[0] == "show" &&
(parts[1] == "strat" || parts[1] == "strategies"))
{
cout << "SHOW STRAT request: " << endl;
socket.send("555 No strategies available\r\n");
}
else if (parts.size() == 2 && parts[0] == "show" &&
parts[1] == "server")
{
cout << "SHOW SERVER request: " << endl;
socket.send("114 my dictionary server for OS Course\r\n");
}
else if (parts.size() == 3 && parts[0] == "show" &&
parts[1] == "info")
{
cout << "SHOW INFO request: " << endl;
process_show_info(socket, parts[2]);
}
else if (parts.size() == 4 && parts[0] == "match")
{
cout << "MATCH request: " << endl;
socket.send("551 Invalid strategy, use \"SHOW STRAT\" for a list of strategies\r\n");
}
else if (parts.size() == 1 && parts[0] == "status")
{
cout << "STATUS request: " << endl;
socket.send("210 server working properly\r\n");
}
else if (parts.size() == 1 && parts[0] == "help")
{
cout << "HELP request: " << endl;
socket.send("113 read https://tools.ietf.org/html/rfc2229 for help\r\n");
}
else if (parts.size() == 1 && parts[0] == "quit")
{
cout << "QUIT request: " << endl;
socket.send("221 Closing Connection\r\n");
return true;
}
else if (!parts.empty() && parts[0] == "client")
{
cout << "CLIENT request: " << endl;
socket.send("250 ok client information received\r\n");
}
else if (parts.size() == 3 && parts[0] == "define")
{
cout << "DEFINE request: " << endl;
process_define_request(socket, parts);
}
else
{
socket.send("502 Command not implemented\r\n");
}
return false;
}
int main(int argc, char* argv[])
{
fill_databases(dicts);
cout << "Dictionary initialization finished: " << endl;
for (const auto& dict: dicts)
{
cout << "Dictionary: " << dict.first << endl;
for (const auto& p: dict.second)
{
cout << p.first << " - " << p.second << endl;
}
cout << "Dictionary finished" << endl;
}
try
{
listener.create();
signal(SIGINT, &sigint_handler);
listener.bind();
listener.listen();
while (!need_to_terminate)
{
dict_socket socket = listener.accept();
socket.send(get_connection_message());
read_result res {"", "", false};
string request;
string rem;
bool first = true;
do
{
//cout << "Reading" << endl;
res = read_until_crlf(socket, rem);
request = res.querry;
rem = res.remainder;
std::replace(request.begin(), request.end(), '\t', ' ');
std::transform(request.begin(), request.end(), request.begin(), ::tolower);
if(process_request(socket, request, first))
{
break;
}
first = false;
} while (!res.empty_input);
}
}
catch (std::runtime_error& e)
{
cout << e.what();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}