diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2019-07-28 21:44:14 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2019-07-28 21:44:14 +0200 |
commit | b5ae90cc23068f20720cae9d0f27f0dcddf6c1cb (patch) | |
tree | 88b9855672c62c8b72b4b38e7a5f68932f5cfaa1 /MemDriverWeb | |
parent | e2591d1d2ef4b456540abf3167a5cf2547c665c4 (diff) |
added KMemDriver Interface Thread, Status template callback
Diffstat (limited to 'MemDriverWeb')
-rw-r--r-- | MemDriverWeb/MemDriverWeb.cpp | 34 | ||||
-rw-r--r-- | MemDriverWeb/MemDriverWeb.vcxproj | 2 | ||||
-rw-r--r-- | MemDriverWeb/minitmpl.cpp | 13 | ||||
-rw-r--r-- | MemDriverWeb/minitmpl.h | 8 | ||||
-rw-r--r-- | MemDriverWeb/www.h | 17 |
5 files changed, 63 insertions, 11 deletions
diff --git a/MemDriverWeb/MemDriverWeb.cpp b/MemDriverWeb/MemDriverWeb.cpp index 2622d87..1d311ab 100644 --- a/MemDriverWeb/MemDriverWeb.cpp +++ b/MemDriverWeb/MemDriverWeb.cpp @@ -2,12 +2,16 @@ #include <iostream> #include <sstream> #include <KInterface.h> + #include "www.h" #include "minitmpl.h" using httplib::Request; using httplib::Response; +static std::mutex ki_mutex; +static bool ki_running = true; + static const char *host = "127.0.0.1"; static const int port = 8080; static const std::string template_content = DEFAULT_TEMPLATE; @@ -20,18 +24,43 @@ static void page_root(const Request &req, Response &res) res.set_content(ss.doTemplateStr(), "text/html"); } -static std::string& template_test_cb(std::string &out) +static std::string& template_test_cb(std::string &out, void *user_ptr) { out.append("--- TEST ---"); return out; } +static std::string& template_status_cb(std::string &out, void *user_ptr) +{ + KInterface &ki = KInterface::getInstance(); + try { + ki.getBuffer(); + } + catch (std::runtime_error &) { + out.append(STATUS_OFFLINE); + return out; + } + out.append(STATUS_ONLINE); + return out; +} + +void kernel_communication_thread(void) { + std::cout << "Kernel Interface Thread Init.." << std::endl; + + while (ki_running) { + std::this_thread::sleep_for(std::chrono::milliseconds(2222)); + std::cout << "Kernel Interface Thread Heartbeat.." << std::endl; + } +} + int main() { KInterface &ki = KInterface::getInstance(); httplib::Server httpServer; + std::thread ki_thread(kernel_communication_thread); TemplateString::registerTemplateCallback("<% CONTENT %>", template_test_cb); + TemplateString::registerTemplateCallback("<% STATUS %>", template_status_cb); std::cout << "Starting WebServer on " << host << ":" << port << "\n"; httpServer.Get("/", page_root); @@ -47,4 +76,7 @@ int main() std::cout << req.method << " " << req.path << std::endl; }); httpServer.listen(host, port); + + ki_running = false; + ki_thread.join(); }
\ No newline at end of file diff --git a/MemDriverWeb/MemDriverWeb.vcxproj b/MemDriverWeb/MemDriverWeb.vcxproj index cb90dc0..9234dc0 100644 --- a/MemDriverWeb/MemDriverWeb.vcxproj +++ b/MemDriverWeb/MemDriverWeb.vcxproj @@ -96,6 +96,7 @@ <ConformanceMode>true</ConformanceMode> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <AdditionalIncludeDirectories>$(SolutionDir)/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> </ClCompile> <Link> <SubSystem>Console</SubSystem> @@ -150,6 +151,7 @@ <ConformanceMode>true</ConformanceMode> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <AdditionalIncludeDirectories>$(SolutionDir)/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> </ClCompile> <Link> <SubSystem>Console</SubSystem> diff --git a/MemDriverWeb/minitmpl.cpp b/MemDriverWeb/minitmpl.cpp index 3d7c34c..92ac44e 100644 --- a/MemDriverWeb/minitmpl.cpp +++ b/MemDriverWeb/minitmpl.cpp @@ -1,17 +1,20 @@ #include "pch.h" #include "minitmpl.h" -std::map<std::string, template_cb> TemplateString::template_callbacks; +std::map<std::string, std::pair<template_cb, std::pair<bool, void *>>> TemplateString::template_callbacks; std::string TemplateString::doTemplateStr() { - size_t pos = 0; + size_t pos; in_cache = str(); - out_cache.clear(); for (auto& key : template_callbacks) { + pos = 0; while ((pos = in_cache.find(key.first, pos)) != std::string::npos) { - in_cache.replace(pos, key.first.length(), key.second(out_cache)); - pos += out_cache.length(); + in_cache.replace(pos, key.first.length(), key.second.first(out_cache, key.second.second.second)); + if (!key.second.second.first ) { + pos += out_cache.length(); + } + out_cache.clear(); } } diff --git a/MemDriverWeb/minitmpl.h b/MemDriverWeb/minitmpl.h index 426579c..820c3ee 100644 --- a/MemDriverWeb/minitmpl.h +++ b/MemDriverWeb/minitmpl.h @@ -3,16 +3,16 @@ #include <map> #include <sstream> -typedef std::function<std::string&(std::string&)> template_cb; +typedef std::function<std::string&(std::string&, void *)> template_cb; class TemplateString : public std::stringstream { public: - static void registerTemplateCallback(const char *variable, template_cb cb) { - TemplateString::template_callbacks[std::string(variable)] = cb; + static void registerTemplateCallback(const char *variable, template_cb cb, void *user_ptr = NULL, bool recursive = false) { + TemplateString::template_callbacks[std::string(variable)] = std::pair<template_cb, std::pair<bool, void *>>(cb, std::pair<bool, void *>(recursive, user_ptr)); } std::string doTemplateStr(); private: std::string in_cache, out_cache; - static std::map<std::string, template_cb> template_callbacks; + static std::map<std::string, std::pair<template_cb, std::pair<bool, void *>>> template_callbacks; };
\ No newline at end of file diff --git a/MemDriverWeb/www.h b/MemDriverWeb/www.h index 48d2fbb..5c9a727 100644 --- a/MemDriverWeb/www.h +++ b/MemDriverWeb/www.h @@ -16,7 +16,22 @@ "<a class = \"nav-link\" href = \"#\">Link 2 </a>\n" \ "</li>\n<li class = \"nav-item\">\n" \ "<a class = \"nav-link\" href = \"#\">Link 3 </a>\n" \ +"</li>\n<li class = \"nav-item\">\n" \ +"<% STATUS %>" \ "</li>\n</ul>\n</nav>\n<br>\n\n" \ "<div class = \"container-fluid\">\n\n" \ "<% CONTENT %>\n\n" \ -"</div>\n\n</body>\n</html>\n\n";
\ No newline at end of file +"</div>\n\n</body>\n</html>\n\n"; + +#define STATUS_OFFLINE \ +"<span class=\"badge badge-pill badge-danger\">Offline</span>\n" + +#define STATUS_ONLINE \ +"<span class=\"badge badge-pill badge-success\">Online</span>\n" + +#define STATUS_PROCESS \ +"<span class=\"badge badge-pill badge-success\">PID <% PID %></span>\n" + +#define PROCESS_FIND \ +"<li class = \"nav-item\">\n" \ +"</li>\n" |