# HG changeset patch # User unc0rr # Date 1197206507 0 # Node ID 0fb6d55ecd663a52561b8f0d6c0dc4e133ca5c09 # Parent b9db5ff5cfdbd7a14154b2a968c8fae53d32999e Register in www works diff -r b9db5ff5cfdb -r 0fb6d55ecd66 QTfrontend/hwform.cpp --- a/QTfrontend/hwform.cpp Sun Dec 09 11:43:56 2007 +0000 +++ b/QTfrontend/hwform.cpp Sun Dec 09 13:21:47 2007 +0000 @@ -413,6 +413,7 @@ hwnet=0; } if(pnetserver) { + pRegisterServer->unregister(); pRegisterServer->deleteLater(); pnetserver->StopServer(); delete pnetserver; diff -r b9db5ff5cfdb -r 0fb6d55ecd66 QTfrontend/netregister.cpp --- a/QTfrontend/netregister.cpp Sun Dec 09 11:43:56 2007 +0000 +++ b/QTfrontend/netregister.cpp Sun Dec 09 13:21:47 2007 +0000 @@ -23,3 +23,8 @@ { } + +void HWNetRegisterServer::unregister() +{ + +} diff -r b9db5ff5cfdb -r 0fb6d55ecd66 QTfrontend/netregister.h --- a/QTfrontend/netregister.h Sun Dec 09 11:43:56 2007 +0000 +++ b/QTfrontend/netregister.h Sun Dec 09 13:21:47 2007 +0000 @@ -27,6 +27,9 @@ public: HWNetRegisterServer(QObject *parent, const QString & descr, quint16 port); + +public slots: + void unregister(); }; #endif // _NET_REGISTER_INCLUDED diff -r b9db5ff5cfdb -r 0fb6d55ecd66 QTfrontend/netwwwserver.cpp --- a/QTfrontend/netwwwserver.cpp Sun Dec 09 11:43:56 2007 +0000 +++ b/QTfrontend/netwwwserver.cpp Sun Dec 09 13:21:47 2007 +0000 @@ -16,15 +16,69 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ +#include +#include +#include +#include #include "netwwwserver.h" +#include "hwconsts.h" HWNetWwwServer::HWNetWwwServer(QObject *parent, const QString & descr, quint16 port) : - HWNetRegisterServer(parent, descr, port) + HWNetRegisterServer(parent, descr, port), timer(0) { - qDebug("www server start"); + http = new QHttp(this); + http->setHost("www.hedgewars.org", 80); + connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool))); + + QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4") + .arg(descr) + .arg(port) + .arg(false ? "true" : "false") + .arg(*cProtoVer); + http->post("/games/create", request.toUtf8()); } -void HWNetWwwServer::onClientRead() +void HWNetWwwServer::onClientRead(int id, bool error) { + if (error) + { + QMessageBox::critical(0, + tr("Error"), + tr("Server registration error") + "\n" + + http->errorString()); + return; + } + QString str = http->readAll(); + + if (!str.size()) return; // ?? + + if (str[1] == QChar('0')) return; // error on server + if (!timer) + { + QStringList sl = str.split(','); + if (sl.size() != 2) return; + servid = sl[0]; + servkey = sl[1]; + + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(updateInList())); + timer->start(60000); + } } + +void HWNetWwwServer::updateInList() +{ + QString request = QString("id=%1&key=%2") + .arg(servid) + .arg(servkey); + http->post("/games/update_game", request.toUtf8()); +} + +void HWNetWwwServer::unregister() +{ + QString request = QString("id=%1&key=%2") + .arg(servid) + .arg(servkey); + http->post("/games/destroy_game", request.toUtf8()); +} diff -r b9db5ff5cfdb -r 0fb6d55ecd66 QTfrontend/netwwwserver.h --- a/QTfrontend/netwwwserver.h Sun Dec 09 11:43:56 2007 +0000 +++ b/QTfrontend/netwwwserver.h Sun Dec 09 13:21:47 2007 +0000 @@ -22,6 +22,9 @@ #include #include "netregister.h" +class QHttp; +class QTimer; + class HWNetWwwServer : public HWNetRegisterServer { Q_OBJECT @@ -29,10 +32,17 @@ public: HWNetWwwServer(QObject *parent, const QString & descr, quint16 port); +public slots: + void unregister(); + private slots: - void onClientRead(); + void onClientRead(int id, bool error); + void updateInList(); private: + QHttp * http; + QTimer * timer; + QString servid, servkey; }; #endif // _NET_WWWSERVER_INCLUDED diff -r b9db5ff5cfdb -r 0fb6d55ecd66 QTfrontend/netwwwwidget.cpp --- a/QTfrontend/netwwwwidget.cpp Sun Dec 09 11:43:56 2007 +0000 +++ b/QTfrontend/netwwwwidget.cpp Sun Dec 09 13:21:47 2007 +0000 @@ -30,21 +30,11 @@ http->setHost("www.hedgewars.org", 80); connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool))); } -// http://hedgewars.org/games/create -// http://www.hedgewars.org/games/update_game?id=1&key=pufidzuk -// http://www.hedgewars.org/games/destroy_game?id=5&key=wrdeough + void HWNetWwwWidget::updateList() { http->abort(); -// example for adding game to server list -/* QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4") - .arg("hedgewarsserver") - .arg(46631) - .arg(false ? "true" : "false") - .arg(*cProtoVer); - http->post("/games/create", request.toUtf8()); -*/ -// query game list + QString request = QString("protocol_version=%1") .arg(*cProtoVer); http->post("/games/list_games", request.toUtf8());