This commit is contained in:
Max-Wilhelm Bruker 2009-11-03 17:26:03 +01:00
parent 29bf3d3774
commit 8b3723b871
16 changed files with 274 additions and 77 deletions

View file

@ -15,25 +15,19 @@ 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 (readElement(xml))
continue;
if (xml->isEndElement()) {
if (xml->name() == getItemType()) {
extractParameters();
qDebug() << "FERTIG";
return true;
} else {
QString tagName = xml->name().toString();
if (!parameters.contains(tagName))
qDebug() << "unrecognized attribute";
else
if (parameters.contains(tagName))
parameters[tagName] = currentElementText;
}
} else if (xml->isCharacters() && !xml->isWhitespace()) {
} else if (xml->isCharacters() && !xml->isWhitespace())
currentElementText = xml->text().toString();
qDebug() << "text: " << currentElementText;
}
}
return false;
}
@ -50,6 +44,8 @@ void ProtocolItem::write(QXmlStreamWriter *xml)
xml->writeTextElement(i.key(), i.value());
}
writeElement(xml);
xml->writeEndElement();
}
@ -151,3 +147,49 @@ ChatEvent::ChatEvent(const QString &_eventName, const QString &_channel)
{
setParameter("channel", channel);
}
bool Event_ChatListChannels::readElement(QXmlStreamReader *xml)
{
if (xml->isStartElement() && (xml->name() == "channel")) {
channelList.append(ChannelInfo(
xml->attributes().value("name").toString(),
xml->attributes().value("description").toString(),
xml->attributes().value("player_count").toString().toInt(),
xml->attributes().value("auto_join").toString().toInt()
));
return true;
}
return false;
}
void Event_ChatListChannels::writeElement(QXmlStreamWriter *xml)
{
for (int i = 0; i < channelList.size(); ++i) {
xml->writeStartElement("channel");
xml->writeAttribute("name", channelList[i].getName());
xml->writeAttribute("description", channelList[i].getDescription());
xml->writeAttribute("player_count", QString::number(channelList[i].getPlayerCount()));
xml->writeAttribute("auto_join", channelList[i].getAutoJoin() ? "1" : "0");
xml->writeEndElement();
}
}
bool Event_ChatListPlayers::readElement(QXmlStreamReader *xml)
{
if (xml->isStartElement() && ((xml->name() == "player"))) {
playerList.append(PlayerInfo(
xml->attributes().value("name").toString()
));
return true;
}
return false;
}
void Event_ChatListPlayers::writeElement(QXmlStreamWriter *xml)
{
for (int i = 0; i < playerList.size(); ++i) {
xml->writeStartElement("player");
xml->writeAttribute("name", playerList[i].getName());
xml->writeEndElement();
}
}

View file

