Commit 33d31e23 authored by louiz’'s avatar louiz’

[slave/master] implement game creation, save in database, with an id

parent 136fa6bc
......@@ -67,3 +67,10 @@ std::tuple<bool, std::string> Database::check_user_password(const db::User& user
return std::make_tuple(true, "");
return std::make_tuple(false, "Password mismatch.");
}
db::Game Database::create_new_game() const
{
db::Game game(this->db);
game.update();
return game;
}
......@@ -8,4 +8,8 @@
<indexfield name="login"/>
</index>
</object>
<object name="Game">
<field name="start" type="datetime"/>
</object>
</database>
......@@ -14,11 +14,13 @@
namespace ph = std::placeholders;
GameServer::GameServer(short port, const std::string& ipc_path):
GameServer::GameServer(short port, const std::string& ipc_path,
const uint64_t game_id):
Game(),
Server<RemoteGameClient>(port),
replay(),
ipc(nullptr)
ipc(nullptr),
game_id(game_id)
{
if (!ipc_path.empty())
{
......@@ -41,13 +43,29 @@ void GameServer::install_parent_stats_timed_event()
{
auto cb = [this]()
{
log_debug("Sending things in the IPC thing");
this->ipc->send("coucou");
this->send_stats();
this->install_parent_stats_timed_event();
};
this->timed_event_handler.install_timed_event(cb, 5);
}
void GameServer::send_stats()
{
log_debug("Sending game stats on the IPC socket");
ser::game::GameInfo info;
info.set_id(this->game_id);
info.set_time(static_cast<uint32_t>(this->current_world_time().count()));
this->ipc_send(info);
}
void GameServer::ipc_send(const google::protobuf::Message& msg)
{
std::string to_send;
msg.SerializeToString(&to_send);
log_debug("Sending, over IPC: " << msg.ShortDebugString());
this->ipc->send(to_send);
}
void GameServer::on_entity_created(const Entity* entity)
{
Team* team = entity->get<Team>();
......
......@@ -23,13 +23,15 @@
#include <world/world.hpp>
#include <game/replay.hpp>
#include <game/turn_handler.hpp>
#include <google/protobuf/message.h>
class IPCEndpoint;
class GameServer: public Game, public Server<RemoteGameClient>
{
public:
GameServer(short port, const std::string& ipc_path);
GameServer(short port, const std::string& ipc_path,
const uint64_t game_id);
~GameServer();
void on_new_client(RemoteGameClient*) override final;
......@@ -129,6 +131,8 @@ public:
private:
void install_parent_stats_timed_event();
void ipc_send(const google::protobuf::Message&);
void send_stats();
GameServer(const GameServer&);
GameServer& operator=(const GameServer&);
......@@ -139,6 +143,7 @@ private:
TimedEventHandler timed_event_handler;
Replay replay;
std::unique_ptr<IPCEndpoint> ipc;
const uint64_t game_id;
};
#endif // __GAME_SERVER_HPP__
......
......@@ -14,6 +14,7 @@ int main(int ac, char** av)
short port;
std::string conf_filename;
std::string ipc_path;
uint64_t game_id;
boost::program_options::variables_map vm;
boost::program_options::options_description desc("Allowed options");
......@@ -28,6 +29,10 @@ int main(int ac, char** av)
"If a path is provided, will connect to that message queue and use it to communicate"
"with some parent process.")
("game_id,g", boost::program_options::value<uint64_t>(&game_id),
"Unique identifier for the game to be started.")
("config,c", boost::program_options::value<std::string>(&conf_filename)->default_value("./batajelo.conf"),
"The filename of the configuration to use");
......@@ -52,11 +57,11 @@ int main(int ac, char** av)
if (!Config::read_conf("./batajelo.conf"))
return 1;
log_debug("Starting game server. Port: " << port << " and config file: " << conf_filename
log_debug("Starting game server, id: " << game_id << ". Port: " << port << " and config file: " << conf_filename
<< " and ipc path [" << ipc_path << "]");
srand(getpid());
GameServer s(port, ipc_path);
GameServer s(port, ipc_path, game_id);
s.start();
utils::Time last_update = utils::now();
......
#include <master_server/master_to_slave_server.hpp>
#include <master_server/master_server.hpp>
#include <logging/logging.hpp>
......@@ -19,3 +20,8 @@ void MasterToSlaveServer::on_client_left(RemoteSlaveClient* client)
{
log_debug("Ending RemoteSlaveClient");
}
Database* MasterToSlaveServer::get_database()
{
return this->master->get_database();
}
......@@ -5,6 +5,7 @@
#include <master_server/remote_slave_client.hpp>
class MasterServer;
class Database;
class MasterToSlaveServer: public Server<RemoteSlaveClient>
{
......@@ -16,6 +17,8 @@ public:
void on_new_client(RemoteSlaveClient* client) override final;
void on_client_left(RemoteSlaveClient* client) override final;
Database* get_database();
private:
MasterServer* master;
......
#include <master_server/remote_slave_client.hpp>
#include <master_server/master_to_slave_server.hpp>
#include <database/database.hpp>
#include "slave.pb.h"
void RemoteSlaveClient::set_server(MasterToSlaveServer* server)
{
......@@ -13,5 +16,11 @@ void RemoteSlaveClient::on_connection_closed()
void RemoteSlaveClient::install_callbacks()
{
this->send_message("START", "");
ser::slave::StartGameRequest req;
auto game = this->server->get_database()->create_new_game();
log_debug("Game id created: " << game.id);
req.set_game_id(game.id);
this->send_message("START", req);
}
package ser.game;
message GameInfo
{
required uint64 id = 1;
// Game time in seconds
optional uint32 time = 2;
}
message Position
{
required sint32 x = 1;
......
#include <slave/child_game.hpp>
#include <logging/logging.hpp>
#include "game.pb.h"
ChildGame::ChildGame(Slave* slave):
pid(0),
slave(slave)
{
this->ipc.watch_read([this](const std::string& msg)
{
log_debug("Received things from child " << this->pid << ": [" << msg << "]");
ser::game::GameInfo info;
// TODO try catch
info.ParseFromString(msg);
log_debug("Received things from child " << this->pid << ": [" << info.ShortDebugString() << "]");
});
}
......
......@@ -59,7 +59,7 @@ void ChildrenHandler::on_child_exit()
this->install_signal_handler();
}
void ChildrenHandler::start_subprocess()
void ChildrenHandler::start_subprocess(const uint64_t game_id)
{
auto child = std::make_unique<ChildGame>(this->slave);
......@@ -67,10 +67,11 @@ void ChildrenHandler::start_subprocess()
if (pid == 0)
{
const short port = 7879;
const char* argv[6] = {
const char* argv[8] = {
"./batajelo_game_server",
"-p", std::to_string(port).data(),
"-i", child->get_ipc_endpoint_path().data(),
"-g", std::to_string(game_id).data(),
nullptr
};
execv("./batajelo_game_server", const_cast<char* const*>(argv));
......
......@@ -26,7 +26,7 @@ class ChildrenHandler
public:
ChildrenHandler(Slave*);
~ChildrenHandler() = default;
void start_subprocess();
void start_subprocess(const uint64_t game_id);
std::size_t size() const;
private:
......
......@@ -23,13 +23,12 @@ void Slave::run()
{
this->running = true;
this->client.start();
while (this->running)
this->client.poll();
IoService::get().run();
}
void Slave::start_game()
void Slave::start_game(const uint64_t game_id)
{
this->children_handler.start_subprocess();
this->children_handler.start_subprocess(game_id);
}
void Slave::fill_info(ser::slave::SlaveInfo& info)
......
......@@ -14,7 +14,7 @@ public:
Slave();
~Slave() = default;
void run();
void start_game();
void start_game(const uint64_t game_id);
/**
* Fill the given structure with the information about this slave.
*/
......
......@@ -56,5 +56,6 @@ void SlaveClient::info_callback(Message* message)
void SlaveClient::start_callback(Message* message)
{
this->slave->start_game();
auto req = message->parse_body_to_protobuf_object<ser::slave::StartGameRequest>();
this->slave->start_game(req.game_id());
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment