QTfrontend/netwwwserver.cpp
changeset 635 0fb6d55ecd66
parent 633 9050a949db21
child 659 5eafb20566f1
equal deleted inserted replaced
634:b9db5ff5cfdb 635:0fb6d55ecd66
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
       
    19 #include <QHttp>
       
    20 #include <QMessageBox>
       
    21 #include <QTimer>
       
    22 #include <QStringList>
    19 #include "netwwwserver.h"
    23 #include "netwwwserver.h"
       
    24 #include "hwconsts.h"
    20 
    25 
    21 HWNetWwwServer::HWNetWwwServer(QObject *parent, const QString & descr, quint16 port) :
    26 HWNetWwwServer::HWNetWwwServer(QObject *parent, const QString & descr, quint16 port) :
    22   HWNetRegisterServer(parent, descr, port)
    27   HWNetRegisterServer(parent, descr, port), timer(0)
    23 {
    28 {
    24 	qDebug("www server start");
    29 	http = new QHttp(this);
       
    30 	http->setHost("www.hedgewars.org", 80);
       
    31 	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
       
    32 
       
    33 	QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4")
       
    34 			.arg(descr)
       
    35 			.arg(port)
       
    36 			.arg(false ? "true" : "false")
       
    37 			.arg(*cProtoVer);
       
    38 	http->post("/games/create", request.toUtf8());
    25 }
    39 }
    26 
    40 
    27 void HWNetWwwServer::onClientRead()
    41 void HWNetWwwServer::onClientRead(int id, bool error)
    28 {
    42 {
       
    43 	if (error)
       
    44 	{
       
    45 		QMessageBox::critical(0,
       
    46 				tr("Error"),
       
    47 				tr("Server registration error") + "\n" +
       
    48 				http->errorString());
       
    49 		return;
       
    50 	}
    29 
    51 
       
    52 	QString str = http->readAll();
       
    53 
       
    54 	if (!str.size()) return; // ??
       
    55 
       
    56 	if (str[1] == QChar('0')) return; // error on server
       
    57 	if (!timer)
       
    58 	{
       
    59 		QStringList sl = str.split(',');
       
    60 		if (sl.size() != 2) return;
       
    61 		servid = sl[0];
       
    62 		servkey = sl[1];
       
    63 
       
    64 		timer = new QTimer(this);
       
    65 		connect(timer, SIGNAL(timeout()), this, SLOT(updateInList()));
       
    66 		timer->start(60000);
       
    67 	}
    30 }
    68 }
       
    69 
       
    70 void HWNetWwwServer::updateInList()
       
    71 {
       
    72 	QString request = QString("id=%1&key=%2")
       
    73 			.arg(servid)
       
    74 			.arg(servkey);
       
    75 	http->post("/games/update_game", request.toUtf8());
       
    76 }
       
    77 
       
    78 void HWNetWwwServer::unregister()
       
    79 {
       
    80 	QString request = QString("id=%1&key=%2")
       
    81 			.arg(servid)
       
    82 			.arg(servkey);
       
    83 	http->post("/games/destroy_game", request.toUtf8());
       
    84 }