@ -12,20 +12,29 @@ class QXmlStreamReader;
class QXmlStreamWriter;
class QXmlStreamAttributes;
enum ItemId {
ItemId_Event_ChatListChannels = ItemId_Other + 1,
ItemId_Event_ChatListPlayers = ItemId_Other + 2,
};
class ProtocolItem : public QObject {
Q_OBJECT
private:
QString currentElementText;
protected:
typedef ProtocolItem *(*NewItemFunction)();
static QHash<QString, NewItemFunction> itemNameHash;
QString itemName;
QMap<QString, QString> parameters;
QString currentElementText;
void setParameter(const QString &name, const QString &value) { parameters[name] = value; }
void setParameter(const QString &name, bool value) { parameters[name] = (value ? "1" : "0"); }
void setParameter(const QString &name, int value) { parameters[name] = QString::number(value); }
virtual void extractParameters() { };
virtual void extractParameters() { }
virtual QString getItemType() const = 0;
virtual bool readElement(QXmlStreamReader * /*xml*/) { return false; }
virtual void writeElement(QXmlStreamWriter * /*xml*/) { }
private:
static void initializeHashAuto();
public:
@ -34,8 +43,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);
bool read(QXmlStreamReader *xml);
void write(QXmlStreamWriter *xml);
};
class Command : public ProtocolItem {
@ -143,4 +152,62 @@ public:
ChatEvent(const QString &_eventName, const QString &_channel);
};
class Event_ChatListChannels : public GenericEvent {
Q_OBJECT
public:
class ChannelInfo {
private:
QString name;
QString description;
int playerCount;
bool autoJoin;
public:
ChannelInfo(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin)
: name(_name), description(_description), playerCount(_playerCount), autoJoin(_autoJoin) { }
QString getName() const { return name; }
QString getDescription() const { return description; }
int getPlayerCount() const { return playerCount; }
bool getAutoJoin() const { return autoJoin; }
};
private:
QList<ChannelInfo> channelList;
public:
Event_ChatListChannels() : GenericEvent("chat_list_channels") { }
int getItemId() const { return ItemId_Event_ChatListChannels; }
void addChannel(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin)
{
channelList.append(ChannelInfo(_name, _description, _playerCount, _autoJoin));
}
const QList<ChannelInfo> &getChannelList() const { return channelList; }
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
};
class Event_ChatListPlayers : public ChatEvent {
Q_OBJECT
public:
class PlayerInfo {
private:
QString name;
public:
PlayerInfo(const QString &_name)
: name(_name) { }
QString getName() const { return name; }
};
private:
QList<PlayerInfo> playerList;
public:
Event_ChatListPlayers(const QString &_channel) : ChatEvent("chat_list_players", _channel) { }
int getItemId() const { return ItemId_Event_ChatListPlayers; }
void addPlayer(const QString &_name)
{
playerList.append(PlayerInfo(_name));
}
const QList<PlayerInfo> &getPlayerList() const { return playerList; }
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
};
#endif

View file

@ -50,6 +50,9 @@ ItemId_Event_SetActivePlayer = 1048,
ItemId_Event_SetActivePhase = 1049,
ItemId_Event_DumpZone = 1050,
ItemId_Event_StopDumpZone = 1051,
ItemId_Event_Welcome = 1052,
ItemId_Other = 1053
ItemId_Event_ChatServerMessage = 1052,
ItemId_Event_ChatJoinChannel = 1053,
ItemId_Event_ChatLeaveChannel = 1054,
ItemId_Event_ChatSay = 1055,
ItemId_Other = 1056
};

View file

@ -551,14 +551,46 @@ void Event_StopDumpZone::extractParameters()
zoneOwnerId = parameters["zone_owner_id"].toInt();
zone = parameters["zone"];
}
Event_Welcome::Event_Welcome(const QString &_message)
: GenericEvent("welcome"), message(_message)
Event_ChatServerMessage::Event_ChatServerMessage(const QString &_channel, const QString &_message)
: ChatEvent("chat_server_message", _channel), message(_message)
{
setParameter("message", message);
}
void Event_Welcome::extractParameters()
void Event_ChatServerMessage::extractParameters()
{
GenericEvent::extractParameters();
ChatEvent::extractParameters();
message = parameters["message"];
}
Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, const QString &_playerName)
: ChatEvent("chat_join_channel", _channel), playerName(_playerName)
{
setParameter("player_name", playerName);
}
void Event_ChatJoinChannel::extractParameters()
{
ChatEvent::extractParameters();
playerName = parameters["player_name"];
}
Event_ChatLeaveChannel::Event_ChatLeaveChannel(const QString &_channel, const QString &_playerName)
: ChatEvent("chat_leave_channel", _channel), playerName(_playerName)
{
setParameter("player_name", playerName);
}
void Event_ChatLeaveChannel::extractParameters()
{
ChatEvent::extractParameters();
playerName = parameters["player_name"];
}
Event_ChatSay::Event_ChatSay(const QString &_channel, const QString &_playerName, const QString &_message)
: ChatEvent("chat_say", _channel), playerName(_playerName), message(_message)
{
setParameter("player_name", playerName);
setParameter("message", message);
}
void Event_ChatSay::extractParameters()
{
ChatEvent::extractParameters();
playerName = parameters["player_name"];
message = parameters["message"];
}
void ProtocolItem::initializeHashAuto()
@ -614,5 +646,8 @@ 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);
itemNameHash.insert("chat_eventchat_server_message", Event_ChatServerMessage::newItem);
itemNameHash.insert("chat_eventchat_join_channel", Event_ChatJoinChannel::newItem);
itemNameHash.insert("chat_eventchat_leave_channel", Event_ChatLeaveChannel::newItem);
itemNameHash.insert("chat_eventchat_say", Event_ChatSay::newItem);
}

