QTfrontend/netwwwserver.cpp
changeset 635 0fb6d55ecd66
parent 633 9050a949db21
child 659 5eafb20566f1
--- 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 <QHttp>
+#include <QMessageBox>
+#include <QTimer>
+#include <QStringList>
 #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());
+}