/* * HostStatusPacket.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "HostStatusPacket.h" #include #include #include 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, uint32_t currentLoad, uint32_t nProcesses, float loadAvg1val, float loadAvg5val, float loadAvg15val) : Packet(type, requestId), loadAvg1(loadAvg1val), loadAvg5(loadAvg5val), loadAvg15(loadAvg15val) { 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); coreStatusData->idleTime = htonl(idleTime); coreStatusData->totalMem = htonl(totalMem); 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); 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); } } } }