Skip to content

Commit

Permalink
feat: added command_manager module
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Aug 15, 2024
1 parent 90e0c4a commit 480d0a1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ add_subdirectory(agent_info)
add_subdirectory(communicator)
add_subdirectory(configuration_parser)
add_subdirectory(agent_queue)
add_subdirectory(command_manager)

find_package(OpenSSL REQUIRED)
find_package(Boost REQUIRED COMPONENTS asio beast)

add_library(Agent ${SOURCES})
target_include_directories(Agent PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(Agent PUBLIC ConfigurationParser Communicator AgentInfo PRIVATE OpenSSL::SSL OpenSSL::Crypto Boost::asio Boost::beast)
target_link_libraries(Agent PUBLIC ConfigurationParser Communicator AgentInfo CommandManager PRIVATE OpenSSL::SSL OpenSSL::Crypto Boost::asio Boost::beast)

if(BUILD_TESTS)
enable_testing()
Expand Down
22 changes: 22 additions & 0 deletions src/agent/command_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.24)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/../../vcpkg/scripts/buildsystems/vcpkg.cmake")
set(VCPKG_MANIFEST_DIR ${CMAKE_SOURCE_DIR}/../../)

project(CommandManager)

add_library(CommandManager src/command_manager.cpp)

target_include_directories(CommandManager PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${JWT_CPP_INCLUDE_DIRS})
#target_link_libraries(CommandManager PUBLIC Boost::asio PRIVATE Boost::beast OpenSSL::SSL OpenSSL::Crypto)

if(BUILD_TESTS)
# Enable testing
enable_testing()
add_subdirectory(tests)
endif()
18 changes: 18 additions & 0 deletions src/agent/command_manager/include/command_manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <boost/asio.hpp>
#include <queue>

namespace command_manager
{
class CommandManager
{
public:
CommandManager();
~CommandManager() {};

template<typename T>
boost::asio::awaitable<void>
ProcessCommandsFromQueue(const std::function<T(std::string, std::string)> GetCommand);
};
} // namespace command_manager
20 changes: 20 additions & 0 deletions src/agent/command_manager/src/command_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <command_manager.hpp>

#include <iostream>

namespace command_manager
{
CommandManager::CommandManager() {}

template<typename T>
boost::asio::awaitable<void>
CommandManager::ProcessCommandsFromQueue(const std::function<T(std::string, std::string)> GetCommand)
{
while (true)
{

std::cout << "Getting command from queue" << std::endl;
auto cmd = co_await GetCommand();
}
}
} // namespace command_manager
Empty file.
4 changes: 4 additions & 0 deletions src/agent/include/agent.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <agent_info.hpp>
#include <agent_queue.hpp>
#include <command_manager.hpp>
#include <communicator.hpp>
#include <configuration_parser.hpp>
#include <signal_handler.hpp>
Expand All @@ -25,4 +27,6 @@ class Agent
AgentInfo m_agentInfo;
configuration::ConfigurationParser m_configurationParser;
communicator::Communicator m_communicator;
command_manager::CommandManager m_commandManager;
MultiTypeQueue m_agentQueue;
};
15 changes: 15 additions & 0 deletions src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,20 @@ void Agent::Run()
m_taskManager.EnqueueTask(m_communicator.StatefulMessageProcessingTask(m_messageQueue));
m_taskManager.EnqueueTask(m_communicator.StatelessMessageProcessingTask(m_messageQueue));

m_taskManager.EnqueueTask(m_commandManager.ProcessCommandsFromQueue(
[this]() -> Message
{
using namespace std::chrono_literals;
const auto executor = co_await boost::asio::this_coro::executor;
std::unique_ptr<boost::asio::steady_timer> timer = std::make_unique<boost::asio::steady_timer>(executor);

if (m_agent_Queue.isEmpty(MessageType::COMMAND))
{
timer->expires_after(1000ms);
co_await timer->async_wait(boost::asio::use_awaitable);
}
co_return m_agent_Queue.getNextNAwaitable(MessageType::COMMAND, 1);
}));

m_signalHandler.WaitForSignal();
}

0 comments on commit 480d0a1

Please sign in to comment.