View file

@ -49,4 +49,7 @@
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
5:chat_server_message:s,message
5:chat_join_channel:s,player_name
5:chat_leave_channel:s,player_name
5:chat_say:s,player_name:s,message

View file

@ -685,15 +685,53 @@ public:
protected:
void extractParameters();
};
class Event_Welcome : public GenericEvent {
class Event_ChatServerMessage : public ChatEvent {
Q_OBJECT
private:
QString message;
public:
Event_Welcome(const QString &_message = QString());
Event_ChatServerMessage(const QString &_channel = QString(), const QString &_message = QString());
QString getMessage() const { return message; }
static ProtocolItem *newItem() { return new Event_Welcome; }
int getItemId() const { return ItemId_Event_Welcome; }
static ProtocolItem *newItem() { return new Event_ChatServerMessage; }
int getItemId() const { return ItemId_Event_ChatServerMessage; }
protected:
void extractParameters();
};
class Event_ChatJoinChannel : public ChatEvent {
Q_OBJECT
private:
QString playerName;
public:
Event_ChatJoinChannel(const QString &_channel = QString(), const QString &_playerName = QString());
QString getPlayerName() const { return playerName; }
static ProtocolItem *newItem() { return new Event_ChatJoinChannel; }
int getItemId() const { return ItemId_Event_ChatJoinChannel; }
protected:
void extractParameters();
};
class Event_ChatLeaveChannel : public ChatEvent {
Q_OBJECT
private:
QString playerName;
public:
Event_ChatLeaveChannel(const QString &_channel = QString(), const QString &_playerName = QString());
QString getPlayerName() const { return playerName; }
static ProtocolItem *newItem() { return new Event_ChatLeaveChannel; }
int getItemId() const { return ItemId_Event_ChatLeaveChannel; }
protected:
void extractParameters();
};
class Event_ChatSay : public ChatEvent {
Q_OBJECT
private:
QString playerName;
QString message;
public:
Event_ChatSay(const QString &_channel = QString(), const QString &_playerName = QString(), const QString &_message = QString());
QString getPlayerName() const { return playerName; }
QString getMessage() const { return message; }
static ProtocolItem *newItem() { return new Event_ChatSay; }
int getItemId() const { return ItemId_Event_ChatSay; }
protected:
void extractParameters();
};

View file

@ -59,6 +59,13 @@ while (<file>) {
$parentConstructorCall = "$baseClass(\"$name1\")";
$constructorParamsH = "";
$constructorParamsCpp = "";
} elsif ($type == 5) {
$type = 'chat_event';
$namePrefix = 'Event';
$baseClass = 'ChatEvent';
$parentConstructorCall = "$baseClass(\"$name1\", _channel)";
$constructorParamsH = "const QString &_channel = QString()";
$constructorParamsCpp = "const QString &_channel";
}
$className = $namePrefix . '_' . $name2;
$itemEnum .= "ItemId_$className = " . ++$itemId . ",\n";

View file

@ -28,7 +28,7 @@ public:
void addClient(Server_ProtocolHandler *player);
void removeClient(Server_ProtocolHandler *player);
virtual QStringList getLoginMessage() const = 0;
virtual QString getLoginMessage() const = 0;
Server_Game *createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, const QString &playerName);
private:
QMap<int, Server_Game *> games;

View file

