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 --- src/Common/RequestHandler.h | 8 +-- .../RequestHandlers/FSInfoRequestHandler.cpp | 39 ++++++----- src/Common/RequestHandlers/FSInfoRequestHandler.h | 3 - .../RequestHandlers/StatusRequestHandler.cpp | 49 ++++++-------- src/Common/RequestHandlers/StatusRequestHandler.h | 33 +--------- src/Common/SystemBackend.h | 12 ++-- src/Common/SystemManager.cpp | 40 ++++++------ src/Common/SystemManager.h | 28 ++++---- .../RequestHandlers/CommandRequestHandler.cpp | 34 ++++------ src/Daemon/RequestHandlers/CommandRequestHandler.h | 6 +- .../SystemBackendProc/SystemBackendProc.cpp | 75 ++++++++++++++-------- src/modules/SystemBackendProc/SystemBackendProc.h | 16 ++--- 12 files changed, 152 insertions(+), 191 deletions(-) diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h index 9c50345..d4a591a 100644 --- a/src/Common/RequestHandler.h +++ b/src/Common/RequestHandler.h @@ -23,7 +23,7 @@ #include "Connection.h" #include "XmlPacket.h" -#include +#include namespace Mad { namespace Common { @@ -36,14 +36,14 @@ class RequestHandler { boost::signal0 finished; Connection *connection; - uint16_t requestId; + boost::uint16_t requestId; // Prevent shallow copy RequestHandler(const RequestHandler &o); RequestHandler& operator=(const RequestHandler &o); protected: - RequestHandler(Connection *connection0, uint16_t requestId0) : connection(connection0), requestId(requestId0) {} + RequestHandler(Connection *connection0, boost::uint16_t requestId0) : connection(connection0), requestId(requestId0) {} boost::signal0& signalFinished() {return finished;} @@ -51,7 +51,7 @@ class RequestHandler { return connection; } - uint16_t getRequestId() const { + boost::uint16_t getRequestId() const { return requestId; } diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp index ed70a8b..663e512 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp @@ -43,31 +43,28 @@ void FSInfoRequestHandler::handlePacket(const XmlPacket &packet) { // TODO Require authentication - if(!SystemManager::get()->getFSInfo(boost::bind(&FSInfoRequestHandler::fsInfoHandler, this, _1))) { - XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + std::vector fsInfo; - sendPacket(ret); - - signalFinished()(); - } -} - -void FSInfoRequestHandler::fsInfoHandler(const std::vector &info) { XmlPacket ret; - ret.setType("OK"); - ret.addList("filesystems"); - for(std::vector::const_iterator fs = info.begin(); fs != info.end(); ++fs) { - ret["filesystems"].addEntry(); - XmlPacket::Entry &entry = ret["filesystems"].back(); + if(SystemManager::get()->getFSInfo(&fsInfo)) { + ret.setType("OK"); + ret.addList("filesystems"); + + for(std::vector::const_iterator fs = fsInfo.begin(); fs != fsInfo.end(); ++fs) { + ret["filesystems"].addEntry(); + XmlPacket::Entry &entry = ret["filesystems"].back(); - entry.add("name", fs->fsName); - entry.add("mountedOn", fs->mountedOn); - entry.add("totalSize", fs->total); - entry.add("usedSize", fs->used); - entry.add("availableSize", fs->available); + entry.add("name", fs->fsName); + entry.add("mountedOn", fs->mountedOn); + entry.add("totalSize", fs->total); + entry.add("usedSize", fs->used); + entry.add("availableSize", fs->available); + } + } + else { + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); } sendPacket(ret); diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.h b/src/Common/RequestHandlers/FSInfoRequestHandler.h index a4ed92c..47fcbd8 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.h +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.h @@ -30,9 +30,6 @@ namespace Common { namespace RequestHandlers { class FSInfoRequestHandler : public RequestHandler { - private: - void fsInfoHandler(const std::vector &info); - protected: virtual void handlePacket(const XmlPacket &packet); diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 0c13bd9..94ec5b2 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -44,40 +44,33 @@ void StatusRequestHandler::handlePacket(const XmlPacket &packet) { // TODO Require authentication - if(!SystemManager::get()->getUptimeInfo(boost::bind(&StatusRequestHandler::uptimeHandler, this, _1, _2))) - needUptime = false; - if(!SystemManager::get()->getMemoryInfo(boost::bind(&StatusRequestHandler::memoryHandler, this, _1, _2, _3, _4))) - needMemory = false; - if(!SystemManager::get()->getLoadInfo(boost::bind(&StatusRequestHandler::loadHandler, this, _1, _2, _3, _4, _5))) - needLoad = false; - - send(); -} - -void StatusRequestHandler::send() { - if(needUptime || needMemory || needLoad || sent) - return; + unsigned long uptime, idleTime; + unsigned long totalMem, freeMem, totalSwap, freeSwap; + unsigned long currentLoad, nProcesses; + float loadAvg1, loadAvg5, loadAvg15; - XmlPacket packet; - packet.setType("OK"); - packet.add("uptime", uptime); - packet.add("idleTime", idleTime); - packet.add("totalMem", totalMem); - packet.add("freeMem", freeMem); - packet.add("totalSwap", totalSwap); - packet.add("freeSwap", freeSwap); - packet.add("currentLoad", currentLoad); - packet.add("nProcesses", nProcesses); - packet.add("loadAvg1", loadAvg1); - packet.add("loadAvg5", loadAvg5); - packet.add("loadAvg15", loadAvg15); + SystemManager::get()->getUptimeInfo(&uptime, &idleTime); + SystemManager::get()->getMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap); + SystemManager::get()->getLoadInfo(¤tLoad, &nProcesses, &loadAvg1, &loadAvg5, &loadAvg15); - sendPacket(packet); + XmlPacket ret; + ret.setType("OK"); - sent = true; + ret.add("uptime", uptime); + ret.add("idleTime", idleTime); + ret.add("totalMem", totalMem); + ret.add("freeMem", freeMem); + ret.add("totalSwap", totalSwap); + ret.add("freeSwap", freeSwap); + ret.add("currentLoad", currentLoad); + ret.add("nProcesses", nProcesses); + ret.add("loadAvg1", loadAvg1); + ret.add("loadAvg5", loadAvg5); + ret.add("loadAvg15", loadAvg15); + sendPacket(ret); signalFinished()(); } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index 8c7d124..151852c 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -28,43 +28,12 @@ namespace Common { namespace RequestHandlers { class StatusRequestHandler : public RequestHandler { - private: - bool needUptime, needMemory, needLoad, sent; - - unsigned long uptime, idleTime; - unsigned long totalMem, freeMem, totalSwap, freeSwap; - unsigned long currentLoad, nProcesses; - float loadAvg1, loadAvg5, loadAvg15; - - void uptimeHandler(unsigned long uptime0, unsigned long idleTime0) { - uptime = uptime0; idleTime = idleTime0; - needUptime = false; - send(); - } - - void memoryHandler(unsigned long totalMem0, unsigned long freeMem0, unsigned long totalSwap0, unsigned long freeSwap0) { - totalMem = totalMem0; freeMem = freeMem0; totalSwap = totalSwap0; freeSwap = freeSwap0; - needMemory = false; - send(); - } - - void loadHandler(unsigned long currentLoad0, unsigned long nProcesses0, float loadAvg10, float loadAvg50, float loadAvg150) { - currentLoad = currentLoad0; nProcesses = nProcesses0; loadAvg1 = loadAvg10; loadAvg5 = loadAvg50; loadAvg15 = loadAvg150; - needLoad = false; - send(); - } - - void send(); - protected: virtual void handlePacket(const XmlPacket &packet); public: StatusRequestHandler(Connection *connection, uint16_t requestId) - : RequestHandler(connection, requestId), - needUptime(true), needMemory(true), needLoad(true), sent(false), - uptime(0), idleTime(0), totalMem(0), freeMem(0), totalSwap(0), freeSwap(0), - currentLoad(0), nProcesses(0), loadAvg1(0), loadAvg5(0), loadAvg15(0) {} + : RequestHandler(connection, requestId) {} }; } diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index ed553c2..7b9ee04 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -31,27 +31,27 @@ class SystemBackend { protected: friend class SystemManager; - virtual bool getUptimeInfo(const boost::function2 &callback _UNUSED_PARAMETER_) { + virtual bool getUptimeInfo(unsigned long *uptime _UNUSED_PARAMETER_, unsigned long *idleTime _UNUSED_PARAMETER_) { return false; } - virtual bool getMemoryInfo(const boost::function4 &callback _UNUSED_PARAMETER_) { + virtual bool getMemoryInfo(unsigned long *totalMem _UNUSED_PARAMETER_, unsigned long *freeMem _UNUSED_PARAMETER_, unsigned long *totalSwap _UNUSED_PARAMETER_, unsigned long *freeSwap _UNUSED_PARAMETER_) { return false; } - virtual bool getLoadInfo(const boost::function5 &callback _UNUSED_PARAMETER_) { + virtual bool getLoadInfo(unsigned long *currentLoad _UNUSED_PARAMETER_, unsigned long *nProcesses _UNUSED_PARAMETER_, float *loadAvg1 _UNUSED_PARAMETER_, float *loadAvg5 _UNUSED_PARAMETER_, float *loadAvg15 _UNUSED_PARAMETER_) { return false; } - virtual bool getFSInfo(const boost::function1& > &callback _UNUSED_PARAMETER_) { + virtual bool getFSInfo(std::vector *fsInfo _UNUSED_PARAMETER_) { return false; } - virtual bool shutdown(const boost::function0 &callback _UNUSED_PARAMETER_) { + virtual bool shutdown() { return false; } - virtual bool reboot(const boost::function0 &callback _UNUSED_PARAMETER_) { + virtual bool reboot() { return false; } diff --git a/src/Common/SystemManager.cpp b/src/Common/SystemManager.cpp index 4a549b7..c5eac0e 100644 --- a/src/Common/SystemManager.cpp +++ b/src/Common/SystemManager.cpp @@ -26,62 +26,62 @@ namespace Common { SystemManager SystemManager::systemManager; -bool SystemManager::Compare::operator() (const SystemBackend *b1, const SystemBackend *b2) { +bool SystemManager::Compare::operator() (boost::shared_ptr b1, boost::shared_ptr b2) { if(b1->getPriority() == b2->getPriority()) - return (b1 > b2); + return (b1.get() > b2.get()); else return (b1->getPriority() > b2->getPriority()); } -bool SystemManager::getUptimeInfo(const boost::function2 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getUptimeInfo(callback)) +bool SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->getUptimeInfo(uptime, idleTime)) return true; } return false; } -bool SystemManager::getMemoryInfo(const boost::function4 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getMemoryInfo(callback)) +bool SystemManager::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->getMemoryInfo(totalMem, freeMem, totalSwap, freeSwap)) return true; } return false; } -bool SystemManager::getLoadInfo(const boost::function5 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getLoadInfo(callback)) +bool SystemManager::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->getLoadInfo(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)) return true; } return false; } -bool SystemManager::getFSInfo(const boost::function1& > &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getFSInfo(callback)) +bool SystemManager::getFSInfo(std::vector *fsInfo) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->getFSInfo(fsInfo)) return true; } return false; } -bool SystemManager::shutdown(const boost::function0 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->shutdown(callback)) +bool SystemManager::shutdown() { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->shutdown()) return true; } return false; } -bool SystemManager::reboot(const boost::function0 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->reboot(callback)) +bool SystemManager::reboot() { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->reboot()) return true; } diff --git a/src/Common/SystemManager.h b/src/Common/SystemManager.h index 20d3c05..ef66db8 100644 --- a/src/Common/SystemManager.h +++ b/src/Common/SystemManager.h @@ -24,7 +24,7 @@ #include #include -#include +#include namespace Mad { namespace Common { @@ -34,12 +34,14 @@ class SystemBackend; class SystemManager { private: struct Compare { - bool operator() (const SystemBackend *b1, const SystemBackend *b2); + bool operator() (boost::shared_ptr b1, boost::shared_ptr b2); }; static SystemManager systemManager; - std::set backends; + std::set, Compare> backends; + + SystemManager() {} public: struct FSInfo { @@ -50,26 +52,22 @@ class SystemManager { long long available; }; - private: - SystemManager() {} - - public: - void registerBackend(SystemBackend *backend) { + void registerBackend(boost::shared_ptr backend) { backends.insert(backend); } - void unregisterBackend(SystemBackend *backend) { + void unregisterBackend(boost::shared_ptr backend) { backends.erase(backend); } - bool getUptimeInfo(const boost::function2 &callback); - bool getMemoryInfo(const boost::function4 &callback); - bool getLoadInfo(const boost::function5 &callback); + bool getUptimeInfo(unsigned long *uptime, unsigned long *idleTime); + bool getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap); + bool getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15); - bool getFSInfo(const boost::function1& > &callback); + bool getFSInfo(std::vector *fsInfo); - bool shutdown(const boost::function0 &callback); - bool reboot(const boost::function0 &callback); + bool shutdown(); + bool reboot(); static SystemManager *get() { return &systemManager; diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp index f7f21de..952837c 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp @@ -22,8 +22,6 @@ #include #include -#include - namespace Mad { namespace Daemon { namespace RequestHandlers { @@ -47,30 +45,24 @@ void CommandRequestHandler::handlePacket(const Common::XmlPacket &packet) { std::string command = packet["command"]; - if(command == "reboot") { - if(Common::SystemManager::get()->shutdown(boost::bind(&CommandRequestHandler::sendReply, this))) - return; + bool ok; + + if(command == "reboot") + ok = Common::SystemManager::get()->shutdown(); + else + ok = Common::SystemManager::get()->reboot(); + + Common::XmlPacket ret; + + if(ok) { + ret.setType("OK"); } else { - if(Common::SystemManager::get()->reboot(boost::bind(&CommandRequestHandler::sendReply, this))) - return; + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); } - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); - sendPacket(ret); - - signalFinished()(); -} - -void CommandRequestHandler::sendReply() { - Common::XmlPacket packet; - packet.setType("OK"); - - sendPacket(packet); - signalFinished()(); } diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.h b/src/Daemon/RequestHandlers/CommandRequestHandler.h index 73aced0..a033c75 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.h +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.h @@ -21,21 +21,17 @@ #define MAD_DAEMON_REQUESTHANDLERS_COMMANDREQUESTHANDLER_H_ #include -#include namespace Mad { namespace Daemon { namespace RequestHandlers { class CommandRequestHandler : public Common::RequestHandler { - private: - void sendReply(); - protected: virtual void handlePacket(const Common::XmlPacket &packet); public: - CommandRequestHandler(Common::Connection *connection, uint16_t requestId) + CommandRequestHandler(Common::Connection *connection, boost::uint16_t requestId) : RequestHandler(connection, requestId) {} }; 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