Debian Codegraph @

src/fakemonitor.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2014 Kevin Funk <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "fakemonitor.h"
22
23 #include "statusview.h"
24 #include "job.h"
25 #include "hostinfo.h"
26
27 #include <QDebug>
28 #include <QStringList>
29 #include <QTime>
30 #include <QTimer>
31 #include <QRandomGenerator>
32
33 #include <icecc/comm.h>
34
35 namespace {
36 // counter variable
37 int JOB_ID = 0;
38
39 const int MAX_JOB_COUNT = 10;
40 const int MAX_HOST_COUNT = 40;
41 const int MAX_JOB_SIZE = 1024 * 1024 * 24;
42
43 const QStringList JOB_FILENAMES(QStringList()
44 << QStringLiteral("/tmp/filename.cc")
45 << QStringLiteral("/some/very/long/path/containing/filename.cc")
46 << QStringLiteral("/some/very/long/path/containing/averyverylongfilename.cc")
47 );
48
49 const QStringList HOST_NAMES(QStringList()
50 << QStringLiteral("Hostname")
51 << QStringLiteral("VeryLongHostname")
52 << QStringLiteral("VeryLongHostname.localdomain")
53 );
54
55 QString randomPlatform()
56 {
57 static const QStringList hostNames = {QStringLiteral("Linux 2.6"), QStringLiteral("Linux 3.2"), QStringLiteral("Linux 3.6")};
58 return hostNames[QRandomGenerator::global()->generate() % hostNames.size()];
59 }
60 }
61
62 FakeMonitor::FakeMonitor(HostInfoManager *manager, QObject *parent)
63 : Monitor(manager, parent)
64 , m_updateTimer(new QTimer(this))
65 {
66 m_updateTimer->setInterval(200);
67 m_updateTimer->start();
68 connect(m_updateTimer, &QTimer::timeout, this, &FakeMonitor::update);
69
70 setSchedulerState(Online);
71
72 for (HostId i = 0; i < MAX_HOST_COUNT; ++i) {
73 createHostInfo(i + 1);
74 }
75 }
76
77 void FakeMonitor::createHostInfo(HostId id)
78 {
79 HostInfo info(id);
80 info.setIp(QStringLiteral("1.0.0.%1").arg(id));
81 info.setMaxJobs(5);
82 info.setName(HOST_NAMES[id % HOST_NAMES.length()] + QString::number(id));
83 info.setColor(info.createColor(info.name()));
84 info.setOffline(false);
85 info.setNoRemote(false);
86 info.setPlatform(randomPlatform());
87 info.setProtocol(PROTOCOL_VERSION);
88 info.setFeatures(id % 2 == 0 ? QStringLiteral("env_xz") : QStringLiteral("env_zstd"));
89 info.setServerLoad(1.0);
90 info.setServerSpeed(10);
91 hostInfoManager()->checkNode(info);
92 }
93
94 void FakeMonitor::update()
95 {
96 // create job
97 const int clientId = (JOB_ID % MAX_HOST_COUNT) + 1;
98 const QString fileName = JOB_FILENAMES[JOB_ID % JOB_FILENAMES.length()];
99 Job job(JOB_ID++, clientId, fileName);
100 time_t rawtime;
101 time(&rawtime);
102 job.startTime = rawtime;
103 job.state = Job::Compiling;
104 job.in_compressed = QRandomGenerator::global()->generate() % MAX_JOB_SIZE * 0.75; // random factor
105 job.in_uncompressed = QRandomGenerator::global()->generate() % MAX_JOB_SIZE;
106 job.in_compressed = QRandomGenerator::global()->generate() % MAX_JOB_SIZE ;
107 job.in_uncompressed = QRandomGenerator::global()->generate() % MAX_JOB_SIZE * 0.75; // random factor
108 job.real_msec = 200;
109 const int serverId = ((JOB_ID + 1) % MAX_HOST_COUNT) + 1;
110 job.server = serverId;
111 emit jobUpdated(job);
112 m_activeJobs << job;
113
114 // clean up old jobs
115 if (m_activeJobs.size() > MAX_JOB_COUNT) {
116 Job job = m_activeJobs.first();
117 m_activeJobs.removeFirst();
118 job.state = Job::Finished;
119 emit jobUpdated(job);
120 }
121
122 Q_FOREACH(const HostInfo * info, hostInfoManager()->hostMap().values()) {
123 if (info->isOffline()) {
124 emit nodeRemoved(info->id());
125 } else {
126 emit nodeUpdated(info->id());
127 }
128 }
129 }