@ -1,46 +1,41 @@
#include "server_chatchannel.h"
#include "server_protocolhandler.h"
Server_ChatChannel::Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage)
Server_ChatChannel::Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage)
: name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage)
{
}
void Server_ChatChannel::addClient(Server_ProtocolHandler *client)
{
/* QString str = QString("chat|join_channel|%1|%2").arg(name).arg(player->getPlayerName());
for (int i = 0; i < size(); ++i)
at(i)->msg(str);
append(player);
sendChatEvent(new Event_ChatJoinChannel(name, client->getPlayerName()));
append(client);
Event_ChatListPlayers *eventCLP = new Event_ChatListPlayers(name);
for (int i = 0; i < size(); ++i)
player->msg(QString("chat|list_players|%1|%2").arg(name).arg(at(i)->getPlayerName()));
for (int i = 0; i < joinMessage.size(); ++i)
player->msg(QString("chat|server_message|%1|%2").arg(name).arg(joinMessage[i]));
eventCLP->addPlayer(at(i)->getPlayerName());
client->enqueueProtocolItem(eventCLP);
client->enqueueProtocolItem(new Event_ChatServerMessage(name, joinMessage));
emit channelInfoChanged();
*/}
}
void Server_ChatChannel::removeClient(Server_ProtocolHandler *client)
{
/* QString str = QString("chat|leave_channel|%1|%2").arg(name).arg(player->getPlayerName());
removeAt(indexOf(player));
for (int i = 0; i < size(); ++i)
at(i)->msg(str);
removeAt(indexOf(client));
sendChatEvent(new Event_ChatLeaveChannel(name, client->getPlayerName()));
emit channelInfoChanged();
*/}
}
void Server_ChatChannel::say(Server_ProtocolHandler *client, const QString &s)
{
/* QString str = QString("chat|say|%1|%2|%3").arg(name).arg(player->getPlayerName()).arg(s);
for (int i = 0; i < size(); ++i)
at(i)->msg(str);
*/}
QString Server_ChatChannel::getChannelListLine() const
{
// return QString("chat|list_channels|%1|%2|%3|%4").arg(name).arg(description).arg(size()).arg(autoJoin ? 1 : 0);
sendChatEvent(new Event_ChatSay(name, client->getPlayerName(), s));
}
void Server_ChatChannel::sendChatEvent(ChatEvent *event)
{
for (int i = 0; i < size(); ++i)
at(i)->sendProtocolItem(event, false);
delete event;
}

View file

@ -6,6 +6,7 @@
#include <QStringList>
class Server_ProtocolHandler;
class ChatEvent;
class Server_ChatChannel : public QObject, public QList<Server_ProtocolHandler *> {
Q_OBJECT
@ -15,16 +16,17 @@ private:
QString name;
QString description;
bool autoJoin;
QStringList joinMessage;
QString joinMessage;
public:
Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage);
Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage);
QString getName() const { return name; }
QString getDescription() const { return description; }
bool getAutoJoin() const { return autoJoin; }
void addClient(Server_ProtocolHandler *client);
void removeClient(Server_ProtocolHandler *client);
void say(Server_ProtocolHandler *client, const QString &s);
QString getChannelListLine() const;
void sendChatEvent(ChatEvent *event);
};
#endif

View file

