QTfrontend/netwwwserver.cpp
author unc0rr
Thu, 13 Dec 2007 14:34:13 +0000
changeset 650 14400b47ed6e
parent 635 0fb6d55ecd66
child 659 5eafb20566f1
permissions -rw-r--r--
Update copyright header
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     1
/*
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com>
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     4
 *
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     8
 *
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    13
 *
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    17
 */
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    18
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    19
#include <QHttp>
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    20
#include <QMessageBox>
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    21
#include <QTimer>
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    22
#include <QStringList>
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    23
#include "netwwwserver.h"
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    24
#include "hwconsts.h"
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    25
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    26
HWNetWwwServer::HWNetWwwServer(QObject *parent, const QString & descr, quint16 port) :
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    27
  HWNetRegisterServer(parent, descr, port), timer(0)
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    28
{
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    29
	http = new QHttp(this);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    30
	http->setHost("www.hedgewars.org", 80);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    31
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    32
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    33
	QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    34
			.arg(descr)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    35
			.arg(port)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    36
			.arg(false ? "true" : "false")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    37
			.arg(*cProtoVer);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    38
	http->post("/games/create", request.toUtf8());
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    39
}
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    40
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    41
void HWNetWwwServer::onClientRead(int id, bool error)
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    42
{
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    43
	if (error)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    44
	{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    45
		QMessageBox::critical(0,
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    46
				tr("Error"),
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    47
				tr("Server registration error") + "\n" +
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    48
				http->errorString());
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    49
		return;
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    50
	}
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    51
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    52
	QString str = http->readAll();
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    53
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    54
	if (!str.size()) return; // ??
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    55
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    56
	if (str[1] == QChar('0')) return; // error on server
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    57
	if (!timer)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    58
	{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    59
		QStringList sl = str.split(',');
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    60
		if (sl.size() != 2) return;
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    61
		servid = sl[0];
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    62
		servkey = sl[1];
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    63
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    64
		timer = new QTimer(this);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    65
		connect(timer, SIGNAL(timeout()), this, SLOT(updateInList()));
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    66
		timer->start(60000);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    67
	}
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    68
}
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    69
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    70
void HWNetWwwServer::updateInList()
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    71
{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    72
	QString request = QString("id=%1&key=%2")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    73
			.arg(servid)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    74
			.arg(servkey);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    75
	http->post("/games/update_game", request.toUtf8());
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    76
}
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    77
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    78
void HWNetWwwServer::unregister()
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    79
{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    80
	QString request = QString("id=%1&key=%2")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    81
			.arg(servid)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    82
			.arg(servkey);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    83
	http->post("/games/destroy_game", request.toUtf8());
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    84
}