Big network update.
[GoMoku3D.git] / src / network / ServerSocket.cpp
blob3166a1a431a17d2c19ad884631a48ba0fee11670
1 /********************************************************************
3 * Copyright (C) 2008 Davide Pesavento
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
22 #include "ServerSocket.h"
23 #include "ServerSettings.h"
25 #define HANDLER_SIGNATURE(token) \
26 void ServerSocket::parse_##token() { \
27 QString elementName = name().toString(); \
28 qDebug() << "parsing" << elementName; \
29 Q_ASSERT_X(elementName == #token, \
30 (QString("ServerSocket::parse_") + #token + "()").toUtf8().constData(), \
31 ("called while parsing " + elementName).toUtf8().constData()); \
32 SET_HANDLER(#token)
34 ServerSocket::ServerSocket(QTcpSocket *socket) : StreamSocket(socket)
36 ServerSettings set;
37 QList<PlayerInfo> infoList = set.playersInfo();
38 for (int i = 0; i < infoList.size(); i++) {
39 if (infoList.at(i).type() == "H") {
40 _localPlayerName = infoList.at(i).name();
41 break;
45 changeState(Connected);
48 void ServerSocket::acceptJoin(int id)
50 Q_ASSERT_X(id >= -1 && id <= 2, "ServerSocket::acceptJoin()", "invalid id");
52 writeTextElement("joinACK", QString::number(id));
53 changeState(AwaitingPlayers);
56 void ServerSocket::refuseJoin(int cause)
58 writeTextElement("joinNAK", QString::number(cause));
61 void ServerSocket::sendGameSettings(int d1, int d2, int num, int timer, bool playing)
63 writeStartElement("settings");
64 writeAttribute("playing", playing ? "true" : "false");
65 writeTextElement("difficultyOne", QString::number(d1));
66 writeTextElement("difficultyTwo", QString::number(d2));
67 writeTextElement("numberOfPlayers", QString::number(num));
68 if (timer > 0) {
69 writeTextElement("timerDuration", QString::number(timer));
71 writeEndElement();
73 changeState(AwaitingJoinRequest);
76 void ServerSocket::sendHistory(QList<Move> history)
78 writeStartElement("history");
79 Move m;
80 foreach (m, history) {
81 serialize(m);
83 writeEndElement();
86 void ServerSocket::sendPlayerJoined(int id, QString name, QString type)
88 Q_ASSERT_X(id >= 0 && id <= 2, "ServerSocket::sendPlayerJoined()", "invalid id");
90 if (type == "H") {
91 type = "human";
92 } else if (type == "A") {
93 type = "ai";
94 } else if (type == "R") {
95 type = "remote";
97 type = type.toLower();
98 if (type != "human" && type != "remote" && type != "ai") {
99 LOG("refusing to send <playerJoined> with invalid player type");
100 return;
103 writeStartElement("playerJoined");
104 writeAttribute("id", QString::number(id));
105 writeTextElement("name", name);
106 writeTextElement("type", type);
107 writeEndElement();
110 void ServerSocket::sendPlayerLeft(int id)
112 Q_ASSERT_X(id >= 0 && id <= 2, "ServerSocket::sendPlayerLeft()", "invalid id");
114 writeTextElement("playerLeft", QString::number(id));
117 void ServerSocket::sendStartGame()
119 changeState(Playing);
121 writeEmptyElement("startGame");
122 writeEmptyElement("foo"); //FIXME
125 BEGIN_TERMINAL_HANDLER(joinRequest)
126 QString mode = attributes().value("gameMode").toString().toLower();
127 if (atEnd()) return;
128 QString name = readElementText();
129 if (state() == AwaitingJoinRequest) {
130 emit joinRequested(mode, name);
131 } else {
132 LOG("dropping <joinRequest> (state = " + stateString() + ")");
134 RESTORE_HANDLER("")
135 END_TERMINAL_HANDLER