This commit is contained in:
Max-Wilhelm Bruker 2009-10-30 13:18:25 +01:00
parent 1c2aa15b22
commit 6923c98dc2
12 changed files with 107 additions and 45 deletions

View file

@ -11,47 +11,47 @@ ProtocolItem::ProtocolItem(const QString &_itemName)
{
}
bool ProtocolItem::read(QXmlStreamReader &xml)
bool ProtocolItem::read(QXmlStreamReader *xml)
{
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
qDebug() << "startElement: " << xml.name().toString();
} else if (xml.isEndElement()) {
qDebug() << "endElement: " << xml.name().toString();
if (xml.name() == getItemType()) {
while (!xml->atEnd()) {
xml->readNext();
if (xml->isStartElement()) {
qDebug() << "startElement: " << xml->name().toString();
} else if (xml->isEndElement()) {
qDebug() << "endElement: " << xml->name().toString();
if (xml->name() == getItemType()) {
extractParameters();
qDebug() << "FERTIG";
deleteLater();
return true;
} else {
QString tagName = xml.name().toString();
QString tagName = xml->name().toString();
if (!parameters.contains(tagName))
qDebug() << "unrecognized attribute";
else
parameters[tagName] = currentElementText;
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
currentElementText = xml.text().toString();
} else if (xml->isCharacters() && !xml->isWhitespace()) {
currentElementText = xml->text().toString();
qDebug() << "text: " << currentElementText;
}
}
return false;
}
void ProtocolItem::write(QXmlStreamWriter &xml)
void ProtocolItem::write(QXmlStreamWriter *xml)
{
xml.writeStartElement(getItemType());
xml->writeStartElement(getItemType());
if (!itemName.isEmpty())
xml.writeAttribute("name", itemName);
xml->writeAttribute("name", itemName);
QMapIterator<QString, QString> i(parameters);
while (i.hasNext()) {
i.next();
xml.writeTextElement(i.key(), i.value());
xml->writeTextElement(i.key(), i.value());
}
xml.writeEndElement();
xml->writeEndElement();
}
ProtocolItem *ProtocolItem::getNewItem(const QString &name)
@ -118,6 +118,11 @@ void ProtocolResponse::initializeHash()
responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed);
}
GenericEvent::GenericEvent(const QString &_eventName)
: ProtocolItem(_eventName)
{
}
void GameEvent::extractParameters()
{
bool ok;

View file

@ -32,8 +32,8 @@ public:
ProtocolItem(const QString &_itemName);
static void initializeHash();
static ProtocolItem *getNewItem(const QString &name);
virtual bool read(QXmlStreamReader &xml);
virtual void write(QXmlStreamWriter &xml);
virtual bool read(QXmlStreamReader *xml);
virtual void write(QXmlStreamWriter *xml);
};
class Command : public ProtocolItem {
@ -101,6 +101,14 @@ public:
static ProtocolItem *newItem() { return new ProtocolResponse; }
};
class GenericEvent : public ProtocolItem {
Q_OBJECT
protected:
QString getItemType() const { return "generic_event"; }
public:
GenericEvent(const QString &_eventName);
};
class GameEvent : public ProtocolItem {
Q_OBJECT
private:

View file

@ -551,6 +551,16 @@ void Event_StopDumpZone::extractParameters()
zoneOwnerId = parameters["zone_owner_id"].toInt();
zone = parameters["zone"];
}
Event_Welcome::Event_Welcome(const QString &_message)
: GenericEvent("welcome"), message(_message)
{
setParameter("message", message);
}
void Event_Welcome::extractParameters()
{
GenericEvent::extractParameters();
message = parameters["message"];
}
void ProtocolItem::initializeHashAuto()
{
itemNameHash.insert("cmdping", Command_Ping::newItem);
@ -604,4 +614,5 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_eventset_active_phase", Event_SetActivePhase::newItem);
itemNameHash.insert("game_eventdump_zone", Event_DumpZone::newItem);
itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem);
itemNameHash.insert("generic_eventwelcome", Event_Welcome::newItem);
}

View file

@ -49,3 +49,4 @@
3:set_active_phase:i,phase
3:dump_zone:i,zone_owner_id:s,zone:i,number_cards
3:stop_dump_zone:i,zone_owner_id:s,zone
4:welcome:s,message

View file

@ -634,5 +634,16 @@ public:
protected:
void extractParameters();
};
class Event_Welcome : public GenericEvent {
Q_OBJECT
private:
QString message;
public:
Event_Welcome(const QString &_message = QString());
QString getMessage() const { return message; }
static ProtocolItem *newItem() { return new Event_Welcome; }
protected:
void extractParameters();
};
#endif

View file

