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.
This commit is contained in:
parent
61a57a7529
commit
a7c1bba666
14 changed files with 50 additions and 62 deletions
|
@ -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
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "Peer.hpp"
|
||||
|
||||
#include "behavior/tasks/undefined/UndefinedTask.hpp"
|
||||
#include "utils/serialize/basics.hpp"
|
||||
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <iostream>
|
||||
#include <bits/unique_lock.h>
|
||||
|
||||
#include "utils/audio.hpp"
|
||||
#include "utils/audio/audio.hpp"
|
||||
|
||||
|
||||
namespace drp::event {
|
||||
|
|
|
@ -7,6 +7,13 @@
|
|||
namespace drp::task {
|
||||
|
||||
|
||||
void ClientTask::use(Context& context, const std::shared_ptr<RemotePeer>& 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;
|
||||
|
||||
|
|
|
@ -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<RemotePeer>& server);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -8,26 +8,18 @@
|
|||
#include <netdb.h>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#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<RemotePeer>& server) {
|
||||
context.me.status = TaskType::SERVER;
|
||||
context.server = server;
|
||||
}
|
||||
|
||||
|
||||
ServerTask::~ServerTask() {
|
||||
// delete the mpg123 handle
|
||||
mpg123_close(this->mpgHandle);
|
||||
|
|
|
@ -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<RemotePeer>& server);
|
||||
|
||||
void handle(Context& context) override;
|
||||
|
||||
|
|
|
@ -10,15 +10,24 @@
|
|||
#include <ifaddrs.h>
|
||||
|
||||
#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<RemotePeer> serverCandidate = *std::ranges::max_element(
|
||||
const std::shared_ptr<RemotePeer> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ namespace drp::task {
|
|||
|
||||
class UndefinedTask final : public BaseTask {
|
||||
public:
|
||||
static void use(Context& context);
|
||||
|
||||
void handle(Context& context) override;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
#pragma once
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace drp::util {
|
||||
|
||||
|
||||
template<class Key, class Value, int limit, class Comparator = std::less<Value>>
|
||||
class CacheMap : std::map<Key, Value> {
|
||||
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<Key, Value>::operator[](key);
|
||||
this->cache();
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue