Skip to content

Commit ddd4c8a

Browse files
committed
feat: Added DispatchCommand function
1 parent e2d0ae7 commit ddd4c8a

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/agent/command_manager/include/command_manager.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ namespace command_manager
1313
~CommandManager() {};
1414

1515
template<typename T>
16-
boost::asio::awaitable<void> ProcessCommandsFromQueue(const std::function<std::optional<T>()> GetCommand)
16+
boost::asio::awaitable<void> ProcessCommandsFromQueue(const std::function<std::optional<T>()> GetCommand,
17+
const std::function<int(T&)> DispatchMessage)
1718
{
1819
using namespace std::chrono_literals;
1920
const auto executor = co_await boost::asio::this_coro::executor;
@@ -29,7 +30,8 @@ namespace command_manager
2930
std::cout << "Queue is empty - WAITING" << std::endl;
3031
continue;
3132
}
32-
// DispatchMessage();
33+
34+
DispatchMessage(cmd.value());
3335
}
3436
}
3537
};

src/agent/src/agent.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ Agent::~Agent()
2222

2323
void Agent::Run()
2424
{
25+
// add some test command to the queue
26+
// const nlohmann::json dataContent = {{"command", {{"name", "001"}, {"type", "stateless"}}},
27+
// {"origin", {{"serverName", "node01"}, {"moduleName", "upgradeModule"}}},
28+
// {"parameters", {{"error", 0}, {"data", "Command received"}}},
29+
// {"status", "Pending"}};
30+
//
31+
32+
const nlohmann::json dataContent = {{"command", "upgradeModule"}, {"status", "Pending"}};
33+
const Message messageToSend {MessageType::COMMAND, dataContent, "CommandManager"};
34+
m_agentQueue.push(messageToSend);
35+
2536
m_taskManager.EnqueueTask(m_communicator.WaitForTokenExpirationAndAuthenticate());
2637

2738
m_taskManager.EnqueueTask(m_communicator.GetCommandsFromManager(
@@ -42,7 +53,44 @@ void Agent::Run()
4253
{
4354
return std::nullopt;
4455
}
45-
return m_agentQueue.getNext(MessageType::COMMAND);
56+
Message m = m_agentQueue.getNext(MessageType::COMMAND);
57+
58+
std::cout << "COMMAND retrieved from Queue:" << std::endl;
59+
60+
// pop message from queue
61+
m_agentQueue.pop(MessageType::COMMAND);
62+
63+
nlohmann::json jdata = m.data.at(0);
64+
for (auto& [key, value] : jdata.items())
65+
{
66+
std::cout << key << ": " << value << std::endl;
67+
}
68+
69+
std::cout << "Data inside data:" << std::endl;
70+
71+
nlohmann::json jdataData = m.data.at(0).at("data");
72+
for (auto& [key, value] : jdataData.items())
73+
{
74+
std::cout << key << ": " << value << std::endl;
75+
}
76+
77+
// change status and push again
78+
// if (jdataData.at("status") == "Pending")
79+
//{
80+
// std::cout << "Message status is Pending. Updating status and pushing back." << std::endl;
81+
// jdataData["status"] = "InProcess";
82+
// Message newMessage(MessageType::COMMAND, jdataData, "CommandManager");
83+
// // m_agentQueue.push(newMessage);
84+
// // return m;
85+
// }
86+
87+
return m;
88+
},
89+
[this](Message& cmd) -> int
90+
{
91+
std::cout << "Dispatching command" << std::endl;
92+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
93+
return 1;
4694
}));
4795

4896
m_signalHandler.WaitForSignal();

0 commit comments

Comments
 (0)