openmw-tes3coop/apps/master/main.cpp

76 lines
2.4 KiB
C++
Raw Normal View History

2017-04-23 05:39:38 +00:00
#include <iostream>
#include <Kbhit.h>
#include <RakSleep.h>
#include <sol.hpp>
2017-04-23 05:39:38 +00:00
#include "MasterServer.hpp"
2017-05-19 16:05:47 +00:00
#include "RestServer.hpp"
#include "AdminRest.hpp"
2017-04-23 05:39:38 +00:00
using namespace RakNet;
using namespace std;
2017-05-19 16:05:47 +00:00
unique_ptr<RestServer> restServer;
shared_ptr<MasterServer> masterServer;
unique_ptr<AdminRest> restAdminServer;
2017-05-19 16:05:47 +00:00
int main(int argc, char* argv[])
2017-04-23 05:39:38 +00:00
{
if (argc != 2)
return 1;
string luaScript(argv[1]);
masterServer = make_shared<MasterServer>(luaScript);
masterServer->luaStuff([](sol::state &state)
{
sol::table config = state["config"];
sol::object restPort = config["restPort"];
if (restPort.get_type() != sol::type::number)
throw runtime_error("config.restPort is not correct");
restServer = make_unique<RestServer>(restPort.as<unsigned short>(), masterServer->GetServers());
sol::object restAdminCert = config["restAdminCert"];
if (restAdminCert.get_type() != sol::type::string)
throw runtime_error("config.restAdminCert is not correct");
sol::object restAdminKey = config["restAdminKey"];
if (restAdminKey.get_type() != sol::type::string)
throw runtime_error("config.restAdminKey is not correct");
sol::object restAdminVerifyFile = config["restAdminVerifyFile"];
if (restAdminVerifyFile.get_type() != sol::type::string)
throw runtime_error("config.restAdminVerifyFile is not correct");
sol::object restAdminPort = config["restAdminPort"];
if (restAdminPort.get_type() != sol::type::number)
throw runtime_error("config.restAdminPort is not correct");
restAdminServer = make_unique<AdminRest>(restAdminCert.as<string>(), restAdminKey.as<string>(),
restAdminVerifyFile.as<string>(), restAdminPort.as<unsigned short>(), masterServer);
});
2017-05-19 16:05:47 +00:00
auto onExit = [](int /*sig*/){
restServer->stop();
restAdminServer->stop();
masterServer->luaStuff([](sol::state &state) {
sol::protected_function func = state["OnExit"];
if (func.valid())
func.call();
});
2017-05-19 16:05:47 +00:00
masterServer->Stop(false);
masterServer->Wait();
};
signal(SIGINT, onExit);
signal(SIGTERM, onExit);
masterServer->Start();
2017-09-13 09:56:38 +00:00
restServer->start();
restAdminServer->start();
2017-05-19 16:05:47 +00:00
masterServer->Wait();
2017-04-23 05:39:38 +00:00
return 0;
}