-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_server.cpp
More file actions
67 lines (61 loc) · 1.88 KB
/
main_server.cpp
File metadata and controls
67 lines (61 loc) · 1.88 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
#include <csignal>
#include "Logger.hpp"
#include "NitworkServer.hpp"
#include "Registry.hpp"
#include "ResourcesManager.hpp"
#include "SystemManagersDirector.hpp"
#include "Systems.hpp"
constexpr int EXIT_EPITECH = 84;
constexpr int PORT_MIN = 0;
constexpr int PORT_MAX = 65535;
static bool isRunning = true;
static void signalHandler(int signum)
{
Logger::info("Interrupt signal (" + std::to_string(signum) + ") received.");
isRunning = false;
signal(SIGINT, SIG_DFL);
}
static bool isNumber(const std::string &str)
{
return std::all_of(str.begin(), str.end(), ::isdigit);
}
static bool checkArgs(int ac, const char **av)
{
if (ac != 3) {
Logger::error("Usage: ./r-type_server <port> <playerNb>");
return false;
}
const std::vector<std::string> args(av + 1, av + ac);
for (const auto &arg : args) {
if (!isNumber(arg)) {
Logger::error("Invalid argument: " + arg);
return false;
}
}
if (std::stoi(args[0]) < PORT_MIN || std::stoi(args[0]) > PORT_MAX || std::stoi(args[1]) < 1) {
Logger::error("Invalid port or playerNb");
return false;
}
return true;
}
int main(int ac, const char **av)
{
#ifndef NDEBUG
Registry::getInstance().getLogger().setLogLevel(Logger::LogLevel::Debug);
#endif
ECS::ResourcesManager::init(av[0]);
if (!checkArgs(ac, av)) {
return EXIT_EPITECH;
}
Logger::info("Starting Server...");
if (!Nitwork::NitworkServer::getInstance().startServer(std::stoi(av[1]), std::stoi(av[2]))) {
return EXIT_EPITECH;
}
Systems::SystemManagersDirector::getInstance().addSystemManager(Systems::getECSSystems());
signal(SIGINT, signalHandler);
while (isRunning) {
Systems::SystemManagersDirector::getInstance().getSystemManager(0).updateSystems();
}
Nitwork::NitworkServer::getInstance().stop();
return 0;
}