mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Started event handling
This commit is contained in:
@@ -12,10 +12,13 @@ class Server
|
||||
{
|
||||
public:
|
||||
Server(lua_State* L);
|
||||
~Server();
|
||||
|
||||
void start(lua_State* L);
|
||||
void stop(lua_State* L);
|
||||
|
||||
private:
|
||||
std::thread* serverThread;
|
||||
json::value answer;
|
||||
|
||||
void handle_options(http_request request);
|
||||
void handle_get(http_request request);
|
||||
|
||||
@@ -24,6 +24,8 @@ extern "C" DllExport int coreDeinit(lua_State* L)
|
||||
|
||||
log("Olympus coreDeinit called successfully");
|
||||
|
||||
server->stop(L);
|
||||
|
||||
delete unitsManager;
|
||||
delete server;
|
||||
delete scheduler;
|
||||
@@ -42,6 +44,8 @@ extern "C" DllExport int coreInit(lua_State* L)
|
||||
|
||||
registerLuaFunctions(L);
|
||||
|
||||
server->start(L);
|
||||
|
||||
initialized = true;
|
||||
return(0);
|
||||
}
|
||||
|
||||
@@ -26,15 +26,25 @@ void handle_eptr(std::exception_ptr eptr)
|
||||
}
|
||||
|
||||
Server::Server(lua_State* L):
|
||||
serverThread(nullptr),
|
||||
runListener(true)
|
||||
{
|
||||
LogInfo(L, "Starting RESTServer");
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Server::start(lua_State* L)
|
||||
{
|
||||
log("Starting RESTServer");
|
||||
serverThread = new thread(&Server::task, this);
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
void Server::stop(lua_State* L)
|
||||
{
|
||||
log("Stopping RESTServer");
|
||||
runListener = false;
|
||||
if (serverThread != nullptr)
|
||||
serverThread->join();
|
||||
}
|
||||
|
||||
void Server::handle_options(http_request request)
|
||||
@@ -59,18 +69,18 @@ void Server::handle_get(http_request request)
|
||||
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
|
||||
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
|
||||
|
||||
auto answer = json::value::object();
|
||||
std::exception_ptr eptr;
|
||||
try {
|
||||
unitsManager->updateAnswer(answer);
|
||||
|
||||
/* Get the logs from the logger */
|
||||
auto logs = json::value::object();
|
||||
logsToJSON(logs); // By reference, for thread safety
|
||||
|
||||
answer[L"airbases"] = airbasesData;
|
||||
answer[L"bullseye"] = bullseyeData;
|
||||
answer[L"logs"] = json::value::object();
|
||||
|
||||
int i = 0;
|
||||
for (auto log : getLogs())
|
||||
answer[L"logs"][to_wstring(i++)] = json::value::string(to_wstring(log));
|
||||
|
||||
answer[L"logs"] = logs;
|
||||
|
||||
response.set_body(answer);
|
||||
}
|
||||
catch (...) {
|
||||
@@ -156,7 +166,10 @@ void Server::task()
|
||||
|
||||
while (runListener);
|
||||
|
||||
listener.close();
|
||||
listener.close()
|
||||
.then([&listener]() {log("RESTServer stopping connections"); })
|
||||
.wait();
|
||||
|
||||
log("RESTServer stopped listening");
|
||||
}
|
||||
catch (exception const& e)
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
void DllExport log(const std::string& sMessage);
|
||||
void DllExport log(const std::wstring& sMessage);
|
||||
std::list<std::string> DllExport getLogs();
|
||||
void DllExport logsToJSON(json::value& json);
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
void Log(const string& sMessage);
|
||||
void Log(const wstring& sMessage);
|
||||
std::list<std::string> getLogs() { return m_logs; };
|
||||
void log(const string& sMessage);
|
||||
void log(const wstring& sMessage);
|
||||
void toJSON(json::value& json);
|
||||
|
||||
static Logger* GetLogger();
|
||||
|
||||
private:
|
||||
Logger();
|
||||
Logger(const Logger&) {}; // copy constructor is private
|
||||
@@ -20,6 +21,8 @@ private:
|
||||
static ofstream m_Logfile;
|
||||
static std::list<std::string> m_logs;
|
||||
|
||||
mutex mutexLock;
|
||||
|
||||
void Open();
|
||||
void Close();
|
||||
};
|
||||
|
||||
@@ -6,15 +6,15 @@
|
||||
|
||||
void log(const string& message)
|
||||
{
|
||||
LOGGER->Log(message);
|
||||
LOGGER->log(message);
|
||||
}
|
||||
|
||||
void log(const wstring& message)
|
||||
{
|
||||
LOGGER->Log(message);
|
||||
LOGGER->log(message);
|
||||
}
|
||||
|
||||
std::list<std::string> getLogs()
|
||||
void logsToJSON(json::value& json)
|
||||
{
|
||||
return LOGGER->getLogs();
|
||||
LOGGER->toJSON(json);
|
||||
}
|
||||
@@ -16,8 +16,7 @@ Logger* Logger::GetLogger()
|
||||
if (m_pThis == NULL) {
|
||||
m_pThis = new Logger();
|
||||
std::filesystem::path dirPath = std::filesystem::temp_directory_path();
|
||||
m_Logfile.open((dirPath.string() + m_sFileName).c_str(), ios::out | ios::app);
|
||||
m_pThis->Log("**************************************************");
|
||||
m_Logfile.open((dirPath.string() + m_sFileName).c_str(), ios::out);
|
||||
}
|
||||
return m_pThis;
|
||||
}
|
||||
@@ -33,8 +32,17 @@ void Logger::Close()
|
||||
m_Logfile.close();
|
||||
}
|
||||
|
||||
void Logger::Log(const string& message)
|
||||
void Logger::toJSON(json::value& json)
|
||||
{
|
||||
lock_guard<mutex> guard(mutexLock);
|
||||
int i = 0;
|
||||
for (auto log : m_logs)
|
||||
json[to_wstring(i++)] = json::value::string(to_wstring(log));
|
||||
}
|
||||
|
||||
void Logger::log(const string& message)
|
||||
{
|
||||
lock_guard<mutex> guard(mutexLock);
|
||||
Open();
|
||||
m_Logfile << CurrentDateTime() << ":\t";
|
||||
m_Logfile << message << "\n";
|
||||
@@ -42,8 +50,9 @@ void Logger::Log(const string& message)
|
||||
Close();
|
||||
}
|
||||
|
||||
void Logger::Log(const wstring& message)
|
||||
void Logger::log(const wstring& message)
|
||||
{
|
||||
lock_guard<mutex> guard(mutexLock);
|
||||
Open();
|
||||
m_Logfile << CurrentDateTime() << ":\t";
|
||||
m_Logfile << to_string(message) << "\n";
|
||||
|
||||
Reference in New Issue
Block a user