@ -85,6 +85,14 @@ void Server_ProtocolHandler::processCommand(Command *command)
sendProtocolItem(new ProtocolResponse(command->getCmdId(), response));
delete command;
while (!itemQueue.isEmpty())
sendProtocolItem(itemQueue.takeFirst());
}
void Server_ProtocolHandler::enqueueProtocolItem(ProtocolItem *item)
{
itemQueue << item;
}
QPair<Server_Game *, Server_Player *> Server_ProtocolHandler::getGame(int gameId) const
@ -106,23 +114,19 @@ ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *c
return ProtocolResponse::RespWrongPassword;
playerName = cmd->getUsername();
/*
Mit enqueueProtocolItem() lösen!
QStringList loginMessage = server->getLoginMessage();
for (int i = 0; i < loginMessage.size(); ++i)
msg("chat|server_message||" + loginMessage[i]);
*/
enqueueProtocolItem(new Event_ChatServerMessage(QString(), server->getLoginMessage()));
return ProtocolResponse::RespOk;
}
ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdChatListChannels(Command_ChatListChannels *cmd)
{
Event_ChatListChannels *event = new Event_ChatListChannels;
QMapIterator<QString, Server_ChatChannel *> channelIterator(server->getChatChannels());
while (channelIterator.hasNext()) {
Server_ChatChannel *c = channelIterator.next().value();
// msg(c->getChannelListLine());
event->addChannel(c->getName(), c->getDescription(), c->size(), c->getAutoJoin());
}
sendProtocolItem(event);
acceptsChatChannelListChanges = true;
return ProtocolResponse::RespOk;

View file

@ -24,6 +24,8 @@ private:
bool acceptsGameListChanges;
bool acceptsChatChannelListChanges;
QList<ProtocolItem *> itemQueue;
ProtocolResponse::ResponseCode cmdPing(Command_Ping *cmd);
ProtocolResponse::ResponseCode cmdLogin(Command_Login *cmd);
ProtocolResponse::ResponseCode cmdChatListChannels(Command_ChatListChannels *cmd);
@ -54,9 +56,6 @@ private:
ProtocolResponse::ResponseCode cmdStopDumpZone(Command_StopDumpZone *cmd, Server_Game *game, Server_Player *player);
ProtocolResponse::ResponseCode cmdDumpAll(Command_DumpAll *cmd, Server_Game *game, Server_Player *player);
ProtocolResponse::ResponseCode cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player);
QList<ProtocolItem *> itemQueue;
void enqueueProtocolItem(ProtocolItem *item);
public:
Server_ProtocolHandler(Server *_server, QObject *parent = 0);
~Server_ProtocolHandler();
@ -66,7 +65,8 @@ public:
const QString &getPlayerName() const { return playerName; }
void processCommand(Command *command);
virtual void sendProtocolItem(ProtocolItem *item) = 0;
virtual void sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0;
void enqueueProtocolItem(ProtocolItem *item);
};
#endif

View file

@ -46,13 +46,13 @@ Servatrice::Servatrice(QObject *parent)
settings->value("name").toString(),
settings->value("description").toString(),
settings->value("autojoin").toBool(),
settings->value("joinmessage").toStringList()
settings->value("joinmessage").toString()
);
addChatChannel(newChannel);
}
settings->endArray();
loginMessage = settings->value("messages/login").toStringList();
loginMessage = settings->value("messages/login").toString();
}
Servatrice::~Servatrice()
@ -121,4 +121,4 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS
return UnknownUser;
}
const QString Servatrice::versionString = "Servatrice 0.20091030";
const QString Servatrice::versionString = "Servatrice 0.20091103";

View file

@ -37,10 +37,10 @@ public:
~Servatrice();
bool openDatabase();
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
QStringList getLoginMessage() const { return loginMessage; }
QString getLoginMessage() const { return loginMessage; }
private:
QTcpServer *tcpServer;
QStringList loginMessage;
QString loginMessage;
QSettings *settings;
};

View file

@ -42,7 +42,7 @@ ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socke
xmlWriter->writeStartElement("cockatrice_server_stream");
xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion));
sendProtocolItem(new Event_Welcome(Servatrice::versionString));
sendProtocolItem(new Event_ChatServerMessage(QString(), Servatrice::versionString));
}
ServerSocketInterface::~ServerSocketInterface()
@ -104,8 +104,9 @@ void ServerSocketInterface::catchSocketError(QAbstractSocket::SocketError socket
deleteLater();
}
void ServerSocketInterface::sendProtocolItem(ProtocolItem *item)
void ServerSocketInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem)
{
item->write(xmlWriter);
if (deleteItem)
delete item;
}

View file

@ -43,7 +43,7 @@ public:
ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent = 0);
~ServerSocketInterface();
void sendProtocolItem(ProtocolItem *item);
void sendProtocolItem(ProtocolItem *item, bool deleteItem = true);
};
#endif