diff options
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/RequestHandler.h | 8 | ||||
-rw-r--r-- | src/Common/RequestHandlers/FSInfoRequestHandler.cpp | 39 | ||||
-rw-r--r-- | src/Common/RequestHandlers/FSInfoRequestHandler.h | 3 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.cpp | 49 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.h | 33 | ||||
-rw-r--r-- | src/Common/SystemBackend.h | 12 | ||||
-rw-r--r-- | src/Common/SystemManager.cpp | 40 | ||||
-rw-r--r-- | src/Common/SystemManager.h | 28 |
8 files changed, 83 insertions, 129 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 <stdint.h> +#include <boost/cstdint.hpp> namespace Mad { namespace Common { @@ -36,14 +36,14 @@ class RequestHandler { boost::signal0<void> 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<void>& 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<SystemManager::FSInfo> fsInfo; - sendPacket(ret); - - signalFinished()(); - } -} - -void FSInfoRequestHandler::fsInfoHandler(const std::vector<SystemManager::FSInfo> &info) { XmlPacket ret; - ret.setType("OK"); - ret.addList("filesystems"); - for(std::vector<SystemManager::FSInfo>::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<SystemManager::FSInfo>::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<SystemManager::FSInfo> &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<void, unsigned long, unsigned long> &callback _UNUSED_PARAMETER_) { + virtual bool getUptimeInfo(unsigned long *uptime _UNUSED_PARAMETER_, unsigned long *idleTime _UNUSED_PARAMETER_) { return false; } - virtual bool getMemoryInfo(const boost::function4<void, unsigned long, unsigned long, unsigned long, unsigned long> &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<void, unsigned long, unsigned long, float, float, float> &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<void, const std::vector<SystemManager::FSInfo>& > &callback _UNUSED_PARAMETER_) { + virtual bool getFSInfo(std::vector<SystemManager::FSInfo> *fsInfo _UNUSED_PARAMETER_) { return false; } - virtual bool shutdown(const boost::function0<void> &callback _UNUSED_PARAMETER_) { + virtual bool shutdown() { return false; } - virtual bool reboot(const boost::function0<void> &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<SystemBackend> b1, boost::shared_ptr<SystemBackend> 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<void, unsigned long, unsigned long> &callback) { - for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getUptimeInfo(callback)) +bool SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) { + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->getUptimeInfo(uptime, idleTime)) return true; } return false; } -bool SystemManager::getMemoryInfo(const boost::function4<void, unsigned long, unsigned long, unsigned long, unsigned long> &callback) { - for(std::set<SystemBackend*>::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<boost::shared_ptr<SystemBackend> >::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<void, unsigned long, unsigned long, float, float, float> &callback) { - for(std::set<SystemBackend*>::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<boost::shared_ptr<SystemBackend> >::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<void, const std::vector<FSInfo>& > &callback) { - for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getFSInfo(callback)) +bool SystemManager::getFSInfo(std::vector<FSInfo> *fsInfo) { + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->getFSInfo(fsInfo)) return true; } return false; } -bool SystemManager::shutdown(const boost::function0<void> &callback) { - for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->shutdown(callback)) +bool SystemManager::shutdown() { + for(std::set<boost::shared_ptr<SystemBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->shutdown()) return true; } return false; } -bool SystemManager::reboot(const boost::function0<void> &callback) { - for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->reboot(callback)) +bool SystemManager::reboot() { + for(std::set<boost::shared_ptr<SystemBackend> >::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 <string> #include <vector> -#include <boost/function.hpp> +#include <boost/smart_ptr.hpp> 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<SystemBackend> b1, boost::shared_ptr<SystemBackend> b2); }; static SystemManager systemManager; - std::set<SystemBackend*, Compare> backends; + std::set<boost::shared_ptr<SystemBackend>, 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<SystemBackend> backend) { backends.insert(backend); } - void unregisterBackend(SystemBackend *backend) { + void unregisterBackend(boost::shared_ptr<SystemBackend> backend) { backends.erase(backend); } - bool getUptimeInfo(const boost::function2<void, unsigned long, unsigned long> &callback); - bool getMemoryInfo(const boost::function4<void, unsigned long, unsigned long, unsigned long, unsigned long> &callback); - bool getLoadInfo(const boost::function5<void, unsigned long, unsigned long, float, float, float> &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<void, const std::vector<FSInfo>& > &callback); + bool getFSInfo(std::vector<FSInfo> *fsInfo); - bool shutdown(const boost::function0<void> &callback); - bool reboot(const boost::function0<void> &callback); + bool shutdown(); + bool reboot(); static SystemManager *get() { return &systemManager; |