From bd2855cb95a7e9ce7d00a485a5ed638fa9496948 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Mon, 26 Oct 2009 17:05:08 +0100 Subject: [PATCH] response codes --- common/protocol.cpp | 76 +++++++++++++++++++++++++++++++----- common/protocol.h | 44 ++++++++++++++++++--- common/protocol_commands.cpp | 62 ++++++++++++++--------------- common/protocol_commands.h | 60 ++++++++++++++-------------- common/protocol_mc.pl | 6 +-- common/widget.cpp | 26 +++++++----- common/widget.h | 4 +- 7 files changed, 186 insertions(+), 92 deletions(-) diff --git a/common/protocol.cpp b/common/protocol.cpp index bef71c0f..cb50eaa6 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -4,14 +4,14 @@ #include "protocol.h" #include "protocol_commands.h" -QHash Command::commandHash; +QHash ProtocolItem::itemNameHash; -Command::Command(const QString &_cmdName) - : cmdName(_cmdName) +ProtocolItem::ProtocolItem(const QString &_itemName) + : itemName(_itemName) { } -bool Command::read(QXmlStreamReader &xml) +bool ProtocolItem::read(QXmlStreamReader &xml) { while (!xml.atEnd()) { xml.readNext(); @@ -19,7 +19,7 @@ bool Command::read(QXmlStreamReader &xml) qDebug() << "startElement: " << xml.name().toString(); } else if (xml.isEndElement()) { qDebug() << "endElement: " << xml.name().toString(); - if (xml.name() == cmdName) { + if (xml.name() == getItemType()) { extractParameters(); qDebug() << "FERTIG"; deleteLater(); @@ -39,9 +39,11 @@ bool Command::read(QXmlStreamReader &xml) return false; } -void Command::write(QXmlStreamWriter &xml) +void ProtocolItem::write(QXmlStreamWriter &xml) { - xml.writeStartElement(cmdName); + xml.writeStartElement(getItemType()); + if (!itemName.isEmpty()) + xml.writeAttribute("name", itemName); QMapIterator i(parameters); while (i.hasNext()) { @@ -52,9 +54,63 @@ void Command::write(QXmlStreamWriter &xml) xml.writeEndElement(); } -Command *Command::getNewCommand(const QString &name) +ProtocolItem *ProtocolItem::getNewItem(const QString &name) { - if (!commandHash.contains(name)) + if (!itemNameHash.contains(name)) return 0; - return commandHash.value(name)(); + return itemNameHash.value(name)(); +} + +void ProtocolItem::initializeHash() +{ + initializeHashAuto(); + itemNameHash.insert("resp", ProtocolResponse::newItem); + ProtocolResponse::initializeHash(); +} + +int Command::lastCmdId = 0; + +Command::Command(const QString &_itemName, int _cmdId) + : ProtocolItem(_itemName), cmdId(_cmdId) +{ + if (cmdId == -1) + cmdId = lastCmdId++; + setParameter("cmd_id", cmdId); +} + +void Command::extractParameters() +{ + bool ok; + cmdId = parameters["cmd_id"].toInt(&ok); + if (!ok) + cmdId = -1; +} + +QHash ProtocolResponse::responseHash; + +ProtocolResponse::ProtocolResponse(int _cmdId, ResponseCode _responseCode) + : ProtocolItem(QString()), cmdId(_cmdId), responseCode(_responseCode) +{ + setParameter("cmd_id", cmdId); + setParameter("response_code", responseHash.key(responseCode)); +} + +void ProtocolResponse::extractParameters() +{ + bool ok; + cmdId = parameters["cmd_id"].toInt(&ok); + if (!ok) + cmdId = -1; + + responseCode = responseHash.value(parameters["response_code"], RespOk); +} + +void ProtocolResponse::initializeHash() +{ + responseHash.insert("ok", RespOk); + responseHash.insert("name_not_found", RespNameNotFound); + responseHash.insert("login_needed", RespLoginNeeded); + responseHash.insert("context_error", RespContextError); + responseHash.insert("wrong_password", RespWrongPassword); + responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed); } diff --git a/common/protocol.h b/common/protocol.h index d4e17a38..917d8977 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -9,28 +9,43 @@ class QXmlStreamReader; class QXmlStreamWriter; +class QXmlStreamAttributes; -class Command : public QObject { +class ProtocolItem : public QObject { Q_OBJECT protected: - typedef Command *(*NewCommandFunction)(); - static QHash commandHash; + typedef ProtocolItem *(*NewItemFunction)(); + static QHash itemNameHash; - QString cmdName; + QString itemName; QMap 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 QString getItemType() const = 0; +private: + static void initializeHashAuto(); public: - Command(const QString &_cmdName); + ProtocolItem(const QString &_itemName); static void initializeHash(); - static Command *getNewCommand(const QString &name); + static ProtocolItem *getNewItem(const QString &name); virtual bool read(QXmlStreamReader &xml); virtual void write(QXmlStreamWriter &xml); }; +class Command : public ProtocolItem { +private: + int cmdId; + static int lastCmdId; +protected: + QString getItemType() const { return "cmd"; } + void extractParameters(); +public: + Command(const QString &_itemName, int _cmdId = -1); +}; + class ChatCommand : public Command { Q_OBJECT private: @@ -67,4 +82,21 @@ public: int getGameId() const { return gameId; } }; +class ProtocolResponse : public ProtocolItem { + Q_OBJECT +public: + enum ResponseCode { RespOk, RespNameNotFound, RespLoginNeeded, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed }; +private: + int cmdId; + ResponseCode responseCode; + static QHash responseHash; +protected: + QString getItemType() const { return "resp"; } + void extractParameters(); +public: + ProtocolResponse(int _cmdId = -1, ResponseCode _responseCode = RespOk); + static void initializeHash(); + static ProtocolItem *newItem() { return new ProtocolResponse; } +}; + #endif diff --git a/common/protocol_commands.cpp b/common/protocol_commands.cpp index 9bee6201..9217aafb 100644 --- a/common/protocol_commands.cpp +++ b/common/protocol_commands.cpp @@ -305,36 +305,36 @@ Command_SubmitDeck::Command_SubmitDeck(int _gameId) : GameCommand("submit_deck", _gameId) { } -void Command::initializeHash() +void ProtocolItem::initializeHashAuto() { - commandHash.insert("ping", Command_Ping::newCommand); - commandHash.insert("login", Command_Login::newCommand); - commandHash.insert("chat_list_channels", Command_ChatListChannels::newCommand); - commandHash.insert("chat_join_channel", Command_ChatJoinChannel::newCommand); - commandHash.insert("chat_leave_channel", Command_ChatLeaveChannel::newCommand); - commandHash.insert("chat_say", Command_ChatSay::newCommand); - commandHash.insert("list_games", Command_ListGames::newCommand); - commandHash.insert("create_game", Command_CreateGame::newCommand); - commandHash.insert("join_game", Command_JoinGame::newCommand); - commandHash.insert("leave_game", Command_LeaveGame::newCommand); - commandHash.insert("say", Command_Say::newCommand); - commandHash.insert("shuffle", Command_Shuffle::newCommand); - commandHash.insert("roll_die", Command_RollDie::newCommand); - commandHash.insert("draw_cards", Command_DrawCards::newCommand); - commandHash.insert("move_card", Command_MoveCard::newCommand); - commandHash.insert("create_token", Command_CreateToken::newCommand); - commandHash.insert("create_arrow", Command_CreateArrow::newCommand); - commandHash.insert("delete_arrow", Command_DeleteArrow::newCommand); - commandHash.insert("set_card_attr", Command_SetCardAttr::newCommand); - commandHash.insert("ready_start", Command_ReadyStart::newCommand); - commandHash.insert("inc_counter", Command_IncCounter::newCommand); - commandHash.insert("add_counter", Command_AddCounter::newCommand); - commandHash.insert("set_counter", Command_SetCounter::newCommand); - commandHash.insert("del_counter", Command_DelCounter::newCommand); - commandHash.insert("next_turn", Command_NextTurn::newCommand); - commandHash.insert("set_active_phase", Command_SetActivePhase::newCommand); - commandHash.insert("dump_zone", Command_DumpZone::newCommand); - commandHash.insert("stop_dump_zone", Command_StopDumpZone::newCommand); - commandHash.insert("dump_all", Command_DumpAll::newCommand); - commandHash.insert("submit_deck", Command_SubmitDeck::newCommand); + itemNameHash.insert("cmdping", Command_Ping::newItem); + itemNameHash.insert("cmdlogin", Command_Login::newItem); + itemNameHash.insert("cmdchat_list_channels", Command_ChatListChannels::newItem); + itemNameHash.insert("cmdchat_join_channel", Command_ChatJoinChannel::newItem); + itemNameHash.insert("cmdchat_leave_channel", Command_ChatLeaveChannel::newItem); + itemNameHash.insert("cmdchat_say", Command_ChatSay::newItem); + itemNameHash.insert("cmdlist_games", Command_ListGames::newItem); + itemNameHash.insert("cmdcreate_game", Command_CreateGame::newItem); + itemNameHash.insert("cmdjoin_game", Command_JoinGame::newItem); + itemNameHash.insert("cmdleave_game", Command_LeaveGame::newItem); + itemNameHash.insert("cmdsay", Command_Say::newItem); + itemNameHash.insert("cmdshuffle", Command_Shuffle::newItem); + itemNameHash.insert("cmdroll_die", Command_RollDie::newItem); + itemNameHash.insert("cmddraw_cards", Command_DrawCards::newItem); + itemNameHash.insert("cmdmove_card", Command_MoveCard::newItem); + itemNameHash.insert("cmdcreate_token", Command_CreateToken::newItem); + itemNameHash.insert("cmdcreate_arrow", Command_CreateArrow::newItem); + itemNameHash.insert("cmddelete_arrow", Command_DeleteArrow::newItem); + itemNameHash.insert("cmdset_card_attr", Command_SetCardAttr::newItem); + itemNameHash.insert("cmdready_start", Command_ReadyStart::newItem); + itemNameHash.insert("cmdinc_counter", Command_IncCounter::newItem); + itemNameHash.insert("cmdadd_counter", Command_AddCounter::newItem); + itemNameHash.insert("cmdset_counter", Command_SetCounter::newItem); + itemNameHash.insert("cmddel_counter", Command_DelCounter::newItem); + itemNameHash.insert("cmdnext_turn", Command_NextTurn::newItem); + itemNameHash.insert("cmdset_active_phase", Command_SetActivePhase::newItem); + itemNameHash.insert("cmddump_zone", Command_DumpZone::newItem); + itemNameHash.insert("cmdstop_dump_zone", Command_StopDumpZone::newItem); + itemNameHash.insert("cmddump_all", Command_DumpAll::newItem); + itemNameHash.insert("cmdsubmit_deck", Command_SubmitDeck::newItem); } diff --git a/common/protocol_commands.h b/common/protocol_commands.h index 51323a12..391afc3b 100644 --- a/common/protocol_commands.h +++ b/common/protocol_commands.h @@ -8,7 +8,7 @@ class Command_Ping : public Command { private: public: Command_Ping(); - static Command *newCommand() { return new Command_Ping; } + static ProtocolItem *newItem() { return new Command_Ping; } }; class Command_Login : public Command { Q_OBJECT @@ -19,7 +19,7 @@ public: Command_Login(const QString &_username = QString(), const QString &_password = QString()); QString getUsername() const { return username; } QString getPassword() const { return password; } - static Command *newCommand() { return new Command_Login; } + static ProtocolItem *newItem() { return new Command_Login; } protected: void extractParameters(); }; @@ -28,7 +28,7 @@ class Command_ChatListChannels : public Command { private: public: Command_ChatListChannels(); - static Command *newCommand() { return new Command_ChatListChannels; } + static ProtocolItem *newItem() { return new Command_ChatListChannels; } }; class Command_ChatJoinChannel : public Command { Q_OBJECT @@ -37,7 +37,7 @@ private: public: Command_ChatJoinChannel(const QString &_channel = QString()); QString getChannel() const { return channel; } - static Command *newCommand() { return new Command_ChatJoinChannel; } + static ProtocolItem *newItem() { return new Command_ChatJoinChannel; } protected: void extractParameters(); }; @@ -46,7 +46,7 @@ class Command_ChatLeaveChannel : public ChatCommand { private: public: Command_ChatLeaveChannel(const QString &_channel = QString()); - static Command *newCommand() { return new Command_ChatLeaveChannel; } + static ProtocolItem *newItem() { return new Command_ChatLeaveChannel; } }; class Command_ChatSay : public ChatCommand { Q_OBJECT @@ -55,7 +55,7 @@ private: public: Command_ChatSay(const QString &_channel = QString(), const QString &_message = QString()); QString getMessage() const { return message; } - static Command *newCommand() { return new Command_ChatSay; } + static ProtocolItem *newItem() { return new Command_ChatSay; } protected: void extractParameters(); }; @@ -64,7 +64,7 @@ class Command_ListGames : public Command { private: public: Command_ListGames(); - static Command *newCommand() { return new Command_ListGames; } + static ProtocolItem *newItem() { return new Command_ListGames; } }; class Command_CreateGame : public Command { Q_OBJECT @@ -79,7 +79,7 @@ public: QString getPassword() const { return password; } int getMaxPlayers() const { return maxPlayers; } bool getSpectatorsAllowed() const { return spectatorsAllowed; } - static Command *newCommand() { return new Command_CreateGame; } + static ProtocolItem *newItem() { return new Command_CreateGame; } protected: void extractParameters(); }; @@ -94,7 +94,7 @@ public: int getGameId() const { return gameId; } QString getPassword() const { return password; } bool getSpectator() const { return spectator; } - static Command *newCommand() { return new Command_JoinGame; } + static ProtocolItem *newItem() { return new Command_JoinGame; } protected: void extractParameters(); }; @@ -103,7 +103,7 @@ class Command_LeaveGame : public GameCommand { private: public: Command_LeaveGame(int _gameId = -1); - static Command *newCommand() { return new Command_LeaveGame; } + static ProtocolItem *newItem() { return new Command_LeaveGame; } }; class Command_Say : public GameCommand { Q_OBJECT @@ -112,7 +112,7 @@ private: public: Command_Say(int _gameId = -1, const QString &_message = QString()); QString getMessage() const { return message; } - static Command *newCommand() { return new Command_Say; } + static ProtocolItem *newItem() { return new Command_Say; } protected: void extractParameters(); }; @@ -121,7 +121,7 @@ class Command_Shuffle : public GameCommand { private: public: Command_Shuffle(int _gameId = -1); - static Command *newCommand() { return new Command_Shuffle; } + static ProtocolItem *newItem() { return new Command_Shuffle; } }; class Command_RollDie : public GameCommand { Q_OBJECT @@ -130,7 +130,7 @@ private: public: Command_RollDie(int _gameId = -1, int _sides = -1); int getSides() const { return sides; } - static Command *newCommand() { return new Command_RollDie; } + static ProtocolItem *newItem() { return new Command_RollDie; } protected: void extractParameters(); }; @@ -141,7 +141,7 @@ private: public: Command_DrawCards(int _gameId = -1, int _number = -1); int getNumber() const { return number; } - static Command *newCommand() { return new Command_DrawCards; } + static ProtocolItem *newItem() { return new Command_DrawCards; } protected: void extractParameters(); }; @@ -162,7 +162,7 @@ public: int getX() const { return x; } int getY() const { return y; } bool getFaceDown() const { return faceDown; } - static Command *newCommand() { return new Command_MoveCard; } + static ProtocolItem *newItem() { return new Command_MoveCard; } protected: void extractParameters(); }; @@ -181,7 +181,7 @@ public: QString getPt() const { return pt; } int getX() const { return x; } int getY() const { return y; } - static Command *newCommand() { return new Command_CreateToken; } + static ProtocolItem *newItem() { return new Command_CreateToken; } protected: void extractParameters(); }; @@ -204,7 +204,7 @@ public: QString getTargetPlayerZone() const { return targetPlayerZone; } int getTargetCardId() const { return targetCardId; } int getColor() const { return color; } - static Command *newCommand() { return new Command_CreateArrow; } + static ProtocolItem *newItem() { return new Command_CreateArrow; } protected: void extractParameters(); }; @@ -215,7 +215,7 @@ private: public: Command_DeleteArrow(int _gameId = -1, int _arrowId = -1); int getArrowId() const { return arrowId; } - static Command *newCommand() { return new Command_DeleteArrow; } + static ProtocolItem *newItem() { return new Command_DeleteArrow; } protected: void extractParameters(); }; @@ -232,7 +232,7 @@ public: int getCardId() const { return cardId; } QString getAttrName() const { return attrName; } QString getAttrValue() const { return attrValue; } - static Command *newCommand() { return new Command_SetCardAttr; } + static ProtocolItem *newItem() { return new Command_SetCardAttr; } protected: void extractParameters(); }; @@ -241,7 +241,7 @@ class Command_ReadyStart : public GameCommand { private: public: Command_ReadyStart(int _gameId = -1); - static Command *newCommand() { return new Command_ReadyStart; } + static ProtocolItem *newItem() { return new Command_ReadyStart; } }; class Command_IncCounter : public GameCommand { Q_OBJECT @@ -252,7 +252,7 @@ public: Command_IncCounter(int _gameId = -1, int _counterId = -1, int _delta = -1); int getCounterId() const { return counterId; } int getDelta() const { return delta; } - static Command *newCommand() { return new Command_IncCounter; } + static ProtocolItem *newItem() { return new Command_IncCounter; } protected: void extractParameters(); }; @@ -269,7 +269,7 @@ public: int getColor() const { return color; } int getRadius() const { return radius; } int getValue() const { return value; } - static Command *newCommand() { return new Command_AddCounter; } + static ProtocolItem *newItem() { return new Command_AddCounter; } protected: void extractParameters(); }; @@ -282,7 +282,7 @@ public: Command_SetCounter(int _gameId = -1, int _counterId = -1, int _value = -1); int getCounterId() const { return counterId; } int getValue() const { return value; } - static Command *newCommand() { return new Command_SetCounter; } + static ProtocolItem *newItem() { return new Command_SetCounter; } protected: void extractParameters(); }; @@ -293,7 +293,7 @@ private: public: Command_DelCounter(int _gameId = -1, int _counterId = -1); int getCounterId() const { return counterId; } - static Command *newCommand() { return new Command_DelCounter; } + static ProtocolItem *newItem() { return new Command_DelCounter; } protected: void extractParameters(); }; @@ -302,7 +302,7 @@ class Command_NextTurn : public GameCommand { private: public: Command_NextTurn(int _gameId = -1); - static Command *newCommand() { return new Command_NextTurn; } + static ProtocolItem *newItem() { return new Command_NextTurn; } }; class Command_SetActivePhase : public GameCommand { Q_OBJECT @@ -311,7 +311,7 @@ private: public: Command_SetActivePhase(int _gameId = -1, int _phase = -1); int getPhase() const { return phase; } - static Command *newCommand() { return new Command_SetActivePhase; } + static ProtocolItem *newItem() { return new Command_SetActivePhase; } protected: void extractParameters(); }; @@ -326,7 +326,7 @@ public: int getPlayerId() const { return playerId; } QString getZoneName() const { return zoneName; } int getNumberCards() const { return numberCards; } - static Command *newCommand() { return new Command_DumpZone; } + static ProtocolItem *newItem() { return new Command_DumpZone; } protected: void extractParameters(); }; @@ -339,7 +339,7 @@ public: Command_StopDumpZone(int _gameId = -1, int _playerId = -1, const QString &_zoneName = QString()); int getPlayerId() const { return playerId; } QString getZoneName() const { return zoneName; } - static Command *newCommand() { return new Command_StopDumpZone; } + static ProtocolItem *newItem() { return new Command_StopDumpZone; } protected: void extractParameters(); }; @@ -348,14 +348,14 @@ class Command_DumpAll : public GameCommand { private: public: Command_DumpAll(int _gameId = -1); - static Command *newCommand() { return new Command_DumpAll; } + static ProtocolItem *newItem() { return new Command_DumpAll; } }; class Command_SubmitDeck : public GameCommand { Q_OBJECT private: public: Command_SubmitDeck(int _gameId = -1); - static Command *newCommand() { return new Command_SubmitDeck; } + static ProtocolItem *newItem() { return new Command_SubmitDeck; } }; #endif diff --git a/common/protocol_mc.pl b/common/protocol_mc.pl index 1ae23399..a3fac356 100755 --- a/common/protocol_mc.pl +++ b/common/protocol_mc.pl @@ -78,7 +78,7 @@ while () { print headerfile "public:\n" . "\t$className($constructorParamsH);\n" . $paramStr4 - . "\tstatic Command *newCommand() { return new $className; }\n" + . "\tstatic ProtocolItem *newItem() { return new $className; }\n" . ($paramStr5 eq '' ? '' : "protected:\n\tvoid extractParameters();\n") . "};\n"; print cppfile $className . "::$className($constructorParamsCpp)\n" @@ -93,14 +93,14 @@ while () { . $paramStr5 . "}\n"; } - $initializeHash .= "\tcommandHash.insert(\"$name1\", $className" . "::newCommand);\n"; + $initializeHash .= "\titemNameHash.insert(\"cmd$name1\", $className" . "::newItem);\n"; } close(file); print headerfile "\n#endif\n"; close(headerfile); -print cppfile "void Command::initializeHash()\n" +print cppfile "void ProtocolItem::initializeHashAuto()\n" . "{\n" . $initializeHash . "}\n"; diff --git a/common/widget.cpp b/common/widget.cpp index 81fee7e3..ca960233 100644 --- a/common/widget.cpp +++ b/common/widget.cpp @@ -5,7 +5,7 @@ #include "protocol_commands.h" Widget::Widget() - : QMainWindow(), currentCommand(0) + : QMainWindow() { edit1 = new QTextEdit; start = new QPushButton; @@ -26,12 +26,14 @@ Widget::Widget() central->setLayout(vbox); setCentralWidget(central); + resize(400, 500); + Command::initializeHash(); } void Widget::startClicked() { - currentCommand = 0; + currentItem = 0; xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("cockatrice_communication"); xmlWriter.writeAttribute("version", "4"); @@ -44,15 +46,18 @@ void Widget::startClicked() Command *test3 = new Command_ChatSay("foobar", "Hallo, dies ist ein Test"); test3->write(xmlWriter); + + ProtocolResponse *test4 = new ProtocolResponse(123, ProtocolResponse::RespContextError); + test4->write(xmlWriter); } bool Widget::readCurrentCommand() { - if (!currentCommand) + if (!currentItem) return false; - if (currentCommand->read(xmlReader)) { + if (currentItem->read(xmlReader)) { qDebug() << "setting to 0"; - currentCommand = 0; + currentItem = 0; } return true; } @@ -65,11 +70,12 @@ void Widget::parseXml() while (!xmlReader.atEnd()) { xmlReader.readNext(); if (xmlReader.isStartElement()) { - QString cmdStr = xmlReader.name().toString(); - qDebug() << "parseXml: startElement: " << cmdStr; - currentCommand = Command::getNewCommand(cmdStr); - if (!currentCommand) - qDebug() << "unrecognized command"; + QString itemType = xmlReader.name().toString(); + QString itemName = xmlReader.attributes().value("name").toString(); + qDebug() << "parseXml: startElement: " << "type =" << itemType << ", name =" << itemName; + currentItem = ProtocolItem::getNewItem(itemType + itemName); + if (!currentItem) + qDebug() << "unrecognized item"; readCurrentCommand(); } } diff --git a/common/widget.h b/common/widget.h index f6c22778..f43d9c88 100644 --- a/common/widget.h +++ b/common/widget.h @@ -8,7 +8,7 @@ class QTextEdit; class QPushButton; class QBuffer; -class Command; +class ProtocolItem; class Widget : public QMainWindow { Q_OBJECT @@ -19,7 +19,7 @@ private: QXmlStreamReader xmlReader; QXmlStreamWriter xmlWriter; - Command *currentCommand; + ProtocolItem *currentItem; bool readCurrentCommand(); void parseBuffer(); void parseXml();