LobbyServer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * LobbyServer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "LobbyServer.h"
  12. #include "SQLiteConnection.h"
  13. #include "../lib/JsonNode.h"
  14. #include <boost/uuid/uuid_generators.hpp>
  15. #include <boost/uuid/uuid_io.hpp>
  16. static const std::string DATABASE_PATH = "/home/ivan/vcmi.db";
  17. static const int LISTENING_PORT = 30303;
  18. //static const std::string SERVER_NAME = GameConstants::VCMI_VERSION + " (server)";
  19. //static const std::string SERVER_UUID = boost::uuids::to_string(boost::uuids::random_generator()());
  20. void LobbyDatabase::prepareStatements()
  21. {
  22. static const std::string insertChatMessageText = R"(
  23. INSERT INTO chatMessages(senderName, messageText) VALUES( ?, ?);
  24. )";
  25. insertChatMessageStatement = database->prepare(insertChatMessageText);
  26. }
  27. void LobbyDatabase::createTableChatMessages()
  28. {
  29. static const std::string statementText = R"(
  30. CREATE TABLE IF NOT EXISTS chatMessages (
  31. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  32. senderName TEXT,
  33. messageText TEXT,
  34. sendTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  35. );
  36. )";
  37. auto statement = database->prepare(statementText);
  38. statement->execute();
  39. }
  40. void LobbyDatabase::initializeDatabase()
  41. {
  42. createTableChatMessages();
  43. }
  44. LobbyDatabase::LobbyDatabase()
  45. {
  46. database = SQLiteInstance::open(DATABASE_PATH, true);
  47. if (!database)
  48. throw std::runtime_error("Failed to open SQLite database!");
  49. initializeDatabase();
  50. prepareStatements();
  51. }
  52. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & messageText)
  53. {
  54. insertChatMessageStatement->setBinds(sender, messageText);
  55. insertChatMessageStatement->execute();
  56. insertChatMessageStatement->reset();
  57. }
  58. void LobbyServer::onNewConnection(const std::shared_ptr<NetworkConnection> &)
  59. {
  60. }
  61. void LobbyServer::onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message)
  62. {
  63. // FIXME: find better approach
  64. const char * payloadBegin = reinterpret_cast<const char*>(message.data());
  65. JsonNode json(payloadBegin, message.size());
  66. if (json["type"].String() == "sendChatMessage")
  67. {
  68. database->insertChatMessage("Unknown", json["messageText"].String());
  69. }
  70. }
  71. LobbyServer::LobbyServer()
  72. : database(new LobbyDatabase())
  73. {
  74. }
  75. int main(int argc, const char * argv[])
  76. {
  77. LobbyServer server;
  78. server.start(LISTENING_PORT);
  79. server.run();
  80. }