@ -47,6 +47,13 @@ while (<file>) {
$parentConstructorCall = "$baseClass(\"$name1\", _gameId, _playerId)";
$constructorParamsH = "int _gameId = -1, int _playerId = -1";
$constructorParamsCpp = "int _gameId, int _playerId";
} elsif ($type == 4) {
$type = 'generic_event';
$namePrefix = 'Event';
$baseClass = 'GenericEvent';
$parentConstructorCall = "$baseClass(\"$name1\")";
$constructorParamsH = "";
$constructorParamsCpp = "";
}
$className = $namePrefix . '_' . $name2;
print headerfile "class $className : public $baseClass {\n"

View file

@ -7,6 +7,7 @@
class Server_Player;
class Command;
class ProtocolItem;
class Server_ProtocolHandler : public QObject {
Q_OBJECT
@ -31,6 +32,7 @@ public:
const QString &getPlayerName() const { return playerName; }
void processCommand(Command *command);
virtual void sendProtocolItem(ProtocolItem *item) = 0;
};
#endif

View file

@ -4,8 +4,8 @@
TEMPLATE = app
TARGET =
DEPENDPATH += . src ../common/src
INCLUDEPATH += . src ../common/src
DEPENDPATH += . src ../common
INCLUDEPATH += . src ../common
MOC_DIR = build
OBJECTS_DIR = build
@ -16,31 +16,31 @@ QT -= gui
HEADERS += src/servatrice.h \
src/serversocketinterface.h \
src/version.h \
../common/src/protocol.h \
../common/src/protocol_items.h \
../common/src/rng_abstract.h \
../common/src/rng_qt.h \
../common/src/server.h \
../common/src/server_arrow.h \
../common/src/server_card.h \
../common/src/server_cardzone.h \
../common/src/server_chatchannel.h \
../common/src/server_counter.h \
../common/src/server_game.h \
../common/src/server_player.h \
../common/src/server_protocolhandler.h
../common/protocol.h \
../common/protocol_items.h \
../common/rng_abstract.h \
../common/rng_qt.h \
../common/server.h \
../common/server_arrow.h \
../common/server_card.h \
../common/server_cardzone.h \
../common/server_chatchannel.h \
../common/server_counter.h \
../common/server_game.h \
../common/server_player.h \
../common/server_protocolhandler.h
SOURCES += src/main.cpp \
src/servatrice.cpp \
src/serversocketinterface.cpp \
../common/src/protocol.cpp \
../common/src/protocol_items.cpp \
../common/src/rng_abstract.cpp \
../common/src/rng_qt.cpp \
../common/src/server.cpp \
../common/src/server_card.cpp \
../common/src/server_cardzone.cpp \
../common/src/server_chatchannel.cpp \
../common/src/server_game.cpp \
../common/src/server_player.cpp \
../common/src/server_protocolhandler.cpp
../common/protocol.cpp \
../common/protocol_items.cpp \
../common/rng_abstract.cpp \
../common/rng_qt.cpp \
../common/server.cpp \
../common/server_card.cpp \
../common/server_cardzone.cpp \
../common/server_chatchannel.cpp \
../common/server_game.cpp \
../common/server_player.cpp \
../common/server_protocolhandler.cpp

View file

@ -117,3 +117,5 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS
} else
return UnknownUser;
}
const QString Servatrice::versionString = "Servatrice 0.20091030";

View file

@ -32,6 +32,7 @@ class Servatrice : public Server
private slots:
void newConnection();
public:
static const QString versionString;
Servatrice(QObject *parent = 0);
~Servatrice();
bool openDatabase();

View file

@ -21,13 +21,16 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include "serversocketinterface.h"
#include "servatrice.h"
#include "protocol.h"
#include "protocol_items.h"
ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent)
: Server_ProtocolHandler(_server, parent), socket(_socket)
{
xmlWriter = new QXmlStreamWriter;
xmlWriter->setDevice(socket);
xmlWriter->setAutoFormatting(true);
xmlReader = new QXmlStreamReader;
@ -38,6 +41,8 @@ ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socke
xmlWriter->writeStartDocument();
xmlWriter->writeStartElement("cockatrice_communication");
xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion));
sendProtocolItem(new Event_Welcome(Servatrice::versionString));
}
ServerSocketInterface::~ServerSocketInterface()
@ -59,7 +64,9 @@ ServerSocketInterface::~ServerSocketInterface()
void ServerSocketInterface::readClient()
{
/* while (canReadLine()) {
xmlReader->addData(socket->readAll());
while (canReadLine()) {
QString line = QString(readLine()).trimmed();
if (line.isNull())
break;
@ -91,3 +98,8 @@ void ServerSocketInterface::catchSocketError(QAbstractSocket::SocketError socket
deleteLater();
}
void ServerSocketInterface::sendProtocolItem(ProtocolItem *item)
{
item->write(xmlWriter);
delete item;
}

View file

@ -41,6 +41,8 @@ private:
public:
ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent = 0);
~ServerSocketInterface();
void sendProtocolItem(ProtocolItem *item);
};
#endif