diff options
Diffstat (limited to 'src/Common/RequestHandlers')
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.cpp | 29 | ||||
-rw-r--r-- | src/Common/RequestHandlers/StatusRequestHandler.h | 36 |
2 files changed, 57 insertions, 8 deletions
diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index dbdf9e1..8494fee 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -28,10 +28,10 @@ namespace Mad { namespace Common { namespace RequestHandlers { -void StatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { +void StatusRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { if(packet.getType() != Net::Packet::STATUS) { Logger::log(Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::UNEXPECTED_PACKET))); + con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::UNEXPECTED_PACKET))); signalFinished().emit(); return; @@ -39,12 +39,27 @@ void StatusRequestHandler::handlePacket(Net::Connection *connection, const Net:: // TODO Require authentication - SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getUptimeInfo(); - SystemBackend::MemoryInfo memInfo = SystemBackend::getMemoryInfo(); - SystemBackend::LoadInfo loadInfo = SystemBackend::getLoadInfo(); + connection = con; + requestId = packet.getRequestId(); - 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)); + if(!SystemBackend::getUptimeInfo(sigc::mem_fun(this, &StatusRequestHandler::uptimeHandler))) + needUptime = false; + if(!SystemBackend::getMemoryInfo(sigc::mem_fun(this, &StatusRequestHandler::memoryHandler))) + needMemory = false; + if(!SystemBackend::getLoadInfo(sigc::mem_fun(this, &StatusRequestHandler::loadHandler))) + needLoad = false; + + send(); +} + +void StatusRequestHandler::send() { + if(needUptime || needMemory || needLoad || sent) + return; + + connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, requestId, uptime, idleTime, + totalMem, freeMem, totalSwap, freeSwap, currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)); + + sent = true; signalFinished().emit(); } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index c5dcbf7..fd907de 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -21,17 +21,51 @@ #define MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ #include "../RequestHandler.h" +#include <stdint.h> namespace Mad { namespace Common { namespace RequestHandlers { class StatusRequestHandler : public RequestHandler { + private: + Net::Connection *connection; + uint16_t requestId; + + 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(Net::Connection *connection, const Net::Packet &packet); public: - StatusRequestHandler() {} + StatusRequestHandler() : 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) {} }; } |