From 1c5442c267a20ca83c5a26c4c7902e6c04859c2d Mon Sep 17 00:00:00 2001 From: faraphel Date: Wed, 6 Nov 2024 23:53:25 +0100 Subject: [PATCH] moved sleep into the tasks instead of the event loop --- source/EventManager.cpp | 4 ---- source/tasks/client/ClientTask.cpp | 4 ++++ source/tasks/server/ServerTask.cpp | 4 +++- source/tasks/undefined/UndefinedTask.cpp | 8 ++++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/source/EventManager.cpp b/source/EventManager.cpp index 691e44a..c968509 100644 --- a/source/EventManager.cpp +++ b/source/EventManager.cpp @@ -109,10 +109,6 @@ void EventManager::loopSender() { // ask the task class to handle the task task->handle(this->context); - - // wait a second - // TODO(Faraphel): might be moved to the tasks directly ? what if they want a lower cooldown ? - std::this_thread::sleep_for(std::chrono::seconds(1)); } // free the address diff --git a/source/tasks/client/ClientTask.cpp b/source/tasks/client/ClientTask.cpp index 0e71ad4..3ee6ef1 100644 --- a/source/tasks/client/ClientTask.cpp +++ b/source/tasks/client/ClientTask.cpp @@ -1,5 +1,7 @@ #include "ClientTask.hpp" +#include + namespace drp::task { @@ -9,6 +11,8 @@ void ClientTask::handle(Context& context) { // TODO(Faraphel): check if the server is still reachable. // if connection lost, go back to undefined mode. + + std::this_thread::sleep_for(std::chrono::seconds(1)); } diff --git a/source/tasks/server/ServerTask.cpp b/source/tasks/server/ServerTask.cpp index 964790b..491a935 100644 --- a/source/tasks/server/ServerTask.cpp +++ b/source/tasks/server/ServerTask.cpp @@ -1,6 +1,7 @@ #include "ServerTask.hpp" #include +#include #include #include #include @@ -8,7 +9,6 @@ #include #include #include -#include #include "../../packets/audio/AudioPacketData.hpp" #include "../../utils/audio.hpp" @@ -66,6 +66,7 @@ void ServerTask::handle(Context& context) { &done ) != MPG123_OK) { std::cerr << "[Server] Could not read audio data from file." << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); return; } @@ -93,6 +94,7 @@ void ServerTask::handle(Context& context) { context.broadcastAddressInfo->ai_addrlen ) == -1) { std::cerr << "[Server] Could not send audio packet: " << strerror(errno) << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); return; } diff --git a/source/tasks/undefined/UndefinedTask.cpp b/source/tasks/undefined/UndefinedTask.cpp index d8bbd1d..d20830e 100644 --- a/source/tasks/undefined/UndefinedTask.cpp +++ b/source/tasks/undefined/UndefinedTask.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "../../Context.hpp" @@ -54,7 +53,12 @@ void UndefinedTask::handle(Context& context) { return; } - // TODO(Faraphel): elect a server among those capable of emitting. + // TODO(Faraphel): check if we have the lowest average ping out of all the peers. If yes, become the server. + // Others peers will connect to us on their next loop. + if (!context.me.serverEnabled) + return; + + ... } // prepare a search message