QTfrontend/netwwwserver.cpp
author unc0rr
Mon, 12 May 2008 19:49:36 +0000
changeset 929 9456e1e77369
parent 883 07a568ba44e0
child 1066 1f1b3686a2b0
permissions -rw-r--r--
- Continue preparation for implementing attack from rope and parachute - Parachute turns off when space button is pressed
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
883
07a568ba44e0 Update copyright info in source files headers
unc0rr
parents: 661
diff changeset
     3
 * Copyright (c) 2007, 2008 Andrey Korotaev <unC0Rr@gmail.com>
633
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
{
659
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    29
	destroyPosted = false;
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    30
	destroyPostId = 0;
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    31
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    32
	http = new QHttp(this);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    33
	http->setHost("www.hedgewars.org", 80);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    34
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    35
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    36
	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
    37
			.arg(descr)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    38
			.arg(port)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    39
			.arg(false ? "true" : "false")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    40
			.arg(*cProtoVer);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    41
	http->post("/games/create", request.toUtf8());
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    42
}
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    43
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    44
void HWNetWwwServer::onClientRead(int id, bool error)
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    45
{
659
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    46
	if (destroyPosted && (id == destroyPostId))
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    47
	{
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    48
		deleteLater();
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    49
		return;
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    50
	}
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    51
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    52
	if (error)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    53
	{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    54
		QMessageBox::critical(0,
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    55
				tr("Error"),
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    56
				tr("Server registration error") + "\n" +
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    57
				http->errorString());
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    58
		return;
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    59
	}
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    60
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    61
	QString str = http->readAll();
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    62
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    63
	if (!str.size()) return; // ??
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    64
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    65
	if (str[1] == QChar('0')) return; // error on server
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    66
	if (!timer)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    67
	{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    68
		QStringList sl = str.split(',');
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    69
		if (sl.size() != 2) return;
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    70
		servid = sl[0];
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    71
		servkey = sl[1];
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    72
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    73
		timer = new QTimer(this);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    74
		connect(timer, SIGNAL(timeout()), this, SLOT(updateInList()));
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    75
		timer->start(60000);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    76
	}
633
9050a949db21 Half implement registration in servers list
unc0rr
parents:
diff changeset
    77
}
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    78
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    79
void HWNetWwwServer::updateInList()
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    80
{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    81
	QString request = QString("id=%1&key=%2")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    82
			.arg(servid)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    83
			.arg(servkey);
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    84
	http->post("/games/update_game", request.toUtf8());
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    85
}
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    86
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    87
void HWNetWwwServer::unregister()
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    88
{
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    89
	QString request = QString("id=%1&key=%2")
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    90
			.arg(servid)
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    91
			.arg(servkey);
659
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    92
	destroyPostId = http->post("/games/destroy_game", request.toUtf8());
5eafb20566f1 - Fix some stupid object casts
unc0rr
parents: 635
diff changeset
    93
	destroyPosted = true;
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 633
diff changeset
    94
}