Debian Codegraph @

src/hostinfo.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2004 Cornelius Schumacher <[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 "hostinfo.h"
22
23 #include <QApplication>
24
25 #include <qdebug.h>
26
27 QVector<QColor> HostInfo::mColorTable;
28 QMap<int, QString> HostInfo::mColorNameMap;
29
30 void HostInfo::initColorTable()
31 {
32 initColor(QStringLiteral("#A5080B"), QApplication::tr("cherry"));
33 initColor(QStringLiteral("#76d26f"), QApplication::tr("pistachio"));
34 initColor(QStringLiteral("#664a08"), QApplication::tr("chocolate"));
35 initColor(QStringLiteral("#4c9dff"), QApplication::tr("smurf"));
36 initColor(QStringLiteral("#6c2ca8"), QApplication::tr("blueberry"));
37 initColor(QStringLiteral("#fa8344"), QApplication::tr("orange"));
38 initColor(QStringLiteral("#55CFBD"), QApplication::tr("mint"));
39 initColor(QStringLiteral("#db1230"), QApplication::tr("strawberry"));
40 initColor(QStringLiteral("#a6ea5e"), QApplication::tr("apple"));
41 initColor(QStringLiteral("#D6A3D8"), QApplication::tr("bubblegum"));
42
43 initColor(QStringLiteral("#f2aa4d"), QApplication::tr("peach"));
44 initColor(QStringLiteral("#aa1387"), QApplication::tr("plum"));
45 initColor(QStringLiteral("#26c3f7"), QApplication::tr("polar sea"));
46 initColor(QStringLiteral("#b8850e"), QApplication::tr("nut"));
47 initColor(QStringLiteral("#6a188d"), QApplication::tr("blackberry"));
48 initColor(QStringLiteral("#24b063"), QApplication::tr("woodruff"));
49 initColor(QStringLiteral("#ffff0f"), QApplication::tr("banana"));
50 initColor(QStringLiteral("#1e1407"), QApplication::tr("mocha"));
51 initColor(QStringLiteral("#29B450"), QApplication::tr("kiwi"));
52 initColor(QStringLiteral("#F8DD31"), QApplication::tr("lemon"));
53
54 initColor(QStringLiteral("#fa7e91"), QApplication::tr("raspberry"));
55 initColor(QStringLiteral("#c5a243"), QApplication::tr("caramel"));
56 initColor(QStringLiteral("#b8bcff"), QApplication::tr("blueberry"));
57 initColor(QStringLiteral("#af3765"), QApplication::tr("blackcurrant"));
58 initColor(QStringLiteral("#f7d36f"), QApplication::tr("passionfruit"));
59 initColor(QStringLiteral("#d51013"), QApplication::tr("pomegranate"));
60 initColor(QStringLiteral("#C2C032"), QApplication::tr("pumpkin"));
61 initColor(QStringLiteral("#f0e8e3"), QApplication::tr("vanilla"));
62 initColor(QStringLiteral("#d8e0e3"), QApplication::tr("stracciatella"));
63 // try to make the count a prime number (reminder: 19, 23, 29, 31)
64 }
65
66 void HostInfo::initColor(const QString &value, const QString &name)
67 {
68 QColor c(value);
69 // modify colors so they become readable
70 c.setHsv(c.hsvHue(), c.hsvSaturation() / 2, (c.value() / 3) + 150);
71 mColorTable.append(c);
72
73 mColorNameMap.insert(c.red() + c.green() * 256 + c.blue() * 65536, name);
74 }
75
76 QString HostInfo::colorName(const QColor &c)
77 {
78 int key = c.red() + c.green() * 256 + c.blue() * 65536;
79
80 return mColorNameMap.value(key, QApplication::tr("<unknown>"));
81 }
82
83 HostInfo::HostInfo(unsigned int id)
84 : mId(id)
85 {
86 }
87
88 QString HostInfo::toolTip() const
89 {
90 return QApplication::translate(("tooltip"),
91 "<h3><b>%1</b></h3>"
92 "<table>"
93 "<tr><td>IP:</td><td>%2</td></tr>"
94 "<tr><td>Platform:</td><td>%3</td></tr>"
95 "<tr><td>Flavor:</td><td> %4</td></tr>"
96 "<tr><td>Id:</td><td>%5</td></tr>"
97 "<tr><td>Speed:</td><td>%6</td></tr>"
98 "</table>"
99 "<p><img align=\"right\" src=\":/images/icemonnode.png\"/></p>")
100 .arg(name().toHtmlEscaped()).arg(ip())
101 .arg(platform()).arg(colorName(color()).toHtmlEscaped())
102 .arg(QString::number(id()))
103 .arg(QString::number(serverSpeed()));
104 }
105
106 void HostInfo::updateFromStatsMap(const StatsMap &stats)
107 {
108 QString name = stats[QStringLiteral("Name")];
109
110 if (name != mName) {
111 mName = name;
112 mColor = createColor(mName);
113 mIp = stats[QStringLiteral("IP")];
114 mPlatform = stats[QStringLiteral("Platform")];
115 mProtocol = stats[QStringLiteral("Version")].toInt();
116 mFeatures = stats[QStringLiteral("Features")];
117 }
118
119 mNoRemote = (stats[QStringLiteral("NoRemote")].compare(QLatin1String("true"), Qt::CaseInsensitive) == 0);
120 mMaxJobs = stats[QStringLiteral("MaxJobs")].toUInt();
121 mOffline = (stats[QStringLiteral("State")] == QLatin1String("Offline"));
122
123 mServerSpeed = stats[QStringLiteral("Speed")].toFloat();
124
125 mServerLoad = stats[QStringLiteral("Load")].toUInt();
126 }
127
128 QColor HostInfo::createColor(const QString &name)
129 {
130 unsigned long h = 0;
131
132 for (uint i = 0; i < ( uint )name.length(); ++i) {
133 const int ch = name[i].unicode();
134 h = (h << 4) + ch;
135 const unsigned long g = h & 0xf0000000;
136 if (g != 0) {
137 h ^= g >> 24;
138 h ^= g;
139 }
140 }
141
142 h += name.length() + (name.length() << 17);
143 h ^= h >> 2;
144
145 // qDebug() << "HostInfo::createColor: " << h % mColorTable.count() << ": " << name << endl;
146
147 return mColorTable[h % mColorTable.count()];
148 }
149
150 QColor HostInfo::createColor()
151 {
152 static int num = 0;
153
154 return mColorTable.at(num++ % mColorTable.count());
155 }
156
157 HostInfoManager::HostInfoManager()
158 {
159 HostInfo::initColorTable();
160 }
161
162 HostInfoManager::~HostInfoManager()
163 {
164 qDeleteAll(mHostMap);
165 }
166
167 HostInfo *HostInfoManager::find(unsigned int hostid) const
168 {
169 return mHostMap.value(hostid, nullptr);
170 }
171
172 void HostInfoManager::checkNode(const HostInfo &info)
173 {
174 HostMap::ConstIterator it = mHostMap.constFind(info.id());
175 if (it == mHostMap.constEnd()) {
176 auto hostInfo = new HostInfo(info);
177 mHostMap.insert(info.id(), hostInfo);
178 emit hostMapChanged();
179 } else {
180 // no-op
181 }
182 }
183
184 HostInfo *HostInfoManager::checkNode(unsigned int hostid,
185 const HostInfo::StatsMap &stats)
186 {
187 HostMap::ConstIterator it = mHostMap.constFind(hostid);
188 HostInfo *hostInfo;
189 if (it == mHostMap.constEnd()) {
190 hostInfo = new HostInfo(hostid);
191 mHostMap.insert(hostid, hostInfo);
192 } else {
193 hostInfo = *it;
194 }
195
196 hostInfo->updateFromStatsMap(stats);
197 if (hostInfo->isOffline()) {
198 mHostMap.remove(hostid);
199 delete hostInfo;
200 hostInfo = nullptr;
201 }
202
203 emit hostMapChanged();
204
205 return hostInfo;
206 }
207
208 QString HostInfoManager::nameForHost(unsigned int id) const
209 {
210 HostInfo *hostInfo = find(id);
211 if (hostInfo) {
212 return hostInfo->name();
213 }
214
215 return QApplication::tr("<unknown>");
216 }
217
218 QColor HostInfoManager::hostColor(unsigned int id) const
219 {
220 if (id) {
221 HostInfo *hostInfo = find(id);
222 if (hostInfo) {
223 QColor tmp = hostInfo->color();
224 Q_ASSERT(tmp.isValid() && (tmp.red() + tmp.green() + tmp.blue()));
225 return tmp;
226 }
227 }
228
229 //qDebug() << "id " << id << " got no color\n";
230 Q_ASSERT(false);
231
232 return QColor(0, 0, 0);
233 }
234
235 unsigned int HostInfoManager::maxJobs(unsigned int id) const
236 {
237 if (id) {
238 HostInfo *hostInfo = find(id);
239 if (hostInfo) {
240 return hostInfo->maxJobs();
241 }
242 }
243
244 return 0;
245 }
246
247 HostInfoManager::HostMap HostInfoManager::hostMap() const
248 {
249 return mHostMap;
250 }
251
252 void HostInfoManager::setSchedulerName(const QString &schedulerName)
253 {
254 mSchedulerName = schedulerName;
255 }
256
257 void HostInfoManager::setNetworkName(const QString &networkName)
258 {
259 mNetworkName = networkName;
260 }