-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationServer.cpp
More file actions
197 lines (172 loc) · 6.34 KB
/
ApplicationServer.cpp
File metadata and controls
197 lines (172 loc) · 6.34 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
187
188
189
190
191
192
193
194
195
196
197
#include "ApplicationServer.hpp"
#ifdef _WIN32
#include <io.h>
#define isatty _isatty
#define fileno _fileno
#endif // _WIN32
ApplicationServer::ApplicationServer()
{
bool console = isatty(fileno(stdin));
i64 ip_address{ INADDR_ANY };
i64 port{ 1111 };
i64 threads_amount{ std::thread::hardware_concurrency() };
std::cout << "\033[92mApplication started.\033[0m" << std::endl;
std::istringstream stdinput("\n\n\n");
GetValue(console ? std::cin : stdinput, "listening IPv4 address", true, INADDR_ANY, &SocketTCP::GetIPAddress, ip_address);
GetValue(console ? std::cin : stdinput, "listening port", true, 1111, &SocketTCP::GetPort, port);
GetValue(console ? std::cin : stdinput, "threads amount", true, std::thread::hardware_concurrency(),
[](string value, i64& result) { return GetNumericValue(value, result, 1, 0xFFFF); }, threads_amount);
root_.reset(new Database::Object("database"));
root_->SetThisPtr(root_);
server_.reset(new SocketTCP::Server::Server(
static_cast<i32>(ip_address),
static_cast<u16>(port),
{ 1, 1, 1 },
[this](SocketTCP::DataBuffer data, SocketTCP::Server::Server::Client& client)
{
string request((char*)data.data());
sstream sendData;
Database::RequestStateObject result{};
if (request == "$dscn")
{
client.disconnect();
return;
}
try
{
result = root_->HandleRequest(request);
}
catch (const std::exception& exception)
{
sendData
<< std::format("\033[91mSome unexpected exception has happened:\n{}\n\033[0m", exception.what())
<< "\033[93mPlease, contact the developer about this issue.\033[0m"
<< std::endl;
client.sendData(sendData.str());
return;
}
sendData << result.ColorizedMessage();
client.sendData(sendData.str());
},
[](SocketTCP::Server::Server::Client& client)
{
client.sendData("\033[93mWelcome! Database is active.\033[0m\n");
},
[](SocketTCP::Server::Server::Client& client) {},
static_cast<u64>(threads_amount))
);
}
ApplicationServer::~ApplicationServer()
{
root_.reset();
}
void ApplicationServer::Run(std::istream& in)
{
server_thread_ = std::jthread([&]()
{
try {
if (server_->start() == SocketTCP::Server::Server::Status::kUp)
server_->joinLoop();
else
{
std::cout
<< "\n\033[91mServer start error! "
<< server_->explainStatus()
<< "\n\033[m..."
<< std::endl;
exit(i32(server_->getStatus()));
}
}
catch (std::exception& except) {
std::cerr << except.what();
}
server_.reset(); });
RunConsole(in);
server_->halt();
server_thread_.join();
//exit(0);
}
void ApplicationServer::RunConsole(std::istream& in)
{
std::cout << "\033[92mDatabase server started.\033[0m" << std::endl;
std::cout << "\033[96mSee 'help' for allowed commands.\033[0m" << std::endl;
bool state_ = true;
string input = "#";
while (state_)
{
try
{
state_ = HandleConsole(input);
}
catch (const std::exception& exception)
{
std::cout
<< std::format("\033[91mSome unexpected exception has happened:\n{}\n\033[0m", exception.what())
<< "\033[93mPlease, contact the developer about this issue.\033[0m"
<< std::endl;
}
if (state_)
{
std::cout << "\033[95m>>\033[0m ";
std::getline(in, input);
}
}
std::cout << "\033[93mHalting...\033[0m" << std::endl;
}
bool ApplicationServer::HandleConsole(string& input)
{
StringExtension::Trim(input);
string serverCommand = input;
StringExtension::ToLower(serverCommand);
if (input.empty() or input.size() == 0 or input[0] == '#')
return true;
if (serverCommand == "exit" or serverCommand == "quit")
{
std::cout << "\033[92mSee you next time.\033[0m" << std::endl;
return false;
}
if (serverCommand == "help" or serverCommand == "?")
{
string commands[] = {
"exit | quit - stops and exits the program",
"help | ? - shows this message",
"status | state - shows the current server state"};
std::cout << "\033[96mCommands:" << std::endl;
for (auto& command : commands)
std::cout << std::format(" {}", command) << std::endl;
std::cout << "\033[0m" << std::endl;
string helpCommand = "help";
auto result = root_->HandleRequest(helpCommand);
std::cout << result.ColorizedMessage() << std::endl;
return true;
}
if (serverCommand == "status" or serverCommand == "state")
{
std::cout
<< "\033[96mStatus:"
<< std::endl
<< std::format(" state: {}", server_->getStatus() == SocketTCP::Server::Server::Status::kUp ? "\033[92mokay" : "\033[91mstopped")
<< std::endl
<< "\033[96m"
<< std::format(" address: {}:{}", SocketTCP::U32ToIpAddress(server_->getIpAddress()), server_->getPort())
<< std::endl
<< std::format(" threads: {}", server_->getThreadPool().getThreadCount())
<< std::endl
<< "\033[0m";
return true;
}
if (serverCommand.find(' ') == string::npos and not Database::kOperations.contains(serverCommand))
{
std::cout << std::format("\033[91mUnknown command: '{}'\033[0m", input) << std::endl;
return true;
}
else if (serverCommand.find(' ') != string::npos)
serverCommand = input.substr(0, input.find(' '));
if (Database::kOperations.contains(serverCommand))
{
std::cout << "\033[96m Requesting a database... It may take some time.\033[0m" << std::endl;
Database::RequestStateObject result = root_->HandleRequest(input);
std::cout << result.ColorizedMessage() << std::endl;
}
return true;
}