diff --git a/vpr/src/server/commconstants.h b/vpr/src/server/commconstants.h index 86c6a8c10d2..c1008e93b99 100644 --- a/vpr/src/server/commconstants.h +++ b/vpr/src/server/commconstants.h @@ -28,9 +28,10 @@ inline const std::string OPTION_DRAW_PATH_CONTOUR{"draw_path_contour"}; inline const std::string KEY_SETUP_PATH_LIST{"setup"}; inline const std::string KEY_HOLD_PATH_LIST{"hold"}; -enum CMD { - CMD_GET_PATH_LIST_ID=0, - CMD_DRAW_PATH_ID +enum class CMD : int { + NONE=-1, + GET_PATH_LIST_ID=0, + DRAW_PATH_ID=1 }; } // namespace comm diff --git a/vpr/src/server/gateio.cpp b/vpr/src/server/gateio.cpp index 7b58f10d823..d40431e7da9 100644 --- a/vpr/src/server/gateio.cpp +++ b/vpr/src/server/gateio.cpp @@ -148,7 +148,7 @@ GateIO::ActivityStatus GateIO::handle_telegrams(std::vector cmd_opt = comm::TelegramParser::try_extract_field_cmd(message); std::optional options_opt = comm::TelegramParser::try_extract_field_options(message); if (job_id_opt && cmd_opt && options_opt) { - TaskPtr task = std::make_unique(job_id_opt.value(), cmd_opt.value(), options_opt.value()); + TaskPtr task = std::make_unique(job_id_opt.value(), static_cast(cmd_opt.value()), options_opt.value()); const comm::TelegramHeader& header = telegram_frame->header; m_logger.queue(LogLevel::Info, "received:", header.info(), task->info(/*skipDuration*/true)); std::unique_lock lock(m_tasks_mutex); diff --git a/vpr/src/server/task.cpp b/vpr/src/server/task.cpp index 837369ba6c7..f05ada6d835 100644 --- a/vpr/src/server/task.cpp +++ b/vpr/src/server/task.cpp @@ -10,7 +10,7 @@ namespace server { -Task::Task(int jobId, int cmd, const std::string& options) +Task::Task(int jobId, comm::CMD cmd, const std::string& options) : m_job_id(jobId), m_cmd(cmd), m_options(options) { m_creation_time = std::chrono::high_resolution_clock::now(); } @@ -55,7 +55,7 @@ std::string Task::info(bool skip_duration) const { std::stringstream ss; ss << "task[" << "id=" << std::to_string(m_job_id) - << ",cmd=" << std::to_string(m_cmd); + << ",cmd=" << std::to_string(static_cast(m_cmd)); if (!skip_duration) { ss << ",exists=" << get_pretty_duration_str_from_ms(time_ms_elapsed()); } @@ -73,7 +73,7 @@ void Task::bake_response() { ss << "{"; ss << "\"" << comm::KEY_JOB_ID << "\":\"" << m_job_id << "\","; - ss << "\"" << comm::KEY_CMD << "\":\"" << m_cmd << "\","; + ss << "\"" << comm::KEY_CMD << "\":\"" << static_cast(m_cmd) << "\","; ss << "\"" << comm::KEY_OPTIONS << "\":\"" << m_options << "\","; if (has_error()) { ss << "\"" << comm::KEY_DATA << "\":\"" << m_error << "\","; diff --git a/vpr/src/server/task.h b/vpr/src/server/task.h index 9fef5268440..1d9ef811f69 100644 --- a/vpr/src/server/task.h +++ b/vpr/src/server/task.h @@ -8,6 +8,7 @@ #include #include "telegramheader.h" +#include "commconstants.h" namespace server { @@ -23,10 +24,10 @@ class Task { * @brief Constructs a new Task object. * * @param job_id The ID of the job associated with the task. - * @param cmd The command associated with the task. + * @param cmd The command ID (see @ref comm::CMD) associated with the task. * @param options Additional options for the task (default: empty string). */ - Task(int job_id, int cmd, const std::string& options = ""); + Task(int job_id, comm::CMD cmd, const std::string& options = ""); Task(const Task&) = delete; Task& operator=(const Task&) = delete; @@ -39,11 +40,11 @@ class Task { int job_id() const { return m_job_id; } /** - * @brief Gets the command associated with the task. + * @brief Gets the command ID associated with the task. * - * @return The command. + * @return The command ID (see @ref comm::CMD). */ - int cmd() const { return m_cmd; } + comm::CMD cmd() const { return m_cmd; } /** * @brief Removes the specified number of bytes from the response buffer. @@ -179,7 +180,7 @@ class Task { private: int m_job_id = -1; - int m_cmd = -1; + comm::CMD m_cmd = comm::CMD::NONE; std::string m_options; std::string m_result; std::string m_error; diff --git a/vpr/src/server/taskresolver.cpp b/vpr/src/server/taskresolver.cpp index abe9e5f3fda..5298521ece0 100644 --- a/vpr/src/server/taskresolver.cpp +++ b/vpr/src/server/taskresolver.cpp @@ -64,12 +64,12 @@ bool TaskResolver::update(ezgl::application* app) { for (auto& task: m_tasks) { if (!task->is_finished()) { switch(task->cmd()) { - case comm::CMD_GET_PATH_LIST_ID: { + case comm::CMD::GET_PATH_LIST_ID: { process_get_path_list_task(app, task); has_processed_task = true; break; } - case comm::CMD_DRAW_PATH_ID: { + case comm::CMD::DRAW_PATH_ID: { process_draw_critical_path_task(app, task); has_processed_task = true; break; diff --git a/vpr/test/test_server_taskresolver.cpp b/vpr/test/test_server_taskresolver.cpp index 4e9ff388341..eda8d5ae82f 100644 --- a/vpr/test/test_server_taskresolver.cpp +++ b/vpr/test/test_server_taskresolver.cpp @@ -8,7 +8,7 @@ TEST_CASE("test_server_taskresolver_cmdSpamFilter", "[vpr]") { server::TaskResolver resolver; - const int cmd = 10; + const comm::CMD cmd = comm::CMD::GET_PATH_LIST_ID; { server::TaskPtr task0 = std::make_unique(1, cmd); @@ -43,7 +43,7 @@ TEST_CASE("test_server_taskresolver_cmdSpamFilter", "[vpr]") { TEST_CASE("test_server_taskresolver_cmdOverrideFilter", "[vpr]") { server::TaskResolver resolver; - const int cmd = 10; + const comm::CMD cmd = comm::CMD::GET_PATH_LIST_ID; { server::TaskPtr task0 = std::make_unique(1, cmd, "1"); @@ -75,14 +75,17 @@ TEST_CASE("test_server_taskresolver_cmdOverrideFilter", "[vpr]") { TEST_CASE("test_server_taskresolver_cmdSpamAndOverrideOptions", "[vpr]") { server::TaskResolver resolver; + const comm::CMD cmd1 = comm::CMD::GET_PATH_LIST_ID; + const comm::CMD cmd2 = comm::CMD::DRAW_PATH_ID; + { - server::TaskPtr task0 = std::make_unique(1, 2, "1"); - server::TaskPtr task1 = std::make_unique(2, 2, "11"); - server::TaskPtr task2 = std::make_unique(3, 2, "222"); - server::TaskPtr task3 = std::make_unique(4, 2, "222"); - server::TaskPtr task4 = std::make_unique(5, 1); - server::TaskPtr task5 = std::make_unique(6, 1); - server::TaskPtr task6 = std::make_unique(7, 1); + server::TaskPtr task0 = std::make_unique(1, cmd2, "1"); + server::TaskPtr task1 = std::make_unique(2, cmd2, "11"); + server::TaskPtr task2 = std::make_unique(3, cmd2, "222"); + server::TaskPtr task3 = std::make_unique(4, cmd2, "222"); + server::TaskPtr task4 = std::make_unique(5, cmd1); + server::TaskPtr task5 = std::make_unique(6, cmd1); + server::TaskPtr task6 = std::make_unique(7, cmd1); resolver.own_task(std::move(task0)); resolver.own_task(std::move(task1)); @@ -101,11 +104,11 @@ TEST_CASE("test_server_taskresolver_cmdSpamAndOverrideOptions", "[vpr]") { const server::TaskPtr& task1 = resolver.tasks().at(1); REQUIRE(task0->job_id() == 3); - REQUIRE(task0->cmd() == 2); + REQUIRE(task0->cmd() == cmd2); REQUIRE(task0->options() == "222"); REQUIRE(task1->job_id() == 5); - REQUIRE(task1->cmd() == 1); + REQUIRE(task1->cmd() == cmd1); REQUIRE(task1->options() == ""); }