비동기 다운로드 구현 완료

This commit is contained in:
2024-10-19 05:06:21 +09:00
parent b4476c68d7
commit 30569f4472
11 changed files with 170 additions and 116 deletions

View File

@@ -6,27 +6,40 @@
#include <thread>
#include <condition_variable>
#include <curl/curl.h>
#include <dpp/dpp.h>
#include <MusicQueue.hpp>
#define WORKER_COUNT 5
namespace bumbleBee {
class AsyncDownloadManager {
public:
static void enqueue(std::string query) {
static AsyncDownloadManager dl;
std::thread th(&bumbleBee::AsyncDownloadManager::enqueueAsyncDL, &dl, query);
static AsyncDownloadManager& getInstance(int worker_count, std::weak_ptr<dpp::cluster> bot, std::weak_ptr<bumbleBee::MusicQueue> musicQueue) {
static AsyncDownloadManager dl(worker_count);
dl.bot = bot;
dl.musicQueue = musicQueue;
return dl;
}
void enqueue(std::string query) {
std::thread th(&bumbleBee::AsyncDownloadManager::enqueueAsyncDL, this, query);
th.detach();
}
private:
AsyncDownloadManager(){
for (int i=0; i<WORKER_COUNT; i++) {
std::thread th(&bumbleBee::AsyncDownloadManager::downloadWorker, &(*this));
th.detach();
AsyncDownloadManager(int worker_count){
worker_thread.reserve(worker_count);
terminate = false;
for (int i=0; i<worker_count; i++) {
worker_thread.emplace_back([this](){this->downloadWorker();});
}
}
~AsyncDownloadManager(){
terminate = true;
dlQueueCondition.notify_all();
for (auto& t : worker_thread) {
t.join();
}
}
~AsyncDownloadManager(){}
AsyncDownloadManager(const AsyncDownloadManager& ref) = delete;
AsyncDownloadManager& operator=(const AsyncDownloadManager& ref) = delete;
void enqueueAsyncDL(std::string query);
void downloadWorker();
@@ -34,6 +47,10 @@ private:
std::queue<std::string> downloadQueue;
std::condition_variable dlQueueCondition;
std::mutex dlQueueMutex;
std::weak_ptr<dpp::cluster> bot;
std::weak_ptr<bumbleBee::MusicQueue> musicQueue;
std::vector<std::thread> worker_thread;
bool terminate;
};
}

View File

@@ -27,19 +27,13 @@ public:
* @brief 파괴자
* @details BumbleBee의 모든 Property를 책임지고 파괴합니다
**/
~BumbleBee();
~BumbleBee() {}
/**
* @fn void start()
* @brief 봇 시작
**/
void start();
/**
* @fn bool addCommand(commands::ICommand cmd)
* @brief ICommand 인터페이스에 맞는 커맨드를 추가
* @warning 이 메소드는 start()메소드가 호출되기 전에 호출되어야 함
**/
bool addCommand(commands::ICommand cmd);
/**
* @fn void on_slashcommand(const dpp::slashcommand_t& event)
@@ -53,9 +47,12 @@ public:
* @param event
**/
void on_ready(const dpp::ready_t &event);
private:
/// @brief DPP 기본 클러스터 객체
std::unique_ptr<dpp::cluster> cluster;
std::shared_ptr<dpp::cluster> cluster;
/// @brief 음악 큐
std::shared_ptr<MusicQueue> queue;
private:
/// @brief db 드라이버
sql::Driver* dbDriver;
/// @brief db 접속 URL
@@ -63,9 +60,7 @@ private:
/// @brief db 접속 속성
std::shared_ptr<sql::Properties> dbProperties;
/// @brief Command 목록
std::vector<commands::ICommand> commands;
/// @brief 음악 큐
std::shared_ptr<MusicQueue> queue;
std::vector<std::shared_ptr<commands::ICommand>> commands;
};
}
#endif