From 426d60a6992259ca80431c59e916073cc31f5261 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 3 Oct 2008 01:30:27 +0200 Subject: Callback-basierte SystemBackends f?r h?here Flexibilit?t --- src/Common/Backends/SystemBackendPosix.cpp | 2 +- src/Common/Backends/SystemBackendPosix.h | 7 ++-- src/Common/Backends/SystemBackendProc.cpp | 52 ++++++++++++++++-------------- src/Common/Backends/SystemBackendProc.h | 6 ++-- 4 files changed, 36 insertions(+), 31 deletions(-) (limited to 'src/Common/Backends') diff --git a/src/Common/Backends/SystemBackendPosix.cpp b/src/Common/Backends/SystemBackendPosix.cpp index b1e088d..3ddf67d 100644 --- a/src/Common/Backends/SystemBackendPosix.cpp +++ b/src/Common/Backends/SystemBackendPosix.cpp @@ -69,7 +69,7 @@ void SystemBackendPosix::childHandler(int) { setChildHandler(); } -bool SystemBackendPosix::exec(sigc::slot resultHandler, const std::string &filename, const std::vector &argv, const std::vector &env) { +bool SystemBackendPosix::exec(const sigc::slot& resultHandler, const std::string &filename, const std::vector &argv, const std::vector &env) { pid_t pid; char **argvp, **envp; diff --git a/src/Common/Backends/SystemBackendPosix.h b/src/Common/Backends/SystemBackendPosix.h index 9efd59f..4a2c572 100644 --- a/src/Common/Backends/SystemBackendPosix.h +++ b/src/Common/Backends/SystemBackendPosix.h @@ -28,6 +28,7 @@ #include #include +#include namespace Mad { namespace Common { @@ -47,8 +48,8 @@ class SystemBackendPosix : public SystemBackend { } protected: - virtual bool doShutdown() {return exec(sigc::slot(), "/sbin/halt");} - virtual bool doReboot() {return exec(sigc::slot(), "/sbin/reboot");} + virtual bool doShutdown(const sigc::slot &callback) {return exec(sigc::hide(callback), "/sbin/halt");} + virtual bool doReboot(const sigc::slot &callback) {return exec(sigc::hide(callback), "/sbin/reboot");} public: ~SystemBackendPosix(); @@ -61,7 +62,7 @@ class SystemBackendPosix : public SystemBackend { SystemBackend::unregisterBackend(&backend); } - static bool exec(sigc::slot resultHandler, const std::string &filename, const std::vector &argv = std::vector(), + static bool exec(const sigc::slot &resultHandler, const std::string &filename, const std::vector &argv = std::vector(), const std::vector &env = std::vector()); }; diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp index 37bfaf3..134998f 100644 --- a/src/Common/Backends/SystemBackendProc.cpp +++ b/src/Common/Backends/SystemBackendProc.cpp @@ -28,37 +28,37 @@ namespace Backends { SystemBackendProc SystemBackendProc::backend; -SystemBackend::UptimeInfo SystemBackendProc::uptimeInfo() { - UptimeInfo uptime = {0, 0}; +bool SystemBackendProc::uptimeInfo(const sigc::slot &callback) { + unsigned long uptime = 0, idleTime = 0; uptimeFile.seekg(0, std::ios::beg); if(!uptimeFile.good()) - return uptime; + return false; float f; uptimeFile >> f; if(!uptimeFile.good()) - return uptime; + return false; - uptime.uptime = (unsigned long)f; + uptime = (unsigned long)f; uptimeFile >> f; - if(!uptimeFile.good()) - return uptime; + if(uptimeFile.good()) + idleTime = (unsigned long)f; - uptime.idleTime = (unsigned long)f; + callback(uptime, idleTime); - return uptime; + return true; } -SystemBackend::MemoryInfo SystemBackendProc::memoryInfo() { - MemoryInfo memInfo = {0, 0, 0, 0}; +bool SystemBackendProc::memoryInfo(const sigc::slot &callback) { + unsigned long totalMem = 0, freeMem = 0, totalSwap = 0, freeSwap = 0; meminfoFile.seekg(0, std::ios::beg); if(!meminfoFile.good()) - return memInfo; + return false; while(!meminfoFile.eof() && meminfoFile.good()) { std::string line; @@ -69,42 +69,46 @@ SystemBackend::MemoryInfo SystemBackendProc::memoryInfo() { if(std::sscanf(line.c_str(), "%s %lu", name, &value) == 2) { if(std::strcmp(name, "MemTotal:") == 0) - memInfo.totalMem = value; + totalMem = value; else if(std::strcmp(name, "MemFree:") == 0) - memInfo.freeMem = value; + freeMem = value; else if(std::strcmp(name, "SwapTotal:") == 0) - memInfo.totalSwap = value; + totalSwap = value; else if(std::strcmp(name, "SwapFree:") == 0) - memInfo.freeSwap = value; + freeSwap = value; } delete [] name; - if(memInfo.totalMem && memInfo.freeMem && memInfo.totalSwap && memInfo.freeSwap) + if(totalMem && freeMem && totalSwap && freeSwap) break; } + callback(totalMem, freeMem, totalSwap, freeSwap); - return memInfo; + return true; } -SystemBackend::LoadInfo SystemBackendProc::loadInfo() { - LoadInfo loadInfo = {0, 0, 0, 0, 0}; +bool SystemBackendProc::loadInfo(const sigc::slot &callback) { + unsigned long currentLoad = 0, nProcesses = 0; + float loadAvg1 = 0, loadAvg5 = 0, loadAvg15 = 0; loadFile.seekg(0, std::ios::beg); if(!loadFile.good()) - return loadInfo; + return false; std::string line; std::getline(loadFile, line); if(line.empty()) - return loadInfo; + return false; + + std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1, &loadAvg5, &loadAvg15, ¤tLoad, &nProcesses); - std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadInfo.loadAvg1, &loadInfo.loadAvg5, &loadInfo.loadAvg15, &loadInfo.currentLoad, &loadInfo.nProcesses); + callback(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15); - return loadInfo; + return true; } } diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h index 619c996..79ff6c5 100644 --- a/src/Common/Backends/SystemBackendProc.h +++ b/src/Common/Backends/SystemBackendProc.h @@ -47,9 +47,9 @@ class SystemBackendProc : public SystemBackend { SystemBackend::unregisterBackend(&backend); } - virtual UptimeInfo uptimeInfo(); - virtual MemoryInfo memoryInfo(); - virtual LoadInfo loadInfo(); + virtual bool uptimeInfo(const sigc::slot &callback); + virtual bool memoryInfo(const sigc::slot &callback); + virtual bool loadInfo(const sigc::slot &callback); }; } -- cgit v1.2.3