summaryrefslogtreecommitdiffstats
path: root/src/Net/Packets/HostStatusPacket.cpp
blob: f998c2c974c1615cf4d4e34b65fbec74ae16c816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * HostStatusPacket.cpp
 *
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#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,
    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);
}

}
}
}