diff options
Diffstat (limited to 'src/Net/Packets')
-rw-r--r-- | src/Net/Packets/HostListPacket.h | 7 | ||||
-rw-r--r-- | src/Net/Packets/HostStatusPacket.cpp | 31 | ||||
-rw-r--r-- | src/Net/Packets/HostStatusPacket.h | 40 |
3 files changed, 69 insertions, 9 deletions
diff --git a/src/Net/Packets/HostListPacket.h b/src/Net/Packets/HostListPacket.h index aba6461..4d0b2d0 100644 --- a/src/Net/Packets/HostListPacket.h +++ b/src/Net/Packets/HostListPacket.h @@ -61,6 +61,13 @@ class HostListPacket : public Packet { return *this; } + HostListPacket& operator=(const HostListPacket &p) { + Packet::operator=(p); + parsePacket(); + + return *this; + } + const std::vector<Common::HostInfo>& getHostInfo() const { return hostList; } diff --git a/src/Net/Packets/HostStatusPacket.cpp b/src/Net/Packets/HostStatusPacket.cpp index 3e50cd6..f998c2c 100644 --- a/src/Net/Packets/HostStatusPacket.cpp +++ b/src/Net/Packets/HostStatusPacket.cpp @@ -19,16 +19,24 @@ #include "HostStatusPacket.h" +#include <cstdio> +#include <cstring> +#include <string> + namespace Mad { namespace Net { namespace Packets { HostStatusPacket::HostStatusPacket(Type type, uint16_t requestId, uint32_t uptime, uint32_t idleTime, - uint32_t totalMem, uint32_t freeMem, uint32_t totalSwap, uint32_t freeSwap) -: Packet(type, requestId) + uint32_t totalMem, uint32_t freeMem, uint32_t totalSwap, uint32_t freeSwap, + uint32_t currentLoad, uint32_t nProcesses, float loadAvg1val, float loadAvg5val, float loadAvg15val) +: Packet(type, requestId), loadAvg1(loadAvg1val), loadAvg5(loadAvg5val), loadAvg15(loadAvg15val) { - setLength(sizeof(CoreStatusData)); + char buf[20]; + std::snprintf(buf, sizeof(buf), "%.2f %.2f %.2f", loadAvg1, loadAvg5, loadAvg15); + + setLength(sizeof(CoreStatusData) + strlen(buf)); coreStatusData = (CoreStatusData*)&rawData->data; coreStatusData->uptime = htonl(uptime); @@ -38,17 +46,30 @@ HostStatusPacket::HostStatusPacket(Type type, uint16_t requestId, uint32_t uptim coreStatusData->freeMem = htonl(freeMem); coreStatusData->totalSwap = htonl(totalSwap); coreStatusData->freeSwap = htonl(freeSwap); + + coreStatusData->currentLoad = htonl(currentLoad); + coreStatusData->nProcesses = htonl(nProcesses); + + std::memcpy(coreStatusData->charData, buf, strlen(buf)); } HostStatusPacket& HostStatusPacket::operator=(const Packet &p) { Packet::operator=(p); - - setLength(sizeof(CoreStatusData)); + if(getLength() < sizeof(CoreStatusData)) + setLength(sizeof(CoreStatusData)); coreStatusData = (CoreStatusData*)&rawData->data; + parsePacket(); + return *this; } +void HostStatusPacket::parsePacket() { + coreStatusData = (CoreStatusData*)&rawData->data; + + std::sscanf(std::string((char*)coreStatusData->charData, getLength()-sizeof(coreStatusData)).c_str(), "%f %f %f", &loadAvg1, &loadAvg5, &loadAvg15); +} + } } } diff --git a/src/Net/Packets/HostStatusPacket.h b/src/Net/Packets/HostStatusPacket.h index 0de63de..fa0910c 100644 --- a/src/Net/Packets/HostStatusPacket.h +++ b/src/Net/Packets/HostStatusPacket.h @@ -36,21 +36,33 @@ class HostStatusPacket : public Packet { uint32_t freeMem; uint32_t totalSwap; uint32_t freeSwap; + + uint32_t currentLoad; + uint32_t nProcesses; + + uint8_t charData[0]; }; CoreStatusData *coreStatusData; + float loadAvg1, loadAvg5, loadAvg15; + + void parsePacket(); + public: HostStatusPacket(Type type, uint16_t requestId, uint32_t uptime = 0, uint32_t idleTime = 0, - uint32_t totalMem = 0, uint32_t freeMem = 0, uint32_t totalSwap = 0, uint32_t freeSwap = 0); + uint32_t totalMem = 0, uint32_t freeMem = 0, uint32_t totalSwap = 0, uint32_t freeSwap = 0, + uint32_t currentLoad = 0, uint32_t nProcesses = 0, float loadAvg1val = 0, float loadAvg5val = 0, float loadAvg15val = 0); HostStatusPacket(const Packet &p) : Packet(p) { - setLength(sizeof(CoreStatusData)); - coreStatusData = (CoreStatusData*)&rawData->data; + if(getLength() < sizeof(CoreStatusData)) + setLength(sizeof(CoreStatusData)); + + parsePacket(); } HostStatusPacket(const HostStatusPacket &p) : Packet(p) { - coreStatusData = (CoreStatusData*)&rawData->data; + parsePacket(); } HostStatusPacket& operator=(const Packet &p); @@ -82,6 +94,26 @@ class HostStatusPacket : public Packet { uint32_t getFreeSwap() const { return ntohl(coreStatusData->freeSwap); } + + uint32_t getCurrentLoad() const { + return ntohl(coreStatusData->currentLoad); + } + + uint32_t getProcessNumber() const { + return ntohl(coreStatusData->nProcesses); + } + + float getLoadAverage1() const { + return loadAvg1; + } + + float getLoadAverage5() const { + return loadAvg5; + } + + float getLoadAverage15() const { + return loadAvg15; + } }; } |