From bea0bb0ff40dca9e5dba55c697c30e4fffaf0f66 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Sep 2008 15:23:27 +0200 Subject: Erlaube mehrere System-Backends --- src/Common/Backends/SystemBackendProc.cpp | 8 ++-- src/Common/Backends/SystemBackendProc.h | 16 +++++--- src/Common/ConfigManager.cpp | 2 +- .../RequestHandlers/StatusRequestHandler.cpp | 6 +-- src/Common/SystemBackend.cpp | 29 ++++++++++++++- src/Common/SystemBackend.h | 43 ++++++++++++---------- src/mad.cpp | 2 +- 7 files changed, 72 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp index e8c7357..37bfaf3 100644 --- a/src/Common/Backends/SystemBackendProc.cpp +++ b/src/Common/Backends/SystemBackendProc.cpp @@ -26,7 +26,9 @@ namespace Mad { namespace Common { namespace Backends { -SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() { +SystemBackendProc SystemBackendProc::backend; + +SystemBackend::UptimeInfo SystemBackendProc::uptimeInfo() { UptimeInfo uptime = {0, 0}; uptimeFile.seekg(0, std::ios::beg); @@ -50,7 +52,7 @@ SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() { return uptime; } -SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() { +SystemBackend::MemoryInfo SystemBackendProc::memoryInfo() { MemoryInfo memInfo = {0, 0, 0, 0}; meminfoFile.seekg(0, std::ios::beg); @@ -86,7 +88,7 @@ SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() { return memInfo; } -SystemBackend::LoadInfo SystemBackendProc::getLoadInfo() { +SystemBackend::LoadInfo SystemBackendProc::loadInfo() { LoadInfo loadInfo = {0, 0, 0, 0, 0}; loadFile.seekg(0, std::ios::beg); diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h index 8caa080..619c996 100644 --- a/src/Common/Backends/SystemBackendProc.h +++ b/src/Common/Backends/SystemBackendProc.h @@ -30,6 +30,8 @@ namespace Backends { class SystemBackendProc : public SystemBackend { private: + static SystemBackendProc backend; + std::ifstream uptimeFile; std::ifstream meminfoFile; std::ifstream loadFile; @@ -37,13 +39,17 @@ class SystemBackendProc : public SystemBackend { SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} public: - static void useBackend() { - setBackend(std::auto_ptr(new SystemBackendProc())); + static void registerBackend() { + SystemBackend::registerBackend(&backend); + } + + static void unregisterBackend() { + SystemBackend::unregisterBackend(&backend); } - virtual UptimeInfo getUptimeInfo(); - virtual MemoryInfo getMemoryInfo(); - virtual LoadInfo getLoadInfo(); + virtual UptimeInfo uptimeInfo(); + virtual MemoryInfo memoryInfo(); + virtual LoadInfo loadInfo(); }; } diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp index 76227b4..c8aea47 100644 --- a/src/Common/ConfigManager.cpp +++ b/src/Common/ConfigManager.cpp @@ -96,7 +96,7 @@ bool ConfigManager::loadFile(const std::string &filename) { } void ConfigManager::initBackends() { - Backends::SystemBackendProc::useBackend(); + Backends::SystemBackendProc::registerBackend(); } } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 6280ab3..ad37964 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -39,9 +39,9 @@ void StatusRequestHandler::handlePacket(Net::Connection *connection, const Net:: // TODO Require authentication - SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getBackend()->getUptimeInfo(); - SystemBackend::MemoryInfo memInfo = SystemBackend::getBackend()->getMemoryInfo(); - SystemBackend::LoadInfo loadInfo = SystemBackend::getBackend()->getLoadInfo(); + SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getUptimeInfo(); + SystemBackend::MemoryInfo memInfo = SystemBackend::getMemoryInfo(); + SystemBackend::LoadInfo loadInfo = SystemBackend::getLoadInfo(); connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, packet.getRequestId(), uptimeInfo.uptime, uptimeInfo.idleTime, memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap, loadInfo.currentLoad, loadInfo.nProcesses, loadInfo.loadAvg1, loadInfo.loadAvg5, loadInfo.loadAvg15)); diff --git a/src/Common/SystemBackend.cpp b/src/Common/SystemBackend.cpp index c9be525..4bbc68a 100644 --- a/src/Common/SystemBackend.cpp +++ b/src/Common/SystemBackend.cpp @@ -22,7 +22,34 @@ namespace Mad { namespace Common { -std::auto_ptr SystemBackend::backend(new SystemBackend()); +std::set SystemBackend::backends; + +SystemBackend::UptimeInfo SystemBackend::getUptimeInfo() { + UptimeInfo ret = {0, 0}; + + for(std::set::iterator backend = backends.begin(); backend != backends.end() && ret.uptime == 0; ++backend) + ret = (*backend)->uptimeInfo(); + + return ret; +} + +SystemBackend::MemoryInfo SystemBackend::getMemoryInfo() { + MemoryInfo ret = {0, 0, 0, 0}; + + for(std::set::iterator backend = backends.begin(); backend != backends.end() && ret.totalMem == 0; ++backend) + ret = (*backend)->memoryInfo(); + + return ret; +} + +SystemBackend::LoadInfo SystemBackend::getLoadInfo() { + LoadInfo ret = {0, 0, 0, 0, 0}; + + for(std::set::iterator backend = backends.begin(); backend != backends.end() && ret.currentLoad == 0 && ret.loadAvg1 == 0 && ret.nProcesses == 0; ++backend) + ret = (*backend)->loadInfo(); + + return ret; +} } } diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index 8ba1e7d..960082e 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -20,24 +20,12 @@ #ifndef MAD_COMMON_SYSTEMBACKEND_H_ #define MAD_COMMON_SYSTEMBACKEND_H_ -#include -#include -#include +#include namespace Mad { namespace Common { class SystemBackend { - private: - static std::auto_ptr backend; - - protected: - SystemBackend() {} - - static void setBackend(std::auto_ptr backend0) { - backend = backend0; - } - public: struct UptimeInfo { unsigned long uptime; @@ -59,26 +47,41 @@ class SystemBackend { float loadAvg15; }; - virtual ~SystemBackend() {} + private: + static std::set backends; + + protected: + SystemBackend() {} + + static void registerBackend(SystemBackend *backend) { + backends.insert(backend); + } + + static void unregisterBackend(SystemBackend *backend) { + backends.erase(backend); + } - virtual UptimeInfo getUptimeInfo() { + virtual UptimeInfo uptimeInfo() { UptimeInfo ret = {0, 0}; return ret; } - virtual MemoryInfo getMemoryInfo() { + virtual MemoryInfo memoryInfo() { MemoryInfo ret = {0, 0, 0, 0}; return ret; } - virtual LoadInfo getLoadInfo() { + virtual LoadInfo loadInfo() { LoadInfo ret = {0, 0, 0, 0, 0}; return ret; } - static SystemBackend *getBackend() { - return backend.get(); - } + public: + virtual ~SystemBackend() {} + + static UptimeInfo getUptimeInfo(); + static MemoryInfo getMemoryInfo(); + static LoadInfo getLoadInfo(); }; } diff --git a/src/mad.cpp b/src/mad.cpp index 309660d..ca4682f 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -46,7 +46,7 @@ int main() { Common::RequestManager::init(false); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::STATUS); - Common::Backends::SystemBackendProc::useBackend(); + Common::Backends::SystemBackendProc::registerBackend(); Net::ClientConnection *connection = new Net::ClientConnection; -- cgit v1.2.3