From b5ae90cc23068f20720cae9d0f27f0dcddf6c1cb Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Sun, 28 Jul 2019 21:44:14 +0200 Subject: added KMemDriver Interface Thread, Status template callback --- MemDriverWeb/MemDriverWeb.cpp | 34 +++++++++++++++++++++++++++++++++- MemDriverWeb/MemDriverWeb.vcxproj | 2 ++ MemDriverWeb/minitmpl.cpp | 13 ++++++++----- MemDriverWeb/minitmpl.h | 8 ++++---- MemDriverWeb/www.h | 17 ++++++++++++++++- 5 files changed, 63 insertions(+), 11 deletions(-) (limited to 'MemDriverWeb') 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 #include #include + #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 @@ true pch.h $(SolutionDir)/include;%(AdditionalIncludeDirectories) + MultiThreadedDebugDLL Console @@ -150,6 +151,7 @@ true pch.h $(SolutionDir)/include;%(AdditionalIncludeDirectories) + MultiThreadedDLL Console 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 TemplateString::template_callbacks; +std::map>> 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 #include -typedef std::function template_cb; +typedef std::function 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>(cb, std::pair(recursive, user_ptr)); } std::string doTemplateStr(); private: std::string in_cache, out_cache; - static std::map template_callbacks; + static std::map>> 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 @@ "Link 2 \n" \ "\n
  • \n" \ "Link 3 \n" \ +"
  • \n
  • \n" \ +"<% STATUS %>" \ "
  • \n\n\n
    \n\n" \ "
    \n\n" \ "<% CONTENT %>\n\n" \ -"
    \n\n\n\n\n"; \ No newline at end of file +"\n\n\n\n\n"; + +#define STATUS_OFFLINE \ +"Offline\n" + +#define STATUS_ONLINE \ +"Online\n" + +#define STATUS_PROCESS \ +"PID <% PID %>\n" + +#define PROCESS_FIND \ +"
  • \n" \ +"
  • \n" -- cgit v1.2.3