From 48ad3ba7fe0765ff66daedc1866ded7890be678a Mon Sep 17 00:00:00 2001 From: "Leon Etienne (ubuntu wsl)" Date: Thu, 24 Sep 2020 15:15:18 +0200 Subject: [PATCH] Added frontend system --- Tubio/Framework.cpp | 8 +++ Tubio/Framework.h | 2 + Tubio/RestInterface.cpp | 15 ++++-- Tubio/RestInterface.h | 2 +- Tubio/RestQueryHandler.cpp | 2 +- Tubio/RestResponseTemplates.h | 2 - Tubio/Tubio.vcxproj | 2 + Tubio/Tubio.vcxproj.filters | 6 +++ Tubio/UIServer.cpp | 91 +++++++++++++++++++++++++++++++++++ Tubio/UIServer.h | 33 +++++++++++++ Tubio/XGControl.cpp | 1 + Tubio/XGControl.h | 2 + Tubio/frontend/index.html | 17 +++++++ 13 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 Tubio/UIServer.cpp create mode 100644 Tubio/UIServer.h create mode 100644 Tubio/frontend/index.html diff --git a/Tubio/Framework.cpp b/Tubio/Framework.cpp index c0914a3..bc8e8f3 100644 --- a/Tubio/Framework.cpp +++ b/Tubio/Framework.cpp @@ -2,6 +2,7 @@ using namespace Logging; using namespace Rest; +using namespace UI; Framework::Framework() { @@ -10,7 +11,9 @@ Framework::Framework() log = new Logger("Framework"); log->cout << "Starting Tubio server..."; log->Flush(); + restInterface = new RestInterface(); + uiServer = new UIServer(); PostInit(); @@ -23,8 +26,10 @@ Framework::Framework() Framework::~Framework() { delete restInterface; + delete uiServer; delete log; + uiServer = nullptr; restInterface = nullptr; log = nullptr; @@ -36,6 +41,7 @@ void Framework::Run() while (XGControl::keepServerRunning) { restInterface->Update(); + uiServer->Update(); } OnExit(); @@ -58,6 +64,7 @@ void Framework::PreInit() void Framework::PostInit() { restInterface->PostInit(); + uiServer->PostInit(); return; } @@ -65,6 +72,7 @@ void Framework::PostInit() void Framework::OnExit() { restInterface->OnExit(); + uiServer->OnExit(); return; } diff --git a/Tubio/Framework.h b/Tubio/Framework.h index f8da4db..f8dd684 100644 --- a/Tubio/Framework.h +++ b/Tubio/Framework.h @@ -1,6 +1,7 @@ #pragma once #include "Logger.h" #include "LogHistory.h" +#include "UIServer.h" #include "RestInterface.h" #include "XGControl.h" @@ -19,6 +20,7 @@ private: void PostExit(); Rest::RestInterface* restInterface; + UI::UIServer* uiServer; Logging::Logger* log; diff --git a/Tubio/RestInterface.cpp b/Tubio/RestInterface.cpp index dbfad08..83c82ca 100644 --- a/Tubio/RestInterface.cpp +++ b/Tubio/RestInterface.cpp @@ -10,8 +10,6 @@ RestInterface::RestInterface() pNc = nullptr; log = new Logger("WebServer"); - isBootedSuccessfully = false; - return; } @@ -37,14 +35,14 @@ bool RestInterface::InitWebServer() { mg_mgr_init(pMgr, NULL); - log->cout << "Starting web server on port " << WEBAPI_SERVER_PORT << "..."; + log->cout << "Starting rest api server on port " << WEBAPI_SERVER_PORT << "..."; log->Flush(); pNc = mg_bind(pMgr, WEBAPI_SERVER_PORT, this->EventHandler); if (pNc == NULL) { - log->cout << log->Err() << "Failed to boot the web server! - Unable to bind listener!"; + log->cout << log->Err() << "Failed to boot rest api web server! - Unable to bind listener! (port: " << WEBAPI_SERVER_PORT << ")"; log->Flush(); return false; } @@ -86,7 +84,11 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p) { Json requestBody; requestBody.Parse(requestBodyRaw); - + + char addr[32]; + mg_sock_addr_to_str(&pNc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP); + XGControl::lastIPThatRequested = std::string(addr); + JsonBlock responseBody; HTTP_STATUS_CODE returnCode; RestQueryHandler::ProcessQuery(requestBody, responseBody, returnCode); @@ -109,6 +111,9 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p) void RestInterface::OnExit() { + log->cout << "Shutting down rest api server..."; + log->Flush(); + mg_mgr_free(pMgr); return; diff --git a/Tubio/RestInterface.h b/Tubio/RestInterface.h index 6da03f8..64f6bf6 100644 --- a/Tubio/RestInterface.h +++ b/Tubio/RestInterface.h @@ -35,6 +35,6 @@ namespace Rest Logging::Logger* log; - bool isBootedSuccessfully; + bool isBootedSuccessfully = false; }; } diff --git a/Tubio/RestQueryHandler.cpp b/Tubio/RestQueryHandler.cpp index e4fe605..fd67879 100644 --- a/Tubio/RestQueryHandler.cpp +++ b/Tubio/RestQueryHandler.cpp @@ -42,7 +42,7 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons { XGControl::keepServerRunning = false; - log->cout << "Shutting down server upon rest request..."; + log->cout << "Shutting down server upon rest request by " << XGControl::lastIPThatRequested << "..."; log->Flush(); responseCode = OK; diff --git a/Tubio/RestResponseTemplates.h b/Tubio/RestResponseTemplates.h index 56473fa..45d1d42 100644 --- a/Tubio/RestResponseTemplates.h +++ b/Tubio/RestResponseTemplates.h @@ -9,7 +9,6 @@ namespace Rest UNAUTHORIZED = 401, FORBIDDEN = 403, NOT_FOUND = 404, - METHOD_NOT_ALLOWED = 405, INTERNAL_SERVER_ERROR = 500, NOT_IMPLEMENTED = 501 }; @@ -25,7 +24,6 @@ namespace Rest static JasonPP::JsonBlock Unauthorized(std::string message); static JasonPP::JsonBlock Forbidden(std::string message); static JasonPP::JsonBlock NotFound(std::string message); - static JasonPP::JsonBlock MethodNotAllowed(std::string message); static JasonPP::JsonBlock InternalServerError(std::string message); static JasonPP::JsonBlock NotImplemented(std::string message); diff --git a/Tubio/Tubio.vcxproj b/Tubio/Tubio.vcxproj index 5a7fda0..2152119 100644 --- a/Tubio/Tubio.vcxproj +++ b/Tubio/Tubio.vcxproj @@ -148,6 +148,7 @@ + @@ -160,6 +161,7 @@ + diff --git a/Tubio/Tubio.vcxproj.filters b/Tubio/Tubio.vcxproj.filters index b84e1dc..8348a32 100644 --- a/Tubio/Tubio.vcxproj.filters +++ b/Tubio/Tubio.vcxproj.filters @@ -48,6 +48,9 @@ Quelldateien + + Quelldateien + @@ -80,5 +83,8 @@ Headerdateien + + Headerdateien + \ No newline at end of file diff --git a/Tubio/UIServer.cpp b/Tubio/UIServer.cpp new file mode 100644 index 0000000..e272893 --- /dev/null +++ b/Tubio/UIServer.cpp @@ -0,0 +1,91 @@ +#include "UIServer.h" + +using namespace UI; +using namespace Logging; + +UIServer::UIServer() +{ + pMgr = new mg_mgr(); + pNc = nullptr; + log = new Logger("UIServer"); + + return; +} + +UIServer::~UIServer() +{ + delete pMgr; + delete log; + + log = nullptr; + pMgr = nullptr; + + return; +} + +void UIServer::PostInit() +{ + isBootedSuccessfully = InitWebServer(); + + return; +} + +bool UIServer::InitWebServer() +{ + mg_mgr_init(pMgr, NULL); + + log->cout << "Starting ui web server on port " << WEBUI_SERVER_PORT << "..."; + log->Flush(); + + pNc = mg_bind(pMgr, WEBUI_SERVER_PORT, this->EventHandler); + + if (pNc == NULL) + { + log->cout << log->Err() << "Failed to boot ui web server! - Unable to bind listener! (port: " << WEBUI_SERVER_PORT << ")"; + log->Flush(); + return false; + } + + mg_set_protocol_http_websocket(pNc); + serve_opts.document_root = "frontend"; // Serve current directory + serve_opts.enable_directory_listing = "no"; + + log->cout << "Started web server successfully!"; + log->Flush(); + isBootedSuccessfully = true; + + return true; +} + + +void UIServer::Update() +{ + mg_mgr_poll(pMgr, WEBUI_SERVER_POLLRATE); + + return; +} + +void UIServer::EventHandler(mg_connection* pNc, int ev, void* p) +{ + switch (ev) + { + case MG_EV_HTTP_REQUEST: + mg_serve_http(pNc, (struct http_message*)p, serve_opts); + break; + } + + return; +} + + +void UIServer::OnExit() +{ + log->cout << "Shutting down ui web server..."; + log->Flush(); + + mg_mgr_free(pMgr); + + return; +} + +mg_serve_http_opts UIServer::serve_opts; \ No newline at end of file diff --git a/Tubio/UIServer.h b/Tubio/UIServer.h new file mode 100644 index 0000000..4715b03 --- /dev/null +++ b/Tubio/UIServer.h @@ -0,0 +1,33 @@ +#pragma once +#include "mongoose.h" +#include "Logger.h" + +#define WEBUI_SERVER_PORT "6970" +#define WEBUI_SERVER_POLLRATE 0 + +namespace UI +{ + class UIServer + { + public: + UIServer(); + ~UIServer(); + + void PostInit(); + void Update(); + void OnExit(); + + private: + bool InitWebServer(); + + static void EventHandler(struct mg_connection* pNc, int ev, void* p); + + struct mg_mgr* pMgr; + struct mg_connection* pNc; + static mg_serve_http_opts serve_opts; + + Logging::Logger* log; + + bool isBootedSuccessfully = false; + }; +} diff --git a/Tubio/XGControl.cpp b/Tubio/XGControl.cpp index 9ddc0d2..2ebd6f9 100644 --- a/Tubio/XGControl.cpp +++ b/Tubio/XGControl.cpp @@ -1,3 +1,4 @@ #include "XGControl.h" bool XGControl::keepServerRunning = false; +std::string XGControl::lastIPThatRequested = ""; diff --git a/Tubio/XGControl.h b/Tubio/XGControl.h index d12aa6e..f546136 100644 --- a/Tubio/XGControl.h +++ b/Tubio/XGControl.h @@ -1,4 +1,5 @@ #pragma once +#include /// /// Class to house control variables @@ -7,5 +8,6 @@ class XGControl { public: static bool keepServerRunning; + static std::string lastIPThatRequested; }; diff --git a/Tubio/frontend/index.html b/Tubio/frontend/index.html new file mode 100644 index 0000000..7ea7ce8 --- /dev/null +++ b/Tubio/frontend/index.html @@ -0,0 +1,17 @@ + + + + + + Hello, World! + + + +
+

+ HELLO, WORLD :3 +

+
+ + +