From 73667d1102735c42cf385442aeb2a1735bb67d6a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 21 May 2009 01:42:33 +0200 Subject: SystemBackend-Interface ueberarbeitet --- .../SystemBackendProc/SystemBackendProc.cpp | 75 ++++++++++++++-------- src/modules/SystemBackendProc/SystemBackendProc.h | 16 ++--- 2 files changed, 55 insertions(+), 36 deletions(-) (limited to 'src/modules/SystemBackendProc') diff --git a/src/modules/SystemBackendProc/SystemBackendProc.cpp b/src/modules/SystemBackendProc/SystemBackendProc.cpp index d238e07..1888227 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.cpp +++ b/src/modules/SystemBackendProc/SystemBackendProc.cpp @@ -24,15 +24,13 @@ #include #include -#include - namespace Mad { namespace Modules { -SystemBackendProc *SystemBackendProc::backend = 0; +boost::shared_ptr SystemBackendProc::backend; -bool SystemBackendProc::getUptimeInfo(const boost::function2 &callback) { - unsigned long uptime = 0, idleTime = 0; +bool SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) { + Net::ThreadManager::get()->detach(); uptimeFile.seekg(0, std::ios::beg); @@ -44,19 +42,27 @@ bool SystemBackendProc::getUptimeInfo(const boost::function2> f; - if(uptimeFile.good()) - idleTime = (unsigned long)f; - - Net::ThreadManager::get()->pushWork(boost::bind(callback, uptime, idleTime)); + if(uptimeFile.good() && idleTime) + *idleTime = (unsigned long)f; return true; } -bool SystemBackendProc::getMemoryInfo(const boost::function4 &callback) { - unsigned long totalMem = 0, freeMem = 0, totalSwap = 0, freeSwap = 0; +bool SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) { + Net::ThreadManager::get()->detach(); + + if(totalMem) + *totalMem = 0; + if(freeMem) + *freeMem = 0; + if(totalSwap) + *totalSwap = 0; + if(freeSwap) + *freeSwap = 0; meminfoFile.seekg(0, std::ios::beg); @@ -71,30 +77,30 @@ bool SystemBackendProc::getMemoryInfo(const boost::function4pushWork(boost::bind(callback, totalMem, freeMem, totalSwap, freeSwap)); - return true; } -bool SystemBackendProc::getLoadInfo(const boost::function5 &callback) { - unsigned long currentLoad = 0, nProcesses = 0; - float loadAvg1 = 0, loadAvg5 = 0, loadAvg15 = 0; +bool SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) { + Net::ThreadManager::get()->detach(); + + unsigned long currentLoadValue = 0, nProcessesValue = 0; + float loadAvg1Value = 0, loadAvg5Value = 0, loadAvg15Value = 0; loadFile.seekg(0, std::ios::beg); @@ -107,9 +113,22 @@ bool SystemBackendProc::getLoadInfo(const boost::function5pushWork(boost::bind(callback, currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)); + if(loadAvg15) + *loadAvg15 = loadAvg15Value; return true; } diff --git a/src/modules/SystemBackendProc/SystemBackendProc.h b/src/modules/SystemBackendProc/SystemBackendProc.h index bf19e22..9b36df4 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.h +++ b/src/modules/SystemBackendProc/SystemBackendProc.h @@ -23,13 +23,14 @@ #include #include +#include namespace Mad { namespace Modules { -class SystemBackendProc : private Common::SystemBackend { +class SystemBackendProc : public Common::SystemBackend, boost::noncopyable { private: - static SystemBackendProc *backend; + static boost::shared_ptr backend; std::ifstream uptimeFile; std::ifstream meminfoFile; @@ -38,16 +39,16 @@ class SystemBackendProc : private Common::SystemBackend { SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} protected: - virtual bool getUptimeInfo(const boost::function2 &callback); - virtual bool getMemoryInfo(const boost::function4 &callback); - virtual bool getLoadInfo(const boost::function5 &callback); + virtual bool getUptimeInfo(unsigned long *uptime, unsigned long *idleTime); + virtual bool getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap); + virtual bool getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15); public: static void registerBackend() { if(backend) return; - backend = new SystemBackendProc(); + backend.reset(new SystemBackendProc()); Common::SystemManager::get()->registerBackend(backend); } @@ -56,8 +57,7 @@ class SystemBackendProc : private Common::SystemBackend { return; Common::SystemManager::get()->unregisterBackend(backend); - delete backend; - backend = 0; + backend.reset(); } }; -- cgit v1.2.3