From aa24bb35098e22d5149a9ae6c2eaecee43fb4a0c Mon Sep 17 00:00:00 2001 From: faraphel Date: Wed, 6 Nov 2024 00:08:29 +0100 Subject: [PATCH] continued developping the undefined task behavior --- CMakeLists.txt | 1 + source/RemotePeer.hpp | 1 + source/events/audio/AudioEvent.cpp | 2 +- source/events/info/InfoEvent.cpp | 2 ++ source/events/search/SearchEvent.cpp | 2 ++ source/packets/search/SearchPacketData.hpp | 13 +++++++++++++ source/tasks/undefined/UndefinedTask.cpp | 5 +++++ source/tasks/undefined/UndefinedTask.hpp | 13 ------------- 8 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 source/packets/search/SearchPacketData.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d880c34..7c3352b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ add_executable(M2-PT-DRP source/Context.hpp source/utils/CacheMap.cpp source/utils/CacheMap.hpp + source/packets/search/SearchPacketData.hpp ) target_include_directories(M2-PT-DRP PRIVATE ${MPG123_INCLUDE_DIRS} diff --git a/source/RemotePeer.hpp b/source/RemotePeer.hpp index 74bf839..9d639c5 100644 --- a/source/RemotePeer.hpp +++ b/source/RemotePeer.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include "tasks/types.hpp" diff --git a/source/events/audio/AudioEvent.cpp b/source/events/audio/AudioEvent.cpp index 2fc81f5..8567edd 100644 --- a/source/events/audio/AudioEvent.cpp +++ b/source/events/audio/AudioEvent.cpp @@ -37,7 +37,7 @@ void AudioEvent::handle( ) { // get the audio data in the content packet::AudioPacketData audioData; - std::memcpy(&audioData, content.data.data(), content.data.size()); + std::memcpy(&audioData, content.data.data(), sizeof(packet::AudioPacketData)); // save it in the audio queue this->audioQueue.push(audioData); // notify that a new audio chunk is available diff --git a/source/events/info/InfoEvent.cpp b/source/events/info/InfoEvent.cpp index 85a6478..9fc107b 100644 --- a/source/events/info/InfoEvent.cpp +++ b/source/events/info/InfoEvent.cpp @@ -23,6 +23,8 @@ void InfoEvent::handle( remotePeer->addressLength = fromAddressLength; std::memcpy(&remotePeer->information, &content, sizeof(Peer)); + // TODO(Faraphel): interpret the timestamp and calculate average ping + // save it in the peers list context.remotePeers.push_back(remotePeer); } diff --git a/source/events/search/SearchEvent.cpp b/source/events/search/SearchEvent.cpp index 1a890ce..97fffdd 100644 --- a/source/events/search/SearchEvent.cpp +++ b/source/events/search/SearchEvent.cpp @@ -31,6 +31,8 @@ void event::SearchEvent::handle( std::memcpy(&packetContent.data, &context.me, sizeof(context.me)); packet.setContent(packetContent); + // TODO(Faraphel): send back the timestamp too + // broadcast our information if (sendto( context.socket, diff --git a/source/packets/search/SearchPacketData.hpp b/source/packets/search/SearchPacketData.hpp new file mode 100644 index 0000000..7809895 --- /dev/null +++ b/source/packets/search/SearchPacketData.hpp @@ -0,0 +1,13 @@ +#pragma once +#include + + +namespace drp::packet { + + +typedef struct { + std::chrono::time_point timestamp; /// timestamp when the search request was sent +} SearchPacketData; + + +} \ No newline at end of file diff --git a/source/tasks/undefined/UndefinedTask.cpp b/source/tasks/undefined/UndefinedTask.cpp index 2978aa4..383cae2 100644 --- a/source/tasks/undefined/UndefinedTask.cpp +++ b/source/tasks/undefined/UndefinedTask.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "../../Context.hpp" @@ -58,11 +59,15 @@ void UndefinedTask::handle(Context& context) { // prepare a search message packet::GenericPacket packet {}; packet::GenericPacketContent packetContent {}; + packet::SearchPacketData packetData {}; packet.channel = 0; packet.securityMode = static_cast(packet::SecurityMode::PLAIN); + packetData.timestamp = std::chrono::high_resolution_clock::now(); packetContent.eventType = static_cast(event::EventType::SEARCH); + packetContent.data = packetData; + // TODO(Faraphel): generate a random broadcast code and put it in the packet. // when sending the response to this message, include this broadcast code. // it will allow us to determinate an estimation of the ping of the machine diff --git a/source/tasks/undefined/UndefinedTask.hpp b/source/tasks/undefined/UndefinedTask.hpp index 9305a53..a72cfd8 100644 --- a/source/tasks/undefined/UndefinedTask.hpp +++ b/source/tasks/undefined/UndefinedTask.hpp @@ -10,19 +10,6 @@ namespace drp::task { class UndefinedTask final : public BaseTask { public: void handle(Context& context) override; - -private: - std::uniform_int_distribution( - std::numeric_limits::min(), - std::numeric_limits::max() - ) broadcastCodeGenerator; - - util::CacheMap< - uint32_t, - std::chrono::high_resolution_clock::time_point, - 128 - > broadcastCode; - };