continued developping the undefined task behavior

This commit is contained in:
faraphel 2024-11-06 00:08:29 +01:00
parent 93f8701ac5
commit aa24bb3509
8 changed files with 25 additions and 14 deletions

View file

@ -48,6 +48,7 @@ add_executable(M2-PT-DRP
source/Context.hpp source/Context.hpp
source/utils/CacheMap.cpp source/utils/CacheMap.cpp
source/utils/CacheMap.hpp source/utils/CacheMap.hpp
source/packets/search/SearchPacketData.hpp
) )
target_include_directories(M2-PT-DRP PRIVATE target_include_directories(M2-PT-DRP PRIVATE
${MPG123_INCLUDE_DIRS} ${MPG123_INCLUDE_DIRS}

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <chrono>
#include <sys/socket.h> #include <sys/socket.h>
#include "tasks/types.hpp" #include "tasks/types.hpp"

View file

@ -37,7 +37,7 @@ void AudioEvent::handle(
) { ) {
// get the audio data in the content // get the audio data in the content
packet::AudioPacketData audioData; 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 // save it in the audio queue
this->audioQueue.push(audioData); this->audioQueue.push(audioData);
// notify that a new audio chunk is available // notify that a new audio chunk is available

View file

@ -23,6 +23,8 @@ void InfoEvent::handle(
remotePeer->addressLength = fromAddressLength; remotePeer->addressLength = fromAddressLength;
std::memcpy(&remotePeer->information, &content, sizeof(Peer)); std::memcpy(&remotePeer->information, &content, sizeof(Peer));
// TODO(Faraphel): interpret the timestamp and calculate average ping
// save it in the peers list // save it in the peers list
context.remotePeers.push_back(remotePeer); context.remotePeers.push_back(remotePeer);
} }

View file

@ -31,6 +31,8 @@ void event::SearchEvent::handle(
std::memcpy(&packetContent.data, &context.me, sizeof(context.me)); std::memcpy(&packetContent.data, &context.me, sizeof(context.me));
packet.setContent(packetContent); packet.setContent(packetContent);
// TODO(Faraphel): send back the timestamp too
// broadcast our information // broadcast our information
if (sendto( if (sendto(
context.socket, context.socket,

View file

@ -0,0 +1,13 @@
#pragma once
#include <chrono>
namespace drp::packet {
typedef struct {
std::chrono::time_point<std::chrono::system_clock> timestamp; /// timestamp when the search request was sent
} SearchPacketData;
}

View file

@ -4,6 +4,7 @@
#include <chrono> #include <chrono>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <bits/random.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "../../Context.hpp" #include "../../Context.hpp"
@ -58,11 +59,15 @@ void UndefinedTask::handle(Context& context) {
// prepare a search message // prepare a search message
packet::GenericPacket packet {}; packet::GenericPacket packet {};
packet::GenericPacketContent packetContent {}; packet::GenericPacketContent packetContent {};
packet::SearchPacketData packetData {};
packet.channel = 0; packet.channel = 0;
packet.securityMode = static_cast<std::uint8_t>(packet::SecurityMode::PLAIN); packet.securityMode = static_cast<std::uint8_t>(packet::SecurityMode::PLAIN);
packetData.timestamp = std::chrono::high_resolution_clock::now();
packetContent.eventType = static_cast<std::uint8_t>(event::EventType::SEARCH); packetContent.eventType = static_cast<std::uint8_t>(event::EventType::SEARCH);
packetContent.data = packetData;
// TODO(Faraphel): generate a random broadcast code and put it in the packet. // TODO(Faraphel): generate a random broadcast code and put it in the packet.
// when sending the response to this message, include this broadcast code. // 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 // it will allow us to determinate an estimation of the ping of the machine

View file

@ -10,19 +10,6 @@ namespace drp::task {
class UndefinedTask final : public BaseTask { class UndefinedTask final : public BaseTask {
public: public:
void handle(Context& context) override; void handle(Context& context) override;
private:
std::uniform_int_distribution(
std::numeric_limits<std::uint16_t>::min(),
std::numeric_limits<std::uint16_t>::max()
) broadcastCodeGenerator;
util::CacheMap<
uint32_t,
std::chrono::high_resolution_clock::time_point,
128
> broadcastCode;
}; };