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
32 #include <icecc/comm.h>
33
34 namespace {
35 // counter variable
36 int JOB_ID = 0;
37
38 const int MAX_JOB_COUNT = 10;
39 const int MAX_HOST_COUNT = 40;
40 const int MAX_JOB_SIZE = 1024 * 1024 * 24;
41
42 const QStringList JOB_FILENAMES(QStringList()
43 << QStringLiteral("/tmp/filename.cc")
44 << QStringLiteral("/some/very/long/path/containing/filename.cc")
45 << QStringLiteral("/some/very/long/path/containing/averyverylongfilename.cc")
46 );
47
48 const QStringList HOST_NAMES(QStringList()
49 << QStringLiteral("Hostname")
50 << QStringLiteral("VeryLongHostname")
51 << QStringLiteral("VeryLongHostname.localdomain")
52 );
53
54 QString randomPlatform()
55 {
56 static const QStringList hostNames = {QStringLiteral("Linux 2.6"), QStringLiteral("Linux 3.2"), QStringLiteral("Linux 3.6")};
57 return hostNames[qrand() % hostNames.size()];
58 }
59 }
60
61 FakeMonitor::FakeMonitor(HostInfoManager *manager, QObject *parent)
62 : Monitor(manager, parent)
63 , m_updateTimer(new QTimer(this))
64 {
65 m_updateTimer->setInterval(200);
66 m_updateTimer->start();
67 connect(m_updateTimer, SIGNAL(timeout()), SLOT(update()));
68
69 setSchedulerState(Online);
70
71 for (HostId i = 0; i < MAX_HOST_COUNT; ++i) {
72 createHostInfo(i + 1);
73 }
74
75 qsrand(QTime::currentTime().msec());
76 }
77
78 void FakeMonitor::createHostInfo(HostId id)
79 {
80 HostInfo info(id);
81 info.setIp(QStringLiteral("1.0.0.%1").arg(id));
82 info.setMaxJobs(5);
83 info.setName(HOST_NAMES[id % HOST_NAMES.length()] + QString::number(id));
84 info.setColor(info.createColor(info.name()));
85 info.setOffline(false);
86 info.setNoRemote(false);
87 info.setPlatform(randomPlatform());
88 info.setProtocol(PROTOCOL_VERSION);
89 info.setFeatures(id % 2 == 0 ? QStringLiteral("env_xz") : QStringLiteral("env_zstd"));
90 info.setServerLoad(1.0);
91 info.setServerSpeed(10);
92 hostInfoManager()->checkNode(info);
93 }
94
95 void FakeMonitor::update()
96 {
97 // create job
98 const int clientId = (JOB_ID % MAX_HOST_COUNT) + 1;
99 const QString fileName = JOB_FILENAMES[JOB_ID % JOB_FILENAMES.length()];
100 Job job(JOB_ID++, clientId, fileName);
101 time_t rawtime;
102 time(&rawtime);
103 job.startTime = rawtime;
104 job.state = Job::Compiling;
105 job.in_compressed = qrand() % MAX_JOB_SIZE * 0.75; // random factor
106 job.in_uncompressed = qrand() % MAX_JOB_SIZE;
107 job.in_compressed = qrand() % MAX_JOB_SIZE ;
108 job.in_uncompressed = qrand() % MAX_JOB_SIZE * 0.75; // random factor
109 job.real_msec = 200;
110 const int serverId = ((JOB_ID + 1) % MAX_HOST_COUNT) + 1;
111 job.server = serverId;
112 emit jobUpdated(job);
113 m_activeJobs << job;
114
115 // clean up old jobs
116 if (m_activeJobs.size() > MAX_JOB_COUNT) {
117 Job job = m_activeJobs.first();
118 m_activeJobs.removeFirst();
119 job.state = Job::Finished;
120 emit jobUpdated(job);
121 }
122
123 Q_FOREACH(const HostInfo * info, hostInfoManager()->hostMap().values()) {
124 if (info->isOffline()) {
125 emit nodeRemoved(info->id());
126 } else {
127 emit nodeUpdated(info->id());
128 }
129 }
130 }