Skip to content

Commit

Permalink
Task::cmd now is comm::CMD enum, add doc reference to such enum to li…
Browse files Browse the repository at this point in the history
…st possible cmd values
  • Loading branch information
w0lek committed Jun 7, 2024
1 parent 86d31a3 commit e3cff27
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
7 changes: 4 additions & 3 deletions vpr/src/server/commconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/server/gateio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ GateIO::ActivityStatus GateIO::handle_telegrams(std::vector<comm::TelegramFrameP
std::optional<int> cmd_opt = comm::TelegramParser::try_extract_field_cmd(message);
std::optional<std::string> options_opt = comm::TelegramParser::try_extract_field_options(message);
if (job_id_opt && cmd_opt && options_opt) {
TaskPtr task = std::make_unique<Task>(job_id_opt.value(), cmd_opt.value(), options_opt.value());
TaskPtr task = std::make_unique<Task>(job_id_opt.value(), static_cast<comm::CMD>(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<std::mutex> lock(m_tasks_mutex);
Expand Down
6 changes: 3 additions & 3 deletions vpr/src/server/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<int>(m_cmd));
if (!skip_duration) {
ss << ",exists=" << get_pretty_duration_str_from_ms(time_ms_elapsed());
}
Expand All @@ -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<int>(m_cmd) << "\",";
ss << "\"" << comm::KEY_OPTIONS << "\":\"" << m_options << "\",";
if (has_error()) {
ss << "\"" << comm::KEY_DATA << "\":\"" << m_error << "\",";
Expand Down
13 changes: 7 additions & 6 deletions vpr/src/server/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <chrono>

#include "telegramheader.h"
#include "commconstants.h"

namespace server {

Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions vpr/src/server/taskresolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions vpr/test/test_server_taskresolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<server::Task>(1, cmd);
Expand Down Expand Up @@ -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<server::Task>(1, cmd, "1");
Expand Down Expand Up @@ -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<server::Task>(1, 2, "1");
server::TaskPtr task1 = std::make_unique<server::Task>(2, 2, "11");
server::TaskPtr task2 = std::make_unique<server::Task>(3, 2, "222");
server::TaskPtr task3 = std::make_unique<server::Task>(4, 2, "222");
server::TaskPtr task4 = std::make_unique<server::Task>(5, 1);
server::TaskPtr task5 = std::make_unique<server::Task>(6, 1);
server::TaskPtr task6 = std::make_unique<server::Task>(7, 1);
server::TaskPtr task0 = std::make_unique<server::Task>(1, cmd2, "1");
server::TaskPtr task1 = std::make_unique<server::Task>(2, cmd2, "11");
server::TaskPtr task2 = std::make_unique<server::Task>(3, cmd2, "222");
server::TaskPtr task3 = std::make_unique<server::Task>(4, cmd2, "222");
server::TaskPtr task4 = std::make_unique<server::Task>(5, cmd1);
server::TaskPtr task5 = std::make_unique<server::Task>(6, cmd1);
server::TaskPtr task6 = std::make_unique<server::Task>(7, cmd1);

resolver.own_task(std::move(task0));
resolver.own_task(std::move(task1));
Expand All @@ -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() == "");
}

Expand Down

0 comments on commit e3cff27

Please sign in to comment.