Register in www works
authorunc0rr
Sun, 09 Dec 2007 13:21:47 +0000
changeset 635 0fb6d55ecd66
parent 634 b9db5ff5cfdb
child 636 dc93ac775bb9
Register in www works
QTfrontend/hwform.cpp
QTfrontend/netregister.cpp
QTfrontend/netregister.h
QTfrontend/netwwwserver.cpp
QTfrontend/netwwwserver.h
QTfrontend/netwwwwidget.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;
--- 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()
+{
+
+}
--- 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
--- 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());
+}
--- 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 <QObject>
 #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
--- 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());