Skip to content

Commit e3cff27

Browse files
committed
Task::cmd now is comm::CMD enum, add doc reference to such enum to list possible cmd values
1 parent 86d31a3 commit e3cff27

File tree

6 files changed

+31
-26
lines changed

6 files changed

+31
-26
lines changed

vpr/src/server/commconstants.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ inline const std::string OPTION_DRAW_PATH_CONTOUR{"draw_path_contour"};
2828
inline const std::string KEY_SETUP_PATH_LIST{"setup"};
2929
inline const std::string KEY_HOLD_PATH_LIST{"hold"};
3030

31-
enum CMD {
32-
CMD_GET_PATH_LIST_ID=0,
33-
CMD_DRAW_PATH_ID
31+
enum class CMD : int {
32+
NONE=-1,
33+
GET_PATH_LIST_ID=0,
34+
DRAW_PATH_ID=1
3435
};
3536

3637
} // namespace comm

vpr/src/server/gateio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ GateIO::ActivityStatus GateIO::handle_telegrams(std::vector<comm::TelegramFrameP
148148
std::optional<int> cmd_opt = comm::TelegramParser::try_extract_field_cmd(message);
149149
std::optional<std::string> options_opt = comm::TelegramParser::try_extract_field_options(message);
150150
if (job_id_opt && cmd_opt && options_opt) {
151-
TaskPtr task = std::make_unique<Task>(job_id_opt.value(), cmd_opt.value(), options_opt.value());
151+
TaskPtr task = std::make_unique<Task>(job_id_opt.value(), static_cast<comm::CMD>(cmd_opt.value()), options_opt.value());
152152
const comm::TelegramHeader& header = telegram_frame->header;
153153
m_logger.queue(LogLevel::Info, "received:", header.info(), task->info(/*skipDuration*/true));
154154
std::unique_lock<std::mutex> lock(m_tasks_mutex);

vpr/src/server/task.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace server {
1212

13-
Task::Task(int jobId, int cmd, const std::string& options)
13+
Task::Task(int jobId, comm::CMD cmd, const std::string& options)
1414
: m_job_id(jobId), m_cmd(cmd), m_options(options) {
1515
m_creation_time = std::chrono::high_resolution_clock::now();
1616
}
@@ -55,7 +55,7 @@ std::string Task::info(bool skip_duration) const {
5555
std::stringstream ss;
5656
ss << "task["
5757
<< "id=" << std::to_string(m_job_id)
58-
<< ",cmd=" << std::to_string(m_cmd);
58+
<< ",cmd=" << std::to_string(static_cast<int>(m_cmd));
5959
if (!skip_duration) {
6060
ss << ",exists=" << get_pretty_duration_str_from_ms(time_ms_elapsed());
6161
}
@@ -73,7 +73,7 @@ void Task::bake_response() {
7373
ss << "{";
7474

7575
ss << "\"" << comm::KEY_JOB_ID << "\":\"" << m_job_id << "\",";
76-
ss << "\"" << comm::KEY_CMD << "\":\"" << m_cmd << "\",";
76+
ss << "\"" << comm::KEY_CMD << "\":\"" << static_cast<int>(m_cmd) << "\",";
7777
ss << "\"" << comm::KEY_OPTIONS << "\":\"" << m_options << "\",";
7878
if (has_error()) {
7979
ss << "\"" << comm::KEY_DATA << "\":\"" << m_error << "\",";

vpr/src/server/task.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <chrono>
99

1010
#include "telegramheader.h"
11+
#include "commconstants.h"
1112

1213
namespace server {
1314

@@ -23,10 +24,10 @@ class Task {
2324
* @brief Constructs a new Task object.
2425
*
2526
* @param job_id The ID of the job associated with the task.
26-
* @param cmd The command associated with the task.
27+
* @param cmd The command ID (see @ref comm::CMD) associated with the task.
2728
* @param options Additional options for the task (default: empty string).
2829
*/
29-
Task(int job_id, int cmd, const std::string& options = "");
30+
Task(int job_id, comm::CMD cmd, const std::string& options = "");
3031

3132
Task(const Task&) = delete;
3233
Task& operator=(const Task&) = delete;
@@ -39,11 +40,11 @@ class Task {
3940
int job_id() const { return m_job_id; }
4041

4142
/**
42-
* @brief Gets the command associated with the task.
43+
* @brief Gets the command ID associated with the task.
4344
*
44-
* @return The command.
45+
* @return The command ID (see @ref comm::CMD).
4546
*/
46-
int cmd() const { return m_cmd; }
47+
comm::CMD cmd() const { return m_cmd; }
4748

4849
/**
4950
* @brief Removes the specified number of bytes from the response buffer.
@@ -179,7 +180,7 @@ class Task {
179180

180181
private:
181182
int m_job_id = -1;
182-
int m_cmd = -1;
183+
comm::CMD m_cmd = comm::CMD::NONE;
183184
std::string m_options;
184185
std::string m_result;
185186
std::string m_error;

vpr/src/server/taskresolver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ bool TaskResolver::update(ezgl::application* app) {
6464
for (auto& task: m_tasks) {
6565
if (!task->is_finished()) {
6666
switch(task->cmd()) {
67-
case comm::CMD_GET_PATH_LIST_ID: {
67+
case comm::CMD::GET_PATH_LIST_ID: {
6868
process_get_path_list_task(app, task);
6969
has_processed_task = true;
7070
break;
7171
}
72-
case comm::CMD_DRAW_PATH_ID: {
72+
case comm::CMD::DRAW_PATH_ID: {
7373
process_draw_critical_path_task(app, task);
7474
has_processed_task = true;
7575
break;

vpr/test/test_server_taskresolver.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
TEST_CASE("test_server_taskresolver_cmdSpamFilter", "[vpr]") {
1010
server::TaskResolver resolver;
11-
const int cmd = 10;
11+
const comm::CMD cmd = comm::CMD::GET_PATH_LIST_ID;
1212

1313
{
1414
server::TaskPtr task0 = std::make_unique<server::Task>(1, cmd);
@@ -43,7 +43,7 @@ TEST_CASE("test_server_taskresolver_cmdSpamFilter", "[vpr]") {
4343

4444
TEST_CASE("test_server_taskresolver_cmdOverrideFilter", "[vpr]") {
4545
server::TaskResolver resolver;
46-
const int cmd = 10;
46+
const comm::CMD cmd = comm::CMD::GET_PATH_LIST_ID;
4747

4848
{
4949
server::TaskPtr task0 = std::make_unique<server::Task>(1, cmd, "1");
@@ -75,14 +75,17 @@ TEST_CASE("test_server_taskresolver_cmdOverrideFilter", "[vpr]") {
7575
TEST_CASE("test_server_taskresolver_cmdSpamAndOverrideOptions", "[vpr]") {
7676
server::TaskResolver resolver;
7777

78+
const comm::CMD cmd1 = comm::CMD::GET_PATH_LIST_ID;
79+
const comm::CMD cmd2 = comm::CMD::DRAW_PATH_ID;
80+
7881
{
79-
server::TaskPtr task0 = std::make_unique<server::Task>(1, 2, "1");
80-
server::TaskPtr task1 = std::make_unique<server::Task>(2, 2, "11");
81-
server::TaskPtr task2 = std::make_unique<server::Task>(3, 2, "222");
82-
server::TaskPtr task3 = std::make_unique<server::Task>(4, 2, "222");
83-
server::TaskPtr task4 = std::make_unique<server::Task>(5, 1);
84-
server::TaskPtr task5 = std::make_unique<server::Task>(6, 1);
85-
server::TaskPtr task6 = std::make_unique<server::Task>(7, 1);
82+
server::TaskPtr task0 = std::make_unique<server::Task>(1, cmd2, "1");
83+
server::TaskPtr task1 = std::make_unique<server::Task>(2, cmd2, "11");
84+
server::TaskPtr task2 = std::make_unique<server::Task>(3, cmd2, "222");
85+
server::TaskPtr task3 = std::make_unique<server::Task>(4, cmd2, "222");
86+
server::TaskPtr task4 = std::make_unique<server::Task>(5, cmd1);
87+
server::TaskPtr task5 = std::make_unique<server::Task>(6, cmd1);
88+
server::TaskPtr task6 = std::make_unique<server::Task>(7, cmd1);
8689

8790
resolver.own_task(std::move(task0));
8891
resolver.own_task(std::move(task1));
@@ -101,11 +104,11 @@ TEST_CASE("test_server_taskresolver_cmdSpamAndOverrideOptions", "[vpr]") {
101104
const server::TaskPtr& task1 = resolver.tasks().at(1);
102105

103106
REQUIRE(task0->job_id() == 3);
104-
REQUIRE(task0->cmd() == 2);
107+
REQUIRE(task0->cmd() == cmd2);
105108
REQUIRE(task0->options() == "222");
106109

107110
REQUIRE(task1->job_id() == 5);
108-
REQUIRE(task1->cmd() == 1);
111+
REQUIRE(task1->cmd() == cmd1);
109112
REQUIRE(task1->options() == "");
110113
}
111114

0 commit comments

Comments
 (0)