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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
This file is part of the Boson game
Copyright (C) 2005 Rivo Laks (rivolaks@hot.ee)
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "webinterface.h"
#include "server.h"
#include "game.h"
#include "player.h"
#include "boson/boversion.h"
#include <qsocket.h>
#include <qregexp.h>
#include <qtextstream.h>
#include <kdebug.h>
#include <kgame/kmessageio.h>
#include "webinterface.moc"
WebInterface::WebInterface(Server* s, Q_UINT16 port) : QServerSocket(port)
{
mServer = s;
boDebug() << "Web interface listening on port " << port << endl;
}
WebInterface::~WebInterface()
{
}
void WebInterface::newConnection(int socket)
{
QSocket* s = new QSocket(this);
connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(s, SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()));
s->setSocket(socket);
}
void WebInterface::readClient()
{
QSocket* socket = (QSocket*)sender();
if(socket->canReadLine())
{
QStringList tokens = QStringList::split(QRegExp("[ \r\n][ \r\n]*"), socket->readLine());
if(tokens[0] == "GET")
{
QTextStream os(socket);
os.setEncoding(QTextStream::UnicodeUTF8);
sendStatistics(os);
socket->close();
}
}
}
void WebInterface::discardClient()
{
QSocket* socket = (QSocket*)sender();
delete socket;
}
void WebInterface::sendStatistics(QTextStream& os)
{
writeHTTPHeader(os);
writeHTMLHeader(os, "Boson Server Statistics");
writeServerInfos(os);
os << "</td></tr><tr><td>\r\n";
writeServerStatistics(os);
os << "</td></tr></table></td><td valign=\"top\">\r\n";
os << "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" class=\"sidebar\"><tr><td>\r\n";
writeGameInfos(os);
// end
writeGameStatistics(os);
writeHTMLFooter(os);
}
void WebInterface::writeHTTPHeader(QTextStream& os)
{
os << "HTTP/1.0 200 Ok\r\n";
os << "Content-Type: text/html; charset:=\"utf-8\"\r\n";
os << "\r\n";
}
void WebInterface::writeHTMLHeader(QTextStream& os, const QString& title)
{
os << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
os << "<html>\r\n";
os << "<head>\r\n";
os << " <title>" << title << "</title>\r\n";
os << " <style type=\"text/css\"> \r\n\
body {color: #00ff00;background-color: #000000;font-family: helvetica,arial;}\r\n \
td {color: #00ff00;}\r\n \
a {color: #40ff40;background-color: transparent;text-decoration: underline;}\r\n \
a:visited {background-color: transparent;color: #40d040;}\r\n \
a:hover {background-color: #204020;color: #00ff00;text-decoration: none;}\r\n \
.sidebarboxtitle {font-weight: bold;}\r\n \
td.boxcell {background-color: #202020;color: #00ff00;}\r\n \
.bigboxtitle {font-weight: bold;}\r\n \
.bigboxsubheader {font-weight: bold;vertical-align: top;}\r\n \
table.main {background-color: #000000;color: #00ff00;}\r\n \
td.mainarea {background-color: #000000;color: #00ff00;}\r\n \
table.sidebar {background-color: #000000;color: #00ff00;}\r\n \
table.sidebarbox {background-color: #004000;color: #00ff00;}\r\n \
td.sidebarboxtitlecell {background-color: #202020;color: #00ff00;}\r\n \
td.sidebarboxcell {background-color: #081808;color: #00ff00;}\r\n \
</style>";
os << "</head>\r\n";
os << "<body>\r\n";
os << "<table width=\"100%\" border=\"0\"><tr><td>\r\n";
os << "<div align=\"center\"><img src=\"http://boson.eu.org/pictures/header_small.jpg\" alt=\"\"></div>\r\n";
os << "<h1 align=\"center\">Boson Server Statistics</h1>\r\n \
<table border=\"0\" cellpadding=\"3\" cellspacing=\"2\" width=\"100%\" class=\"main\">\r\n\
<tr valign=\"top\"><td>\r\n\
<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" class=\"sidebar\">\r\n\
<tr><td>\r\n";
}
void WebInterface::writeHTMLFooter(QTextStream& os)
{
os << "</table></td></tr></table></td></tr></table>\r\n";
os << "</td></tr>\r\n";
os << "<tr><td>© 2005 <a href=\"http://boson.eu.org\" target=\"_blank\">The Boson Team</a></td></tr>\r\n";
os << "</table></body></html>\r\n";
}
void WebInterface::writeServerInfos(QTextStream& os)
{
QString sServerStatus;
Game* game = mServer->game();
if(mServer->isOfferingConnections())
{
if (!game)
sServerStatus = "Listening for new connections on port " + QString::number(mServer->serverPort());
else if(game->gameInited())
sServerStatus = "NOT accepting new connections";
else
sServerStatus = "Listening for new connections on port " + QString::number(mServer->serverPort());
}
else
{
sServerStatus = "NOT accepting new connections";
}
os << "<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"sidebarbox\">\r\n \
<tr><td class=\"sidebarboxtitlecell\">\r\n \
<font class=\"sidebarboxtitle\"> Server Info</font>\r\n \
</td></tr>\r\n \
<tr><td class=\"sidebarboxcell\">\r\n \
<table width=\"100%\">\r\n \
<tr><td class=\"bigboxsubheader\">Version</td><td>" << BOSON_VERSION_STRING << "</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Started</td><td>" << mServer->timeServerStarted().toString() << "</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Status</td><td>" << sServerStatus << "</td></tr>\r\n \
</table>\r\n \
</td></tr>\r\n \
</table>\r\n \
<br>\r\n";
}
void WebInterface::writeServerStatistics(QTextStream& os)
{
os << "<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"sidebarbox\">\r\n \
<tr><td class=\"sidebarboxtitlecell\">\r\n \
<font class=\"sidebarboxtitle\"> Server Statistics</font>\r\n \
</td></tr>\r\n \
<tr><td class=\"sidebarboxcell\">\r\n \
<table width=\"100%\">\r\n \
<tr><td class=\"bigboxsubheader\">Incoming traffic</td><td>" << mServer->inTraffic() / 1024 << "kb</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Outgoing traffic</td><td>" << mServer->outTraffic() / 1024 << "kb</td></tr>\r\n \
</table>\r\n \
</td></tr>\r\n \
</table>\r\n \
<br>\r\n";
}
void WebInterface::writeGameInfos(QTextStream& os)
{
int clientcount = 0;
int playercount = 0;
QString sGameMap;
QString sGameSize;
QString sGameGroundTheme;
QString sGameComment;
QString sGameStarted;
Game* game = mServer->game();
if(mServer->clientCount() > 0)
{
clientcount = mServer->clientCount();
playercount = game->playerCount();
}
else
{
clientcount = mServer->clientCount();
playercount = 0;
}
if(game)
{
clientcount--;
if(game->gameStarted())
{
sGameStarted = "The game is running";
sGameMap = game->mapName();
sGameSize = QString::number(game->mapWidth()) + "x" + QString::number(game->mapHeight());
sGameGroundTheme = game->mapGroundTheme();
sGameComment = game->mapComment();
}
else if(game->gamePaused())
{
sGameStarted = "The game is paused";
sGameMap = game->mapName();
sGameSize = QString::number(game->mapWidth()) + "x" + QString::number(game->mapHeight());
sGameGroundTheme = game->mapGroundTheme();
sGameComment = game->mapComment();
}
else if(game->gameInited())
sGameStarted = "The game is loading...";
else
sGameStarted = "The game is not yet started";
}
else
{
sGameStarted = "The game is not yet started";
}
os << "<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"sidebarbox\">\r\n \
<tr><td class=\"sidebarboxtitlecell\">\r\n \
<font class=\"sidebarboxtitle\"> Game Info</font>\r\n \
</td></tr>\r\n \
<tr><td class=\"sidebarboxcell\">\r\n \
<table width=\"100%\">\r\n \
<tr><td class=\"bigboxsubheader\">Status</td><td>" << sGameStarted << "</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Clients/Players</td><td>" << clientcount << "/" << playercount << "</td></tr>\r\n";
if(game && (game->gameStarted() || game->gamePaused()))
{
os << " <tr><td class=\"bigboxsubheader\">Map</td><td>" << sGameMap << "</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Map size</td><td>" << sGameSize << "</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Map ground theme</td><td>" << sGameGroundTheme<< "</td></tr>\r\n \
<tr><td class=\"bigboxsubheader\">Map comment</td><td width=\"100\">" << sGameComment << "</td></tr>\r\n";
}
os << " </table>\r\n \
</td></tr>\r\n \
</table>\r\n \
<br>\r\n";
}
void WebInterface::writeGameStatistics(QTextStream& os)
{
if(mServer->clientCount() > 0)
{
os << "<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\" width=\"100%\" class=\"sidebarbox\">\r\n \
<tr><td class=\"sidebarboxtitlecell\">\r\n \
<font class=\"sidebarboxtitle\"> Game Statistics</font>\r\n \
</td></tr>\r\n \
<tr><td class=\"sidebarboxcell\">\r\n";
writeClientStats(os);
writePlayerStats(os);
os << "</td></tr></table><br>";
}
}
void WebInterface::writeClientStats(QTextStream& os)
{
int clientcount = mServer->clientCount();
if(mServer->game())
{
clientcount--;
}
if(clientcount == 1)
os << " There is 1 client in the game:<br>\r\n";
else
os << " There are " << clientcount << " clients in the game:<br>\r\n";
QValueList<Q_UINT32> clientids = mServer->clientIDs();
QValueList<Q_UINT32>::iterator it = clientids.begin();
for(; it != clientids.end(); it++)
{
Q_UINT32 id = *it;
if(id == mServer->gameClientId())
{
continue;
}
KMessageIO* client = mServer->findClient(id);
os << " " << id << ": " << client->peerName() << ":" << client->peerPort() << "<br>\r\n";
}
}
void WebInterface::writePlayerStats(QTextStream& os)
{
Game* game = mServer->game();
if(game->playerCount() == 1)
os << " There is 1 player in game:<br>\r\n";
else
os << " There are " << game->playerCount() << " players in game:<br>\r\n";
QPtrListIterator<KPlayer> itP(*game->playerList());
while(itP.current())
{
Player* p = (Player*)itP.current();
if(p->name() != "Neutral")
os << " " << p->userId() << ": " << p->name() << " - " << p->unitCount() << " units<br>\r\n";
else
os << "<i> " << p->userId() << ": " << p->name() << " - " << p->unitCount() << " units</i><br>\r\n";
++itP;
}
}
|