diff --git a/Tubio/Framework.cpp b/Tubio/Framework.cpp
index bc8e8f3..6a45e58 100644
--- a/Tubio/Framework.cpp
+++ b/Tubio/Framework.cpp
@@ -2,7 +2,6 @@
using namespace Logging;
using namespace Rest;
-using namespace UI;
Framework::Framework()
{
@@ -13,7 +12,6 @@ Framework::Framework()
log->Flush();
restInterface = new RestInterface();
- uiServer = new UIServer();
PostInit();
@@ -26,10 +24,8 @@ Framework::Framework()
Framework::~Framework()
{
delete restInterface;
- delete uiServer;
delete log;
- uiServer = nullptr;
restInterface = nullptr;
log = nullptr;
@@ -41,7 +37,6 @@ void Framework::Run()
while (XGControl::keepServerRunning)
{
restInterface->Update();
- uiServer->Update();
}
OnExit();
@@ -64,7 +59,6 @@ void Framework::PreInit()
void Framework::PostInit()
{
restInterface->PostInit();
- uiServer->PostInit();
return;
}
@@ -72,7 +66,6 @@ void Framework::PostInit()
void Framework::OnExit()
{
restInterface->OnExit();
- uiServer->OnExit();
return;
}
diff --git a/Tubio/Framework.h b/Tubio/Framework.h
index f8dd684..f8da4db 100644
--- a/Tubio/Framework.h
+++ b/Tubio/Framework.h
@@ -1,7 +1,6 @@
#pragma once
#include "Logger.h"
#include "LogHistory.h"
-#include "UIServer.h"
#include "RestInterface.h"
#include "XGControl.h"
@@ -20,7 +19,6 @@ private:
void PostExit();
Rest::RestInterface* restInterface;
- UI::UIServer* uiServer;
Logging::Logger* log;
diff --git a/Tubio/Logger.cpp b/Tubio/Logger.cpp
index 5713018..97a3de3 100644
--- a/Tubio/Logger.cpp
+++ b/Tubio/Logger.cpp
@@ -8,9 +8,7 @@ Logger::Logger(std::string identifier)
std::stringstream ss;
- rawIdentifier = identifier;
- ss << "[" << rawIdentifier << "]";
- this->identifier = ss.str();
+ this->identifier = identifier;
return;
}
@@ -47,11 +45,11 @@ std::string Logger::Flush()
strftime(timeBuf, 100, "%d.%m.%Y - %T", &currTm);
std::stringstream bufOut;
- bufOut << "<" << timeBuf << "> " << identifier << TypeToPrefix(type) << ": " << cout.str();
+ bufOut << "<" << timeBuf << "> [" << identifier << "]" << TypeToPrefix(type) << ((additionalInfo.length() > 0) ? " " : "") << additionalInfo << ": " << cout.str();
LogEntry* newEntry = new LogEntry;
newEntry->message = bufOut.str();
- newEntry->identifier = rawIdentifier;
+ newEntry->identifier = identifier;
newEntry->timestamp = currTime;
newEntry->type = type;
LogHistory::AddLogToHistory(newEntry);
@@ -67,7 +65,6 @@ std::string Logger::Type(LOG_TYPE type)
{
if (!IsInitializedSanityCheck()) return "";
-
this->type = type;
return "";
}
diff --git a/Tubio/Logger.h b/Tubio/Logger.h
index 47794df..7d88b3f 100644
--- a/Tubio/Logger.h
+++ b/Tubio/Logger.h
@@ -21,6 +21,9 @@ namespace Logging
//Sets the buffered string
void Set(std::string str);
+ //Sets additional information to be appended after the identifier
+ void SetAdditionalInformation(std::string str) { additionalInfo = str; };
+
//Prints the buffered string to the console and clears it
std::string Flush();
@@ -42,7 +45,7 @@ namespace Logging
bool IsInitializedSanityCheck();
std::string identifier;
- std::string rawIdentifier;
+ std::string additionalInfo = "";
LOG_TYPE type = LOG_TYPE::LOG;
bool isInitialized = false;
diff --git a/Tubio/RestInterface.cpp b/Tubio/RestInterface.cpp
index 83c82ca..2ff88f6 100644
--- a/Tubio/RestInterface.cpp
+++ b/Tubio/RestInterface.cpp
@@ -48,6 +48,8 @@ bool RestInterface::InitWebServer()
}
mg_set_protocol_http_websocket(pNc);
+ frontend_serve_opts.document_root = "frontend";
+ frontend_serve_opts.enable_directory_listing = "no";
log->cout << "Started web server successfully!";
log->Flush();
@@ -78,28 +80,15 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
case MG_EV_HTTP_REQUEST:
http_message* hpm = (http_message*)p;
- std::string requestBodyRaw = FixUnterminatedString(hpm->body.p, hpm->body.len);
-
- if (IsJsonValid(requestBodyRaw))
+ std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len);
+
+ if ((requestedUri == "/api"))
{
- 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);
-
- Json response(responseBody);
- ServeStringToConnection(pNc, response.Render(), returnCode);
+ ProcessAPIRequest(pNc, ev, p);
}
else
{
- Json errorJson = RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Received json is fucked up");
- ServeStringToConnection(pNc, errorJson.Render(), HTTP_STATUS_CODE::BAD_REQUEST);
+ mg_serve_http(pNc, (struct http_message*)p, frontend_serve_opts);
}
@@ -109,6 +98,39 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
return;
}
+void RestInterface::ProcessAPIRequest(mg_connection* pNc, int ev, void* p)
+{
+ // Get struct with http message informations
+ http_message* hpm = (http_message*)p;
+
+ // Get the transmitted message body
+ std::string requestBodyRaw = FixUnterminatedString(hpm->body.p, hpm->body.len);
+
+ // Check for the body being valid json
+ if (IsJsonValid(requestBodyRaw))
+ {
+ Json requestBody;
+ requestBody.Parse(requestBodyRaw);
+
+ char addr[32];
+ mg_sock_addr_to_str(&pNc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
+
+ JsonBlock responseBody;
+ HTTP_STATUS_CODE returnCode;
+ RestQueryHandler::ProcessQuery(std::string(addr), requestBody, responseBody, returnCode);
+
+ Json response(responseBody);
+ ServeStringToConnection(pNc, response.Render(), returnCode);
+ }
+ else // return error message for invalid json
+ {
+ Json errorJson = RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Received json is fucked up");
+ ServeStringToConnection(pNc, errorJson.Render(), HTTP_STATUS_CODE::BAD_REQUEST);
+ }
+
+ return;
+}
+
void RestInterface::OnExit()
{
log->cout << "Shutting down rest api server...";
@@ -130,3 +152,5 @@ std::string RestInterface::FixUnterminatedString(const char* cstr, const std::si
return ss.str();
}
+
+mg_serve_http_opts RestInterface::frontend_serve_opts;
diff --git a/Tubio/RestInterface.h b/Tubio/RestInterface.h
index 64f6bf6..2276f0d 100644
--- a/Tubio/RestInterface.h
+++ b/Tubio/RestInterface.h
@@ -24,6 +24,7 @@ namespace Rest
private:
bool InitWebServer();
+ static void ProcessAPIRequest(struct mg_connection* pNc, int ev, void* p);
static void EventHandler(struct mg_connection* pNc, int ev, void* p);
static void ServeStringToConnection(struct mg_connection* c, std::string str, int httpStatusCode = 200);
@@ -32,6 +33,7 @@ namespace Rest
struct mg_mgr* pMgr;
struct mg_connection* pNc;
+ static mg_serve_http_opts frontend_serve_opts;
Logging::Logger* log;
diff --git a/Tubio/RestQueryHandler.cpp b/Tubio/RestQueryHandler.cpp
index fd67879..6a69baf 100644
--- a/Tubio/RestQueryHandler.cpp
+++ b/Tubio/RestQueryHandler.cpp
@@ -11,8 +11,10 @@ void RestQueryHandler::PreInit()
return;
}
-bool RestQueryHandler::ProcessQuery(const Json& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
+bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
+ log->SetAdditionalInformation(std::string("@") + clientAdress);
+
if (!ValidateField("request", JSON_DATA_TYPE::STRING, request, responseBody))
{
responseCode = BAD_REQUEST;
@@ -42,7 +44,7 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons
{
XGControl::keepServerRunning = false;
- log->cout << "Shutting down server upon rest request by " << XGControl::lastIPThatRequested << "...";
+ log->cout << "Shutting down server upon rest request...";
log->Flush();
responseCode = OK;
diff --git a/Tubio/RestQueryHandler.h b/Tubio/RestQueryHandler.h
index 8291555..01ef0a0 100644
--- a/Tubio/RestQueryHandler.h
+++ b/Tubio/RestQueryHandler.h
@@ -11,7 +11,7 @@ namespace Rest
public:
static void PreInit();
- static bool ProcessQuery(const JasonPP::Json& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
+ static bool ProcessQuery(const std::string clientAdress, const JasonPP::Json& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static void PostExit();
diff --git a/Tubio/Tubio.vcxproj b/Tubio/Tubio.vcxproj
index 2152119..5a7fda0 100644
--- a/Tubio/Tubio.vcxproj
+++ b/Tubio/Tubio.vcxproj
@@ -148,7 +148,6 @@
-
@@ -161,7 +160,6 @@
-
diff --git a/Tubio/Tubio.vcxproj.filters b/Tubio/Tubio.vcxproj.filters
index 8348a32..b84e1dc 100644
--- a/Tubio/Tubio.vcxproj.filters
+++ b/Tubio/Tubio.vcxproj.filters
@@ -48,9 +48,6 @@
Quelldateien
-
- Quelldateien
-
@@ -83,8 +80,5 @@
Headerdateien
-
- Headerdateien
-
\ No newline at end of file
diff --git a/Tubio/UIServer.cpp b/Tubio/UIServer.cpp
deleted file mode 100644
index e272893..0000000
--- a/Tubio/UIServer.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-#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
deleted file mode 100644
index 4715b03..0000000
--- a/Tubio/UIServer.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#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 2ebd6f9..9ddc0d2 100644
--- a/Tubio/XGControl.cpp
+++ b/Tubio/XGControl.cpp
@@ -1,4 +1,3 @@
#include "XGControl.h"
bool XGControl::keepServerRunning = false;
-std::string XGControl::lastIPThatRequested = "";
diff --git a/Tubio/XGControl.h b/Tubio/XGControl.h
index f546136..2a29a42 100644
--- a/Tubio/XGControl.h
+++ b/Tubio/XGControl.h
@@ -8,6 +8,5 @@ class XGControl
{
public:
static bool keepServerRunning;
- static std::string lastIPThatRequested;
};