diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-05-22 14:21:06 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-05-22 14:21:06 +0200 |
commit | 0480576d43fb7ddcc16de9dab0619a1424f129fd (patch) | |
tree | 633f11b0ee7b9bfe20478cd47e8d6ec397627ad5 /src | |
parent | 264cd7947d7291f78065f12824523ba6178a9936 (diff) | |
download | mad-0480576d43fb7ddcc16de9dab0619a1424f129fd.tar mad-0480576d43fb7ddcc16de9dab0619a1424f129fd.zip |
SystemBackend ?berarbeitet
Diffstat (limited to 'src')
-rw-r--r-- | src/Common/RequestHandlers/FSInfoRequestHandler.cpp | 11 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.cpp | 11 | ||||
-rw-r--r-- | src/Common/SystemBackend.h | 24 | ||||
-rw-r--r-- | src/Common/SystemManager.cpp | 96 | ||||
-rw-r--r-- | src/Common/SystemManager.h | 14 | ||||
-rw-r--r-- | src/Daemon/RequestHandlers/CommandRequestHandler.cpp | 21 | ||||
-rw-r--r-- | src/modules/SystemBackendPosix/SystemBackendPosix.cpp | 18 | ||||
-rw-r--r-- | src/modules/SystemBackendPosix/SystemBackendPosix.h | 6 | ||||
-rw-r--r-- | src/modules/SystemBackendProc/SystemBackendProc.cpp | 22 | ||||
-rw-r--r-- | src/modules/SystemBackendProc/SystemBackendProc.h | 6 |
10 files changed, 142 insertions, 87 deletions
diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp index 663e512..94756b5 100644 --- a/src/Common/RequestHandlers/FSInfoRequestHandler.cpp +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.cpp @@ -47,7 +47,9 @@ void FSInfoRequestHandler::handlePacket(const XmlPacket &packet) { XmlPacket ret; - if(SystemManager::get()->getFSInfo(&fsInfo)) { + try { + SystemManager::get()->getFSInfo(&fsInfo); + ret.setType("OK"); ret.addList("filesystems"); @@ -62,9 +64,12 @@ void FSInfoRequestHandler::handlePacket(const XmlPacket &packet) { entry.add("availableSize", fs->available); } } - else { + catch(Net::Exception e) { ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); } sendPacket(ret); diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 94ec5b2..c564680 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -50,12 +50,15 @@ void StatusRequestHandler::handlePacket(const XmlPacket &packet) { float loadAvg1, loadAvg5, loadAvg15; + XmlPacket ret; - SystemManager::get()->getUptimeInfo(&uptime, &idleTime); - SystemManager::get()->getMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap); - SystemManager::get()->getLoadInfo(¤tLoad, &nProcesses, &loadAvg1, &loadAvg5, &loadAvg15); + try { + SystemManager::get()->getUptimeInfo(&uptime, &idleTime); + SystemManager::get()->getMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap); + SystemManager::get()->getLoadInfo(¤tLoad, &nProcesses, &loadAvg1, &loadAvg5, &loadAvg15); + } + catch(Net::Exception e) {} - XmlPacket ret; ret.setType("OK"); ret.add("uptime", uptime); diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index 7b9ee04..1543207 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -31,28 +31,28 @@ class SystemBackend { protected: friend class SystemManager; - virtual bool getUptimeInfo(unsigned long *uptime _UNUSED_PARAMETER_, unsigned long *idleTime _UNUSED_PARAMETER_) { - return false; + virtual void getUptimeInfo(unsigned long *uptime _UNUSED_PARAMETER_, unsigned long *idleTime _UNUSED_PARAMETER_) throw(Net::Exception) { + throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); } - 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 void getMemoryInfo(unsigned long *totalMem _UNUSED_PARAMETER_, unsigned long *freeMem _UNUSED_PARAMETER_, unsigned long *totalSwap _UNUSED_PARAMETER_, unsigned long *freeSwap _UNUSED_PARAMETER_) throw(Net::Exception) { + throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); } - 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 void getLoadInfo(unsigned long *currentLoad _UNUSED_PARAMETER_, unsigned long *nProcesses _UNUSED_PARAMETER_, float *loadAvg1 _UNUSED_PARAMETER_, float *loadAvg5 _UNUSED_PARAMETER_, float *loadAvg15 _UNUSED_PARAMETER_) throw(Net::Exception) { + throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); } - virtual bool getFSInfo(std::vector<SystemManager::FSInfo> *fsInfo _UNUSED_PARAMETER_) { - return false; + virtual void getFSInfo(std::vector<SystemManager::FSInfo> *fsInfo _UNUSED_PARAMETER_) throw(Net::Exception) { + throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); } - virtual bool shutdown() { - return false; + virtual void shutdown() throw(Net::Exception) { + throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); } - virtual bool reboot() { - return false; + virtual void reboot() throw(Net::Exception) { + throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); } virtual int getPriority() const { diff --git a/src/Common/SystemManager.cpp b/src/Common/SystemManager.cpp index c5eac0e..3c89a33 100644 --- a/src/Common/SystemManager.cpp +++ b/src/Common/SystemManager.cpp @@ -34,58 +34,106 @@ bool SystemManager::Compare::operator() (boost::shared_ptr<SystemBackend> b1, bo } -bool SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) { +void SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getUptimeInfo(uptime, idleTime)) - return true; + try { + (*backend)->getUptimeInfo(uptime, idleTime); + return; + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } -bool SystemManager::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) { +void SystemManager::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getMemoryInfo(totalMem, freeMem, totalSwap, freeSwap)) - return true; + try { + (*backend)->getMemoryInfo(totalMem, freeMem, totalSwap, freeSwap); + return; + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } -bool SystemManager::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) { +void SystemManager::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getLoadInfo(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)) - return true; + try { + (*backend)->getLoadInfo(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15); + return; + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } -bool SystemManager::getFSInfo(std::vector<FSInfo> *fsInfo) { +void SystemManager::getFSInfo(std::vector<FSInfo> *fsInfo) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getFSInfo(fsInfo)) - return true; + try { + (*backend)->getFSInfo(fsInfo); + return; + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } -bool SystemManager::shutdown() { +void SystemManager::shutdown() throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->shutdown()) - return true; + try { + (*backend)->shutdown(); + return; + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } -bool SystemManager::reboot() { +void SystemManager::reboot() throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->reboot()) - return true; + try { + (*backend)->reboot(); + return; + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } } diff --git a/src/Common/SystemManager.h b/src/Common/SystemManager.h index ef66db8..9252b2b 100644 --- a/src/Common/SystemManager.h +++ b/src/Common/SystemManager.h @@ -26,6 +26,8 @@ #include <boost/smart_ptr.hpp> +#include <Net/Exception.h> + namespace Mad { namespace Common { @@ -60,14 +62,14 @@ class SystemManager { backends.erase(backend); } - 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); + void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception); + void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception); + void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception); - bool getFSInfo(std::vector<FSInfo> *fsInfo); + void getFSInfo(std::vector<FSInfo> *fsInfo) throw(Net::Exception); - bool shutdown(); - bool reboot(); + void shutdown() throw(Net::Exception); + void reboot() throw(Net::Exception); static SystemManager *get() { return &systemManager; diff --git a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp index 952837c..f8b7c31 100644 --- a/src/Daemon/RequestHandlers/CommandRequestHandler.cpp +++ b/src/Daemon/RequestHandlers/CommandRequestHandler.cpp @@ -45,21 +45,22 @@ void CommandRequestHandler::handlePacket(const Common::XmlPacket &packet) { std::string command = packet["command"]; - bool ok; - - if(command == "reboot") - ok = Common::SystemManager::get()->shutdown(); - else - ok = Common::SystemManager::get()->reboot(); - Common::XmlPacket ret; - if(ok) { + try { + if(command == "reboot") + Common::SystemManager::get()->shutdown(); + else + Common::SystemManager::get()->reboot(); + ret.setType("OK"); } - else { + catch(Net::Exception e) { ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); } sendPacket(ret); diff --git a/src/modules/SystemBackendPosix/SystemBackendPosix.cpp b/src/modules/SystemBackendPosix/SystemBackendPosix.cpp index a215c86..35e6c4f 100644 --- a/src/modules/SystemBackendPosix/SystemBackendPosix.cpp +++ b/src/modules/SystemBackendPosix/SystemBackendPosix.cpp @@ -29,12 +29,12 @@ namespace Modules { boost::shared_ptr<SystemBackendPosix> SystemBackendPosix::backend; -bool SystemBackendPosix::getFSInfo(std::vector<Common::SystemManager::FSInfo> *fsInfo) { +void SystemBackendPosix::getFSInfo(std::vector<Common::SystemManager::FSInfo> *fsInfo) throw(Net::Exception) { Net::ThreadManager::get()->detach(); FILE *pipe = popen("/bin/df -P -k", "r"); if(!pipe) - return false; + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); char buffer[1024]; std::string output; @@ -47,7 +47,7 @@ bool SystemBackendPosix::getFSInfo(std::vector<Common::SystemManager::FSInfo> *f pclose(pipe); if(!fsInfo) - return true; + return; fsInfo->clear(); @@ -75,19 +75,21 @@ bool SystemBackendPosix::getFSInfo(std::vector<Common::SystemManager::FSInfo> *f delete [] mountedOn; } - return true; + return; } -bool SystemBackendPosix::shutdown() { +void SystemBackendPosix::shutdown() throw(Net::Exception) { Net::ThreadManager::get()->detach(); - return (system("/sbin/halt") == 0); + if(system("/sbin/halt") != 0) + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); } -bool SystemBackendPosix::reboot() { +void SystemBackendPosix::reboot() throw(Net::Exception) { Net::ThreadManager::get()->detach(); - return (system("/sbin/reboot") == 0); + if(system("/sbin/reboot") != 0) + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); } } diff --git a/src/modules/SystemBackendPosix/SystemBackendPosix.h b/src/modules/SystemBackendPosix/SystemBackendPosix.h index 2ec026d..3cdb43c 100644 --- a/src/modules/SystemBackendPosix/SystemBackendPosix.h +++ b/src/modules/SystemBackendPosix/SystemBackendPosix.h @@ -38,10 +38,10 @@ class SystemBackendPosix : public Common::SystemBackend { static boost::shared_ptr<SystemBackendPosix> backend; protected: - virtual bool getFSInfo(std::vector<Common::SystemManager::FSInfo> *fsInfo); + virtual void getFSInfo(std::vector<Common::SystemManager::FSInfo> *fsInfo) throw(Net::Exception); - virtual bool shutdown(); - virtual bool reboot(); + virtual void shutdown() throw(Net::Exception); + virtual void reboot() throw(Net::Exception); public: ~SystemBackendPosix(); diff --git a/src/modules/SystemBackendProc/SystemBackendProc.cpp b/src/modules/SystemBackendProc/SystemBackendProc.cpp index 1888227..ab074b2 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.cpp +++ b/src/modules/SystemBackendProc/SystemBackendProc.cpp @@ -29,18 +29,18 @@ namespace Modules { boost::shared_ptr<SystemBackendProc> SystemBackendProc::backend; -bool SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) { +void SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception) { Net::ThreadManager::get()->detach(); uptimeFile.seekg(0, std::ios::beg); if(!uptimeFile.good()) - return false; + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); float f; uptimeFile >> f; if(!uptimeFile.good()) - return false; + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); if(uptime) *uptime = (unsigned long)f; @@ -48,11 +48,9 @@ bool SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idle uptimeFile >> f; if(uptimeFile.good() && idleTime) *idleTime = (unsigned long)f; - - return true; } -bool SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) { +void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception) { Net::ThreadManager::get()->detach(); if(totalMem) @@ -67,7 +65,7 @@ bool SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *fr meminfoFile.seekg(0, std::ios::beg); if(!meminfoFile.good()) - return false; + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); while(!meminfoFile.eof() && meminfoFile.good()) { std::string line; @@ -92,11 +90,9 @@ bool SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *fr if((!totalMem || *totalMem) && (!freeMem || *freeMem) && (!totalSwap || *totalSwap) && (!freeSwap || *freeSwap)) break; } - - return true; } -bool SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) { +void SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception) { Net::ThreadManager::get()->detach(); unsigned long currentLoadValue = 0, nProcessesValue = 0; @@ -105,13 +101,13 @@ bool SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *n loadFile.seekg(0, std::ios::beg); if(!loadFile.good()) - return false; + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); std::string line; std::getline(loadFile, line); if(line.empty()) - return false; + throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1Value, &loadAvg5Value, &loadAvg15Value, ¤tLoadValue, &nProcessesValue); @@ -129,8 +125,6 @@ bool SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *n if(loadAvg15) *loadAvg15 = loadAvg15Value; - - return true; } } diff --git a/src/modules/SystemBackendProc/SystemBackendProc.h b/src/modules/SystemBackendProc/SystemBackendProc.h index 9b36df4..eb2f8a6 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.h +++ b/src/modules/SystemBackendProc/SystemBackendProc.h @@ -39,9 +39,9 @@ class SystemBackendProc : public Common::SystemBackend, boost::noncopyable { SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} protected: - 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); + virtual void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception); + virtual void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception); + virtual void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception); public: static void registerBackend() { |