From a7c1bba6667a68aedf15c74f806c5fd0899ebe2d Mon Sep 17 00:00:00 2001 From: study-faraphel Date: Tue, 26 Nov 2024 21:57:37 +0100 Subject: [PATCH] reorganised some parts of the code Utils moved to subdirectories Task activation have been moved to static member of the Task class, instead of being manually activated. --- CMakeLists.txt | 9 +++-- source/Peer.cpp | 1 + source/behavior/events/audio/AudioEvent.cpp | 2 +- source/behavior/tasks/client/ClientTask.cpp | 9 ++++- source/behavior/tasks/client/ClientTask.hpp | 9 ++++- source/behavior/tasks/server/ServerTask.cpp | 19 +++++----- source/behavior/tasks/server/ServerTask.hpp | 3 +- .../tasks/undefined/UndefinedTask.cpp | 23 +++++++----- .../tasks/undefined/UndefinedTask.hpp | 2 ++ source/utils/CacheMap.hpp | 35 ------------------- source/utils/{ => audio}/audio.cpp | 0 source/utils/{ => audio}/audio.hpp | 0 source/utils/{ => network}/network.cpp | 0 source/utils/{ => network}/network.hpp | 0 14 files changed, 50 insertions(+), 62 deletions(-) delete mode 100644 source/utils/CacheMap.hpp rename source/utils/{ => audio}/audio.cpp (100%) rename source/utils/{ => audio}/audio.hpp (100%) rename source/utils/{ => network}/network.cpp (100%) rename source/utils/{ => network}/network.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 915326d..ae12486 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,8 @@ pkg_check_modules(OpenSSL REQUIRED openssl) add_executable(M2-PT-DRP source/main.cpp source/packets/audio/AudioPacketData.hpp - source/utils/audio.cpp - source/utils/audio.hpp + source/utils/audio/audio.cpp + source/utils/audio/audio.hpp source/Manager.cpp source/Manager.hpp source/packets/base/Packet.hpp @@ -49,7 +49,6 @@ add_executable(M2-PT-DRP source/behavior/tasks/client/ClientTask.cpp source/behavior/tasks/client/ClientTask.hpp source/Context.hpp - source/utils/CacheMap.hpp source/packets/search/SearchPacketData.hpp source/packets/base/PacketContent.cpp source/packets/base/PacketContent.hpp @@ -58,8 +57,8 @@ add_executable(M2-PT-DRP source/utils/time/Chrony.cpp source/utils/time/Chrony.hpp source/Peer.hpp - source/utils/network.cpp - source/utils/network.hpp + source/utils/network/network.cpp + source/utils/network/network.hpp source/Peer.cpp source/RemotePeer.cpp source/Context.cpp diff --git a/source/Peer.cpp b/source/Peer.cpp index 47c2b2b..9efba66 100644 --- a/source/Peer.cpp +++ b/source/Peer.cpp @@ -1,5 +1,6 @@ #include "Peer.hpp" +#include "behavior/tasks/undefined/UndefinedTask.hpp" #include "utils/serialize/basics.hpp" diff --git a/source/behavior/events/audio/AudioEvent.cpp b/source/behavior/events/audio/AudioEvent.cpp index da08089..a38ceb9 100644 --- a/source/behavior/events/audio/AudioEvent.cpp +++ b/source/behavior/events/audio/AudioEvent.cpp @@ -3,7 +3,7 @@ #include #include -#include "utils/audio.hpp" +#include "utils/audio/audio.hpp" namespace drp::event { diff --git a/source/behavior/tasks/client/ClientTask.cpp b/source/behavior/tasks/client/ClientTask.cpp index 4f6e618..fd6f6cc 100644 --- a/source/behavior/tasks/client/ClientTask.cpp +++ b/source/behavior/tasks/client/ClientTask.cpp @@ -7,6 +7,13 @@ namespace drp::task { +void ClientTask::use(Context& context, const std::shared_ptr& server) { + context.me.status = TaskType::CLIENT; + context.server = server; +} + + + void ClientTask::handle(Context& context) { // get the server hostname char host[NI_MAXHOST]; @@ -20,7 +27,7 @@ void ClientTask::handle(Context& context) { // connect to the chrony server // TODO(Faraphel): only once ? - FILE* chronyProcess = popen(("chronyc add server " + std::string(host) + " 2>&1").c_str(), "r"); + FILE* chronyProcess = popen(("chronyc add server " + std::string(host) + " iburst 2>&1").c_str(), "r"); if (pclose(chronyProcess) == -1) std::cerr << "[Task - Client] Failed to connect to chrony server !" << std::endl; diff --git a/source/behavior/tasks/client/ClientTask.hpp b/source/behavior/tasks/client/ClientTask.hpp index 28c606e..c6a531a 100644 --- a/source/behavior/tasks/client/ClientTask.hpp +++ b/source/behavior/tasks/client/ClientTask.hpp @@ -6,9 +6,16 @@ namespace drp::task { -class ClientTask : public BaseTask { +class ClientTask final : public BaseTask { public: void handle(Context& context) override; + + /** + * Set this task as the current one. + * @param context the context to apply the state on. + * @param server the server to use. + */ + static void use(Context& context, const std::shared_ptr& server); }; diff --git a/source/behavior/tasks/server/ServerTask.cpp b/source/behavior/tasks/server/ServerTask.cpp index e3e7fc4..87e74c6 100644 --- a/source/behavior/tasks/server/ServerTask.cpp +++ b/source/behavior/tasks/server/ServerTask.cpp @@ -8,26 +8,18 @@ #include #include #include +#include #include #include "packets/audio/AudioPacketData.hpp" #include "packets/base/Packet.hpp" #include "packets/base/SecurityMode.hpp" -#include "utils/audio.hpp" +#include "utils/audio/audio.hpp" namespace drp::task { -/* -ServerTask::use(Context& context) { - context.server = serverCandidate; - context.me.status = TaskType::SERVER; - return; -} -*/ - - ServerTask::ServerTask() { this->channels = 0; this->encoding = 0; @@ -57,6 +49,13 @@ ServerTask::ServerTask() { throw std::runtime_error("[Task - Server] Could not get the format of the file."); } + +void ServerTask::use(Context& context, const std::shared_ptr& server) { + context.me.status = TaskType::SERVER; + context.server = server; +} + + ServerTask::~ServerTask() { // delete the mpg123 handle mpg123_close(this->mpgHandle); diff --git a/source/behavior/tasks/server/ServerTask.hpp b/source/behavior/tasks/server/ServerTask.hpp index f86e17d..a510a68 100644 --- a/source/behavior/tasks/server/ServerTask.hpp +++ b/source/behavior/tasks/server/ServerTask.hpp @@ -20,8 +20,9 @@ public: /** * Set this task as the current one. * @param context the context to apply the state on. + * @param server the server to use. */ - // void use(Context &context); + static void use(Context& context, const std::shared_ptr& server); void handle(Context& context) override; diff --git a/source/behavior/tasks/undefined/UndefinedTask.cpp b/source/behavior/tasks/undefined/UndefinedTask.cpp index b0fa504..e07b17f 100644 --- a/source/behavior/tasks/undefined/UndefinedTask.cpp +++ b/source/behavior/tasks/undefined/UndefinedTask.cpp @@ -10,15 +10,24 @@ #include #include "Context.hpp" +#include "behavior/tasks/client/ClientTask.hpp" +#include "behavior/tasks/server/ServerTask.hpp" #include "packets/base/Packet.hpp" #include "packets/base/SecurityMode.hpp" #include "packets/search/SearchPacketData.hpp" -#include "utils/network.hpp" +#include "utils/network/network.hpp" namespace drp::task { +void UndefinedTask::use(Context &context) { + context.me.status = TaskType::UNDEFINED; + context.server = nullptr; + context.remotePeers.clear(); +} + + void UndefinedTask::handle(Context& context) { std::cout << "[Task - Undefined] List of peers: " << std::endl; for (const auto& remotePeer : context.remotePeers) @@ -34,9 +43,8 @@ void UndefinedTask::handle(Context& context) { ); // if a server have been found, use it if (server != context.remotePeers.end()) { - // if a server have been found, use it - context.server = *server; - context.me.status = TaskType::CLIENT; + // go into client mode + ClientTask::use(context, *server); return; } @@ -51,7 +59,7 @@ void UndefinedTask::handle(Context& context) { // TODO(Faraphel): should use the machine with the lowest average ping if (context.me.serverEnabled) { // find the remote peer with the highest id that can be a server - std::shared_ptr serverCandidate = *std::ranges::max_element( + const std::shared_ptr serverCandidate = *std::ranges::max_element( context.remotePeers, [&](auto& remotePeer1, auto& remotePeer2) { return ( @@ -64,9 +72,8 @@ void UndefinedTask::handle(Context& context) { // check if we are this peer if (util::network::is_localhost(serverCandidate->address, serverCandidate->addressLength)) { std::cout << "[Task - Undefined] Becoming server..." << std::endl; - // set ourselves as the server - context.server = serverCandidate; - context.me.status = TaskType::SERVER; + // go into server mode + ServerTask::use(context, serverCandidate); return; } } diff --git a/source/behavior/tasks/undefined/UndefinedTask.hpp b/source/behavior/tasks/undefined/UndefinedTask.hpp index a72cfd8..af50d08 100644 --- a/source/behavior/tasks/undefined/UndefinedTask.hpp +++ b/source/behavior/tasks/undefined/UndefinedTask.hpp @@ -9,6 +9,8 @@ namespace drp::task { class UndefinedTask final : public BaseTask { public: + static void use(Context& context); + void handle(Context& context) override; }; diff --git a/source/utils/CacheMap.hpp b/source/utils/CacheMap.hpp deleted file mode 100644 index 9d980ea..0000000 --- a/source/utils/CacheMap.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include -#include - - -namespace drp::util { - - -template> -class CacheMap : std::map { -public: - void cache() { - // check if the limit have been reached. - if (this->size() <= limit) - return; - - // apply the comparator on all the value to find the minimum. - auto minimum = this->begin(); - for (const auto& it : this) - if (Comparator()(it.second, minimum)) - minimum = it; - - // delete this lowest value. - this->erase(minimum); - } - - Value& operator[](const Key& key) { - const auto& value = std::map::operator[](key); - this->cache(); - return value; - } -}; - - -} diff --git a/source/utils/audio.cpp b/source/utils/audio/audio.cpp similarity index 100% rename from source/utils/audio.cpp rename to source/utils/audio/audio.cpp diff --git a/source/utils/audio.hpp b/source/utils/audio/audio.hpp similarity index 100% rename from source/utils/audio.hpp rename to source/utils/audio/audio.hpp diff --git a/source/utils/network.cpp b/source/utils/network/network.cpp similarity index 100% rename from source/utils/network.cpp rename to source/utils/network/network.cpp diff --git a/source/utils/network.hpp b/source/utils/network/network.hpp similarity index 100% rename from source/utils/network.hpp rename to source/utils/network/network.hpp